From 4b9b5e827b7df031fde586ecc29f8401f15619dd Mon Sep 17 00:00:00 2001 From: Manuel Ebert Date: Mon, 22 Jul 2013 10:11:37 +0200 Subject: [PATCH] Tests for multiple journals --- features/configs/multiple.json | 16 +++++++++++++ features/environment.py | 2 ++ features/journals/work.journal | 0 features/multiple_journals.feature | 36 ++++++++++++++++++++++++++++++ features/steps/core.py | 34 ++++++++++++++++++++++++++-- jrnl/Journal.py | 1 - 6 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 features/configs/multiple.json create mode 100644 features/journals/work.journal create mode 100644 features/multiple_journals.feature diff --git a/features/configs/multiple.json b/features/configs/multiple.json new file mode 100644 index 00000000..af7a3e15 --- /dev/null +++ b/features/configs/multiple.json @@ -0,0 +1,16 @@ +{ + "default_hour": 9, + "timeformat": "%Y-%m-%d %H:%M", + "linewrap": 80, + "encrypt": false, + "editor": "", + "default_minute": 0, + "highlight": true, + "password": "", + "journals": { + "default": "features/journals/simple.journal", + "work": "features/journals/work.journal", + "ideas": "features/journals/nothing.journal" + }, + "tagsymbols": "@" +} diff --git a/features/environment.py b/features/environment.py index f374c25a..59763616 100644 --- a/features/environment.py +++ b/features/environment.py @@ -17,6 +17,8 @@ def after_scenario(context, scenario): for folder in ("configs", "journals"): original = os.path.join("features", folder) backup = os.path.join("features", folder+"_backup") + for filename in os.listdir(original): + os.remove(os.path.join(original, filename)) for filename in os.listdir(backup): shutil.copy2(os.path.join(backup, filename), original) shutil.rmtree(backup) diff --git a/features/journals/work.journal b/features/journals/work.journal new file mode 100644 index 00000000..e69de29b diff --git a/features/multiple_journals.feature b/features/multiple_journals.feature new file mode 100644 index 00000000..fb026d2e --- /dev/null +++ b/features/multiple_journals.feature @@ -0,0 +1,36 @@ +Feature: Multiple journals + + Scenario: Loading a config with two journals + Given we use the config "multiple.json" + Then journal "default" should have 2 entries + and journal "work" should have 0 entries + + Scenario: Write to default config by default + Given we use the config "multiple.json" + When we run "jrnl this goes to default" + Then journal "default" should have 3 entries + and journal "work" should have 0 entries + + Scenario: Write to specified journal + Given we use the config "multiple.json" + When we run "jrnl work a long day in the office" + Then journal "default" should have 2 entries + and journal "work" should have 1 entry + + Scenario: Tell user which journal was used + Given we use the config "multiple.json" + When we run "jrnl work a long day in the office" + Then the output should contain "Entry added to work journal" + + Scenario: Write to specified journal with a timestamp + Given we use the config "multiple.json" + When we run "jrnl work 23 july 2012: a long day in the office" + Then journal "default" should have 2 entries + and journal "work" should have 1 entry + and journal "work" should contain "2012-07-23" + + Scenario: Create new journals as required + Given we use the config "multiple.json" + Then journal "ideas" should not exist + When we run "jrnl ideas 23 july 2012: sell my junk on ebay and make lots of money" + Then journal "ideas" should have 1 entry diff --git a/features/steps/core.py b/features/steps/core.py index 130fe5b4..36369edc 100644 --- a/features/steps/core.py +++ b/features/steps/core.py @@ -1,5 +1,5 @@ from behave import * -from jrnl import jrnl +from jrnl import jrnl, Journal import os import sys import json @@ -15,6 +15,16 @@ def read_journal(journal_name="default"): journal = journal_file.read() return journal +def open_journal(journal_name="default"): + with open(jrnl.CONFIG_PATH) as config_file: + config = json.load(config_file) + journals = config['journals'] + if type(journals) is dict: # We can override the default config on a by-journal basis + config['journal'] = journals.get(journal_name) + else: # But also just give them a string to point to the journal file + config['journal'] = journal + return Journal.Journal(**config) + @given('we use the config "{config_file}"') def set_config(context, config_file): full_path = os.path.join("features/configs", config_file) @@ -52,8 +62,28 @@ def check_output_inline(context, text): assert text in out @then('the journal should contain "{text}"') -@then('journal {journal_name} should contain "{text}"') +@then('journal "{journal_name}" should contain "{text}"') def check_journal_content(context, text, journal_name="default"): journal = read_journal(journal_name) assert text in journal +@then('journal "{journal_name}" should not exist') +def journal_doesnt_exist(context, journal_name="default"): + with open(jrnl.CONFIG_PATH) as config_file: + config = json.load(config_file) + journal_path = config['journals'][journal_name] + print journal_path + assert not os.path.exists(journal_path) + +@then('the journal should have {number:d} entries') +@then('the journal should have {number:d} entry') +@then('journal "{journal_name}" should have {number:d} entries') +@then('journal "{journal_name}" should have {number:d} entry') +def check_journal_content(context, number, journal_name="default"): + journal = open_journal(journal_name) + assert len(journal.entries) == number + +@then('fail') +def debug_fail(context): + assert False + diff --git a/jrnl/Journal.py b/jrnl/Journal.py index 4c49ad39..9deb2a38 100644 --- a/jrnl/Journal.py +++ b/jrnl/Journal.py @@ -50,7 +50,6 @@ class Journal(object): 'linewrap': 80, } self.config.update(kwargs) - # Set up date parser consts = pdt.Constants(usePyICU=False) consts.DOWParseStyle = -1 # "Monday" will be either today or the last Monday