def test_querying(create, tmpdir): for list in 'abc': for i, location in enumerate('abc'): create('test{}.ics'.format(i), ('SUMMARY:test_querying\r\n' 'LOCATION:{}\r\n').format(location), list_name=list) db = Database([str(tmpdir.ensure_dir(l)) for l in 'abc'], str(tmpdir.join('cache'))) assert len(set(db.todos())) == 9 assert len(set(db.todos(lists='ab'))) == 6 assert len(set(db.todos(lists='ab', location='a'))) == 2
def test_querying(create, tmpdir): for list in "abc": for i, location in enumerate("abc"): create( "test{}.ics".format(i), ("SUMMARY:test_querying\r\nLOCATION:{}\r\n").format(location), list_name=list, ) db = Database([str(tmpdir.ensure_dir(list_)) for list_ in "abc"], str(tmpdir.join("cache"))) assert len(set(db.todos())) == 9 assert len(set(db.todos(lists="ab"))) == 6 assert len(set(db.todos(lists="ab", location="a"))) == 2
def test_change_paths(tmpdir, create): old_todos = set("abcdefghijk") for x in old_todos: create("{}.ics".format(x), "SUMMARY:{}\n".format(x), x) tmpdir.mkdir("3") db = Database([tmpdir.join(x) for x in old_todos], tmpdir.join("cache.sqlite")) assert {t.summary for t in db.todos()} == old_todos db.paths = [str(tmpdir.join("3"))] db.update_cache() assert len(list(db.lists())) == 1 assert not list(db.todos())
def test_change_paths(tmpdir, create): old_todos = set('abcdefghijk') for x in old_todos: create('{}.ics'.format(x), 'SUMMARY:{}\n'.format(x), x) tmpdir.mkdir('3') db = Database([tmpdir.join(x) for x in old_todos], tmpdir.join('cache.sqlite')) assert {t.summary for t in db.todos()} == old_todos db.paths = [str(tmpdir.join('3'))] db.update_cache() assert len(list(db.lists())) == 1 assert not list(db.todos())
def test_dtstamp(tmpdir, runner, create): """Test that we add the DTSTAMP to new entries as per RFC5545.""" result = runner.invoke(cli, ["new", "-l", "default", "test event"]) assert not result.exception db = Database([tmpdir.join("default")], tmpdir.join("/dtstamp_cache")) todo = list(db.todos())[0] assert todo.dtstamp is not None assert todo.dtstamp == datetime.datetime.now(tz=tzlocal())
def test_default_priority(tmpdir, runner, create): """Test setting the due date using the default_due config parameter""" path = tmpdir.join("config") path.write("default_priority = 3\n", "a") runner.invoke(cli, ["new", "-l", "default", "aaa"]) db = Database([tmpdir.join("default")], tmpdir.join("/default_list")) todo = list(db.todos())[0] assert todo.priority == 3
def test_default_priority(tmpdir, runner, create): """Test setting the due date using the default_due config parameter""" path = tmpdir.join('config') path.write('default_priority = 3\n', 'a') runner.invoke(cli, ['new', '-l', 'default', 'aaa']) db = Database([tmpdir.join('default')], tmpdir.join('/default_list')) todo = list(db.todos())[0] assert todo.priority == 3
def test_dtstamp(tmpdir, runner, create): """ Test that we add the DTSTAMP to new entries as per RFC5545. """ result = runner.invoke(cli, ['new', '-l', 'default', 'test event']) assert not result.exception db = Database([tmpdir.join('default')], tmpdir.join('/dtstamp_cache')) todo = list(db.todos())[0] assert todo.dtstamp is not None assert todo.dtstamp.tzinfo is pytz.utc
def test_no_default_priority(tmpdir, runner, create): """Test setting the due date using the default_due config parameter""" runner.invoke(cli, ["new", "-l", "default", "aaa"]) db = Database([tmpdir.join("default")], tmpdir.join("/default_list")) todo = list(db.todos())[0] assert todo.priority == 0 todo_file = tmpdir.join("default").join(todo.filename) todo_ics = todo_file.read_text("utf-8") assert "PRIORITY" not in todo_ics
def test_no_default_priority(tmpdir, runner, create): """Test setting the due date using the default_due config parameter""" runner.invoke(cli, ['new', '-l', 'default', 'aaa']) db = Database([tmpdir.join('default')], tmpdir.join('/default_list')) todo = list(db.todos())[0] assert todo.priority == 0 todo_file = tmpdir.join('default').join(todo.filename) todo_ics = todo_file.read_text('utf-8') assert 'PRIORITY' not in todo_ics
def test_default_list(tmpdir, runner, create): """Test the default_list config parameter""" result = runner.invoke(cli, ["new", "test default list"]) assert result.exception path = tmpdir.join("config") path.write("default_list = default\n", "a") result = runner.invoke(cli, ["new", "test default list"]) assert not result.exception db = Database([tmpdir.join("default")], tmpdir.join("/default_list")) todo = list(db.todos())[0] assert todo.summary == "test default list"
def test_default_list(tmpdir, runner, create): """Test the default_list config parameter""" result = runner.invoke(cli, ['new', 'test default list']) assert result.exception path = tmpdir.join('config') path.write('default_list = default\n', 'a') result = runner.invoke(cli, ['new', 'test default list']) assert not result.exception db = Database([tmpdir.join('default')], tmpdir.join('/default_list')) todo = list(db.todos())[0] assert todo.summary == 'test default list'
def test_default_due(tmpdir, runner, create, default_due, expected_due_hours): """Test setting the due date using the default_due config parameter""" if default_due is not None: path = tmpdir.join("config") path.write("default_due = {}\n".format(default_due), "a") runner.invoke(cli, ["new", "-l", "default", "aaa"]) db = Database([tmpdir.join("default")], tmpdir.join("/default_list")) todo = list(db.todos())[0] if expected_due_hours is None: assert todo.due is None else: assert (todo.due - todo.created_at) == datetime.timedelta( hours=expected_due_hours)
def test_default_due(tmpdir, runner, create, default_due, expected_due_hours): """Test setting the due date using the default_due config parameter""" if default_due is not None: path = tmpdir.join('config') path.write('default_due = {}\n'.format(default_due), 'a') runner.invoke(cli, ['new', '-l', 'default', 'aaa']) db = Database([tmpdir.join('default')], tmpdir.join('/default_list')) todo = list(db.todos())[0] if expected_due_hours is None: assert todo.due is None else: assert (todo.due - todo.created_at) == datetime.timedelta( hours=expected_due_hours)
def test_due_naive(tmpdir, runner, create): now = datetime.now() for i in [1, 23, 25, 48]: due = now + timedelta(hours=i) create( 'test_{}.ics'.format(i), 'SUMMARY:{}\n' 'DUE;VALUE=DATE-TIME:{}\n'.format( i, due.strftime("%Y%m%dT%H%M%S"), )) db = Database([tmpdir.join('default')], tmpdir.join('cache.sqlite')) todos = list(db.todos(due=24)) assert len(todos) == 2 assert todos[0].summary == "23" assert todos[1].summary == "1"
def test_default_due( tmpdir, runner, create, default_due, expected_due_hours ): """Test setting the due date using the default_due config parameter""" if default_due is not None: path = tmpdir.join('config') path.write('default_due = {}\n'.format(default_due), 'a') runner.invoke(cli, ['new', '-l', 'default', 'aaa']) db = Database([tmpdir.join('default')], tmpdir.join('/default_list')) todo = list(db.todos())[0] if expected_due_hours is None: assert todo.due is None else: assert (todo.due - todo.created_at) == datetime.timedelta( hours=expected_due_hours )
def test_due_aware(tmpdir, runner, create, now_for_tz): db = Database([tmpdir.join('default')], tmpdir.join('cache.sqlite')) l = next(db.lists()) for tz in ['CET', 'HST']: for i in [1, 23, 25, 48]: todo = FileTodo() todo.due = now_for_tz(tz) + timedelta(hours=i) todo.summary = '{}'.format(i) db.save(todo, l) todos = list(db.todos(due=24)) assert len(todos) == 4 assert todos[0].summary == "23" assert todos[1].summary == "23" assert todos[2].summary == "1" assert todos[3].summary == "1"
def test_due_naive(tmpdir, runner, create): now = datetime.now() for i in [1, 23, 25, 48]: due = now + timedelta(hours=i) create( 'test_{}.ics'.format(i), 'SUMMARY:{}\n' 'DUE;VALUE=DATE-TIME:{}\n'.format( i, due.strftime("%Y%m%dT%H%M%S"), ) ) db = Database([tmpdir.join('default')], tmpdir.join('cache.sqlite')) todos = list(db.todos(due=24)) assert len(todos) == 2 assert todos[0].summary == "23" assert todos[1].summary == "1"
def test_due_aware(tmpdir, runner, create, now_for_tz): db = Database([tmpdir.join("default")], tmpdir.join("cache.sqlite")) list_ = next(db.lists()) for tz in ["CET", "HST"]: for i in [1, 23, 25, 48]: todo = Todo(new=True) todo.due = now_for_tz(tz) + timedelta(hours=i) todo.summary = "{}".format(i) todo.list = list_ db.save(todo) todos = list(db.todos(due=24)) assert len(todos) == 4 assert todos[0].summary == "23" assert todos[1].summary == "23" assert todos[2].summary == "1" assert todos[3].summary == "1"
def test_due_aware(tmpdir, runner, create): now = datetime.now() db = Database([tmpdir.join('default')], tmpdir.join('cache.sqlite')) l = next(db.lists()) for tz in ['CET', 'HST']: for i in [1, 23, 25, 48]: todo = FileTodo() todo.due = (now + timedelta(hours=i)).replace(tzinfo=tzlocal()) \ .astimezone(pytz.timezone(tz)) todo.summary = '{}'.format(i) db.save(todo, l) todos = list(db.todos(due=24)) assert len(todos) == 4 assert todos[0].summary == "23" assert todos[1].summary == "23" assert todos[2].summary == "1" assert todos[3].summary == "1"
def test_retain_tz(tmpdir, create, default_database): create( 'ar.ics', 'SUMMARY:blah.ar\n' 'DUE;VALUE=DATE-TIME;TZID=HST:20160102T000000\n' ) create( 'de.ics', 'SUMMARY:blah.de\n' 'DUE;VALUE=DATE-TIME;TZID=CET:20160102T000000\n' ) db = Database([tmpdir.join('default')], tmpdir.join('cache.sqlite')) todos = list(db.todos()) assert len(todos) == 2 assert todos[0].due == datetime( 2016, 1, 2, 0, 0, tzinfo=tzoffset(None, -36000) ) assert todos[1].due == datetime( 2016, 1, 2, 0, 0, tzinfo=tzoffset(None, 3600) )
class TodoStore(): def __init__(self): config = load_config() paths = [ path for path in glob.iglob(expanduser(config['main']['path'])) if isdir(path) ] if not paths: raise Exception('No todos found!') self.db = Database(paths, config['main']['cache_path']) def get_todos(self): return self.db.todos(status='ANY') def _set_status(self, todo, status): todo.status = status self.db.save(todo) def todo(self, todo): todo.completed_at = None todo.percent_complete = 0 self._set_status(todo, 'NEEDS-ACTION') def in_progress(self, todo): todo.completed_at = None self._set_status(todo, 'IN-PROCESS') def done(self, todo): todo.complete() self.db.save(todo) def cancel(self, todo): todo.cancel() self.db.save(todo)