From ebf126bfcf721fb08e2eb4471147925394a883fa Mon Sep 17 00:00:00 2001 From: Curtis St Pierre Date: Tue, 13 Sep 2022 21:34:02 -0700 Subject: [PATCH] test:add test for issue #144 --- advanced_filters/tests/test_usage.py | 27 ++++++++++++++-- .../migrations/0002_auto_20220223_0605.py | 30 ++++++++++++++++++ tests/customers/models.py | 5 +++ tests/factories.py | 18 +++++++++++ .../migrations/0002_auto_20220223_0605.py | 31 +++++++++++++++++++ 5 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 tests/customers/migrations/0002_auto_20220223_0605.py create mode 100644 tests/reps/migrations/0002_auto_20220223_0605.py diff --git a/advanced_filters/tests/test_usage.py b/advanced_filters/tests/test_usage.py index edca0d8..250764a 100644 --- a/advanced_filters/tests/test_usage.py +++ b/advanced_filters/tests/test_usage.py @@ -2,7 +2,7 @@ from django.contrib.auth.models import Permission from django.db.models import Q from django.urls import reverse -from tests.factories import ClientFactory, SalesRepFactory +from tests.factories import ClientFactory, SalesRepFactory, AttributeFactory from ..admin import AdvancedListFilters from ..models import AdvancedFilter @@ -35,9 +35,20 @@ def advanced_filter(user): return af +@pytest.fixture +def advanced_filter_m2m(user): + af = AdvancedFilterFactory.build( + title="M2M test", url="foo", model="customers.Client", created_by=user + ) + af.query = Q(attributes__name__contains="1") & Q(attributes__name__contains="2") + af.save() + return af + + @pytest.fixture(autouse=True) def clients(user): - ClientFactory.create_batch(8, assigned_to=user, language="en") + AttributeFactory.create_batch(3) + ClientFactory.create_batch(8, attributes=('1', '2', '3'), assigned_to=user, language="en") ClientFactory.create_batch(2, assigned_to=user, language="ru") @@ -73,3 +84,15 @@ def test_filters_available_to_groups(client, user, advanced_filter): assert cl.filter_specs if hasattr(cl, "queryset"): assert cl.queryset.count() == 2 + + +def test_many_to_many_filters(client, user, advanced_filter_m2m): + group = user.groups.create() + advanced_filter_m2m.groups.add(group) + url = reverse(URL_NAME_CLIENT_CHANGELIST) + res = client.get(url, data={"_afilter": advanced_filter_m2m.pk}) + assert res.status_code == 200 + cl = res.context_data["cl"] + assert cl.filter_specs + if hasattr(cl, "queryset"): + assert cl.queryset.count() == 8 diff --git a/tests/customers/migrations/0002_auto_20220223_0605.py b/tests/customers/migrations/0002_auto_20220223_0605.py new file mode 100644 index 0000000..ba69aca --- /dev/null +++ b/tests/customers/migrations/0002_auto_20220223_0605.py @@ -0,0 +1,30 @@ +# Generated by Django 2.2 on 2022-02-23 06:05 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('customers', '0001_initial'), + ] + + operations = [ + migrations.CreateModel( + name='Attribute', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=100)), + ], + ), + migrations.AlterField( + model_name='client', + name='language', + field=models.CharField(choices=[('en', 'English'), ('sp', 'Spanish'), ('it', 'Italian')], default='en', max_length=8), + ), + migrations.AddField( + model_name='client', + name='attributes', + field=models.ManyToManyField(blank=True, to='customers.Attribute'), + ), + ] diff --git a/tests/customers/models.py b/tests/customers/models.py index 31238aa..231ea01 100644 --- a/tests/customers/models.py +++ b/tests/customers/models.py @@ -4,6 +4,10 @@ from django.utils.translation import gettext_lazy as _ +class Attribute(models.Model): + name = models.CharField(max_length=100) + + class Client(AbstractBaseUser): VALID_LANGUAGES = ( ('en', 'English'), @@ -23,3 +27,4 @@ class Client(AbstractBaseUser): 'active. Unselect this instead of deleting accounts.')) assigned_to = models.ForeignKey('reps.SalesRep', on_delete=models.CASCADE) date_joined = models.DateTimeField(_('date joined'), default=timezone.now) + attributes = models.ManyToManyField(Attribute, blank=True) diff --git a/tests/factories.py b/tests/factories.py index 00e3e12..07d073c 100644 --- a/tests/factories.py +++ b/tests/factories.py @@ -20,9 +20,27 @@ def _create(cls, model_class, *args, **kwargs): return manager.create_user(*args, **kwargs) +class AttributeFactory(factory.django.DjangoModelFactory): + class Meta: + model = 'customers.Attribute' + + name = factory.Sequence(lambda n: 'attribute%d' % n) + + class ClientFactory(factory.django.DjangoModelFactory): class Meta: model = 'customers.Client' first_name = factory.faker.Faker('first_name') email = factory.Sequence(lambda n: 'c%d@foo.com' % n) + + @factory.post_generation + def attributes(self, create, extracted, **kwargs): + if not create: + # Simple build, do nothing. + return + + if extracted: + # A list of groups were passed in, use them + for attribute in extracted: + self.attributes.add(attribute) diff --git a/tests/reps/migrations/0002_auto_20220223_0605.py b/tests/reps/migrations/0002_auto_20220223_0605.py new file mode 100644 index 0000000..e2680ef --- /dev/null +++ b/tests/reps/migrations/0002_auto_20220223_0605.py @@ -0,0 +1,31 @@ +# Generated by Django 2.2 on 2022-02-23 06:05 + +import django.contrib.auth.models +import django.contrib.auth.validators +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('reps', '0001_initial'), + ] + + operations = [ + migrations.AlterModelManagers( + name='salesrep', + managers=[ + ('objects', django.contrib.auth.models.UserManager()), + ], + ), + migrations.AlterField( + model_name='salesrep', + name='last_name', + field=models.CharField(blank=True, max_length=150, verbose_name='last name'), + ), + migrations.AlterField( + model_name='salesrep', + name='username', + field=models.CharField(error_messages={'unique': 'A user with that username already exists.'}, help_text='Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=150, unique=True, validators=[django.contrib.auth.validators.UnicodeUsernameValidator()], verbose_name='username'), + ), + ]