mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-10 16:48:31 +02:00
Combine JSON and XML parsing tests
Co-authored-by: Jonathan Wren <jonathan@nowandwren.com>
This commit is contained in:
parent
ef6ed93ecd
commit
3cc3e387c5
2 changed files with 55 additions and 18 deletions
|
@ -265,9 +265,10 @@ Feature: Custom formats
|
||||||
Given we use the config "tags.yaml"
|
Given we use the config "tags.yaml"
|
||||||
And we use the password "test" if prompted
|
And we use the password "test" if prompted
|
||||||
When we run "jrnl --export xml"
|
When we run "jrnl --export xml"
|
||||||
Then the output should be a valid XML string
|
Then the output should be valid XML
|
||||||
And "entries" node in the xml output should have 2 elements
|
Given we parse the output as XML
|
||||||
And "tags" in the xml output should contain
|
Then "entries" node in the parsed output should have 2 elements
|
||||||
|
And "tags" in the parsed output should be
|
||||||
@idea
|
@idea
|
||||||
@journal
|
@journal
|
||||||
@dan
|
@dan
|
||||||
|
|
|
@ -173,6 +173,11 @@ def which_output_stream():
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
@fixture
|
||||||
|
def parsed_output():
|
||||||
|
return {"lang": None, "obj": None}
|
||||||
|
|
||||||
|
|
||||||
# ----- STEPS ----- #
|
# ----- STEPS ----- #
|
||||||
@given("we have a keyring", target_fixture="keyring")
|
@given("we have a keyring", target_fixture="keyring")
|
||||||
@given(parse("we have a {keyring_type} keyring"), target_fixture="keyring")
|
@given(parse("we have a {keyring_type} keyring"), target_fixture="keyring")
|
||||||
|
@ -447,6 +452,7 @@ def cache_dir_contains_files(file_list, cache_dir):
|
||||||
|
|
||||||
assert actual_files == expected_files, [actual_files, expected_files]
|
assert actual_files == expected_files, [actual_files, expected_files]
|
||||||
|
|
||||||
|
|
||||||
@then(parse("the output should be valid {language_name}"))
|
@then(parse("the output should be valid {language_name}"))
|
||||||
def assert_output_is_valid_language(cli_run, language_name):
|
def assert_output_is_valid_language(cli_run, language_name):
|
||||||
language_name = language_name.upper()
|
language_name = language_name.upper()
|
||||||
|
@ -458,30 +464,60 @@ def assert_output_is_valid_language(cli_run, language_name):
|
||||||
else:
|
else:
|
||||||
assert False, f"Language name {language_name} not recognized"
|
assert False, f"Language name {language_name} not recognized"
|
||||||
|
|
||||||
@then(parse('"{item}" node in the xml output should have {number:d} elements'))
|
|
||||||
def assert_xml_output_entries_count(item, number, cli_run):
|
@given(parse("we parse the output as {language_name}"), target_fixture="parsed_output")
|
||||||
|
def parse_output_as_language(cli_run, language_name):
|
||||||
|
language_name = language_name.upper()
|
||||||
output = cli_run["stdout"]
|
output = cli_run["stdout"]
|
||||||
xml_tree = ElementTree.fromstring(output)
|
|
||||||
|
|
||||||
xml_tags = (node.tag for node in xml_tree)
|
if language_name == "XML":
|
||||||
assert item in xml_tags, str(list(xml_tags))
|
parsed_output = ElementTree.fromstring(output)
|
||||||
|
elif language_name == "JSON":
|
||||||
|
parsed_output = json.loads(output)
|
||||||
|
else:
|
||||||
|
assert False, f"Language name {language_name} not recognized"
|
||||||
|
|
||||||
actual_entry_count = len(xml_tree.find(item))
|
return {"lang": language_name, "obj": parsed_output}
|
||||||
assert actual_entry_count == number, actual_entry_count
|
|
||||||
|
|
||||||
|
|
||||||
@then(parse('"tags" in the xml output should contain\n{expected_tags}'))
|
@then(parse('"{node_name}" node in the parsed output should have {number:d} elements'))
|
||||||
def assert_xml_output_tags(expected_tags, cli_run):
|
def assert_parsed_output_item_count(node_name, number, parsed_output):
|
||||||
output = cli_run["stdout"]
|
lang = parsed_output["lang"]
|
||||||
|
obj = parsed_output["obj"]
|
||||||
|
|
||||||
|
if lang == "XML":
|
||||||
|
xml_node_names = (node.tag for node in obj)
|
||||||
|
assert node_name in xml_node_names, str(list(xml_node_names))
|
||||||
|
|
||||||
|
actual_entry_count = len(obj.find(node_name))
|
||||||
|
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])
|
||||||
|
|
||||||
|
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):
|
||||||
|
lang = parsed_output["lang"]
|
||||||
|
obj = parsed_output["obj"]
|
||||||
expected_tags = expected_tags.split("\n")
|
expected_tags = expected_tags.split("\n")
|
||||||
|
|
||||||
xml_tree = ElementTree.fromstring(output)
|
if lang == "XML":
|
||||||
|
xml_node_names = (node.tag for node in obj)
|
||||||
|
assert "tags" in xml_node_names, str(list(xml_node_names))
|
||||||
|
|
||||||
xml_tags = (node.tag for node in xml_tree)
|
actual_tags = set(t.attrib["name"] for t in obj.find("tags"))
|
||||||
assert "tags" in xml_tags, str(list(xml_tags))
|
assert actual_tags == set(expected_tags), [actual_tags, set(expected_tags)]
|
||||||
|
|
||||||
actual_tags = set(t.attrib["name"] for t in xml_tree.find("tags"))
|
elif lang == "JSON":
|
||||||
assert actual_tags == set(expected_tags), [actual_tags, set(expected_tags)]
|
assert False, "JSON not implemented in this step"
|
||||||
|
|
||||||
|
else:
|
||||||
|
assert False, f"Language name {lang} not recognized"
|
||||||
|
|
||||||
|
|
||||||
@then(parse('there should be {number:d} "{item}" elements'))
|
@then(parse('there should be {number:d} "{item}" elements'))
|
||||||
|
|
Loading…
Add table
Reference in a new issue