Skip to content

Commit

Permalink
3025 As an STT user, I need an accurate error report when I space-fil…
Browse files Browse the repository at this point in the history
…l `COUNTY_FIPS_CODE` (#3045)

* 3025 Corrected the None case and added tests

* added import for from DataFileSummaryFactory

* linting

* correct the failing test

* fixed linting

* corrected validator message

---------

Co-authored-by: Alex P <[email protected]>
  • Loading branch information
raftmsohani and ADPennington authored Jul 16, 2024
1 parent afd1e63 commit 3c36b81
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
79 changes: 78 additions & 1 deletion tdrs-backend/tdpservice/parsers/test/test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
from tdpservice.search_indexes.models.tribal import Tribal_TANF_T5, Tribal_TANF_T6, Tribal_TANF_T7
from tdpservice.search_indexes.models.ssp import SSP_M1, SSP_M2, SSP_M3, SSP_M4, SSP_M5, SSP_M6, SSP_M7
from tdpservice.search_indexes import documents
from tdpservice.parsers.test.factories import DataFileSummaryFactory, ParsingFileFactory
from tdpservice.data_files.models import DataFile
from tdpservice.parsers import util
from .. import schema_defs, aggregates
from elasticsearch.helpers.errors import BulkIndexError
import logging
Expand All @@ -24,7 +26,41 @@

settings.GENERATE_TRAILER_ERRORS = True

# TODO: the name of this test doesn't make perfect sense anymore since it will always have errors and no records now.
@pytest.fixture
def test_datafile(stt_user, stt):
"""Fixture for small_correct_file."""
return util.create_test_datafile('small_correct_file.txt', stt_user, stt)


@pytest.fixture
def test_header_datafile(stt_user, stt):
"""Fixture for header test."""
return util.create_test_datafile('tanf_section1_header_test.txt', stt_user, stt)


@pytest.fixture
def dfs():
"""Fixture for DataFileSummary."""
return DataFileSummaryFactory.build()


@pytest.fixture
def t2_invalid_dob_file():
"""Fixture for T2 file with an invalid DOB."""
parsing_file = ParsingFileFactory(
year=2021,
quarter='Q1',
file__name='t2_invalid_dob_file.txt',
file__section='Active Case Data',
file__data=(b'HEADER20204A25 TAN1ED\n'
b'T22020101111111111212Q897$9 3WTTTTTY@W222122222222101221211001472201140000000000000000000000000'
b'0000000000000000000000000000000000000000000000000000000000291\n'
b'TRAILER0000001 ')
)
return parsing_file

# TODO: the name of this test doesn't make perfect sense anymore since it will always have errors now.
# TODO: parametrize and merge with test_zero_filled_fips_code_file
@pytest.mark.django_db
def test_parse_small_correct_file(small_correct_file, dfs):
"""Test parsing of small_correct_file."""
Expand Down Expand Up @@ -1695,6 +1731,47 @@ def test_parse_m5_cat2_invalid_23_24_file(m5_cat2_invalid_23_24_file, dfs):
for e in parser_errors:
assert e.error_message in error_msgs


@pytest.fixture
def test_file_zero_filled_fips_code():
"""Fixture for T1 file with an invalid CITIZENSHIP_STATUS."""
parsing_file = ParsingFileFactory(
year=2021,
quarter='Q1',
file__name='t3_invalid_citizenship_file.txt',
file__section='Active Case Data',
file__data=(b'HEADER20241A01000TAN2ED\n'
b'T1202401 2132333 0140951112 43312 03 0 0 2 554145' +
b' 0 0 0 0 0 0 0 0 0 0222222 0 02229 22 \n' +
b'T2202401 21323333219550117WT@TB9BT92122222222223 1329911 34' +
b' 32 699 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0' +
b' 0 0 0 0 0 01623 0 0 0\n' +
b'T2202401 21323333219561102WTT@WBP992122221222222 2329911 28' +
b' 32 699 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0' +
b' 0 0 0 0 0 01432 0 0 0\n' +
b'T3202401 2132333120070906WT@@#ZY@W212222122 63981 0 012' +
b'0050201WTTYT#TT0212222122 63981 0 0 \n' +
b'TRAILER 4 ')
)
return parsing_file


@pytest.mark.django_db()
def test_zero_filled_fips_code_file(test_file_zero_filled_fips_code, dfs):
"""Test parsing a file with zero filled FIPS code."""
# TODO: this test can be merged as parametrized test with "test_parse_small_correct_file"
dfs.datafile = test_file_zero_filled_fips_code
test_file_zero_filled_fips_code.year = 2024
test_file_zero_filled_fips_code.quarter = 'Q2'
test_file_zero_filled_fips_code.save()

parse.parse_datafile(test_file_zero_filled_fips_code, dfs)

parser_errors = ParserError.objects.filter(file=test_file_zero_filled_fips_code)
assert 'T1 Item 2 (County FIPS Code): field is required but a value was not' + \
' provided.' in [i.error_message for i in parser_errors]


@pytest.mark.parametrize("file, batch_size, model, record_type, num_errors", [
('tanf_s1_exact_dup_file', 10000, TANF_T1, "T1", 3),
('tanf_s1_exact_dup_file', 1, TANF_T1, "T1", 3), # This forces an in memory and database deletion of records.
Expand Down
2 changes: 2 additions & 0 deletions tdrs-backend/tdpservice/parsers/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,7 @@ def ssp_ssn_decryption_func(value, **kwargs):
def zero_pad(digits):
"""Zero pad a string."""
def transform(value, **kwargs):
if value is None:
return None
return value.lstrip().zfill(digits)
return transform

0 comments on commit 3c36b81

Please sign in to comment.