-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: tests for the get_geiq_df() function
- Loading branch information
Showing
3 changed files
with
152 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
import pytest | ||
from faker import Faker | ||
|
||
from itou.companies.management.commands.import_geiq import get_geiq_df | ||
from itou.utils.export import generate_excel_sheet | ||
from tests.utils.test import create_fake_postcode | ||
|
||
|
||
faker = Faker() | ||
|
||
FILE_HEADERS = ["Nom", "Rue", "Rue (suite)", "Code Postal", "Ville", "SIRET", "e-mail"] | ||
|
||
|
||
def generate_data(rows=185, rows_with_empty_siret=0, rows_with_empty_email=0): | ||
data = [] | ||
for _ in range(rows): | ||
if rows_with_empty_siret > 0: | ||
siret = "" | ||
rows_with_empty_siret -= 1 | ||
else: | ||
siret = faker.numerify("1#############") | ||
|
||
if rows_with_empty_email > 0: | ||
email = "" | ||
rows_with_empty_email -= 1 | ||
else: | ||
email = faker.email() | ||
|
||
data.append( | ||
[ | ||
faker.name(), | ||
faker.street_address(), | ||
"Sous l'escalier", | ||
create_fake_postcode(), | ||
faker.city(), | ||
siret, | ||
email, | ||
] | ||
) | ||
return data | ||
|
||
|
||
def test_get_geiq_df(sftp_directory, faker): | ||
# Correct data | ||
rows = 185 | ||
rows_with_empty_siret = 0 | ||
rows_with_empty_email = 0 | ||
data = generate_data( | ||
rows=rows, rows_with_empty_siret=rows_with_empty_siret, rows_with_empty_email=rows_with_empty_email | ||
) | ||
file_path = sftp_directory.joinpath(faker.geiq_filename()) | ||
with open(file_path, "wb") as xlsxfile: | ||
workbook = generate_excel_sheet(FILE_HEADERS, data) | ||
workbook.save(xlsxfile) | ||
df, info_stats = get_geiq_df(file_path) | ||
assert df.shape == (rows, 8) | ||
assert info_stats == { | ||
"rows_in_file": rows, | ||
"rows_with_a_siret": rows, | ||
"rows_after_deduplication": rows, | ||
"rows_with_empty_email": rows_with_empty_email, | ||
} | ||
|
||
# File too small, need at least 150 rows | ||
rows = 140 | ||
rows_with_empty_siret = 0 | ||
rows_with_empty_email = 0 | ||
data = generate_data( | ||
rows=rows, rows_with_empty_siret=rows_with_empty_siret, rows_with_empty_email=rows_with_empty_email | ||
) | ||
file_path = sftp_directory.joinpath(faker.geiq_filename()) | ||
with open(file_path, "wb") as xlsxfile: | ||
workbook = generate_excel_sheet(FILE_HEADERS, data) | ||
workbook.save(xlsxfile) | ||
with pytest.raises(AssertionError): | ||
df, info_stats = get_geiq_df(file_path) | ||
|
||
# Too many missing emails | ||
rows = 185 | ||
rows_with_empty_siret = 0 | ||
rows_with_empty_email = 100 | ||
data = generate_data( | ||
rows=rows, rows_with_empty_siret=rows_with_empty_siret, rows_with_empty_email=rows_with_empty_email | ||
) | ||
file_path = sftp_directory.joinpath(faker.geiq_filename()) | ||
with open(file_path, "wb") as xlsxfile: | ||
workbook = generate_excel_sheet(FILE_HEADERS, data) | ||
workbook.save(xlsxfile) | ||
with pytest.raises(AssertionError): | ||
df, info_stats = get_geiq_df(file_path) | ||
|
||
# Some missing emails | ||
rows = 185 | ||
rows_with_empty_siret = 0 | ||
rows_with_empty_email = 20 | ||
data = generate_data( | ||
rows=rows, rows_with_empty_siret=rows_with_empty_siret, rows_with_empty_email=rows_with_empty_email | ||
) | ||
file_path = sftp_directory.joinpath(faker.geiq_filename()) | ||
with open(file_path, "wb") as xlsxfile: | ||
workbook = generate_excel_sheet(FILE_HEADERS, data) | ||
workbook.save(xlsxfile) | ||
df, info_stats = get_geiq_df(file_path) | ||
assert df.shape == (rows - rows_with_empty_email, 8) | ||
assert info_stats == { | ||
"rows_in_file": rows, | ||
"rows_with_a_siret": rows, | ||
"rows_after_deduplication": rows, | ||
"rows_with_empty_email": rows_with_empty_email, | ||
} | ||
|
||
# Too many missing sirets | ||
rows = 185 | ||
rows_with_empty_siret = 100 | ||
rows_with_empty_email = 0 | ||
data = generate_data( | ||
rows=rows, rows_with_empty_siret=rows_with_empty_siret, rows_with_empty_email=rows_with_empty_email | ||
) | ||
file_path = sftp_directory.joinpath(faker.geiq_filename()) | ||
with open(file_path, "wb") as xlsxfile: | ||
workbook = generate_excel_sheet(FILE_HEADERS, data) | ||
workbook.save(xlsxfile) | ||
with pytest.raises(AssertionError): | ||
df, info_stats = get_geiq_df(file_path) | ||
|
||
# Some missing sirets | ||
rows = 185 | ||
rows_with_empty_siret = 2020 | ||
rows_with_empty_email = 0 | ||
data = generate_data( | ||
rows=rows, rows_with_empty_siret=rows_with_empty_siret, rows_with_empty_email=rows_with_empty_email | ||
) | ||
file_path = sftp_directory.joinpath(faker.geiq_filename()) | ||
with open(file_path, "wb") as xlsxfile: | ||
workbook = generate_excel_sheet(FILE_HEADERS, data) | ||
workbook.save(xlsxfile) | ||
df, info_stats = get_geiq_df(file_path) | ||
assert df.shape == (rows - rows_with_empty_siret, 8) | ||
assert info_stats == { | ||
"rows_in_file": rows, | ||
"rows_with_a_siret": rows - rows_with_empty_siret, | ||
"rows_after_deduplication": rows, | ||
"rows_with_empty_email": 0, | ||
} | ||
|
||
# TODO(ewen): duplicated rows |