Skip to content

Commit

Permalink
add test on matching function
Browse files Browse the repository at this point in the history
  • Loading branch information
Perrine Letellier committed Dec 20, 2024
1 parent 6b9ed71 commit b150b4b
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 12 deletions.
2 changes: 2 additions & 0 deletions data/etl/teleicare_history/extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ def match_companies_on_siret_or_vat():
Le matching pourrait aussi être fait sur
* Q(social_name__icontains=etab.etab_raison_sociale)
* Q(commercial_name__icontains=etab.etab_enseigne)
* Q(email__icontains=etab.etab_courriel)
* Q(phone_number__icontains=etab.etab_telephone)
Mais il serait moins précis.
"""
nb_vat_match = 0
Expand Down
27 changes: 27 additions & 0 deletions data/factories/teleicare_history/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import random
import string

import factory
import faker
from phonenumber_field.phonenumber import PhoneNumber

from data.choices import CountryChoices
from data.models.teleicare_history.etablissement import IcaEtablissement
from data.utils.string_utils import make_random_str
from data.factories.company import _make_siret, _make_vat, _make_phone_number


class EtablissementFactory(factory.django.DjangoModelFactory):
class Meta:
model = IcaEtablissement

etab_ident = factory.Sequence(lambda n: n + 1)
etab_raison_sociale = factory.Faker("company", locale="FR")
etab_enseigne = factory.Faker("company", locale="FR")
etab_siret = factory.LazyFunction(_make_siret)
etab_numero_tva_intra = factory.LazyFunction(_make_siret)
pays_ident = factory.Faker("pyint", min_value=0, max_value=200)
etab_nb_compte_autorise = factory.Faker("pyint", min_value=0, max_value=5)
# contact
etab_telephone = factory.LazyFunction(_make_phone_number)
etab_courriel = factory.Faker("email", locale="FR")
24 changes: 12 additions & 12 deletions data/models/teleicare_history/etablissement.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ class IcaEtablissement(models.Model):
cog_ident = models.IntegerField(blank=True, null=True)
etab_ident_parent = models.IntegerField(blank=True, null=True)
pays_ident = models.IntegerField()
etab_siret = models.TextField(blank=True, null=True)
etab_numero_tva_intra = models.TextField(blank=True, null=True)
etab_siret = models.CharField(blank=True, null=True)
etab_numero_tva_intra = models.CharField(blank=True, null=True)
etab_raison_sociale = models.TextField()
etab_enseigne = models.TextField(blank=True, null=True)
etab_adre_ville = models.TextField(blank=True, null=True)
etab_adre_cp = models.TextField(blank=True, null=True)
etab_adre_voie = models.TextField(blank=True, null=True)
etab_adre_comp = models.TextField(blank=True, null=True)
etab_adre_comp2 = models.TextField(blank=True, null=True)
etab_adre_dist = models.TextField(blank=True, null=True)
etab_telephone = models.TextField(blank=True, null=True)
etab_fax = models.TextField(blank=True, null=True)
etab_courriel = models.TextField(blank=True, null=True)
etab_site_internet = models.TextField(blank=True, null=True)
etab_adre_ville = models.CharField(blank=True, null=True)
etab_adre_cp = models.CharField(blank=True, null=True)
etab_adre_voie = models.CharField(blank=True, null=True)
etab_adre_comp = models.CharField(blank=True, null=True)
etab_adre_comp2 = models.CharField(blank=True, null=True)
etab_adre_dist = models.CharField(blank=True, null=True)
etab_telephone = models.CharField(blank=True, null=True)
etab_fax = models.CharField(blank=True, null=True)
etab_courriel = models.CharField(blank=True, null=True)
etab_site_internet = models.CharField(blank=True, null=True)
etab_ica_faconnier = models.BooleanField(blank=True, null=True)
etab_ica_fabricant = models.BooleanField(blank=True, null=True)
etab_ica_conseil = models.BooleanField(blank=True, null=True)
Expand Down
65 changes: 65 additions & 0 deletions data/tests/test_teleicare_history_importer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from django.db import connection
from django.test import TestCase

from data.etl.teleicare_history.extractor import match_companies_on_siret_or_vat
from data.factories.company import CompanyFactory, _make_siret, _make_vat
from data.factories.teleicare_history import EtablissementFactory
from data.models.teleicare_history.etablissement import IcaEtablissement


class TeleicareHistoryImporterTestCase(TestCase):
"""
Cette classe vise à tester les fonction liées à l'import d'historique de la plateforme TeleIcare
"""

def setUp(self):
"""
Adapted from: https://stackoverflow.com/a/49800437
"""
super().setUp()
with connection.schema_editor() as schema_editor:
schema_editor.create_model(IcaEtablissement)

if IcaEtablissement._meta.db_table not in connection.introspection.table_names():
raise ValueError(
"Table `{table_name}` is missing in test database.".format(
table_name=IcaEtablissement._meta.db_table
)
)

def tearDown(self):
super().tearDown()

with connection.schema_editor() as schema_editor:
schema_editor.delete_model(IcaEtablissement)

def test_match_companies_on_siret_or_vat(self):
"""
Une entreprise enregistrée dans Teleicare ayant le même SIRET ou n° TVA intracom
qu'une entreprise enregistrée dans Compl'Alim doit être liée par le siccrf_id
"""
# create_model_table(IcaEtablissement)
siret = _make_siret()
etablissement_with_siret = EtablissementFactory(etab_siret=siret)
company_with_siret = CompanyFactory(siret=siret)

vat = _make_vat()
etablissement_with_vat = EtablissementFactory(etab_siret=None, etab_numero_tva_intra=vat)
company_with_vat = CompanyFactory(vat=vat)

random_company = CompanyFactory()
random_etablissement = EtablissementFactory()

match_companies_on_siret_or_vat()
company_with_siret.refresh_from_db()
etablissement_with_siret.refresh_from_db()
company_with_vat.refresh_from_db()
etablissement_with_vat.refresh_from_db()
random_company.refresh_from_db()
random_etablissement.refresh_from_db()

# self.assertEqual(company_with_siret.siccrf_id, etablissement_with_siret.etab_ident)
self.assertEqual(company_with_vat.siccrf_id, etablissement_with_vat.etab_ident)
self.assertNotEqual(random_company.siccrf_id, random_etablissement.etab_ident)
self.assertEqual(random_company.siccrf_id, None)
# delete_model_table(IcaEtablissement)

0 comments on commit b150b4b

Please sign in to comment.