Tests for multiple journals

This commit is contained in:
Manuel Ebert 2013-07-22 10:11:37 +02:00
parent f9bdc13210
commit 4b9b5e827b
6 changed files with 86 additions and 3 deletions

View file

@ -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": "@"
}

View file

@ -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)

View file

View file

@ -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

View file

@ -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

View file

@ -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