Implement tag tests in JSON

Co-authored-by: Jonathan Wren <jonathan@nowandwren.com>
This commit is contained in:
Micah Jerome Ellison 2021-04-10 14:58:10 -07:00 committed by Jonathan Wren
parent 3cc3e387c5
commit 48c9d9fa16
2 changed files with 25 additions and 13 deletions

View file

@ -6,11 +6,13 @@ Feature: Custom formats
When we run "jrnl --format json"
Then we should get no error
And the output should be valid JSON
And "entries" in the json output should have 3 elements
And "tags" in the json output should contain "@ipsum"
And "tags" in the json output should contain "@tagone"
And "tags" in the json output should contain "@tagthree"
And "tags" in the json output should contain "@tagtwo"
Given we parse the output as JSON
Then "entries" node in the parsed output should have 3 elements
And "tags" in the parsed output should be
@ipsum
@tagone
@tagtwo
@tagthree
And entry 1 should have an array "tags" with 3 elements
And entry 2 should have an array "tags" with 1 elements
And entry 3 should have an array "tags" with 2 elements

View file

@ -499,23 +499,33 @@ def assert_parsed_output_item_count(node_name, number, parsed_output):
else:
assert False, f"Language name {lang} not recognized"
@then(parse('"tags" in the parsed output should be\n{expected_tags}'))
def assert_xml_output_tags(expected_tags, cli_run, parsed_output):
@then(parse('"{field_name}" in the parsed output should be\n{expected_keys}'))
def assert_xml_output_tags(field_name, expected_keys, cli_run, parsed_output):
lang = parsed_output["lang"]
obj = parsed_output["obj"]
expected_tags = expected_tags.split("\n")
expected_keys = expected_keys.split("\n")
if lang == "XML":
xml_node_names = (node.tag for node in obj)
assert "tags" in xml_node_names, str(list(xml_node_names))
assert field_name in xml_node_names, str(list(xml_node_names))
if field_name == "tags":
actual_tags = set(t.attrib["name"] for t in obj.find("tags"))
assert actual_tags == set(expected_tags), [actual_tags, set(expected_tags)]
assert set(actual_tags) == set(expected_keys), [actual_tags, set(expected_keys)]
else:
assert False, "This test only works for tags in XML"
elif lang == "JSON":
assert False, "JSON not implemented in this step"
my_obj = obj
for node in field_name.split("."):
try:
my_obj = my_obj[int(node)]
except ValueError:
assert field_name in my_obj
my_obj = my_obj[node]
assert set(expected_keys) == set(my_obj)
else:
assert False, f"Language name {lang} not recognized"