Skip to content

Commit bb0c62a

Browse files
committed
add test
1 parent bd3ad70 commit bb0c62a

File tree

4 files changed

+64
-35
lines changed

4 files changed

+64
-35
lines changed

nssp/tests/conftest.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import copy
22
import json
3-
import time
4-
from unittest.mock import patch, MagicMock
5-
6-
import pytest
73
from pathlib import Path
4+
from unittest.mock import patch
85

9-
from delphi_nssp.run import run_module
6+
import pytest
107
from delphi_nssp.constants import DATASET_ID
8+
from delphi_nssp.run import run_module
119

1210
TEST_DIR = Path(__file__).parent
1311

@@ -20,6 +18,9 @@
2018
with open(f"{TEST_DIR}/test_data/page_100_hrr.json", "r") as f:
2119
HRR_TEST_DATA = json.load(f)
2220

21+
with open(f"{TEST_DIR}/test_data/page_no_data.json", "r") as f:
22+
EMPTY_TEST_DATA = json.load(f)
23+
2324
@pytest.fixture(scope="session")
2425
def params():
2526
params = {
@@ -99,3 +100,26 @@ def side_effect(*args, **kwargs):
99100
mock_get.side_effect = side_effect
100101
run_module(params)
101102

103+
@pytest.fixture(scope="function")
104+
def run_as_module_empty(params):
105+
"""
106+
Fixture to use EMPTY_TEST_DATA when testing run_module.
107+
108+
This fixture patches socrara to return the predefined test
109+
data where relevent data is empty.
110+
"""
111+
112+
def _run_as_module_empty():
113+
with patch("sodapy.Socrata.get") as mock_get:
114+
115+
def side_effect(*args, **kwargs):
116+
if kwargs["offset"] == 0:
117+
if DATASET_ID in args[0]:
118+
return EMPTY_TEST_DATA
119+
else:
120+
return []
121+
122+
mock_get.side_effect = side_effect
123+
run_module(params)
124+
125+
return _run_as_module_empty

nssp/tests/test_data/page.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,6 @@
198198
"buildnumber": "2025-02-28"
199199
},
200200
{
201-
"_comment":"This record is for testing the case where all signals data is NA for a county",
202201
"week_end":"2022-10-15T00:00:00.000",
203202
"geography":"Colorado",
204203
"county":"Chaffee",
@@ -213,7 +212,6 @@
213212
"buildnumber":"2025-02-28"
214213
},
215214
{
216-
"_comment":"This record is for testing the case where some signal data (combined signals) is NA for a county",
217215
"week_end":"2022-10-15T00:00:00.000",
218216
"geography":"Colorado",
219217
"county":"Arapahoe",

nssp/tests/test_data/page_100_hrr.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,6 @@
198198
"buildnumber": "2025-02-28"
199199
},
200200
{
201-
"_comment":"This record is for testing the case where all signals data is NA for a county",
202201
"week_end":"2022-10-15T00:00:00.000",
203202
"geography":"Colorado",
204203
"county":"Chaffee",
@@ -213,7 +212,6 @@
213212
"buildnumber":"2025-02-28"
214213
},
215214
{
216-
"_comment":"This record is for testing the case where some signal data (combined signals) is NA for a county",
217215
"week_end":"2022-10-15T00:00:00.000",
218216
"geography":"Colorado",
219217
"county":"Arapahoe",

nssp/tests/test_run.py

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
import glob
2-
from datetime import datetime, date
3-
import json
4-
from pathlib import Path
5-
from unittest.mock import patch
6-
import tempfile
2+
import logging
73
import os
8-
import time
9-
from datetime import datetime
4+
from pathlib import Path
105

116
import numpy as np
127
import pandas as pd
8+
from delphi_nssp.constants import GEOS, SIGNALS_MAP
9+
from delphi_nssp.run import add_needed_columns
1310
from epiweeks import Week
14-
from pandas.testing import assert_frame_equal
15-
from delphi_nssp.constants import GEOS, SIGNALS, SIGNALS_MAP, DATASET_ID
16-
from delphi_nssp.run import (
17-
add_needed_columns
18-
)
1911

2012

13+
def remove_backup_and_receiving(params):
14+
export_dir = params["common"]["export_dir"]
15+
for file in Path(export_dir).glob("*.csv"):
16+
os.remove(file)
17+
18+
today = pd.Timestamp.today().strftime("%Y%m%d")
19+
backup_dir = glob.glob(f"{Path(params['common']['backup_dir'])}/{today}*")
20+
for file in backup_dir:
21+
os.remove(file)
22+
2123
class TestRun:
2224
def test_add_needed_columns(self):
2325
df = pd.DataFrame({"geo_id": ["us"], "val": [1]})
@@ -68,16 +70,10 @@ def test_output_files_exist(self, params, run_as_module):
6870
]
6971
assert set(expected_columns).issubset(set(df.columns.values))
7072

71-
#Verify that there's no NA/empty values in the val columns
73+
# Verify that there's no NA/empty values in the val columns
7274
assert not df["val"].isnull().any()
7375

74-
for file in Path(export_dir).glob("*.csv"):
75-
os.remove(file)
76-
77-
today = pd.Timestamp.today().strftime("%Y%m%d")
78-
backup_dir = glob.glob(f"{Path(params['common']['backup_dir'])}/{today}*")
79-
for file in backup_dir:
80-
os.remove(file)
76+
remove_backup_and_receiving(params)
8177

8278
def test_valid_hrr(self, run_as_module_hrr, params):
8379
export_dir = params["common"]["export_dir"]
@@ -88,10 +84,23 @@ def test_valid_hrr(self, run_as_module_hrr, params):
8884
df = pd.read_csv(f)
8985
assert (df.val == 100).all()
9086

91-
for file in Path(export_dir).glob("*.csv"):
92-
os.remove(file)
87+
remove_backup_and_receiving(params)
88+
89+
def test_empty_data(self, run_as_module_empty, params, caplog):
90+
"""
91+
Tests correct handling when there is a geo and signal combination that has no data.
92+
"""
93+
94+
caplog.set_level(logging.WARNING)
95+
run_as_module_empty()
96+
assert "No data for signal and geo combination" in caplog.text
97+
98+
export_dir = params["common"]["export_dir"]
99+
csv_files = [f for f in Path(export_dir).glob("*.csv")]
100+
101+
# Since only one national entry in page_no_data.json with numeric data,
102+
# while the two counties have no numeric fields,
103+
# there should be no county, hrr, hhs, or msa files.
104+
assert not any(geo in f.name for geo in ["county", "hrr", "hhs", "msa"] for f in csv_files)
93105

94-
today = pd.Timestamp.today().strftime("%Y%m%d")
95-
backup_dir = glob.glob(f"{Path(params['common']['backup_dir'])}/{today}*")
96-
for file in backup_dir:
97-
os.remove(file)
106+
remove_backup_and_receiving(params)

0 commit comments

Comments
 (0)