Skip to content

Commit

Permalink
test: add pyxform validation checks to xlsform tests
Browse files Browse the repository at this point in the history
  • Loading branch information
spwoodcock committed Sep 20, 2024
1 parent 46e15ea commit 5d04c1a
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions tests/test_update_xlsform.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
from pathlib import Path

from openpyxl import Workbook, load_workbook, worksheet
from pyxform.xls2xform import convert as xform_convert

from osm_fieldwork.update_xlsform import append_mandatory_fields
from osm_fieldwork.xlsforms import buildings, healthcare


async def test_merge_mandatory_fields():
Expand All @@ -34,15 +36,17 @@ async def test_merge_mandatory_fields():

updated_form = await append_mandatory_fields(form_bytes, "buildings")
workbook = load_workbook(filename=BytesIO(updated_form.getvalue()))
# Write merged xlsform to file for debugging
with open("merged_xlsform.xlsx", "wb") as merged_xlsform:
merged_xlsform.write(updated_form.getvalue())

check_survey_sheet(workbook)
check_choices_sheet(workbook)
check_entities_sheet(workbook)
check_form_title(workbook)

# Write merged xlsform to file for debugging
with open("merged_xlsform.xlsx", "wb") as merged_xlsform:
merged_xlsform.write(updated_form.getvalue())
# Check it's still a valid xlsform by converting to XML
xform_convert(updated_form)


async def test_add_extra_select_from_file():
Expand Down Expand Up @@ -82,6 +86,24 @@ async def test_add_task_ids_to_choices():
assert task_id in name_column, f"Task ID {task_id} not found in the choices sheet."


async def test_buildings_xlsform():
"""Merge and test if buildings form is a valid XLSForm."""
with open(buildings, "rb") as xlsform:
form_bytes = BytesIO(xlsform.read())
updated_form = await append_mandatory_fields(form_bytes, "buildings")
# Check it's still a valid xlsform by converting to XML
xform_convert(updated_form)


async def test_healthcare_xlsform():
"""Merge and test if buildings form is a valid XLSForm."""
with open(healthcare, "rb") as xlsform:
form_bytes = BytesIO(xlsform.read())
updated_form = await append_mandatory_fields(form_bytes, "healthcare")
# Check it's still a valid xlsform by converting to XML
xform_convert(updated_form)


def check_survey_sheet(workbook: Workbook) -> None:
"""Check the 'survey' sheet values and ensure no duplicates in 'name' column."""
survey_sheet = get_sheet(workbook, "survey")
Expand Down Expand Up @@ -126,7 +148,8 @@ def check_form_title(workbook: Workbook) -> None:
form_title_col_index = get_column_index(settings_sheet, "form_title")

form_title_value = settings_sheet.cell(row=2, column=form_title_col_index).value
assert form_title_value == "buildings", "form_title field is not set to 'buildings'"
# NOTE the 's' is stripped from 'buildings' to make it singular
assert form_title_value == "building", "form_title field is not set to 'building'"


def get_sheet(workbook: Workbook, sheet_name: str) -> worksheet.worksheet.Worksheet:
Expand All @@ -137,10 +160,13 @@ def get_sheet(workbook: Workbook, sheet_name: str) -> worksheet.worksheet.Worksh


def check_for_duplicates(sheet: worksheet.worksheet.Worksheet, col_index: int) -> None:
"""Check for any duplicate values in a specific column of a sheet."""
"""Check for any duplicate values in a specific column of a sheet, ignoring None values."""
seen_values = set()
for row in sheet.iter_rows(min_col=col_index, max_col=col_index, min_row=2):
value = row[0].value
if value is None:
# Skip None values, allowing them to appear multiple times
continue
if value in seen_values:
raise AssertionError(f"Duplicate value '{value}' found in column '{col_index}' of sheet '{sheet.title}'.")
seen_values.add(value)
Expand Down

0 comments on commit 5d04c1a

Please sign in to comment.