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

Retirer les CommuneFactory et CountryFactory #5591

Merged
merged 6 commits into from
Feb 13, 2025
Merged

Conversation

francoisfreitag
Copy link
Contributor

@francoisfreitag francoisfreitag commented Feb 11, 2025

🤔 Pourquoi ?

Être plus proche de la réalité, où nous utilisons le référentiel ASP sans créer ces objets.
Notamment, lors de la création d’un candidat, sa commune de naissance devrait être valide le jour de sa naissance. Au lieu d’apprendre aux factories comment créer ces communes, recherchons une commune existante.

@francoisfreitag francoisfreitag added the no-changelog Ne doit pas figurer dans le journal des changements. label Feb 11, 2025
@francoisfreitag francoisfreitag self-assigned this Feb 11, 2025
tests/www/apply/test_process.py Show resolved Hide resolved
# Job seeker born outside of France
country = CountryOutsideEuropeFactory()
country = Country.objects.order_by("?").exclude(group=Country.Group.FRANCE).first()
Copy link
Contributor

Choose a reason for hiding this comment

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

Est-ce qu'il ne faudrait pas directement fournir un Trait born_outside_of_france ?

Copy link
Contributor

Choose a reason for hiding this comment

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

born_in_france=False avec un Maybe ? Me semble que ça devrais fonctionner

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Deal !

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Je ne suis pas trop fan de lui donner forcément un pays de naissance, si c’est bien ta suggestion. Je suis parti sur la solution de Xavier : born_in_france, born_outside_france.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ce que j'avais en tête :

        born_in_france = factory.Maybe('born_in_france', yes_declaration=factory.Trait(
            jobseeker_profile__birth_place=factory.LazyAttribute(
                lambda instance: Commune.objects.order_by("?")
                .filter(
                    start_date__lte=instance.birthdate,
                    end_date__gte=instance.birthdate,
                )
                .first(),
            ),
            jobseeker_profile__birth_country=factory.LazyAttribute(lambda _: Country.objects.get(name="FRANCE")),
        ), no_declaration=factory.Trait(
            jobseeker_profile__birth_country=factory.LazyAttribute(
                lambda _: Country.objects.order_by("?").exclude(group=Country.Group.FRANCE).first()
            ),
        ))

Mais ça ne fonctionne pas :(, a priori faut faire ça :

        born_in_france = factory.Trait(
            jobseeker_profile__birth_place=factory.Maybe(
                "born_in_france",
                factory.LazyAttribute(
                    lambda instance: Commune.objects.order_by("?")
                    .filter(
                        start_date__lte=instance.birthdate,
                        end_date__gte=instance.birthdate,
                    )
                    .first(),
                ),
            ),
            jobseeker_profile__birth_country=factory.Maybe(
                factory.LazyAttribute(lambda _: Country.objects.get(name="FRANCE")),
                factory.LazyAttribute(
                    lambda _: Country.objects.order_by("?").exclude(group=Country.Group.FRANCE).first()
                ),
            ),
        )

Mais c'est vraiment pas ouf :´(.


# Europe and Commune filled
profile.birth_country = asp.CountryEuropeFactory()
profile.birth_country = not_france
Copy link
Contributor

Choose a reason for hiding this comment

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

Techniquement cette ligne n'est pas utile, mais si tu veux la garder pour être plus explicite alors je la mettrais après .birth_place = asp.CommuneFactory() pour avoir le même schéma dans les deux blocs et/ou un petit commentaire pour délimiter le cas testé.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Elle a été retirée, c’est du state invalide (un pays de naissance hors de France et une commune).

tests/users/factories.py Show resolved Hide resolved
# Job seeker born outside of France
country = CountryOutsideEuropeFactory()
country = Country.objects.order_by("?").exclude(group=Country.Group.FRANCE).first()
Copy link
Contributor

Choose a reason for hiding this comment

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

born_in_france=False avec un Maybe ? Me semble que ça devrais fonctionner

tests/www/apply/test_process.py Show resolved Hide resolved
bob.jobseeker_profile.save()
dylan.jobseeker_profile.birth_place = CommuneFactory()
dylan.jobseeker_profile.birth_country = CountryFactory()
dylan.jobseeker_profile.save()
Copy link
Collaborator

Choose a reason for hiding this comment

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

J'aime !

@@ -172,7 +167,12 @@ class Params:
)
.first(),
),
jobseeker_profile__birth_country=factory.LazyAttribute(country_france),
jobseeker_profile__birth_country=factory.LazyAttribute(lambda _: Country.objects.get(name="FRANCE")),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Pourquoi ne pas avoir gardé le cache ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Parce que je pense qu’il ne sert pas beaucoup (pas d’impact notable sur la durée des tests) et #5591 (comment).

Copy link
Collaborator

@celine-m-s celine-m-s left a comment

Choose a reason for hiding this comment

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

🤩

# Job seeker born outside of France
country = CountryOutsideEuropeFactory()
country = Country.objects.order_by("?").exclude(group=Country.Group.FRANCE).first()
Copy link
Contributor

Choose a reason for hiding this comment

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

Ce que j'avais en tête :

        born_in_france = factory.Maybe('born_in_france', yes_declaration=factory.Trait(
            jobseeker_profile__birth_place=factory.LazyAttribute(
                lambda instance: Commune.objects.order_by("?")
                .filter(
                    start_date__lte=instance.birthdate,
                    end_date__gte=instance.birthdate,
                )
                .first(),
            ),
            jobseeker_profile__birth_country=factory.LazyAttribute(lambda _: Country.objects.get(name="FRANCE")),
        ), no_declaration=factory.Trait(
            jobseeker_profile__birth_country=factory.LazyAttribute(
                lambda _: Country.objects.order_by("?").exclude(group=Country.Group.FRANCE).first()
            ),
        ))

Mais ça ne fonctionne pas :(, a priori faut faire ça :

        born_in_france = factory.Trait(
            jobseeker_profile__birth_place=factory.Maybe(
                "born_in_france",
                factory.LazyAttribute(
                    lambda instance: Commune.objects.order_by("?")
                    .filter(
                        start_date__lte=instance.birthdate,
                        end_date__gte=instance.birthdate,
                    )
                    .first(),
                ),
            ),
            jobseeker_profile__birth_country=factory.Maybe(
                factory.LazyAttribute(lambda _: Country.objects.get(name="FRANCE")),
                factory.LazyAttribute(
                    lambda _: Country.objects.order_by("?").exclude(group=Country.Group.FRANCE).first()
                ),
            ),
        )

Mais c'est vraiment pas ouf :´(.

post_data = {
"email": "[email protected]",
"title": "M",
"first_name": "Bob",
"last_name": "Saint Clar",
"birthdate": birthdate.isoformat(),
"birth_country": birth_country.pk,
"birth_country": user.jobseeker_profile.birth_country_id,
Copy link
Contributor

Choose a reason for hiding this comment

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

Tu pourrais le laisser inchangé non ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

En effet. Initialement, je pensais dégager complètement la variable birth_country, mais elle est utilisée dans le dernier assert du test pour vérifier que le pays de naissance est toujours le même pays.
Je ne suis pas convaincu de la pertinence de cet assert, mais c’est pas le but de cette PR ;)

The JobSeekerFactory and JobSeekerProfileFactory are in use in the
application at large.

The note about the fixtures is now documented with conftest.py (always
load the fixture, for the whole session), so the developer shouldn’t
have to worry about it anymore.
The object just got created by the factory, no need to load it from the
database.
Ensure the birth_place start_date and end_date attribute are around the
birthdate, so that data is realistic.

Updated the factory to use actual objects from the ASP and not a fakes,
as the application doesn’t manage Commune and Country objects.

Remove the with_birth_place trait, as it would setup an incomplete
object (birth country isn’t France). It was only used once and was
merged into the born_in_france trait.
Use the ASP reference instead to be more realistic, as the application
does not create Country objects.

Removed tests for impossible or unused scenario:
- european country vs world country, as it makes no difference to the
  application.
- filling commune and selecting a world country, that’s invalid state
Use the ASP reference instead to be more realistic, as the application
does not create Commune objects.
@francoisfreitag francoisfreitag added this pull request to the merge queue Feb 13, 2025
Merged via the queue into master with commit 6f6b233 Feb 13, 2025
11 checks passed
@francoisfreitag francoisfreitag deleted the ff/factories branch February 13, 2025 12:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no-changelog Ne doit pas figurer dans le journal des changements.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants