Skip to content

Commit

Permalink
test: add tests for appending mandatory fields
Browse files Browse the repository at this point in the history
  • Loading branch information
spwoodcock committed Sep 4, 2024
1 parent 8081032 commit ea24463
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 10 deletions.
68 changes: 58 additions & 10 deletions tests/test_update_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,76 @@

def test_merge_mandatory_fields():
"""Merge the mandatory fields XLSForm to a test survey form."""
buildings_form = Path(__file__).parent.parent / "osm_fieldwork" / "xlsforms" / "buildings.xls"
with open(buildings_form, "rb") as xlsform:
test_form = Path(__file__).parent / "testdata" / "test_form_for_mandatory_fields.xls"
with open(test_form, "rb") as xlsform:
form_bytes = BytesIO(xlsform.read())

updated_form = update_xls_form(form_bytes)

# Load the updated form using openpyxl
workbook = load_workbook(filename=BytesIO(updated_form.getvalue()))

# Check the 'survey' sheet
if "survey" not in workbook.sheetnames:
raise ValueError("The 'survey' sheet was not found in the workbook")
sheet = workbook["survey"]
survey_sheet = workbook["survey"]

# Find the index of the 'name' column
name_col = None
for col in sheet.iter_cols(1, sheet.max_column):
for col in survey_sheet.iter_cols(1, survey_sheet.max_column):
if col[0].value == "name":
name_col = col
break

assert name_col is not None, "The 'name' column was not found."

# Check if 'existing' is present in the 'name' column (skip the header)
existing_found = any(cell.value == "existing" for cell in name_col[1:])
# Check if certain fields are present in the 'name' column (skip the header)
existing_field = any(cell.value == "existing" for cell in name_col[1:])
assert existing_field, "'existing' field not found in the 'name' column."

status_field = any(cell.value == "status" for cell in name_col[1:])
assert status_field, "'status' field not found in the 'name' column."

digitisation_correct_field = any(cell.value == "digitisation_correct" for cell in name_col[1:])
assert digitisation_correct_field, "'digitisation_correct' field not found in the 'name' column."

# Check that the 'name' column does not have a duplicate entry for 'username'
username_count = sum(1 for cell in name_col[1:] if cell.value == "username")
assert username_count <= 1, "Duplicate 'username' entry found in the 'name' column."

# Check the 'choices' sheet
if "choices" not in workbook.sheetnames:
raise ValueError("The 'choices' sheet was not found in the workbook")
choices_sheet = workbook["choices"]

# Find the index of the 'name' column in the 'choices' sheet
choices_name_col = None
for col in choices_sheet.iter_cols(1, choices_sheet.max_column):
if col[0].value == "name":
choices_name_col = col
break

assert choices_name_col is not None, "'name' column was not found in the 'choices' sheet."

# Test: Check that the 'choices' sheet does not have duplicate entries for 'yes' and 'no'
yes_count = sum(1 for cell in choices_name_col[1:] if cell.value == "yes")
no_count = sum(1 for cell in choices_name_col[1:] if cell.value == "no")
assert yes_count <= 1, "Duplicate 'yes' entry found in the 'value' column of 'choices' sheet."
assert no_count <= 1, "Duplicate 'no' entry found in the 'value' column of 'choices' sheet."

# Check the 'entities' sheet
if "entities" not in workbook.sheetnames:
raise ValueError("The 'entities' sheet was not found in the workbook")
entities_sheet = workbook["entities"]

# Find the index of the 'label' column in the 'entities' sheet
entities_label_col = None
for col in entities_sheet.iter_cols(1, entities_sheet.max_column):
if col[0].value == "label":
entities_label_col = col
break

assert entities_label_col is not None, "'label' column was not found in the 'entities' sheet."

# Check that the 'entities' label value of 'test label' is replaced by required value
test_label_present = any(cell.value == "test label" for cell in entities_label_col[1:])
assert not test_label_present, "'test label' found in the 'label' column of 'entities' sheet."

assert existing_found, "'existing' value not found in the 'name' column."
# TODO add test to check that digitisation questions come at end of sheet
Binary file not shown.

0 comments on commit ea24463

Please sign in to comment.