From 430182a0a55f0fd5ac6d8a800a71b46346737569 Mon Sep 17 00:00:00 2001 From: Micah Jerome Ellison Date: Sat, 10 Apr 2021 15:26:26 -0700 Subject: [PATCH] Implement JSON tests and remove "node" nomenclature from tests Co-authored-by: Jonathan Wren --- tests/features/format.feature | 18 ++++++++++-------- tests/step_defs/conftest.py | 29 ++++++++++++++++++++++------- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/tests/features/format.feature b/tests/features/format.feature index 811e0d5e..9c38216f 100644 --- a/tests/features/format.feature +++ b/tests/features/format.feature @@ -7,15 +7,15 @@ Feature: Custom formats Then we should get no error And the output should be valid JSON Given we parse the output as JSON - Then "entries" node in the parsed output should have 3 elements + Then "entries" 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 + And "entries.0.tags" in the parsed output should have 3 elements + And "entries.1.tags" in the parsed output should have 1 elements + And "entries.2.tags" in the parsed output should have 2 elements Examples: configs | config_file | @@ -24,12 +24,14 @@ Feature: Custom formats | basic_folder.yaml | | basic_dayone.yaml | - Scenario: Exporting dayone to json + Scenario: Exporting dayone to json should include UUID Given we use the config "dayone.yaml" When we run "jrnl --export json" Then we should get no error And the output should be valid JSON - And the json output should contain entries.0.uuid = "4BB1F46946AD439996C9B59DE7C4DDC1" + Given we parse the output as JSON + Then "entries.0.uuid" in the parsed output should be + 4BB1F46946AD439996C9B59DE7C4DDC1 Scenario Outline: Printing a journal that has multiline entries with tags Given we use the config "" @@ -248,7 +250,7 @@ Feature: Custom formats And we use the password "test" if prompted When we run "jrnl --export xml" Then the output should be a valid XML string - And "entries" node in the xml output should have 3 elements + And "entries" in the xml output should have 3 elements And "tags" in the xml output should contain @ipsum @tagone @@ -269,7 +271,7 @@ Feature: Custom formats When we run "jrnl --export xml" Then the output should be valid XML Given we parse the output as XML - Then "entries" node in the parsed output should have 2 elements + Then "entries" in the parsed output should have 2 elements And "tags" in the parsed output should be @idea @journal diff --git a/tests/step_defs/conftest.py b/tests/step_defs/conftest.py index c7732656..7a651093 100644 --- a/tests/step_defs/conftest.py +++ b/tests/step_defs/conftest.py @@ -480,7 +480,7 @@ def parse_output_as_language(cli_run, language_name): return {"lang": language_name, "obj": parsed_output} -@then(parse('"{node_name}" node in the parsed output should have {number:d} elements')) +@then(parse('"{node_name}" in the parsed output should have {number:d} elements')) def assert_parsed_output_item_count(node_name, number, parsed_output): lang = parsed_output["lang"] obj = parsed_output["obj"] @@ -493,14 +493,23 @@ def assert_parsed_output_item_count(node_name, number, parsed_output): assert actual_entry_count == number, actual_entry_count elif lang == "JSON": - assert node_name in obj, [node_name, obj] - assert len(obj[node_name]) == number, len(obj[node_name]) + my_obj = obj + + for node in node_name.split("."): + try: + my_obj = my_obj[int(node)] + except ValueError: + assert node in my_obj + my_obj = my_obj[node] + + assert len(my_obj) == number, len(my_obj) else: assert False, f"Language name {lang} not recognized" + @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): +def assert_output_field_content(field_name, expected_keys, cli_run, parsed_output): lang = parsed_output["lang"] obj = parsed_output["obj"] expected_keys = expected_keys.split("\n") @@ -511,7 +520,10 @@ def assert_xml_output_tags(field_name, expected_keys, cli_run, parsed_output): 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)] + assert set(actual_tags) == set(expected_keys), [ + actual_tags, + set(expected_keys), + ] else: assert False, "This test only works for tags in XML" @@ -522,10 +534,13 @@ def assert_xml_output_tags(field_name, expected_keys, cli_run, parsed_output): try: my_obj = my_obj[int(node)] except ValueError: - assert field_name in my_obj + assert node in my_obj, [my_obj.keys(), node] my_obj = my_obj[node] - assert set(expected_keys) == set(my_obj) + if type(my_obj) is str: + my_obj = [my_obj] + + assert set(expected_keys) == set(my_obj), [set(my_obj), set(expected_keys)] else: assert False, f"Language name {lang} not recognized"