Add JSON parsing test and combine it with XML parsing test

Co-authored-by: Jonathan Wren <jonathan@nowandwren.com>
This commit is contained in:
Micah Jerome Ellison 2021-04-10 12:43:51 -07:00 committed by Jonathan Wren
parent 3b044e3044
commit ef6ed93ecd
2 changed files with 15 additions and 11 deletions

View file

@ -5,7 +5,7 @@ Feature: Custom formats
And we use the password "test" if prompted And we use the password "test" if prompted
When we run "jrnl --format json" When we run "jrnl --format json"
Then we should get no error Then we should get no error
And the output should be parsable as json And the output should be valid JSON
And "entries" in the json output should have 3 elements 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 "@ipsum"
And "tags" in the json output should contain "@tagone" And "tags" in the json output should contain "@tagone"
@ -26,7 +26,7 @@ Feature: Custom formats
Given we use the config "dayone.yaml" Given we use the config "dayone.yaml"
When we run "jrnl --export json" When we run "jrnl --export json"
Then we should get no error Then we should get no error
And the output should be parsable as json And the output should be valid JSON
And the json output should contain entries.0.uuid = "4BB1F46946AD439996C9B59DE7C4DDC1" And the json output should contain entries.0.uuid = "4BB1F46946AD439996C9B59DE7C4DDC1"
Scenario Outline: Printing a journal that has multiline entries with tags Scenario Outline: Printing a journal that has multiline entries with tags
@ -67,9 +67,9 @@ Feature: Custom formats
Given we use the config "<config_file>" Given we use the config "<config_file>"
And we use the password "test" if prompted And we use the password "test" if prompted
When we run "jrnl -until 'August 2020' --format json" When we run "jrnl -until 'August 2020' --format json"
Then the output should be parsable as json Then the output should be valid JSON
Then we should get no error Then we should get no error
And the output should be parsable as json And the output should be valid JSON
And "entries" in the json output should have 2 elements And "entries" in the json output should have 2 elements
And "tags" in the json output should contain "@ipsum" 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 "@tagone"

View file

@ -2,6 +2,7 @@
# License: https://www.gnu.org/licenses/gpl-3.0.html # License: https://www.gnu.org/licenses/gpl-3.0.html
import ast import ast
import json
import os import os
from collections import defaultdict from collections import defaultdict
from keyring import backend from keyring import backend
@ -446,13 +447,16 @@ 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("the output should be a valid XML string") def assert_output_is_valid_language(cli_run, language_name):
def assert_valid_xml_string(cli_run): language_name = language_name.upper()
output = cli_run["stdout"] if language_name == "XML":
xml_tree = ElementTree.fromstring(output) xml_tree = ElementTree.fromstring(cli_run["stdout"])
assert xml_tree, output assert xml_tree, "Invalid XML"
elif language_name == "JSON":
assert json.loads(cli_run["stdout"]), "Invalid JSON"
else:
assert False, f"Language name {language_name} not recognized"
@then(parse('"{item}" node in the xml output should have {number:d} elements')) @then(parse('"{item}" node in the xml output should have {number:d} elements'))
def assert_xml_output_entries_count(item, number, cli_run): def assert_xml_output_entries_count(item, number, cli_run):