Skip to content

Commit b753744

Browse files
authored
Ap66-issue42-mocking_blob_access (#50)
* updates for testing * more catalog testing * new testing sub-module * adding first dataset testing * catalog functionality tested * fixing path and adding doctests * doctest for simple stuff * version pump * heading fix * Update coverage badge --------- Co-authored-by: cdc-ap66 <cdc-ap66@users.noreply.github.com>
1 parent 4e2a203 commit b753744

9 files changed

Lines changed: 330 additions & 13 deletions

File tree

cfa/scenarios/dataops/datasets/catalog.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,4 +260,16 @@ def get_data(
260260

261261

262262
def list_datasets() -> list[str]:
263+
"""
264+
Lists all available datasets in the catalog
265+
266+
Returns:
267+
list[str]: list of dataset names
268+
269+
Examples:
270+
>>> datasets = list_datasets()
271+
>>> 'covid19vax_trends' in datasets
272+
True
273+
274+
"""
263275
return [x for x in datasets.__dict__.keys()]

cfa/scenarios/dataops/datasets/schemas/covid19vax_trends.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,30 +46,42 @@ def __repr__(self) -> str:
4646
str,
4747
checks=[
4848
pa.Check.str_matches(
49-
r"^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{3}$"
49+
r"^[0-9]{4}-[0-9]{2}-[0-9]{2}(T[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{3})?$"
5050
)
5151
],
5252
),
5353
"location": pa.Column(
5454
str, checks=[pa.Check.str_matches(r"^[A-Z]{2}$")]
5555
),
5656
"demographic_category": pa.Column(
57-
str, checks=[pa.Check.str_matches(r"^[A-Za-z0-9\_\-]+$")]
57+
str, checks=[pa.Check.str_matches(r"^[A-Za-z0-9\_\-\+\<\>]+$")]
5858
),
5959
"census": pa.Column(
6060
int, checks=[pa.Check.in_range(0, 10_000_000_000)], nullable=True
6161
),
6262
"administered_dose1": pa.Column(
63-
int, checks=[pa.Check.in_range(0, 10_000_000_000)], nullable=True
63+
int,
64+
checks=[pa.Check.in_range(0, 10_000_000_000)],
65+
nullable=True,
66+
coerce=True,
6467
),
6568
"series_complete_yes": pa.Column(
66-
int, checks=[pa.Check.in_range(0, 10_000_000_000)], nullable=True
69+
int,
70+
checks=[pa.Check.in_range(0, 10_000_000_000)],
71+
nullable=True,
72+
coerce=True,
6773
),
6874
"booster_doses": pa.Column(
69-
int, checks=[pa.Check.in_range(0, 10_000_000_000)], nullable=True
75+
int,
76+
checks=[pa.Check.in_range(0, 10_000_000_000)],
77+
nullable=True,
78+
coerce=True,
7079
),
7180
"second_booster": pa.Column(
72-
int, checks=[pa.Check.in_range(0, 10_000_000_000)], nullable=True
81+
int,
82+
checks=[pa.Check.in_range(0, 10_000_000_000)],
83+
nullable=True,
84+
coerce=True,
7385
),
7486
"administered_dose1_pct_agegroup": pa.Column(
7587
float, checks=[pa.Check.in_range(0, 100)], nullable=True
@@ -92,7 +104,7 @@ def __repr__(self) -> str:
92104
str,
93105
checks=[
94106
pa.Check.str_matches(
95-
r"^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{3}$"
107+
r"^[0-9]{4}-[0-9]{2}-[0-9]{2}(T[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{3})?$"
96108
)
97109
],
98110
),

changelog.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@ The versioning pattern is `YYYY.MM.DD.micro(a/b/{none if release})
88

99
---
1010

11+
## [2025.06.13.0a]
12+
13+
### Added
14+
15+
- blob mocking and testing using synthetic data to increase coverage of dataset catalog
16+
- tests for covid19_vax_trends dataset and transform that can be duplicated for other tests
17+
18+
### Updated
19+
20+
- `cfa/scenarios/dataops/datasets/schemas/covid19vax_trends.py` to fix some errors with schema validation in synthetic data
21+
- `tests/conftest.py` to include blob write function mocking fixture so it can be reused
22+
1123
## [2025.06.10.0a]
1224

1325
### Added

docs/assets/badges/coverage.svg

Lines changed: 3 additions & 3 deletions
Loading

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "cfa.scenarios.dataops"
3-
version = "2025.06.10.0a"
3+
version = "2025.06.13.0a"
44
description = "Data cataloging, ETL, modeling, verification, and validation for the CFA Scenarios Team"
55
authors = [
66
{name = "Phil Rogers", email = "ap66@cdc.gov"}
@@ -48,4 +48,4 @@ build-backend = "poetry.core.masonry.api"
4848
state_report_pdf = "cfa.scenarios.dataops.workflows.post_processing.state_report_pdf:main"
4949

5050
[tool.pytest.ini_options]
51-
addopts = "-vv --cov=cfa.scenarios.dataops --cov-report html --cov-report term"
51+
addopts = "--doctest-modules -vv --cov=cfa --cov-report html --cov-report term"

tests/conftest.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,16 @@
55
def data_dir(tmpdir_factory):
66
"""Fixture to create a temporary data directory for tests."""
77
return tmpdir_factory.mktemp("data")
8+
9+
10+
@fixture(scope="session")
11+
def mock_write_blob_stream():
12+
def mock_write_blob_stream(
13+
data,
14+
blob_url: str,
15+
account_name: str,
16+
container_name: str,
17+
) -> None:
18+
return None
19+
20+
return mock_write_blob_stream

tests/datasets/__init__.py

Whitespace-only changes.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import pandas as pd
2+
import pandera.pandas as pa
3+
import pytest
4+
from pandera.errors import SchemaError
5+
6+
from cfa.scenarios.dataops.datasets.schemas.covid19vax_trends import (
7+
extract_schema,
8+
load_schema,
9+
raw_synth_data,
10+
tf_synth_data,
11+
)
12+
from cfa.scenarios.dataops.etl.covid19vax_trends import transform
13+
14+
15+
def test_covid19_vax_trends_schemas():
16+
assert isinstance(extract_schema, pa.DataFrameSchema)
17+
assert isinstance(load_schema, pa.DataFrameSchema)
18+
19+
# Check if the schemas have the expected columns
20+
assert isinstance(extract_schema(raw_synth_data), pd.DataFrame)
21+
assert isinstance(load_schema(tf_synth_data), pd.DataFrame)
22+
23+
with pytest.raises(SchemaError):
24+
ex_cols = raw_synth_data.columns
25+
extract_schema(
26+
raw_synth_data.rename(
27+
columns={col: col + "_renamed" for col in ex_cols}
28+
)
29+
)
30+
31+
with pytest.raises(SchemaError):
32+
tf_cols = tf_synth_data.columns
33+
load_schema(
34+
tf_synth_data.rename(
35+
columns={col: col + "_renamed" for col in tf_cols}
36+
)
37+
)
38+
39+
40+
def test_covid19_vax_trends_transform():
41+
extract_df = raw_synth_data
42+
tf_df = transform(extract_df)
43+
44+
# Check if the transformed DataFrame matches the expected schema
45+
assert isinstance(load_schema(tf_df), pd.DataFrame)

0 commit comments

Comments
 (0)