Skip to content

Commit

Permalink
test:add test for issue modlinltd#144
Browse files Browse the repository at this point in the history
  • Loading branch information
curtisstpierre committed Sep 14, 2022
1 parent 5f56318 commit c36ed33
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 2 deletions.
27 changes: 25 additions & 2 deletions advanced_filters/tests/test_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")


Expand Down Expand Up @@ -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
30 changes: 30 additions & 0 deletions tests/customers/migrations/0002_auto_20220223_0605.py
Original file line number Diff line number Diff line change
@@ -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'),
),
]
5 changes: 5 additions & 0 deletions tests/customers/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand All @@ -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)
18 changes: 18 additions & 0 deletions tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,27 @@ def _prepare(cls, create, **kwargs):
return user


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%[email protected]' % 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)
31 changes: 31 additions & 0 deletions tests/reps/migrations/0002_auto_20220223_0605.py
Original file line number Diff line number Diff line change
@@ -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'),
),
]

0 comments on commit c36ed33

Please sign in to comment.