# Path: tests/factories.py
# 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)


import json
import sys
import django
import factory
import pytest
from datetime import timedelta
from operator import attrgetter
from django.utils import timezone
from django.utils.encoding import force_str
from django.urls import reverse
from tests.factories import ClientFactory
def test_distinct_database_choices(user, client, settings):
    settings.ADVANCED_FILTERS_MAX_CHOICES = 4
    ClientFactory.create_batch(5, assigned_to=user, email="foo@bar.com")
    view_url = reverse(
        URL_NAME, kwargs=dict(model="customers.Client", field_name="email")
    )
    response = client.get(view_url)
    assert_json(
        response.content, {"results": [{"id": "foo@bar.com", "text": "foo@bar.com"}]}
    )


def test_choices_no_date_fields_support(user, client, settings):
    settings.ADVANCED_FILTERS_MAX_CHOICES = 4
    logins = [timezone.now(), timezone.now() - timedelta(days=1), None]
    ClientFactory.create_batch(
        3, assigned_to=user, email="foo@bar.com", last_login=factory.Iterator(logins)
    )
    view_url = reverse(
        URL_NAME, kwargs=dict(model="customers.Client", field_name="last_login")
    )
    response = client.get(view_url)
    assert_json(response.content, {"results": []})


def test_choices_has_null(user, client, settings):
    settings.ADVANCED_FILTERS_MAX_CHOICES = 4
    named_users = ClientFactory.create_batch(2, assigned_to=user)
    names = [None] + sorted({nu.first_name for nu in named_users})
    assert len(named_users) == 2

# Path: tests/factories.py
# 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)


# Path: advanced_filters/tests/test_get_field_choices_view.py
import json
import sys
import django
import factory
import pytest
from datetime import timedelta
from operator import attrgetter
from django.utils import timezone
from django.utils.encoding import force_str
from django.urls import reverse
from tests.factories import ClientFactory

def test_distinct_database_choices(user, client, settings):
    settings.ADVANCED_FILTERS_MAX_CHOICES = 4
    ClientFactory.create_batch(5, assigned_to=user, email="foo@bar.com")
    view_url = reverse(
        URL_NAME, kwargs=dict(model="customers.Client", field_name="email")
    )
    response = client.get(view_url)
    assert_json(
        response.content, {"results": [{"id": "foo@bar.com", "text": "foo@bar.com"}]}
    )


def test_choices_no_date_fields_support(user, client, settings):
    settings.ADVANCED_FILTERS_MAX_CHOICES = 4
    logins = [timezone.now(), timezone.now() - timedelta(days=1), None]
    ClientFactory.create_batch(
        3, assigned_to=user, email="foo@bar.com", last_login=factory.Iterator(logins)
    )
    view_url = reverse(
        URL_NAME, kwargs=dict(model="customers.Client", field_name="last_login")
    )
    response = client.get(view_url)
    assert_json(response.content, {"results": []})


def test_choices_has_null(user, client, settings):
    settings.ADVANCED_FILTERS_MAX_CHOICES = 4
    named_users = ClientFactory.create_batch(2, assigned_to=user)
    names = [None] + sorted({nu.first_name for nu in named_users})
    assert len(named_users) == 2

    ClientFactory.create_batch(2, assigned_to=user, first_name=None)