Implement datetime handling in pytest-bdd

- This was awful and convoluted

Co-authored-by: Micah Jerome Ellison <micah.jerome.ellison@gmail.com>
This commit is contained in:
Jonathan Wren 2021-05-01 15:57:16 -07:00
parent cda07bf8d9
commit 4aabb73847
16 changed files with 133 additions and 87 deletions

View file

@ -36,10 +36,10 @@ Feature: Reading and writing to journal with custom date formats
When we run "jrnl <command>"
Then we should see the message "Entry added"
When we run "jrnl -n 1"
Then the output should contain "<output>"
Then the output should contain "<expected_output>"
Examples: Day-first Dates
| config_file | command | output |
| config_file | command | expected_output |
| little_endian_dates.yaml | 2020-09-19: My first entry. | 19.09.2020 09:00 My first entry. |
| little_endian_dates.yaml | 2020-08-09: My second entry. | 09.08.2020 09:00 My second entry. |
| little_endian_dates.yaml | 2020-02-29: Test. | 29.02.2020 09:00 Test. |
@ -53,10 +53,10 @@ Feature: Reading and writing to journal with custom date formats
Scenario Outline: Searching for dates with custom date
Given we use the config "<config_file>"
When we run "jrnl <command>"
Then the output should be "<output>"
Then the output should be "<expected_output>"
Examples: Day-first Dates
| config_file | command | output |
| config_file | command | expected_output |
| little_endian_dates.yaml | -on '2013-07-10' --short | 10.07.2013 15:40 Life is good. |
| little_endian_dates.yaml | -on 'june 9 2013' --short | 09.06.2013 15:39 My first entry. |
| little_endian_dates.yaml | -on 'july 10 2013' --short | 10.07.2013 15:40 Life is good. |
@ -83,47 +83,48 @@ Feature: Reading and writing to journal with custom date formats
Then the output should not contain "Life is good"
And the output should not contain "But I'm better."
Scenario Outline: Create entry using day of the week as entry date.
Scenario Outline: Create entry using day of the week as entry date one.
Given we use the config "simple.yaml"
And now is "2019-03-12 01:30:32 PM"
When we run "jrnl <command>"
Then we should see the message "Entry added"
When we run "jrnl -1"
Then the output should contain "<output>"
Then the output should contain "<expected_output>"
Then the output should contain the date "<date>"
Examples: Days of the week
| command | output | date |
| Monday: entry on a monday | entry on a monday | monday at 9am |
| Tuesday: entry on a tuesday | entry on a tuesday | tuesday at 9am |
| Wednesday: entry on a wednesday | entry on a wednesday | wednesday at 9am |
| Thursday: entry on a thursday | entry on a thursday | thursday at 9am |
| Friday: entry on a friday | entry on a friday | friday at 9am |
| Saturday: entry on a saturday | entry on a saturday | saturday at 9am |
| Sunday: entry on a sunday | entry on a sunday | sunday at 9am |
| sunday: entry on a sunday | entry on a sunday | sunday at 9am |
| sUndAy: entry on a sunday | entry on a sunday | sunday at 9am |
| command | expected_output | date |
| Monday: entry on a monday | entry on a monday | 2019-03-11 09:00 |
| Tuesday: entry on a tuesday | entry on a tuesday | 2019-03-05 09:00 |
| Wednesday: entry on a wednesday | entry on a wednesday | 2019-03-06 09:00 |
| Thursday: entry on a thursday | entry on a thursday | 2019-03-07 09:00 |
| Friday: entry on a friday | entry on a friday | 2019-03-08 09:00 |
| Saturday: entry on a saturday | entry on a saturday | 2019-03-09 09:00 |
| Sunday: entry on a sunday | entry on a sunday | 2019-03-10 09:00 |
| sunday: entry on a sunday | entry on a sunday | 2019-03-10 09:00 |
| sUndAy: entry on a sunday | entry on a sunday | 2019-03-10 09:00 |
Scenario Outline: Create entry using day of the week as entry date.
Scenario Outline: Create entry using day of the week as entry date two.
Given we use the config "simple.yaml"
And now is "2019-03-12 01:30:32 PM"
When we run "jrnl <command>"
Then we should see the message "Entry added"
When we run "jrnl -1"
Then the output should contain "<output>"
Then the output should contain "<expected_output>"
Then the output should contain the date "<date>"
Examples: Days of the week
| command | output | date |
| Mon: entry on a monday | entry on a monday | monday at 9am |
| Tue: entry on a tuesday | entry on a tuesday | tuesday at 9am |
| Wed: entry on a wednesday | entry on a wednesday | wednesday at 9am |
| Thu: entry on a thursday | entry on a thursday | thursday at 9am |
| Fri: entry on a friday | entry on a friday | friday at 9am |
| Sat: entry on a saturday | entry on a saturday | saturday at 9am |
| Sun: entry on a sunday | entry on a sunday | sunday at 9am |
| sun: entry on a sunday | entry on a sunday | sunday at 9am |
| sUn: entry on a sunday | entry on a sunday | sunday at 9am |
| command | expected_output | date |
| Mon: entry on a monday | entry on a monday | 2019-03-11 09:00 |
| Tue: entry on a tuesday | entry on a tuesday | 2019-03-05 09:00 |
| Wed: entry on a wednesday | entry on a wednesday | 2019-03-06 09:00 |
| Thu: entry on a thursday | entry on a thursday | 2019-03-07 09:00 |
| Fri: entry on a friday | entry on a friday | 2019-03-08 09:00 |
| Sat: entry on a saturday | entry on a saturday | 2019-03-09 09:00 |
| Sun: entry on a sunday | entry on a sunday | 2019-03-10 09:00 |
| sun: entry on a sunday | entry on a sunday | 2019-03-10 09:00 |
| sUn: entry on a sunday | entry on a sunday | 2019-03-10 09:00 |
Scenario: Journals with unreadable dates should still be loaded

View file

@ -4,6 +4,7 @@
import ast
import json
import os
from datetime import datetime
from collections import defaultdict
from keyring import backend
from keyring import set_keyring
@ -15,6 +16,7 @@ import re
import shutil
import tempfile
from unittest.mock import patch
from unittest.mock import MagicMock
from xml.etree import ElementTree
from pytest_bdd import given
@ -31,6 +33,7 @@ from jrnl.cli import cli
from jrnl.config import load_config
from jrnl.os_compat import split_args
from jrnl.os_compat import on_windows
from jrnl.time import __get_pdt_calendar
class TestKeyring(backend.KeyringBackend):
@ -99,10 +102,6 @@ def pytest_bdd_apply_tag(tag, function):
# ----- UTILS ----- #
def failed_msg(msg, expected, actual):
return f"{msg}\nExpected:\n{expected}\n---end---\nActual:\n{actual}\n---end---\n"
def read_value_from_string(string):
if string[0] == "{":
# Handle value being a dictionary
@ -142,6 +141,11 @@ def password():
return ""
@fixture
def now_date():
return {"datetime": datetime, "calendar_parse": __get_pdt_calendar()}
@fixture
def cache_dir():
return {"exists": False, "path": ""}
@ -251,6 +255,29 @@ def we_enter_editor(editor_method, editor_input, editor_state):
editor_state["intent"] = {"method": file_method, "input": editor_input}
@given(parse('now is "<date_str>"'), target_fixture="now_date")
@given(parse('now is "{date_str}"'), target_fixture="now_date")
def now_is_str(date_str):
class DatetimeMagicMock(MagicMock):
# needed because jrnl does some reflection on datetime
def __instancecheck__(self, subclass):
return isinstance(subclass, datetime)
my_date = datetime.strptime(date_str, "%Y-%m-%d %I:%M:%S %p")
# jrnl uses two different classes to parse dates, so both must be mocked
datetime_mock = DatetimeMagicMock(wraps=datetime)
datetime_mock.now.return_value = my_date
pdt = __get_pdt_calendar()
calendar_mock = MagicMock(wraps=pdt)
calendar_mock.parse.side_effect = lambda date_str_input: pdt.parse(
date_str_input, my_date
)
return {"datetime": datetime_mock, "calendar_parse": calendar_mock}
@then(parse("the editor should have been called"))
@then(parse("the editor should have been called with {num_args} arguments"))
def count_editor_args(num_args, cli_run, editor_state):
@ -328,9 +355,9 @@ def we_run(
cli_run,
capsys,
password,
keyring,
cache_dir,
editor,
now_date,
):
if cache_dir["exists"]:
command = command.format(cache_dir=cache_dir["path"])
@ -354,6 +381,8 @@ def we_run(
patch("sys.stdin.read", side_effect=user_input) as mock_stdin, \
patch("builtins.input", side_effect=user_input) as mock_input, \
patch("getpass.getpass", side_effect=password) as mock_getpass, \
patch("datetime.datetime", new=now_date["datetime"]), \
patch("jrnl.time.__get_pdt_calendar", return_value=now_date["calendar_parse"]), \
patch("jrnl.install.get_config_path", return_value=config_path), \
patch("jrnl.config.get_config_path", return_value=config_path), \
patch("subprocess.call", side_effect=editor) as mock_editor \
@ -392,48 +421,53 @@ def output_should_match(regex, cli_run):
assert matches, f"\nRegex didn't match:\n{regex}\n{str(out)}\n{str(matches)}"
@then(parse("the output should contain\n{output}"))
@then(parse('the output should contain "{output}"'))
@then('the output should contain "<output>"')
@then(parse("the {which_output_stream} output should contain\n{output}"))
@then(parse('the {which_output_stream} output should contain "{output}"'))
def output_should_contain(output, which_output_stream, cli_run):
assert output
@then(parse("the output should contain\n{expected_output}"))
@then(parse('the output should contain "{expected_output}"'))
@then('the output should contain "<expected_output>"')
@then(parse("the {which_output_stream} output should contain\n{expected_output}"))
@then(parse('the {which_output_stream} output should contain "{expected_output}"'))
def output_should_contain(expected_output, which_output_stream, cli_run):
assert expected_output
if which_output_stream is None:
assert (output in cli_run["stdout"]) or (output in cli_run["stderr"])
assert (expected_output in cli_run["stdout"]) or (
expected_output in cli_run["stderr"]
)
elif which_output_stream == "standard":
assert output in cli_run["stdout"]
assert expected_output in cli_run["stdout"]
elif which_output_stream == "error":
assert output in cli_run["stderr"]
assert expected_output in cli_run["stderr"]
else:
assert output in cli_run[which_output_stream]
assert expected_output in cli_run[which_output_stream]
@then(parse("the output should not contain\n{output}"))
@then(parse('the output should not contain "{output}"'))
@then('the output should not contain "<output>"')
def output_should_not_contain(output, cli_run):
assert output not in cli_run["stdout"]
@then(parse("the output should not contain\n{expected_output}"))
@then(parse('the output should not contain "{expected_output}"'))
@then('the output should not contain "<expected_output>"')
def output_should_not_contain(expected_output, cli_run):
assert expected_output not in cli_run["stdout"]
@then(parse("the output should be\n{expected_output}"))
@then(parse('the output should be "{expected_output}"'))
@then('the output should be "<expected_output>"')
def output_should_be(expected_output, cli_run):
actual = cli_run["stdout"].strip()
expected = expected_output.strip()
assert expected == actual
@then(parse("the output should be\n{str_value}"))
@then(parse('the output should be "{str_value}"'))
@then('the output should be "<str_value>"')
@then("the output should be empty")
def output_should_be(str_value, cli_run):
actual_out = cli_run["stdout"].strip()
expected = str_value.strip()
assert expected == actual_out, failed_msg(
"Output does not match.", expected, actual_out
)
def output_should_be_empty(cli_run):
actual = cli_run["stdout"].strip()
assert actual == ""
@then('the output should contain the date "<date>"')
def output_should_contain_date(output, cli_run):
assert output and output in cli_run["stdout"]
def output_should_contain_date(date, cli_run):
assert date and date in cli_run["stdout"]
@then("the output should contain pyproject.toml version")
@ -574,12 +608,12 @@ def assert_output_is_valid_language(cli_run, language_name):
@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"]
actual_output = cli_run["stdout"]
if language_name == "XML":
parsed_output = ElementTree.fromstring(output)
parsed_output = ElementTree.fromstring(actual_output)
elif language_name == "JSON":
parsed_output = json.loads(output)
parsed_output = json.loads(actual_output)
else:
assert False, f"Language name {language_name} not recognized"
@ -669,6 +703,6 @@ def assert_output_field_content(
@then(parse('there should be {number:d} "{item}" elements'))
def count_elements(number, item, cli_run):
output = cli_run["stdout"]
xml_tree = ElementTree.fromstring(output)
actual_output = cli_run["stdout"]
xml_tree = ElementTree.fromstring(actual_output)
assert len(xml_tree.findall(".//" + item)) == number