Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mise à jour de la dépendance factory boy #517

Merged
merged 1 commit into from
Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 34 additions & 33 deletions aidants_connect_habilitation/tests/factories.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import factory
from django.conf import settings
from django.core.exceptions import ValidationError
from factory import fuzzy
from factory import Faker, LazyFunction, SubFactory
from factory.django import DjangoModelFactory
from factory.fuzzy import FuzzyChoice, FuzzyInteger, FuzzyText

from phonenumber_field.phonenumber import to_python

Expand All @@ -18,7 +19,7 @@ def _generate_valid_phone():
does not consider valid which makes tests to randomly fail. This
function always generate a valid french phone number for PhoneNumberField"""
for try_attempt in range(10):
phone = factory.Faker("phone_number", locale="fr_FR").generate()
phone = Faker("phone_number").evaluate(None, None, {"locale": "fr_FR"})
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Celui-ci est particulièrement relou. La nouvelle méthode a une signature probablement plus logique pour son utilisation en interne mais pas pour son utilisation publique.

if to_python(phone, settings.PHONENUMBER_DEFAULT_REGION).is_valid():
break
else:
Expand All @@ -29,48 +30,48 @@ def _generate_valid_phone():
return phone


class IssuerFactory(factory.DjangoModelFactory):
first_name = factory.Faker("first_name")
last_name = factory.Faker("last_name")
email = factory.Faker("email")
profession = factory.Faker("job")
phone = factory.LazyFunction(_generate_valid_phone)
class IssuerFactory(DjangoModelFactory):
first_name = Faker("first_name")
last_name = Faker("last_name")
email = Faker("email")
profession = Faker("job")
phone = LazyFunction(_generate_valid_phone)

class Meta:
model = Issuer


class OrganisationRequestFactory(factory.DjangoModelFactory):
issuer = factory.SubFactory(IssuerFactory)
class OrganisationRequestFactory(DjangoModelFactory):
issuer = SubFactory(IssuerFactory)

draft_id = None

type_id = fuzzy.FuzzyChoice(
type_id = FuzzyChoice(
[x.value for x in RequestOriginConstants if x != RequestOriginConstants.OTHER]
)
name = factory.Faker("company")
siret = fuzzy.FuzzyInteger(111_111_111, 999_999_999)
address = factory.Faker("street_address")
zipcode = factory.Faker("postcode")
city = factory.Faker("city")
name = Faker("company")
siret = FuzzyInteger(111_111_111, 999_999_999)
address = Faker("street_address")
zipcode = Faker("postcode")
city = Faker("city")

partner_administration = factory.Faker("company")
partner_administration = Faker("company")

public_service_delegation_attestation = ""

france_services_label = fuzzy.FuzzyChoice([True, False])
france_services_label = FuzzyChoice([True, False])

web_site = factory.Faker("url")
web_site = Faker("url")

mission_description = fuzzy.FuzzyText(length=12)
mission_description = FuzzyText(length=12)

avg_nb_demarches = fuzzy.FuzzyInteger(0)
avg_nb_demarches = FuzzyInteger(0)

manager_first_name = factory.Faker("first_name")
manager_last_name = factory.Faker("last_name")
manager_email = factory.Faker("email")
manager_profession = factory.Faker("job")
manager_phone = factory.LazyFunction(_generate_valid_phone)
manager_first_name = Faker("first_name")
manager_last_name = Faker("last_name")
manager_email = Faker("email")
manager_profession = Faker("job")
manager_phone = LazyFunction(_generate_valid_phone)

cgu = True
dpo = True
Expand All @@ -81,13 +82,13 @@ class Meta:
model = OrganisationRequest


class AidantRequestFactory(factory.DjangoModelFactory):
organisation = factory.SubFactory(OrganisationRequestFactory)
class AidantRequestFactory(DjangoModelFactory):
organisation = SubFactory(OrganisationRequestFactory)

first_name = factory.Faker("first_name")
last_name = factory.Faker("last_name")
email = factory.Faker("email")
profession = factory.Faker("job")
first_name = Faker("first_name")
last_name = Faker("last_name")
email = Faker("email")
profession = Faker("job")

class Meta:
model = AidantRequest
5 changes: 3 additions & 2 deletions aidants_connect_habilitation/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from django.test.client import Client
from django.urls import reverse
from factory import Faker
from faker.config import DEFAULT_LOCALE

from aidants_connect_habilitation.forms import (
AidantRequestFormSet,
Expand Down Expand Up @@ -86,7 +87,7 @@ def test_404_on_bad_issuer_id(self):

def test_on_correct_issuer_id_post_updates_model(self):
model: Issuer = IssuerFactory()
new_name = Faker("first_name").generate()
new_name = Faker("first_name").evaluate(None, None, {"locale": DEFAULT_LOCALE})
form = IssuerForm(data={**model_to_dict(model), "first_name": new_name})

if not form.is_valid():
Expand Down Expand Up @@ -196,7 +197,7 @@ def test_on_correct_issuer_id_post_updates_model(self):
model: OrganisationRequest = OrganisationRequestFactory(
issuer=self.issuer, draft_id=uuid4()
)
new_name = Faker("company").generate()
new_name = Faker("company").evaluate(None, None, {"locale": DEFAULT_LOCALE})
form = OrganisationRequestForm(data={**model_to_dict(model), "name": new_name})

if not form.is_valid():
Expand Down
2 changes: 1 addition & 1 deletion aidants_connect_habilitation/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Type, TypeVar

from django.forms import BaseFormSet, ModelForm
from factory import DjangoModelFactory
from factory.django import DjangoModelFactory

from aidants_connect_habilitation.tests import factories

Expand Down
77 changes: 43 additions & 34 deletions aidants_connect_web/tests/factories.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
from collections.abc import Iterable

import factory
from datetime import timedelta
from django.contrib.auth import get_user_model
from django.utils.timezone import now

from django_otp.plugins.otp_totp.models import TOTPDevice
from factory import (
Faker,
LazyAttribute,
PostGenerationMethodCall,
SelfAttribute,
Sequence,
SubFactory,
post_generation,
)
from factory.django import DjangoModelFactory

from aidants_connect_web.models import (
Autorisation,
Expand All @@ -23,7 +32,7 @@
)


class OrganisationFactory(factory.DjangoModelFactory):
class OrganisationFactory(DjangoModelFactory):
name = "COMMUNE D'HOULBEC COCHEREL"
siret = 123
address = "45 avenue du Général de Gaulle, 27120 HOULBEC COCHEREL"
Expand All @@ -32,39 +41,39 @@ class Meta:
model = Organisation


class CarteTOTPFactory(factory.DjangoModelFactory):
serial_number = factory.Sequence(lambda n: f"SN{n}")
seed = factory.Faker("hexify")
class CarteTOTPFactory(DjangoModelFactory):
serial_number = Sequence(lambda n: f"SN{n}")
seed = Faker("hexify")

class Meta:
model = CarteTOTP


class TOTPDeviceFactory(factory.DjangoModelFactory):
class TOTPDeviceFactory(DjangoModelFactory):
class Meta:
model = TOTPDevice


class OrganisationTypeFactory(factory.DjangoModelFactory):
class OrganisationTypeFactory(DjangoModelFactory):
name = "Type par défaut"

class Meta:
model = OrganisationType


class AidantFactory(factory.DjangoModelFactory):
username = factory.Faker("email")
email = factory.SelfAttribute("username")
password = factory.PostGenerationMethodCall("set_password", "motdepassedethierry")
class AidantFactory(DjangoModelFactory):
username = Faker("email")
email = SelfAttribute("username")
password = PostGenerationMethodCall("set_password", "motdepassedethierry")
last_name = "Goneau"
first_name = "Thierry"
profession = "secrétaire"
organisation = factory.SubFactory(OrganisationFactory)
organisation = SubFactory(OrganisationFactory)

class Meta:
model = get_user_model()

@factory.post_generation
@post_generation
def post(self, create, _, **kwargs):
if not create:
return
Expand All @@ -76,42 +85,42 @@ def post(self, create, _, **kwargs):
self.responsable_de.add(self.organisation)


class HabilitationRequestFactory(factory.DjangoModelFactory):
class HabilitationRequestFactory(DjangoModelFactory):
first_name = "Jean"
last_name = "Dupont"
email = factory.Faker("email")
organisation = factory.SubFactory(OrganisationFactory)
email = Faker("email")
organisation = SubFactory(OrganisationFactory)
profession = "Secrétaire"

class Meta:
model = HabilitationRequest


class UsagerFactory(factory.DjangoModelFactory):
class UsagerFactory(DjangoModelFactory):
given_name = "Homer"
family_name = "Simpson"
birthdate = "1902-06-30"
gender = Usager.GENDER_MALE
birthplace = "27681"
birthcountry = Usager.BIRTHCOUNTRY_FRANCE
email = "homer@simpson.com"
sub = factory.Sequence(lambda n: f"avalidsub{n}")
sub = Sequence(lambda n: f"avalidsub{n}")

class Meta:
model = Usager


class MandatFactory(factory.DjangoModelFactory):
organisation = factory.SubFactory(OrganisationFactory)
usager = factory.SubFactory(UsagerFactory)
creation_date = factory.LazyAttribute(lambda f: now())
class MandatFactory(DjangoModelFactory):
organisation = SubFactory(OrganisationFactory)
usager = SubFactory(UsagerFactory)
creation_date = LazyAttribute(lambda f: now())
duree_keyword = "SHORT"
expiration_date = factory.LazyAttribute(lambda f: now() + timedelta(days=1))
expiration_date = LazyAttribute(lambda f: now() + timedelta(days=1))

class Meta:
model = Mandat

@factory.post_generation
@post_generation
def post(self, create, _, **kwargs):
authorisations = kwargs.get("create_authorisations", None)
if not create or not isinstance(authorisations, Iterable):
Expand All @@ -126,7 +135,7 @@ def post(self, create, _, **kwargs):


class RevokedMandatFactory(MandatFactory):
@factory.post_generation
@post_generation
def post(self, create, _, **kwargs):
authorisations = kwargs.get("create_authorisations", None)
if not create or not isinstance(authorisations, Iterable):
Expand All @@ -141,12 +150,12 @@ def post(self, create, _, **kwargs):


class ExpiredMandatFactory(MandatFactory):
expiration_date = factory.LazyAttribute(lambda f: now() - timedelta(days=365))
expiration_date = LazyAttribute(lambda f: now() - timedelta(days=365))


class AutorisationFactory(factory.DjangoModelFactory):
class AutorisationFactory(DjangoModelFactory):
demarche = "justice"
mandat = factory.SubFactory(MandatFactory)
mandat = SubFactory(MandatFactory)
revocation_date = None

class Meta:
Expand All @@ -161,12 +170,12 @@ class Meta:
model = Autorisation


class ConnectionFactory(factory.DjangoModelFactory):
class ConnectionFactory(DjangoModelFactory):
class Meta:
model = Connection


class JournalFactory(factory.DjangoModelFactory):
class JournalFactory(DjangoModelFactory):
class Meta:
model = Journal

Expand All @@ -182,20 +191,20 @@ def _create(cls, model_class, *args, **kwargs):
class AttestationJournalFactory(JournalFactory):
action = "create_attestation"
is_remote_mandat = False
access_token = factory.Faker("md5")
access_token = Faker("md5")
duree = 6


class DatavizRegionFactory(factory.DjangoModelFactory):
class DatavizRegionFactory(DjangoModelFactory):
class Meta:
model = DatavizRegion


class DatavizDepartmentFactory(factory.DjangoModelFactory):
class DatavizDepartmentFactory(DjangoModelFactory):
class Meta:
model = DatavizDepartment


class DatavizDepartmentsToRegionFactory(factory.DjangoModelFactory):
class DatavizDepartmentsToRegionFactory(DjangoModelFactory):
class Meta:
model = DatavizDepartmentsToRegion
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ black==21.12b0
celery[redis]==5.2.3
coverage==6.2
entrypoints==0.3
factory-boy==2.12.0
factory-boy==3.2.1
flake8==3.8.3
freezegun==0.3.15
gunicorn==20.1.0
Expand Down