Skip to content

Commit

Permalink
More thorough validation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoblurye committed May 22, 2019
1 parent a1dae55 commit a2b02a2
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 13 deletions.
14 changes: 8 additions & 6 deletions cidc_schemas/template_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,6 @@ def _group_rows(rows) -> RowGroup:
row_type, *content = row
row_groups[row_type].append(content)

n_headers = len(row_groups.get(RowType.HEADER, []))
assert n_headers == 1, f"Expected no more than one header row for data table"

return row_groups

@staticmethod
Expand All @@ -134,7 +131,7 @@ def _get_data_schemas(self, row_groups, data_schemas: Dict[str, dict]) -> List[d
n_columns = len(header_row)
for i, data_row in enumerate(data_rows):
n_entries = len(data_row)
assert n_entries == n_columns, f"The {i + 1}th data row has too few entries"
assert n_entries == n_columns, f"The {i + 1}th data row has the wrong number of entries"

schemas = [self._get_schema(header, data_schemas)
for header in header_row if header]
Expand Down Expand Up @@ -188,11 +185,16 @@ def _validate_worksheet(self, worksheet_name: str, ws_schema: dict) -> List[str]
flat_data_schemas = {
**flat_data_schemas, **section}

# Validate data rows
n_headers = len(row_groups[RowType.HEADER])
assert n_headers == 1, f"Exactly one header row expected, but found {n_headers}"
headers = row_groups[RowType.HEADER][0]
assert all(
headers), f"Found an empty header cell at index {headers.index(None)}"

data_schemas = self._get_data_schemas(
row_groups, flat_data_schemas)

# Validate data rows
headers = row_groups[RowType.HEADER][0]
for data_row in row_groups[RowType.DATA]:
for col, value in enumerate(data_row):
invalid_reason = validate_instance(
Expand Down
66 changes: 59 additions & 7 deletions tests/test_template_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# NOTE: see conftest.py for pbmc_template and tiny_template fixture definitions


def test_valid_tiny(tiny_template):
def test_valid(tiny_template):
"""Test that a known-valid spreadsheet is considered valid"""
tiny_valid = {
'TEST_SHEET': [
Expand All @@ -34,14 +34,68 @@ def test_valid_tiny(tiny_template):
assert reader.validate(tiny_template)


def test_valid_tiny_from_excel(tiny_template):
def test_valid_from_excel(tiny_template):
"""Test that the reader can load from a small xlsx file"""
tiny_xlsx = os.path.join(TEST_DATA_DIR, 'tiny_manifest.xlsx')
reader = XlTemplateReader.from_excel(tiny_xlsx)
assert reader.validate(tiny_template)


def test_invalid_tiny(tiny_template):
def search_error_message(workbook, template, error, msg_fragment):
reader = XlTemplateReader(workbook)
with pytest.raises(error) as e:
reader.validate(template)

assert msg_fragment in str(e.value)


def test_missing_headers(tiny_template):
"""Test that a spreadsheet with empty headers raises an assertion error"""
tiny_missing = {
'TEST_SHEET': [
(RowType.HEADER, 'test_property', None, 'test_time'),
]
}

search_error_message(tiny_missing, tiny_template,
AssertionError, 'empty header cell')


def test_wrong_number_of_headers(tiny_template):
"""Test that a spreadsheet with multiple or no headers raises an assertion error"""
tiny_double = {
'TEST_SHEET': [
(RowType.HEADER, 'test_property', 'test_date', 'test_time'),
(RowType.HEADER, 'test_property', 'test_date', 'test_time'),
]
}

tiny_no_headers = {
'TEST_SHEET': [
(RowType.DATA, 1, 2, 3)
]
}

search_error_message(tiny_double, tiny_template,
AssertionError, 'one header row expected')

search_error_message(tiny_no_headers, tiny_template,
AssertionError, 'one header row expected')


def test_missing_schema(tiny_template):
"""Test that a spreadsheet with an unknown property raises an assertion error"""
tiny_missing = {
'TEST_SHEET': [
(RowType.PREAMBLE, 'missing_property', 'foo'),
]
}

search_error_message(tiny_missing, tiny_template,
AssertionError, 'No schema found')


def test_invalid(tiny_template):
"""Test that a known-invalid spreadsheet is considered invalid"""
tiny_invalid = {
'TEST_SHEET': [
Expand All @@ -54,10 +108,8 @@ def test_invalid_tiny(tiny_template):
]
}

reader = XlTemplateReader(tiny_invalid)

with pytest.raises(ValidationError):
reader.validate(tiny_template)
search_error_message(tiny_invalid, tiny_template,
ValidationError, '')


def test_pbmc_validation(pbmc_template):
Expand Down

0 comments on commit a2b02a2

Please sign in to comment.