From c500730ae646d747e49677fbbb38870c1f3d4533 Mon Sep 17 00:00:00 2001 From: Jonathan Wren Date: Sat, 20 Feb 2021 12:28:48 -0800 Subject: [PATCH] Implement and add datetime tests - Deletes most of the datetime tests so that they can be re-added as they are implemented in pytest-bdd - Implements steps that check the journal and output for strings - Get rid of deployment tag (we're not using it, anyway) Co-authored-by: Micah Jerome Ellison --- tests/features/build.feature | 1 - tests/features/datetime.feature | 148 +------------------------------ tests/step_defs/conftest.py | 43 ++++++--- tests/step_defs/test_features.py | 4 +- 4 files changed, 36 insertions(+), 160 deletions(-) diff --git a/tests/features/build.feature b/tests/features/build.feature index 4725ea85..e9b47b49 100644 --- a/tests/features/build.feature +++ b/tests/features/build.feature @@ -1,6 +1,5 @@ Feature: Build process - @deployment_tests Scenario: Version numbers should stay in sync Given we use the config "simple.yaml" When we run "jrnl --version" diff --git a/tests/features/datetime.feature b/tests/features/datetime.feature index 8fe335c9..cbb31ff4 100644 --- a/tests/features/datetime.feature +++ b/tests/features/datetime.feature @@ -5,151 +5,7 @@ Feature: Reading and writing to journal with custom date formats Given we use the config "simple.yaml" When we run "jrnl 2013-11-30 15:42: Project Started." Then we should see the message "Entry added" - And the journal should contain "[2013-11-30 15:42] Project Started." + When we run "jrnl -999" + Then the output should contain "2013-11-30 15:42 Project Started." - Scenario: Dates can be in the future - # https://github.com/jrnl-org/jrnl/issues/185 - Given we use the config "simple.yaml" - When we run "jrnl 26/06/2099: Planet? Earth. Year? 2099." - Then we should see the message "Entry added" - And the journal should contain "[2099-06-26 09:00] Planet?" - Scenario: Loading a sample journal with custom date - Given we use the config "little_endian_dates.yaml" - When we run "jrnl -n 2" - Then we should get no error - And the output should be - """ - 09.06.2013 15:39 My first entry. - | Everything is alright - - 10.07.2013 15:40 Life is good. - | But I'm better. - """ - - Scenario Outline: Writing an entry from command line with custom date - Given we use the config ".yaml" - When we run "jrnl " - Then we should see the message "Entry added" - When we run "jrnl -n 1" - Then the output should contain "" - - Examples: Day-first Dates - | config | input | output | - | little_endian_dates | 2020-09-19: My first entry. | 19.09.2020 09:00 My first entry. | - | little_endian_dates | 2020-08-09: My second entry. | 09.08.2020 09:00 My second entry. | - | little_endian_dates | 2020-02-29: Test. | 29.02.2020 09:00 Test. | - | little_endian_dates | 2019-02-29: Test. | 2019-02-29: Test. | - | little_endian_dates | 2020-08-32: Test. | 2020-08-32: Test. | - | little_endian_dates | 2032-02-01: Test. | 01.02.2032 09:00 Test. | - | little_endian_dates | 2020-01-01: Test. | 01.01.2020 09:00 Test. | - | little_endian_dates | 2020-12-31: Test. | 31.12.2020 09:00 Test. | - - Scenario Outline: Searching for dates with custom date - Given we use the config ".yaml" - When we run "jrnl -on '' --short" - Then the output should be "" - - Examples: Day-first Dates - | config | input | output | - | little_endian_dates | 2013-07-10 | 10.07.2013 15:40 Life is good. | - | little_endian_dates | june 9 2013 | 09.06.2013 15:39 My first entry. | - | little_endian_dates | july 10 2013 | 10.07.2013 15:40 Life is good. | - | little_endian_dates | june 2013 | 09.06.2013 15:39 My first entry. | - | little_endian_dates | july 2013 | 10.07.2013 15:40 Life is good. | - # @todo month alone with no year should work - # | little_endian_dates | june | 09.06.2013 15:39 My first entry. | - # | little_endian_dates | july | 10.07.2013 15:40 Life is good. | - - Scenario: Writing an entry at the prompt with custom date - Given we use the config "little_endian_dates.yaml" - When we run "jrnl" and enter "2013-05-10: I saw Elvis. He's alive." - Then we should get no error - And the journal should contain "[10.05.2013 09:00] I saw Elvis." - And the journal should contain "He's alive." - - Scenario: Viewing today's entries does not print the entire journal - # https://github.com/jrnl-org/jrnl/issues/741 - Given we use the config "simple.yaml" - When we run "jrnl -on today" - 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. - Given we use the config "simple.yaml" - When we run "jrnl : This is an entry on a ." - Then we should see the message "Entry added" - When we run "jrnl -1" - Then the output should contain " at 9am" in the local time - And the output should contain "This is an entry on a ." - - Examples: Days of the week - | day | - | Monday | - | Tuesday | - | Wednesday | - | Thursday | - | Friday | - | Saturday | - | Sunday | - | sunday | - | sUndAy | - - Scenario Outline: Create entry using day of the week abbreviations as entry date. - Given we use the config "simple.yaml" - When we run "jrnl : This is an entry on a ." - Then we should see the message "Entry added" - When we run "jrnl -1" - Then the output should contain " at 9am" in the local time - - Examples: Days of the week - | day | weekday | - | mon | Monday | - | tue | Tuesday | - | wed | Wednesday | - | thu | Thursday | - | fri | Friday | - | sat | Saturday | - | sun | Sunday | - - Scenario: Journals with unreadable dates should still be loaded - Given we use the config "unreadabledates.yaml" - When we run "jrnl -2" - Then the output should contain "I've lost track of time." - And the output should contain "Time has no meaning." - - Scenario: Journals with readable dates AND unreadable dates should still contain all data. - Given we use the config "mostlyreadabledates.yaml" - When we run "jrnl -3" - Then the output should contain "Time machines are possible." - Then the output should contain "I'm going to activate the machine." - And the output should contain "I've crossed so many timelines. Is there any going back?" - And the journal should have 3 entries - - Scenario: Update near-valid dates after journal is edited - Given we use the config "mostlyreadabledates.yaml" - When we run "jrnl 2222-08-19: I have made it exactly one month into the future." - Then the journal should contain "[2019-07-01 14:23] Entry subject" - - Scenario: Integers in square brackets should not be read as dates - Given we use the config "brackets.yaml" - When we run "jrnl -1" - Then the output should contain "[1] line starting with 1" - - # broken still - @skip - Scenario: Dayone entries without timezone information are interpreted in current timezone - Given we use the config "dayone.yaml" - When we run "jrnl -until 'feb 2013'" - Then we should get no error - And the output should contain "2013-01-17T18:37Z" in the local time - - Scenario: Loading entry with ambiguous time stamp in timezone-aware journal (like Dayone) - #https://github.com/jrnl-org/jrnl/issues/153 - Given we use the config "bug153.yaml" - When we run "jrnl -1" - Then we should get no error - And the output should be - """ - 2013-10-27 03:27 Some text. - """ diff --git a/tests/step_defs/conftest.py b/tests/step_defs/conftest.py index 1ebfe974..a764d090 100644 --- a/tests/step_defs/conftest.py +++ b/tests/step_defs/conftest.py @@ -1,49 +1,60 @@ # Copyright (C) 2012-2021 jrnl contributors # License: https://www.gnu.org/licenses/gpl-3.0.html -import shutil import os import re +import shutil import tempfile -import toml +from unittest.mock import patch -from pytest import fixture from pytest_bdd import given from pytest_bdd import then from pytest_bdd import when from pytest_bdd.parsers import parse -from unittest.mock import patch +from pytest import fixture +import toml from jrnl import __version__ -from jrnl.os_compat import split_args from jrnl.cli import cli +from jrnl.os_compat import split_args + # ----- FIXTURES ----- # @fixture def cli_run(): return {"status": 0, "stdout": None, "stderr": None} + @fixture def temp_dir(): return tempfile.TemporaryDirectory() + @fixture def working_dir(request): return os.path.join(request.config.rootpath, "tests") + @fixture def toml_version(working_dir): pyproject = os.path.join(working_dir, "..", "pyproject.toml") pyproject_contents = toml.load(pyproject) return pyproject_contents["tool"]["poetry"]["version"] + +@fixture +def read_journal(journal_name="default"): + configuration = load_config(context.config_path) + with open(configuration["journals"][journal_name]) as journal_file: + journal = journal_file.read() + return journal + + # ----- STEPS ----- # @given(parse('we use the config "{config_file}"'), target_fixture="config_path") def set_config(config_file, temp_dir, working_dir): # Copy the config file over - config_source = os.path.join( - working_dir, "data", "configs", config_file - ) + config_source = os.path.join(working_dir, "data", "configs", config_file) config_dest = os.path.join(temp_dir.name, config_file) shutil.copy2(config_source, config_dest) @@ -102,11 +113,23 @@ def matches_std_output(regex, cli_run): @then(parse("the output should contain\n{text}")) @then(parse('the output should contain "{text}"')) def check_output_inline(text, cli_run): - assert text and text in cli_run['stdout'] + assert text and text in cli_run["stdout"] @then("the output should contain pyproject.toml version") def check_output_version_inline(cli_run, toml_version): - out = cli_run['stdout'] + out = cli_run["stdout"] assert toml_version in out, toml_version + +@then(parse('we should see the message "{text}"')) +def check_message(text, cli_run): + out = cli_run["stderr"] + assert text in out, [text, out] + + +@then(parse('the journal should contain "{text}"')) +@then(parse('journal "{journal_name}" should contain "{text}"')) +def check_journal_content(context, text, journal_name="default"): + journal = read_journal(context, journal_name) + assert text in journal, journal diff --git a/tests/step_defs/test_features.py b/tests/step_defs/test_features.py index b93f2140..5426d0c0 100644 --- a/tests/step_defs/test_features.py +++ b/tests/step_defs/test_features.py @@ -2,7 +2,7 @@ from pytest_bdd import scenarios scenarios("../features/build.feature") scenarios("../features/core.feature") -# scenarios("../features/datetime.feature") +scenarios("../features/datetime.feature") # scenarios("../features/delete.feature") # scenarios("../features/encrypt.feature") # scenarios("../features/file_storage.feature") @@ -15,5 +15,3 @@ scenarios("../features/core.feature") # scenarios("../features/tag.feature") # scenarios("../features/upgrade.feature") # scenarios("../features/write.feature") - -