From 48c9d9fa166ed0a48adf719f5d447bdc5073b1df Mon Sep 17 00:00:00 2001 From: Micah Jerome Ellison Date: Sat, 10 Apr 2021 14:58:10 -0700 Subject: [PATCH] Implement tag tests in JSON Co-authored-by: Jonathan Wren --- tests/features/format.feature | 12 +++++++----- tests/step_defs/conftest.py | 26 ++++++++++++++++++-------- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/tests/features/format.feature b/tests/features/format.feature index a06d4c95..811e0d5e 100644 --- a/tests/features/format.feature +++ b/tests/features/format.feature @@ -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 diff --git a/tests/step_defs/conftest.py b/tests/step_defs/conftest.py index 1cc14f5a..c7732656 100644 --- a/tests/step_defs/conftest.py +++ b/tests/step_defs/conftest.py @@ -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)) - actual_tags = set(t.attrib["name"] for t in obj.find("tags")) - assert actual_tags == set(expected_tags), [actual_tags, set(expected_tags)] + if field_name == "tags": + actual_tags = set(t.attrib["name"] for t in obj.find("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"