Skip to content

Commit

Permalink
Add basic test suite for COI (#1518)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattbowen-usds committed Aug 2, 2022
1 parent a6ab3cd commit 073c1fe
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# pylint: disable=protected-access
from unittest import mock
import pathlib
import requests

from data_pipeline.etl.sources.child_opportunity_index.etl import (
ChildOpportunityIndex,
)
from data_pipeline.tests.sources.example.test_etl import TestETL
from data_pipeline.utils import get_module_logger

logger = get_module_logger(__name__)


class TestChildOpportunityIndexETL(TestETL):
"""Tests the COI ETL.
This uses pytest-snapshot.
To update individual snapshots: $ poetry run pytest
data_pipeline/tests/sources/national_risk_index/test_etl.py::TestClassNameETL::<testname>
--snapshot-update
"""

_ETL_CLASS = ChildOpportunityIndex

_SAMPLE_DATA_PATH = pathlib.Path(__file__).parents[0] / "data"
_SAMPLE_DATA_FILE_NAME = "NRI_Table_CensusTracts.csv"
_SAMPLE_DATA_ZIP_FILE_NAME = "NRI_Table_CensusTracts.zip"
_EXTRACT_TMP_FOLDER_NAME = "NationalRiskIndexETL"

def setup_method(self, _method, filename=__file__):
"""Invoke `setup_method` from Parent, but using the current file name.
This code can be copied identically between all child classes.
"""
super().setup_method(_method=_method, filename=filename)

# XXX: Refactor since I just straight copied it out of NRI's
def _setup_etl_instance_and_run_extract(self, mock_etl, mock_paths):
with mock.patch("data_pipeline.utils.requests") as requests_mock:
zip_file_fixture_src = self._DATA_DIRECTORY_FOR_TEST / "coi.zip"
tmp_path = mock_paths[1]

# Create mock response.
with open(zip_file_fixture_src, mode="rb") as file:
file_contents = file.read()
response_mock = requests.Response()
response_mock.status_code = 200
# pylint: disable=protected-access
response_mock._content = file_contents

# Return text fixture:
requests_mock.get = mock.MagicMock(return_value=response_mock)

# Instantiate the ETL class.
etl = ChildOpportunityIndex()

# Monkey-patch the temporary directory to the one used in the test
etl.TMP_PATH = tmp_path

# Run the extract method.
etl.extract()

return etl

def test_init(self, mock_etl, mock_paths):
"""Tests that the ChildOpportunityIndexETL class was initialized
correctly.
"""

etl = ChildOpportunityIndex()
data_path, t_ = mock_paths
assert etl.DATA_PATH == data_path
assert etl.COLUMNS_TO_KEEP == [
"GEOID10_TRACT",
"Summer days above 90F",
"Percent low access to healthy food",
"Percent impenetrable surface areas",
"Third grade reading proficiency",
]
assert etl.GEOID_FIELD_NAME == "GEOID10"
assert etl.GEOID_TRACT_FIELD_NAME == "GEOID10_TRACT"
assert etl.TRACT_INPUT_COLUMN_NAME == "geoid"
assert etl.EXTREME_HEAT_INPUT_FIELD == "HE_HEAT"
assert etl.HEALTHY_FOOD_INPUT_FIELD == "HE_FOOD"
assert etl.IMPENETRABLE_SURFACES_INPUT_FIELD == "HE_GREEN"
assert etl.READING_INPUT_FIELD == "ED_READING"

0 comments on commit 073c1fe

Please sign in to comment.