Skip to content

Commit

Permalink
Require the label column
Browse files Browse the repository at this point in the history
  • Loading branch information
lognaturel committed Nov 15, 2022
1 parent 5553690 commit a2dbd10
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 33 deletions.
6 changes: 4 additions & 2 deletions pyxform/xls2json.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,16 +355,18 @@ def get_entity_declaration(workbook_dict: Dict) -> Dict:
)
entity = entities_sheet[0]

if not ("label" in entity):
raise PyXFormError("The entities sheet is missing the required label column.")

creation_condition = entity["create_if"] if "create_if" in entity else "1"
label = entity["label"] if "label" in entity else ""

return {
"name": "entity",
"type": "entity",
"parameters": {
"dataset": entities_sheet[0]["dataset"],
"create": creation_condition,
"label": label,
"label": entity["label"],
},
}

Expand Down
77 changes: 46 additions & 31 deletions tests/test_entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ def test_dataset_in_entities_sheet__adds_meta_entity_block(self):
self.assertPyxformXform(
name="data",
md="""
| survey | | | |
| | type | name | label |
| | text | a | A |
| entities | | | |
| | dataset | | |
| | trees | | |
| survey | | | |
| | type | name | label |
| | text | a | A |
| entities | | | |
| | dataset | label | |
| | trees | a | |
""",
xml__xpath_match=["/h:html/h:head/x:model/x:instance/x:data/x:meta/x:entity"],
)
Expand All @@ -39,12 +39,12 @@ def test_dataset_in_entities_sheet__adds_dataset_attribute_to_entity(self):
self.assertPyxformXform(
name="data",
md="""
| survey | | | |
| | type | name | label |
| | text | a | A |
| entities | | | |
| | dataset | | |
| | trees | | |
| survey | | | |
| | type | name | label |
| | text | a | A |
| entities | | | |
| | dataset | label | |
| | trees | a | |
""",
xml__xpath_match=[
'/h:html/h:head/x:model/x:instance/x:data/x:meta/x:entity[@dataset = "trees"]'
Expand All @@ -55,12 +55,12 @@ def test_dataset_in_entities_sheet__defaults_to_always_creating(self):
self.assertPyxformXform(
name="data",
md="""
| survey | | | |
| | type | name | label |
| | text | a | A |
| entities | | | |
| | dataset | | |
| | trees | | |
| survey | | | |
| | type | name | label |
| | text | a | A |
| entities | | | |
| | dataset | label | |
| | trees | a | |
""",
xml__xpath_match=[
'/h:html/h:head/x:model/x:instance/x:data/x:meta/x:entity[@create = "1"]'
Expand All @@ -75,8 +75,8 @@ def test_create_if_in_entities_sheet__puts_expression_on_bind(self):
| | type | name | label |
| | text | a | A |
| entities | | | |
| | dataset | create_if | |
| | trees | string-length(a) > 3 | |
| | dataset | create_if | label |
| | trees | string-length(a) > 3 | a |
""",
xml__xpath_match=[
'/h:html/h:head/x:model/x:bind[@nodeset = "/data/meta/entity/@create" and @calculate = "string-length(a) > 3"]',
Expand All @@ -88,12 +88,12 @@ def test_dataset_in_entities_sheet__adds_id_attribute_and_model_nodes_to_entity(
self.assertPyxformXform(
name="data",
md="""
| survey | | | |
| | type | name | label |
| | text | a | A |
| entities | | | |
| | dataset | | |
| | trees | | |
| survey | | | |
| | type | name | label |
| | text | a | A |
| entities | | | |
| | dataset | label | |
| | trees | a | |
""",
xml__xpath_match=[
'/h:html/h:head/x:model/x:instance/x:data/x:meta/x:entity[@id = ""]',
Expand Down Expand Up @@ -123,15 +123,30 @@ def test_label_and_create_if_in_entities_sheet__expand_node_selectors_to_xpath(s
self.assertPyxformXform(
name="data",
md="""
| survey | | | |
| | type | name | label |
| | text | a | A |
| entities | | | |
| | dataset | label | create_if |
| survey | | | |
| | type | name | label |
| | text | a | A |
| entities | | | |
| | dataset | label | create_if |
| | trees | ${a} | string-length(${a}) > 3 |
""",
xml__xpath_match=[
'/h:html/h:head/x:model/x:bind[@nodeset = "/data/meta/entity/@create" and @calculate = "string-length( /data/a ) > 3"]',
'/h:html/h:head/x:model/x:bind[@nodeset = "/data/meta/entity/label" and @type = "string" and @readonly = "true()" and @calculate = " /data/a "]',
],
)

def test_entity_label__required(self):
self.assertPyxformXform(
name="data",
md="""
| survey | | | |
| | type | name | label |
| | text | a | A |
| entities | | | |
| | dataset | | |
| | trees | | |
""",
errored=True,
error__contains=["The entities sheet is missing the required label column."],
)

0 comments on commit a2dbd10

Please sign in to comment.