Skip to content
Draft
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
1 change: 1 addition & 0 deletions django/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ dev = [
"pytest-django",
"ruff",
"pre-commit>=4.3.0", # https://github.com/pre-commit/pre-commit
"factory-boy", # https://github.com/FactoryBoy/factory_boy
]

[tool.pytest.ini_options]
Expand Down
188 changes: 188 additions & 0 deletions django/tests/core/factories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
from json import loads
from pathlib import Path

import factory.fuzzy

from docurba.core.enums import CommuneType
from docurba.core.models import (
Commune,
CommuneProcedure,
Departement,
Procedure,
Region,
TypeCollectivite,
TypeDocument,
)

REGIONS = {
"76": "Occitanie",
"84": "Auvergne-Rhône-Alpes",
"93": "Provence-Alpes-Côte d'Azur",
}

# For the moment, thoses files are copied from Nuxt's middleware to share the same data.
# Same as nuxt/server-middleware/Data/regions.json
# These files have been used when populating the Django tables.
# See the `collectivites_en_base` management command.

# Subset of nuxt/server-middleware/Data/departements.json
# with departements 13, 30 and 84 and their communes.
# TODO: replace by TESTS_DIR

Check warning on line 30 in django/tests/core/factories.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Complete the task associated to this "TODO" comment.

See more on https://sonarcloud.io/project/issues?id=MTES-MCT_Docurba&issues=AZ1umYkCO3px3FwJYAUX&open=AZ1umYkCO3px3FwJYAUX&pullRequest=1982
DEPARTEMENTS_DICT = loads(
Path(Path(__file__).parent / "geo_data" / "departements.json").read_text()
)
DEPARTEMENTS = {
departement["code"]: {
"name": departement["intitule"],
"region_insee_code": departement["region"]["code"],
}
for departement in DEPARTEMENTS_DICT
}


def generate_communes() -> dict:
# 661 communes
communes = {}
for departement in DEPARTEMENTS_DICT:
for commune in departement["communes"]:
communes[commune["code"]] = {
"name": commune["intitule"],
"departement_insee_code": departement["code"],
"region_insee_code": departement["region"]["code"],
}
return communes


COMMUNES = generate_communes()


class RegionFactory(factory.django.DjangoModelFactory):
class Meta:
model = Region
django_get_or_create = ("code_insee",)

code_insee = factory.fuzzy.FuzzyChoice(REGIONS.keys())
nom = factory.LazyAttribute(lambda o: REGIONS[o.code_insee])


class DepartementFactory(factory.django.DjangoModelFactory):
class Meta:
model = Departement
django_get_or_create = ("code_insee",)

code_insee = factory.fuzzy.FuzzyChoice(DEPARTEMENTS.keys())
region = factory.SubFactory(
RegionFactory,
code_insee=factory.LazyAttribute(
lambda o: DEPARTEMENTS[o.factory_parent.code_insee]["region_insee_code"]
),
)


class CollectiviteFactory(factory.django.DjangoModelFactory):
class Meta:
model = Commune
django_get_or_create = ("code_insee_unique",)

code_insee_unique = "200035087" # factory.fuzzy.FuzzyChoice(COMMUNES.keys())
type = TypeCollectivite.CC # Don't mess with COMD and COMA
id = factory.LazyFunction(
lambda: (
f"{factory.SelfAttribute('code_insee_unique')}_{factory.SelfAttribute('type')}"
)
)
nom = "CA Terre de Provence" # factory.LazyAttribute(lambda o: COMMUNES[o.code_insee_unique]["name"])
competence_plan = False
competence_schema = False
departement = factory.SubFactory(
DepartementFactory,
code_insee="13",
# code_insee=factory.LazyAttribute(

Check warning on line 100 in django/tests/core/factories.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this commented out code.

See more on https://sonarcloud.io/project/issues?id=MTES-MCT_Docurba&issues=AZ1umYkCO3px3FwJYAUY&open=AZ1umYkCO3px3FwJYAUY&pullRequest=1982
# lambda o: COMMUNES[o.factory_parent.code_insee_unique][
# "departement_insee_code"
# ]
# ),
)


class CommuneFactory(factory.django.DjangoModelFactory):
class Meta:
model = Commune
django_get_or_create = ("code_insee_unique",)

# Collectivite fields
code_insee_unique = factory.fuzzy.FuzzyChoice(COMMUNES.keys())
type = CommuneType.COM # Don't mess with COMD and COMA
nom = factory.LazyAttribute(lambda o: COMMUNES[o.code_insee_unique]["name"])
competence_plan = False
competence_schema = False
# adhesions = models.ManyToManyField(

Check warning on line 119 in django/tests/core/factories.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this commented out code.

See more on https://sonarcloud.io/project/issues?id=MTES-MCT_Docurba&issues=AZ1umYkCO3px3FwJYAUZ&open=AZ1umYkCO3px3FwJYAUZ&pullRequest=1982
# "self", related_name="collectivites_adherentes", symmetrical=False
# )

# Commune only fields
# procedures = models.ManyToManyField(
# Procedure, through="CommuneProcedure", related_name="perimetre"
# )
# TODO: Trait (is optional)

Check warning on line 127 in django/tests/core/factories.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Complete the task associated to this "TODO" comment.

See more on https://sonarcloud.io/project/issues?id=MTES-MCT_Docurba&issues=AZ1umYkCO3px3FwJYAUa&open=AZ1umYkCO3px3FwJYAUa&pullRequest=1982
# intercommunalite = models.ForeignKey(
# Collectivite, models.DO_NOTHING, null=True, related_name="communes"
# )
# TODO: Trait

Check warning on line 131 in django/tests/core/factories.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Complete the task associated to this "TODO" comment.

See more on https://sonarcloud.io/project/issues?id=MTES-MCT_Docurba&issues=AZ1umYkCO3px3FwJYAUb&open=AZ1umYkCO3px3FwJYAUb&pullRequest=1982
# nouvelle = models.ForeignKey(
# "self", models.DO_NOTHING, null=True, related_name="deleguee"
# )
# TODO: post_generation because of the Materialized View.

Check warning on line 135 in django/tests/core/factories.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Complete the task associated to this "TODO" comment.

See more on https://sonarcloud.io/project/issues?id=MTES-MCT_Docurba&issues=AZ1umYkCO3px3FwJYAUc&open=AZ1umYkCO3px3FwJYAUc&pullRequest=1982
# adhesions_deep = models.ManyToManyField(
# Collectivite,
# through="ViewCommuneAdhesionsDeep",
# related_name="communes_adherentes_deep",
# )

departement = factory.SubFactory(
DepartementFactory,
code_insee=factory.LazyAttribute(
lambda o: COMMUNES[o.factory_parent.code_insee_unique][
"departement_insee_code"
]
),
)

@factory.lazy_attribute
def id(self) -> str:
return f"{self.code_insee_unique}_{self.type}"


class ProcedureFactory(factory.django.DjangoModelFactory):
class Meta:
model = Procedure

collectivite_porteuse = factory.SubFactory(CollectiviteFactory)
doc_type = factory.fuzzy.FuzzyChoice(TypeDocument)

@factory.post_generation
def with_perimetre(obj, create, extracted, **kwargs) -> None:

Check failure on line 164 in django/tests/core/factories.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rename "obj" to "self" or add the missing "self" parameter.

See more on https://sonarcloud.io/project/issues?id=MTES-MCT_Docurba&issues=AZ1umYkCO3px3FwJYAUd&open=AZ1umYkCO3px3FwJYAUd&pullRequest=1982
a = "a"

Check warning on line 165 in django/tests/core/factories.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove the unused local variable "a".

See more on https://sonarcloud.io/project/issues?id=MTES-MCT_Docurba&issues=AZ1umYkCO3px3FwJYAUe&open=AZ1umYkCO3px3FwJYAUe&pullRequest=1982
if not create:
return
perimetre = extracted or CommuneFactory()
for commune in perimetre:
CommuneProcedureFactory(
procedure=obj,
collectivite_code=commune.code_insee_unique,
collectivite_type=commune.type,
departement=commune.departement.code_insee,
)


class CommuneProcedureFactory(factory.django.DjangoModelFactory):
class Meta:
model = CommuneProcedure

procedure = factory.SubFactory(ProcedureFactory)
collectivite_code = factory.fuzzy.FuzzyChoice(COMMUNES.keys())
collectivite_type = CommuneType.COM
opposable = False
departement = factory.LazyAttribute(
lambda o: COMMUNES[o.collectivite_code]["departement_insee_code"]
)
Loading
Loading