Skip to content

Commit

Permalink
tests: merge JobSeekerWithAddressFactory into JobSeekerFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
xavfernandez committed Sep 3, 2024
1 parent e2c1c34 commit bf1f2fe
Show file tree
Hide file tree
Showing 23 changed files with 129 additions and 143 deletions.
20 changes: 10 additions & 10 deletions tests/api/__snapshots__/test_geiq.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@
'previous': None,
'results': list([
dict({
'adresse_code_postal': '58160',
'adresse_ligne_1': '42 Rue du clos de la Grange',
'adresse_code_postal': '35000',
'adresse_ligne_1': '12 rue Georges Bizet',
'adresse_ligne_2': '',
'adresse_ville': 'Sauvigny-les-Bois',
'adresse_ville': 'Rennes',
'auteur_diagnostic': 'GEIQ',
'civilite': 'F',
'criteres_eligibilite': list([
Expand All @@ -105,10 +105,10 @@
'nb_heures_formation': 1664,
'niveau_formation': 'N3',
'niveau_qualification': 'SQ',
'nom': 'Dupont',
'nom': 'Doe',
'poste_occupe': 'N1101',
'precision_prescripteur': "HUDA - Hébergement d'urgence pour demandeurs d'asile",
'prenom': 'Sacha',
'prenom': 'Jane',
'prequalifications': list([
dict({
'code': 'AFPR',
Expand All @@ -122,10 +122,10 @@
'type_qualification': 'CQP',
}),
dict({
'adresse_code_postal': '58160',
'adresse_ligne_1': '42 Rue du clos de la Grange',
'adresse_code_postal': '35000',
'adresse_ligne_1': '12 rue Georges Bizet',
'adresse_ligne_2': '',
'adresse_ville': 'Sauvigny-les-Bois',
'adresse_ville': 'Rennes',
'auteur_diagnostic': 'PRESCRIPTEUR',
'civilite': 'F',
'criteres_eligibilite': list([
Expand All @@ -143,10 +143,10 @@
'nb_heures_formation': 12,
'niveau_formation': 'N3',
'niveau_qualification': 'N3',
'nom': 'Dupont',
'nom': 'Doe',
'poste_occupe': 'N1101',
'precision_prescripteur': 'France Travail',
'prenom': 'Sacha',
'prenom': 'Jane',
'prequalifications': list([
]),
'prescripteur_origine': 'PE',
Expand Down
4 changes: 2 additions & 2 deletions tests/api/test_geiq.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from itou.users.enums import UserKind
from tests.companies.factories import CompanyFactory
from tests.job_applications.factories import JobApplicationFactory, PriorActionFactory
from tests.users.factories import ItouStaffFactory, JobSeekerWithAddressFactory
from tests.users.factories import ItouStaffFactory, JobSeekerFactory


def _api_client():
Expand Down Expand Up @@ -103,7 +103,7 @@ def test_candidatures_geiq_nominal(snapshot):
assert response.status_code == 200
assert response.json() == snapshot(name="empty")

job_seeker = JobSeekerWithAddressFactory(for_snapshot=True, jobseeker_profile__education_level="51")
job_seeker = JobSeekerFactory(for_snapshot=True, jobseeker_profile__education_level="51")

job_application = JobApplicationFactory(
pk=uuid.UUID("bf657b69-3245-430c-b461-09c6792b9504"),
Expand Down
5 changes: 3 additions & 2 deletions tests/asp/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
from itou.common_apps.address.format import compute_hexa_address
from itou.utils.mocks.address_format import BAN_GEOCODING_API_RESULTS_MOCK, mock_get_geocoding_data
from itou.utils.mocks.geocoding import BAN_GEOCODING_API_NO_RESULT_MOCK
from tests.users.factories import JobSeekerFactory, JobSeekerWithAddressFactory
from tests.users.factories import JobSeekerFactory


def _users_with_mock_address(idx):
address = BAN_GEOCODING_API_RESULTS_MOCK[idx]
return JobSeekerWithAddressFactory(
return JobSeekerFactory(
with_address=True,
address_line_1=address.get("address_line_1"),
post_code=address.get("post_code"),
)
Expand Down
14 changes: 7 additions & 7 deletions tests/common_apps/address/test_qpv_zrr_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,30 @@

import logging

from tests.users.factories import JobSeekerFactory, JobSeekerWithAddressFactory
from tests.users.factories import JobSeekerFactory


def test_simple_address():
assert not JobSeekerWithAddressFactory().address_in_qpv
assert not JobSeekerWithAddressFactory().zrr_city_name
assert not JobSeekerFactory(with_address=True).address_in_qpv
assert not JobSeekerFactory(with_address=True).zrr_city_name


def test_address_in_qpv():
job_seeker = JobSeekerWithAddressFactory(with_address_in_qpv=True)
job_seeker = JobSeekerFactory(with_address_in_qpv=True)

assert job_seeker.address_on_one_line == job_seeker.address_in_qpv


def test_city_in_zrr():
job_seeker = JobSeekerWithAddressFactory(with_city_in_zrr=True)
job_seeker = JobSeekerFactory(with_city_in_zrr=True)
city_name, partially_in_zrr = job_seeker.zrr_city_name

assert city_name == job_seeker.city
assert not partially_in_zrr


def test_city_partially_in_zrr():
job_seeker = JobSeekerWithAddressFactory(with_city_partially_in_zrr=True)
job_seeker = JobSeekerFactory(with_city_partially_in_zrr=True)
city_name, partially_in_zrr = job_seeker.zrr_city_name

assert city_name == job_seeker.city
Expand All @@ -37,7 +37,7 @@ def test_city_partially_in_zrr():

def test_zrr_warnings(caplog):
caplog.set_level(logging.WARNING)
job_seeker = JobSeekerWithAddressFactory()
job_seeker = JobSeekerFactory(with_address=True)
job_seeker.zrr_city_name

assert "Can't match INSEE code" in caplog.text
Expand Down
8 changes: 4 additions & 4 deletions tests/employee_record/__snapshots__/test_serializers.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
'dateNaissance': '01/01/1990',
'idItou': 'a08dbdb523633cfc59dfdb297307a1',
'nomNaissance': None,
'nomUsage': 'DUPONT',
'nomUsage': 'DOE',
'passDateDeb': '01/01/2000',
'passDateFin': '01/01/3000',
'passIae': '999999999999',
'prenom': 'SACHA',
'prenom': 'JANE',
'sufPassIae': None,
})
# ---
Expand All @@ -31,11 +31,11 @@
'dateNaissance': '01/01/1990',
'idItou': 'a08dbdb523633cfc59dfdb297307a1',
'nomNaissance': None,
'nomUsage': 'DUPONT',
'nomUsage': 'DOE',
'passDateDeb': '01/01/2000',
'passDateFin': '01/01/3000',
'passIae': '999999999999',
'prenom': 'SACHA',
'prenom': 'JANE',
'sufPassIae': None,
})
# ---
Expand Down
3 changes: 1 addition & 2 deletions tests/gps/test_create_beneficiary.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
from tests.users.factories import (
EmployerFactory,
JobSeekerFactory,
JobSeekerWithAddressFactory,
PrescriberFactory,
)

Expand All @@ -43,7 +42,7 @@ def test_create_job_seeker(_mock, client):
user = prescriber_organization.members.first()
client.force_login(user)

dummy_job_seeker = JobSeekerWithAddressFactory.build(
dummy_job_seeker = JobSeekerFactory.build(
jobseeker_profile__with_hexa_address=True,
jobseeker_profile__with_education_level=True,
with_ban_geoloc_address=True,
Expand Down
3 changes: 1 addition & 2 deletions tests/gps/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from tests.users.factories import (
EmployerFactory,
JobSeekerFactory,
JobSeekerWithAddressFactory,
PrescriberFactory,
)
from tests.utils.htmx.test import assertSoupEqual, update_page_with_htmx
Expand Down Expand Up @@ -216,7 +215,7 @@ def test_my_groups(snapshot, client):


def test_access_as_jobseeker(client):
user = JobSeekerWithAddressFactory()
user = JobSeekerFactory(with_address=True)
client.force_login(user)

response = client.get(reverse("gps:my_groups"))
Expand Down
5 changes: 2 additions & 3 deletions tests/job_applications/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
)
from tests.users.factories import (
JobSeekerFactory,
JobSeekerWithAddressFactory,
PrescriberFactory,
)

Expand All @@ -41,7 +40,7 @@ class Meta:

class Params:
job_seeker_with_address = factory.Trait(
job_seeker=factory.SubFactory(JobSeekerWithAddressFactory, with_mocked_address=True)
job_seeker=factory.SubFactory(JobSeekerFactory, with_mocked_address=True)
)
with_approval = factory.Trait(
state=models.JobApplicationState.ACCEPTED,
Expand Down Expand Up @@ -234,7 +233,7 @@ class JobApplicationWithCompleteJobSeekerProfileFactory(JobApplicationWithApprov
"""

job_seeker = factory.SubFactory(
JobSeekerWithAddressFactory,
JobSeekerFactory,
with_mocked_address=True,
jobseeker_profile__with_hexa_address=True,
jobseeker_profile__with_education_level=True,
Expand Down
11 changes: 6 additions & 5 deletions tests/scripts/test_users_geolocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from django.core import management
from httpx import Response

from tests.users.factories import JobSeekerWithAddressFactory
from tests.users.factories import JobSeekerFactory


def mock_ban_api(user_id):
Expand Down Expand Up @@ -37,7 +37,7 @@ def run_command(*args, **kwargs):


def test_update_dry_run():
JobSeekerWithAddressFactory(is_active=True, without_geoloc=True)
JobSeekerFactory(is_active=True, with_address=True, without_geoloc=True)

out, _ = run_command("update")

Expand All @@ -48,7 +48,7 @@ def test_update_dry_run():

@respx.mock
def test_update_wet_run():
user = JobSeekerWithAddressFactory(
user = JobSeekerFactory(
is_active=True,
without_geoloc=True,
address_line_1="10 rue du Moulin du Gue",
Expand Down Expand Up @@ -81,7 +81,8 @@ def test_export_dry_run():
def test_export_wet_run():
coords = "SRID=4326;POINT (-1.963752 48.658983)"
score = 0.97
JobSeekerWithAddressFactory(
JobSeekerFactory(
with_address=True,
is_active=True,
coords=coords,
geocoding_score=score,
Expand Down Expand Up @@ -109,7 +110,7 @@ def test_import_dry_run():


def test_import_wet_run():
user = JobSeekerWithAddressFactory(is_active=True)
user = JobSeekerFactory(is_active=True, with_address=True)
path = os.path.join(django_settings.IMPORT_DIR, "sample_user_geoloc.csv")

with open(path, "w") as f:
Expand Down
62 changes: 23 additions & 39 deletions tests/users/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,31 +191,14 @@ class Params:
coords="POINT (7.644817 48.515883)",
geocoding_score=0.8745736363636364,
)
for_snapshot = factory.Trait(
public_id="7614fc4b-aef9-4694-ab17-12324300180a",
title="MME",
first_name="Jane",
last_name="Doe",
email="jane.doe@test.local",
phone="0612345678",
birthdate=datetime.date(1990, 1, 1),
address_line_1="12 rue Georges Bizet",
post_code="35000",
city="Rennes",
department="35",
jobseeker_profile__for_snapshot=True,
)

@classmethod
def _adjust_kwargs(cls, **kwargs):
# Deactivate automatic creation of JobSeekerProfile in User.save
# since the RelatedFactory will take care of it
kwargs["_auto_create_job_seeker_profile"] = False
return kwargs


class JobSeekerWithAddressFactory(JobSeekerFactory):
class Params:
with_address = factory.Trait(
address_line_1=factory.Faker("street_address", locale="fr_FR"),
department=factory.fuzzy.FuzzyChoice(DEPARTMENTS.keys()),
post_code=factory.Faker("postalcode"),
city=factory.Faker("city", locale="fr_FR"),
with_geoloc=True,
)
with_address_in_qpv = factory.Trait(
address_line_1="Rue du turfu",
post_code="93300",
Expand All @@ -233,6 +216,10 @@ class Params:
post_code="97429",
city="Petite-Île",
)
with_geoloc = factory.Trait(
coords="POINT(0 0)",
geocoding_score=0.5,
)
without_geoloc = factory.Trait(
coords=None,
geocoding_score=None,
Expand All @@ -241,26 +228,24 @@ class Params:
for_snapshot = factory.Trait(
public_id="7614fc4b-aef9-4694-ab17-12324300180a",
title="MME",
first_name="Sacha",
last_name="Dupont",
first_name="Jane",
last_name="Doe",
email="jane.doe@test.local",
phone="0612345678",
birthdate=datetime.date(1990, 1, 1),
address_line_1="12 rue Georges Bizet",
post_code="35000",
city="Rennes",
department="35",
jobseeker_profile__for_snapshot=True,
address_line_1="42 Rue du clos de la Grange",
post_code="58160",
city="Sauvigny-les-Bois",
)

address_line_1 = factory.Faker("street_address", locale="fr_FR")
department = factory.fuzzy.FuzzyChoice(DEPARTMENTS.keys())
post_code = factory.Faker("postalcode")
city = factory.Faker("city", locale="fr_FR")

coords = "POINT(0 0)"
geocoding_score = 0.5

@classmethod
def _adjust_kwargs(cls, **kwargs):
kwargs = super()._adjust_kwargs(**kwargs)
# Deactivate automatic creation of JobSeekerProfile in User.save
# since the RelatedFactory will take care of it
kwargs["_auto_create_job_seeker_profile"] = False

# Using ZRR or QPV means that we must have some factories / data ready beforehand
# Did not find a better way to do Traits additional setup...
if kwargs.get("with_address_in_qpv"):
Expand All @@ -273,7 +258,6 @@ def _adjust_kwargs(cls, **kwargs):
if kwargs.get("with_city_partially_in_zrr"):
ZRRFactory(insee_code="97405")
create_city_partially_in_zrr()

return kwargs

@factory.post_generation
Expand Down
14 changes: 9 additions & 5 deletions tests/users/test_management_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
PrescriberOrganizationFactory,
PrescriberPoleEmploiFactory,
)
from tests.users.factories import EmployerFactory, JobSeekerFactory, JobSeekerWithAddressFactory
from tests.users.factories import EmployerFactory, JobSeekerFactory
from tests.utils.test import TestCase


Expand Down Expand Up @@ -1010,10 +1010,14 @@ def test_wet_run_limits_history_to_a_year(self, caplog, respx_mock, settings):

@freeze_time("2023-05-01")
def test_update_job_seeker_coords(settings, capsys, respx_mock):
js1 = JobSeekerWithAddressFactory(coords="POINT (2.387311 48.917735)", geocoding_score=0.65) # score too low
js2 = JobSeekerWithAddressFactory(coords=None, geocoding_score=0.9) # no coords
js3 = JobSeekerWithAddressFactory(coords="POINT (5.43567 12.123876)", geocoding_score=0.76) # score too low
JobSeekerWithAddressFactory(with_address_in_qpv=True)
js1 = JobSeekerFactory(
with_address=True, coords="POINT (2.387311 48.917735)", geocoding_score=0.65
) # score too low
js2 = JobSeekerFactory(with_address=True, coords=None, geocoding_score=0.9) # no coords
js3 = JobSeekerFactory(
with_address=True, coords="POINT (5.43567 12.123876)", geocoding_score=0.76
) # score too low
JobSeekerFactory(with_address_in_qpv=True)

settings.API_BAN_BASE_URL = "https://geo.foo"
respx_mock.post("https://geo.foo/search/csv/").respond(
Expand Down
Loading

0 comments on commit bf1f2fe

Please sign in to comment.