From 3c1496eed2936d75904d6842106bdf008cdc6953 Mon Sep 17 00:00:00 2001 From: Stav Shamir Date: Sun, 26 Jan 2020 14:03:24 +0200 Subject: [PATCH] Add scenario for XML export --- features/exporting.feature | 7 +++++++ features/steps/export_steps.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/features/exporting.feature b/features/exporting.feature index 11edcfbe..5fcdf33f 100644 --- a/features/exporting.feature +++ b/features/exporting.feature @@ -82,3 +82,10 @@ Feature: Exporting a Journal More stuff more stuff again """ + + Scenario: Exporting to XML + Given we use the config "tags.yaml" + When we run "jrnl --export xml" + Then the output should be a valid XML string + And "entries" node in the xml output should have 2 elements + And "tags" in the xml output should contain ["@idea", "@journal", "@dan"] diff --git a/features/steps/export_steps.py b/features/steps/export_steps.py index 7128d120..a3704b74 100644 --- a/features/steps/export_steps.py +++ b/features/steps/export_steps.py @@ -1,4 +1,5 @@ import json +from xml.etree import ElementTree from behave import then @@ -49,3 +50,34 @@ def check_json_output_path(context, path, value): struct = struct[node] assert struct == value, struct + +@then('the output should be a valid XML string') +def assert_valid_xml_string(context): + output = context.stdout_capture.getvalue() + xml_tree = ElementTree.fromstring(output) + assert xml_tree, output + + +@then('"entries" node in the xml output should have {number:d} elements') +def assert_xml_output_entries_count(context, number): + output = context.stdout_capture.getvalue() + xml_tree = ElementTree.fromstring(output) + + xml_tags = (node.tag for node in xml_tree) + assert "entries" in xml_tags, str(list(xml_tags)) + + actual_entry_count = len(xml_tree.find("entries")) + assert actual_entry_count == number, actual_entry_count + + +@then('"tags" in the xml output should contain {expected_tags_json_list}') +def assert_xml_output_tags(context, expected_tags_json_list): + output = context.stdout_capture.getvalue() + xml_tree = ElementTree.fromstring(output) + + xml_tags = (node.tag for node in xml_tree) + assert "tags" in xml_tags, str(list(xml_tags)) + + expected_tags = json.loads(expected_tags_json_list) + actual_tags = set(t.attrib["name"] for t in xml_tree.find("tags")) + assert actual_tags == set(expected_tags), [actual_tags, set(expected_tags)]