--- /dev/null
+[report]
+omit = */tests.py
- TASK_VERSION=v2.2.0
- TASK_VERSION=v2.3.0
- TASK_VERSION=v2.4.0
- - TASK_VERSION=2.4.1
+ - TASK_VERSION=v2.4.1
+ - TASK_VERSION=2.4.2
python:
- "2.6"
- "2.7"
# built documents.
#
# The short X.Y version.
-version = '0.8.0'
+version = '0.9.0'
# The full version, including alpha/beta/rc tags.
-release = '0.8.0'
+release = '0.9.0'
# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
>>> task['id']
15
>>> task['due']
- datetime.datetime(2013, 12, 5, 0, 0)
+ datetime.datetime(2015, 2, 5, 0, 0, tzinfo=<DstTzInfo 'Europe/Berlin' CET+1:00:00 STD>)
>>> task['tags']
['work', 'servers']
>>> task3 == task1
True
+Accessing original values
+-------------------------
+
+To access the saved state of the Task, use dict-like access using the
+``original`` attribute:
+
+ >>> t = Task(tw, description="tidy up")
+ >>> t.save()
+ >>> t['description'] = "tidy up the kitchen and bathroom"
+ >>> t['description']
+ "tidy up the kitchen and bathroom"
+ >>> t.original['description']
+ "tidy up"
+
+When you save the task, original values are refreshed to reflect the
+saved state of the task:
+
+ >>> t.save()
+ >>> t.original['description']
+ "tidy up the kitchen and bathroom"
+
+Dealing with dates and time
+---------------------------
+
+Any timestamp-like attributes of the tasks are converted to timezone-aware
+datetime objects. To achieve this, Tasklib leverages ``pytz`` Python module,
+which brings the Olsen timezone databaze to Python.
+
+This shields you from annoying details of Daylight Saving Time shifts
+or conversion between different timezones. For example, to list all the
+tasks which are due midnight if you're currently in Berlin:
+
+ >>> myzone = pytz.timezone('Europe/Berlin')
+ >>> midnight = myzone.localize(datetime(2015,2,2,0,0,0))
+ >>> tw.tasks.filter(due__before=midnight)
+
+However, this is still a little bit tedious. That's why TaskWarrior object
+is capable of automatic timezone detection, using the ``tzlocal`` Python
+module. If your system timezone is set to 'Europe/Berlin', following example
+will work the same way as the previous one:
+
+ >>> tw.tasks.filter(due__before=datetime(2015,2,2,0,0,0))
+
+You can also use simple dates when filtering:
+
+ >>> tw.tasks.filter(due__before=date(2015,2,2))
+
+In such case, a 00:00:00 is used as the time component.
+
+Of course, you can use datetime naive objects when initializing Task object
+or assigning values to datetime atrributes:
+
+ >>> t = Task(tw, description="Buy new shoes", due=date(2015,2,5))
+ >>> t['due']
+ datetime.datetime(2015, 2, 5, 0, 0, tzinfo=<DstTzInfo 'Europe/Berlin' CET+1:00:00 STD>)
+ >>> t['due'] = date(2015,2,6,15,15,15)
+ >>> t['due']
+ datetime.datetime(2015, 2, 6, 15, 15, 15, tzinfo=<DstTzInfo 'Europe/Berlin' CET+1:00:00 STD>)
+
+However, since timezone-aware and timezone-naive datetimes are not comparable
+in Python, this can cause some unexpected behaviour:
+
+ >>> from datetime import datetime
+ >>> now = datetime.now()
+ >>> t = Task(tw, description="take out the trash now")
+ >>> t['due'] = now
+ >>> now
+ datetime.datetime(2015, 2, 1, 19, 44, 4, 770001)
+ >>> t['due']
+ datetime.datetime(2015, 2, 1, 19, 44, 4, 770001, tzinfo=<DstTzInfo 'Europe/Berlin' CET+1:00:00 STD>)
+ >>> t['due'] == now
+ Traceback (most recent call last):
+ File "<stdin>", line 1, in <module>
+ TypeError: can't compare offset-naive and offset-aware datetimes
+
+If you want to compare datetime aware value with datetime naive value, you need
+to localize the naive value first:
+
+ >>> from datetime import datetime
+ >>> from tasklib.task import local_zone
+ >>> now = local_zone.localize(datetime.now())
+ >>> t['due'] = now
+ >>> now
+ datetime.datetime(2015, 2, 1, 19, 44, 4, 770001, tzinfo=<DstTzInfo 'Europe/Berlin' CET+1:00:00 STD>)
+ >>> t['due'] == now
+ True
+
+Also, note that it does not matter whether the timezone aware datetime objects
+are set in the same timezone:
+
+ >>> import pytz
+ >>> t['due']
+ datetime.datetime(2015, 2, 1, 19, 44, 4, 770001, tzinfo=<DstTzInfo 'Europe/Berlin' CET+1:00:00 STD>)
+ >>> now.astimezone(pytz.utc)
+ datetime.datetime(2015, 2, 1, 18, 44, 4, 770001, tzinfo=<UTC>)
+ >>> t['due'] == now.astimezone(pytz.utc)
+ True
+
+
Working with annotations
------------------------
>>> annotation = annotated_task['annotations'][0]
>>> annotation['entry']
- datetime.datetime(2015, 1, 3, 21, 13, 55)
+ datetime.datetime(2015, 1, 3, 21, 13, 55, tzinfo=<DstTzInfo 'Europe/Berlin' CET+1:00:00 STD>)
>>> annotation['description']
u'Yeah, I am annotated!'
This removes the need for maintaining two copies of the same code base and/or
boilerplate code.
+In ``on-modify`` events, tasklib loads both the original version and the modified
+version of the task to the returned ``Task`` object. To access the original data
+(in read-only manner), use ``original`` dict-like attribute:
+
+ >>> t = Task.from_input()
+ >>> t['description']
+ "Modified description"
+ >>> t.original['description']
+ "Original description"
Working with UDAs
-----------------
from setuptools import setup, find_packages
-version = '0.8.0'
+version = '0.9.0'
setup(
name='tasklib',
packages=find_packages(),
include_package_data=True,
test_suite='tasklib.tests',
- install_requires=['six==1.5.2'],
+ install_requires=['six==1.5.2', 'pytz', 'tzlocal'],
classifiers=[
'Development Status :: 4 - Beta',
'Programming Language :: Python',
import json
import logging
import os
+import pytz
import six
import sys
import subprocess
+import tzlocal
DATE_FORMAT = '%Y%m%dT%H%M%SZ'
REPR_OUTPUT_SIZE = 10
VERSION_2_4_0 = six.u('2.4.0')
logger = logging.getLogger(__name__)
+local_zone = tzlocal.get_localzone()
class TaskWarriorException(Exception):
pass
+class ReadOnlyDictView(object):
+ """
+ Provides simplified read-only view upon dict object.
+ """
+
+ def __init__(self, viewed_dict):
+ self.viewed_dict = viewed_dict
+
+ def __getitem__(self, key):
+ return copy.deepcopy(self.viewed_dict.__getitem__(key))
+
+ def __contains__(self, k):
+ return self.viewed_dict.__contains__(k)
+
+ def __iter__(self):
+ for value in self.viewed_dict:
+ yield copy.deepcopy(value)
+
+ def __len__(self):
+ return len(self.viewed_dict)
+
+ def get(self, key, default=None):
+ return copy.deepcopy(self.viewed_dict.get(key, default))
+
+ def items(self):
+ return [copy.deepcopy(v) for v in self.viewed_dict.items()]
+
+ def values(self):
+ return [copy.deepcopy(v) for v in self.viewed_dict.values()]
+
+
class SerializingObject(object):
"""
Common ancestor for TaskResource & TaskFilter, since they both
not export empty-valued attributes) if the attribute
is not iterable (e.g. list or set), in which case
a empty iterable should be used.
+
+ Normalizing methods should hold the following contract:
+ - They are used to validate and normalize the user input.
+ Any attribute value that comes from the user (during Task
+ initialization, assignign values to Task attributes, or
+ filtering by user-provided values of attributes) is first
+ validated and normalized using the normalize_{key} method.
+ - If validation or normalization fails, normalizer is expected
+ to raise ValueError.
"""
def _deserialize(self, key, value):
lambda x: x if x is not None else '')
return dehydrate_func(value)
+ def _normalize(self, key, value):
+ """
+ Use normalize_<key> methods to normalize user input. Any user
+ input will be normalized at the moment it is used as filter,
+ or entered as a value of Task attribute.
+ """
+
+ # None value should not be converted by normalizer
+ if value is None:
+ return None
+
+ normalize_func = getattr(self, 'normalize_{0}'.format(key),
+ lambda x: x)
+
+ return normalize_func(value)
+
def timestamp_serializer(self, date):
if not date:
return ''
+
+ # Any serialized timestamp should be localized, we need to
+ # convert to UTC before converting to string (DATE_FORMAT uses UTC)
+ date = date.astimezone(pytz.utc)
+
return date.strftime(DATE_FORMAT)
def timestamp_deserializer(self, date_str):
if not date_str:
return None
- return datetime.datetime.strptime(date_str, DATE_FORMAT)
+
+ # Return timestamp localized in the local zone
+ naive_timestamp = datetime.datetime.strptime(date_str, DATE_FORMAT)
+ localized_timestamp = pytz.utc.localize(naive_timestamp)
+ return localized_timestamp.astimezone(local_zone)
def serialize_entry(self, value):
return self.timestamp_serializer(value)
def deserialize_entry(self, value):
return self.timestamp_deserializer(value)
+ def normalize_entry(self, value):
+ return self.datetime_normalizer(value)
+
def serialize_modified(self, value):
return self.timestamp_serializer(value)
def deserialize_modified(self, value):
return self.timestamp_deserializer(value)
+ def normalize_modified(self, value):
+ return self.datetime_normalizer(value)
+
+ def serialize_start(self, value):
+ return self.timestamp_serializer(value)
+
+ def deserialize_start(self, value):
+ return self.timestamp_deserializer(value)
+
+ def normalize_start(self, value):
+ return self.datetime_normalizer(value)
+
+ def serialize_end(self, value):
+ return self.timestamp_serializer(value)
+
+ def deserialize_end(self, value):
+ return self.timestamp_deserializer(value)
+
+ def normalize_end(self, value):
+ return self.datetime_normalizer(value)
+
def serialize_due(self, value):
return self.timestamp_serializer(value)
def deserialize_due(self, value):
return self.timestamp_deserializer(value)
+ def normalize_due(self, value):
+ return self.datetime_normalizer(value)
+
def serialize_scheduled(self, value):
return self.timestamp_serializer(value)
def deserialize_scheduled(self, value):
return self.timestamp_deserializer(value)
+ def normalize_scheduled(self, value):
+ return self.datetime_normalizer(value)
+
def serialize_until(self, value):
return self.timestamp_serializer(value)
def deserialize_until(self, value):
return self.timestamp_deserializer(value)
+ def normalize_until(self, value):
+ return self.datetime_normalizer(value)
+
def serialize_wait(self, value):
return self.timestamp_serializer(value)
def deserialize_wait(self, value):
return self.timestamp_deserializer(value)
+ def normalize_wait(self, value):
+ return self.datetime_normalizer(value)
+
+ def serialize_annotations(self, value):
+ value = value if value is not None else []
+
+ # This may seem weird, but it's correct, we want to export
+ # a list of dicts as serialized value
+ serialized_annotations = [json.loads(annotation.export_data())
+ for annotation in value]
+ return serialized_annotations if serialized_annotations else ''
+
def deserialize_annotations(self, data):
return [TaskAnnotation(self, d) for d in data] if data else []
uuids = raw_uuids.split(',')
return set(self.warrior.tasks.get(uuid=uuid) for uuid in uuids if uuid)
+ def datetime_normalizer(self, value):
+ """
+ Normalizes date/datetime value (considered to come from user input)
+ to localized datetime value. Following conversions happen:
+
+ naive date -> localized datetime with the same date, and time=midnight
+ naive datetime -> localized datetime with the same value
+ localized datetime -> localized datetime (no conversion)
+ """
+
+ if (isinstance(value, datetime.date)
+ and not isinstance(value, datetime.datetime)):
+ # Convert to local midnight
+ value_full = datetime.datetime.combine(value, datetime.time.min)
+ localized = local_zone.localize(value_full)
+ elif isinstance(value, datetime.datetime) and value.tzinfo is None:
+ # Convert to localized datetime object
+ localized = local_zone.localize(value)
+ else:
+ # If the value is already localized, there is no need to change
+ # time zone at this point. Also None is a valid value too.
+ localized = value
+
+ return localized
+
+ def normalize_uuid(self, value):
+ # Enforce sane UUID
+ if not isinstance(value, six.string_types) or value == '':
+ raise ValueError("UUID must be a valid non-empty string, "
+ "not: {}".format(value))
+
+ return value
+
class TaskResource(SerializingObject):
read_only_fields = []
def __setitem__(self, key, value):
if key in self.read_only_fields:
raise RuntimeError('Field \'%s\' is read-only' % key)
+
+ # Normalize the user input before saving it
+ value = self._normalize(key, value)
self._data[key] = value
def __str__(self):
def __repr__(self):
return str(self)
+ def export_data(self):
+ """
+ Exports current data contained in the Task as JSON
+ """
+
+ # We need to remove spaces for TW-1504, use custom separators
+ data_tuples = ((key, self._serialize(key, value))
+ for key, value in six.iteritems(self._data))
+
+ # Empty string denotes empty serialized value, we do not want
+ # to pass that to TaskWarrior.
+ data_tuples = filter(lambda t: t[1] is not '', data_tuples)
+ data = dict(data_tuples)
+ return json.dumps(data, separators=(',',':'))
+
+ @property
+ def _modified_fields(self):
+ writable_fields = set(self._data.keys()) - set(self.read_only_fields)
+ for key in writable_fields:
+ new_value = self._data.get(key)
+ old_value = self._original_data.get(key)
+
+ # Make sure not to mark data removal as modified field if the
+ # field originally had some empty value
+ if key in self._data and not new_value and not old_value:
+ continue
+
+ if new_value != old_value:
+ yield key
+
+ @property
+ def modified(self):
+ return bool(list(self._modified_fields))
+
class TaskAnnotation(TaskResource):
read_only_fields = ['entry', 'description']
pass
@classmethod
- def from_input(cls, input_file=sys.stdin, modify=None):
+ def from_input(cls, input_file=sys.stdin, modify=None, warrior=None):
"""
Creates a Task object, directly from the stdin, by reading one line.
If modify=True, two lines are used, first line interpreted as the
but defaults to sys.stdin.
"""
- # TaskWarrior instance is set to None
- task = cls(None)
-
# Detect the hook type if not given directly
name = os.path.basename(sys.argv[0])
modify = name.startswith('on-modify') if modify is None else modify
+ # Create the TaskWarrior instance if none passed
+ if warrior is None:
+ hook_parent_dir = os.path.dirname(os.path.dirname(sys.argv[0]))
+ warrior = TaskWarrior(data_location=hook_parent_dir)
+
+ # TaskWarrior instance is set to None
+ task = cls(warrior)
+
# Load the data from the input
task._load_data(json.loads(input_file.readline().strip()))
# __init__ methods, that would be confusing
# Rather unfortunate syntax due to python2.6 comaptiblity
- self._load_data(dict((key, self._serialize(key, value))
- for (key, value) in six.iteritems(kwargs)))
+ self._data = dict((key, self._normalize(key, value))
+ for (key, value) in six.iteritems(kwargs))
+ self._original_data = copy.deepcopy(self._data)
+
+ # Provide read only access to the original data
+ self.original = ReadOnlyDictView(self._original_data)
def __unicode__(self):
return self['description']
# If the tasks are not saved, return hash of instance id
return id(self).__hash__()
- @property
- def _modified_fields(self):
- writable_fields = set(self._data.keys()) - set(self.read_only_fields)
- for key in writable_fields:
- new_value = self._data.get(key)
- old_value = self._original_data.get(key)
-
- # Make sure not to mark data removal as modified field if the
- # field originally had some empty value
- if key in self._data and not new_value and not old_value:
- continue
-
- if new_value != old_value:
- yield key
-
- @property
- def modified(self):
- return bool(list(self._modified_fields))
-
@property
def completed(self):
return self['status'] == six.text_type('completed')
self.warrior.execute_command([self['uuid'], 'delete'])
# Refresh the status again, so that we have updated info stored
+ self.refresh(only_fields=['status', 'start', 'end'])
+
+ def start(self):
+ if not self.saved:
+ raise Task.NotSaved("Task needs to be saved before it can be started")
+
+ # Refresh, and raise exception if task is already completed/deleted
self.refresh(only_fields=['status'])
+ if self.completed:
+ raise Task.CompletedTask("Cannot start a completed task")
+ elif self.deleted:
+ raise Task.DeletedTask("Deleted task cannot be started")
+
+ self.warrior.execute_command([self['uuid'], 'start'])
+
+ # Refresh the status again, so that we have updated info stored
+ self.refresh(only_fields=['status', 'start'])
def done(self):
if not self.saved:
self.warrior.execute_command([self['uuid'], 'done'])
# Refresh the status again, so that we have updated info stored
- self.refresh(only_fields=['status'])
+ self.refresh(only_fields=['status', 'start', 'end'])
def save(self):
if self.saved and not self.modified:
else:
self._load_data(new_data)
- def export_data(self):
- """
- Exports current data contained in the Task as JSON
- """
-
- # We need to remove spaces for TW-1504, use custom separators
- data_tuples = ((key, self._serialize(key, value))
- for key, value in six.iteritems(self._data))
-
- # Empty string denotes empty serialized value, we do not want
- # to pass that to TaskWarrior.
- data_tuples = filter(lambda t: t[1] is not '', data_tuples)
- data = dict(data_tuples)
- return json.dumps(data, separators=(',',':'))
-
class TaskFilter(SerializingObject):
"""
A set of parameters to filter the task list with.
# Replace the value with empty string, since that is the
# convention in TW for empty values
attribute_key = key.split('.')[0]
+
+ # Since this is user input, we need to normalize before we serialize
+ value = self._normalize(key, value)
value = self._serialize(attribute_key, value)
# If we are filtering by uuid:, do not use uuid keyword
self.config = {
'data.location': os.path.expanduser(data_location),
'confirmation': 'no',
- 'dependency.confirmation': 'no', # See TW-1483 or taskrc man page
+ 'dependency.confirmation': 'no', # See TW-1483 or taskrc man page
+ 'recurrence.confirmation': 'no', # Necessary for modifying R tasks
}
self.tasks = TaskQuerySet(self)
self.version = self._get_version()
stdout, stderr = [x.decode('utf-8') for x in p.communicate()]
return stdout.strip('\n')
- def execute_command(self, args, config_override={}):
+ def execute_command(self, args, config_override={}, allow_failure=True):
command_args = self._get_command_args(
args, config_override=config_override)
logger.debug(' '.join(command_args))
p = subprocess.Popen(command_args, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout, stderr = [x.decode('utf-8') for x in p.communicate()]
- if p.returncode:
+ if p.returncode and allow_failure:
if stderr.strip():
- error_msg = stderr.strip().splitlines()[-1]
+ error_msg = stderr.strip()
else:
error_msg = stdout.strip()
raise TaskWarriorException(error_msg)
return stdout.strip().split('\n')
+ def enforce_recurrence(self):
+ # Run arbitrary report command which will trigger generation
+ # of recurrent tasks.
+ # TODO: Make a version dependant enforcement once
+ # TW-1531 is handled
+ self.execute_command(['next'], allow_failure=False)
+
def filter_tasks(self, filter_obj):
+ self.enforce_recurrence()
args = ['export', '--'] + filter_obj.get_filter_params()
tasks = []
for line in self.execute_command(args):
# coding=utf-8
+import copy
import datetime
import itertools
+import json
+import pytz
import six
import shutil
import tempfile
import unittest
-from .task import TaskWarrior, Task
+from .task import TaskWarrior, Task, ReadOnlyDictView, local_zone, DATE_FORMAT
# http://taskwarrior.org/docs/design/task.html , Section: The Attributes
TASK_STANDARD_ATTRS = (
'priority',
'depends',
'tags',
- 'annotation',
+ 'annotations',
)
class TasklibTest(unittest.TestCase):
filtered_task = self.tw.tasks.get(project="random")
self.assertEqual(filtered_task['project'], "random")
+ def test_filter_with_empty_uuid(self):
+ self.assertRaises(ValueError, lambda: self.tw.tasks.get(uuid=''))
+
+ def test_filter_dummy_by_status(self):
+ t = Task(self.tw, description="test")
+ t.save()
+
+ tasks = self.tw.tasks.filter(status=t['status'])
+ self.assertEqual(list(tasks), [t])
+
+ def test_filter_dummy_by_uuid(self):
+ t = Task(self.tw, description="test")
+ t.save()
+
+ tasks = self.tw.tasks.filter(uuid=t['uuid'])
+ self.assertEqual(list(tasks), [t])
+
+ def test_filter_dummy_by_entry(self):
+ t = Task(self.tw, description="test")
+ t.save()
+
+ tasks = self.tw.tasks.filter(entry=t['entry'])
+ self.assertEqual(list(tasks), [t])
+
+ def test_filter_dummy_by_description(self):
+ t = Task(self.tw, description="test")
+ t.save()
+
+ tasks = self.tw.tasks.filter(description=t['description'])
+ self.assertEqual(list(tasks), [t])
+
+ def test_filter_dummy_by_start(self):
+ t = Task(self.tw, description="test")
+ t.save()
+ t.start()
+
+ tasks = self.tw.tasks.filter(start=t['start'])
+ self.assertEqual(list(tasks), [t])
+
+ def test_filter_dummy_by_end(self):
+ t = Task(self.tw, description="test")
+ t.save()
+ t.done()
+
+ tasks = self.tw.tasks.filter(end=t['end'])
+ self.assertEqual(list(tasks), [t])
+
+ def test_filter_dummy_by_due(self):
+ t = Task(self.tw, description="test", due=datetime.datetime.now())
+ t.save()
+
+ tasks = self.tw.tasks.filter(due=t['due'])
+ self.assertEqual(list(tasks), [t])
+
+ def test_filter_dummy_by_until(self):
+ t = Task(self.tw, description="test")
+ t.save()
+
+ tasks = self.tw.tasks.filter(until=t['until'])
+ self.assertEqual(list(tasks), [t])
+
+ def test_filter_dummy_by_modified(self):
+ # Older TW version does not support bumping modified
+ # on save
+ if self.tw.version < six.text_type('2.2.0'):
+ # Python2.6 does not support SkipTest. As a workaround
+ # mark the test as passed by exiting.
+ if getattr(unittest, 'SkipTest', None) is not None:
+ raise unittest.SkipTest()
+ else:
+ return
+
+ t = Task(self.tw, description="test")
+ t.save()
+
+ tasks = self.tw.tasks.filter(modified=t['modified'])
+ self.assertEqual(list(tasks), [t])
+
+ def test_filter_dummy_by_scheduled(self):
+ t = Task(self.tw, description="test")
+ t.save()
+
+ tasks = self.tw.tasks.filter(scheduled=t['scheduled'])
+ self.assertEqual(list(tasks), [t])
+
+ def test_filter_dummy_by_tags(self):
+ t = Task(self.tw, description="test", tags=["home"])
+ t.save()
+
+ tasks = self.tw.tasks.filter(tags=t['tags'])
+ self.assertEqual(list(tasks), [t])
+
+ def test_filter_dummy_by_projects(self):
+ t = Task(self.tw, description="test", project="random")
+ t.save()
+
+ tasks = self.tw.tasks.filter(project=t['project'])
+ self.assertEqual(list(tasks), [t])
+
+ def test_filter_by_priority(self):
+ t = Task(self.tw, description="test", priority="H")
+ t.save()
+
+ tasks = self.tw.tasks.filter(priority=t['priority'])
+ self.assertEqual(list(tasks), [t])
+
class TaskTest(TasklibTest):
t = Task(self.tw, description="test task")
self.assertRaises(Task.NotSaved, t.refresh)
+ def test_start_unsaved_task(self):
+ t = Task(self.tw, description="test task")
+ self.assertRaises(Task.NotSaved, t.start)
+
def test_delete_deleted_task(self):
t = Task(self.tw, description="test task")
t.save()
self.assertRaises(Task.CompletedTask, t.done)
+ def test_start_completed_task(self):
+ t = Task(self.tw, description="test task")
+ t.save()
+ t.done()
+
+ self.assertRaises(Task.CompletedTask, t.start)
+
def test_complete_deleted_task(self):
t = Task(self.tw, description="test task")
t.save()
self.assertRaises(Task.DeletedTask, t.done)
+ def test_start_completed_task(self):
+ t = Task(self.tw, description="test task")
+ t.save()
+ t.done()
+
+ self.assertRaises(Task.CompletedTask, t.start)
+
+ def test_starting_task(self):
+ t = Task(self.tw, description="test task")
+ now = t.datetime_normalizer(datetime.datetime.now())
+ t.save()
+ t.start()
+
+ self.assertTrue(now.replace(microsecond=0) <= t['start'])
+ self.assertEqual(t['status'], 'pending')
+
+ def test_completing_task(self):
+ t = Task(self.tw, description="test task")
+ now = t.datetime_normalizer(datetime.datetime.now())
+ t.save()
+ t.done()
+
+ self.assertTrue(now.replace(microsecond=0) <= t['end'])
+ self.assertEqual(t['status'], 'completed')
+
+ def test_deleting_task(self):
+ t = Task(self.tw, description="test task")
+ now = t.datetime_normalizer(datetime.datetime.now())
+ t.save()
+ t.delete()
+
+ self.assertTrue(now.replace(microsecond=0) <= t['end'])
+ self.assertEqual(t['status'], 'deleted')
+
def test_modify_simple_attribute_without_space(self):
t = Task(self.tw, description="test")
t.save()
for deserializer in deserializers:
self.assertTrue(deserializer('') in (None, [], set()))
+ def test_normalizers_handling_none(self):
+ # Test that any normalizer can handle None as a valid value
+ t = Task(self.tw)
+
+ for key in TASK_STANDARD_ATTRS:
+ t._normalize(key, None)
+ def test_recurrent_task_generation(self):
+ today = datetime.date.today()
+ t = Task(self.tw, description="brush teeth",
+ due=today, recur="daily")
+ t.save()
+ self.assertEqual(len(self.tw.tasks.pending()), 2)
class TaskFromHookTest(TasklibTest):
'"description":"test task"}')
def test_setting_up_from_add_hook_input(self):
- t = Task.from_input(input_file=self.input_add_data)
+ t = Task.from_input(input_file=self.input_add_data, warrior=self.tw)
self.assertEqual(t['description'], "Buy some milk")
self.assertEqual(t.pending, True)
def test_setting_up_from_modified_hook_input(self):
- t = Task.from_input(input_file=self.input_modify_data, modify=True)
+ t = Task.from_input(input_file=self.input_modify_data, modify=True,
+ warrior=self.tw)
self.assertEqual(t['description'], "Buy some milk finally")
self.assertEqual(t.pending, False)
self.assertEqual(t.completed, True)
def test_export_data(self):
t = Task(self.tw, description="test task",
- project="Home", due=datetime.datetime(2015,1,1,23,23,23))
+ project="Home",
+ due=pytz.utc.localize(datetime.datetime(2015,1,1,23,23,23)))
# Check that the output is a permutation of:
# {"project":"Home","description":"test task","due":"20150101232323Z"}
self.assertTrue(any(t.export_data() == expected
for expected in allowed_output))
+class TimezoneAwareDatetimeTest(TasklibTest):
+
+ def setUp(self):
+ super(TimezoneAwareDatetimeTest, self).setUp()
+ self.zone = local_zone
+ self.localdate_naive = datetime.datetime(2015,2,2)
+ self.localtime_naive = datetime.datetime(2015,2,2,0,0,0)
+ self.localtime_aware = self.zone.localize(self.localtime_naive)
+ self.utctime_aware = self.localtime_aware.astimezone(pytz.utc)
+
+ def test_timezone_naive_datetime_setitem(self):
+ t = Task(self.tw, description="test task")
+ t['due'] = self.localtime_naive
+ self.assertEqual(t['due'], self.localtime_aware)
+
+ def test_timezone_naive_datetime_using_init(self):
+ t = Task(self.tw, description="test task", due=self.localtime_naive)
+ self.assertEqual(t['due'], self.localtime_aware)
+
+ def test_filter_by_naive_datetime(self):
+ t = Task(self.tw, description="task1", due=self.localtime_naive)
+ t.save()
+ matching_tasks = self.tw.tasks.filter(due=self.localtime_naive)
+ self.assertEqual(len(matching_tasks), 1)
+
+ def test_serialize_naive_datetime(self):
+ t = Task(self.tw, description="task1", due=self.localtime_naive)
+ self.assertEqual(json.loads(t.export_data())['due'],
+ self.utctime_aware.strftime(DATE_FORMAT))
+
+ def test_timezone_naive_date_setitem(self):
+ t = Task(self.tw, description="test task")
+ t['due'] = self.localdate_naive
+ self.assertEqual(t['due'], self.localtime_aware)
+
+ def test_timezone_naive_date_using_init(self):
+ t = Task(self.tw, description="test task", due=self.localdate_naive)
+ self.assertEqual(t['due'], self.localtime_aware)
+
+ def test_filter_by_naive_date(self):
+ t = Task(self.tw, description="task1", due=self.localdate_naive)
+ t.save()
+ matching_tasks = self.tw.tasks.filter(due=self.localdate_naive)
+ self.assertEqual(len(matching_tasks), 1)
+
+ def test_serialize_naive_date(self):
+ t = Task(self.tw, description="task1", due=self.localdate_naive)
+ self.assertEqual(json.loads(t.export_data())['due'],
+ self.utctime_aware.strftime(DATE_FORMAT))
+
+ def test_timezone_aware_datetime_setitem(self):
+ t = Task(self.tw, description="test task")
+ t['due'] = self.localtime_aware
+ self.assertEqual(t['due'], self.localtime_aware)
+
+ def test_timezone_aware_datetime_using_init(self):
+ t = Task(self.tw, description="test task", due=self.localtime_aware)
+ self.assertEqual(t['due'], self.localtime_aware)
+
+ def test_filter_by_aware_datetime(self):
+ t = Task(self.tw, description="task1", due=self.localtime_aware)
+ t.save()
+ matching_tasks = self.tw.tasks.filter(due=self.localtime_aware)
+ self.assertEqual(len(matching_tasks), 1)
+
+ def test_serialize_aware_datetime(self):
+ t = Task(self.tw, description="task1", due=self.localtime_aware)
+ self.assertEqual(json.loads(t.export_data())['due'],
+ self.utctime_aware.strftime(DATE_FORMAT))
class AnnotationTest(TasklibTest):
task.save()
self.assertEqual(task['project'], 'test')
+ def test_serialize_annotations(self):
+ # Test that serializing annotations is possible
+ t = Task(self.tw, description="test")
+ t.save()
+
+ t.add_annotation("annotation1")
+ t.add_annotation("annotation2")
+
+ data = t._serialize('annotations', t._data['annotations'])
+
+ self.assertEqual(len(data), 2)
+ self.assertEqual(type(data[0]), dict)
+ self.assertEqual(type(data[1]), dict)
+
+ self.assertEqual(data[0]['description'], "annotation1")
+ self.assertEqual(data[1]['description'], "annotation2")
+
class UnicodeTest(TasklibTest):
def test_non_unicode_task(self):
Task(self.tw, description="test task").save()
self.tw.tasks.get()
+
+class ReadOnlyDictViewTest(unittest.TestCase):
+
+ def setUp(self):
+ self.sample = dict(l=[1,2,3], d={'k':'v'})
+ self.original_sample = copy.deepcopy(self.sample)
+ self.view = ReadOnlyDictView(self.sample)
+
+ def test_readonlydictview_getitem(self):
+ l = self.view['l']
+ self.assertEqual(l, self.sample['l'])
+
+ # Assert that modification changed only copied value
+ l.append(4)
+ self.assertNotEqual(l, self.sample['l'])
+
+ # Assert that viewed dict is not changed
+ self.assertEqual(self.sample, self.original_sample)
+
+ def test_readonlydictview_contains(self):
+ self.assertEqual('l' in self.view, 'l' in self.sample)
+ self.assertEqual('d' in self.view, 'd' in self.sample)
+ self.assertEqual('k' in self.view, 'k' in self.sample)
+
+ # Assert that viewed dict is not changed
+ self.assertEqual(self.sample, self.original_sample)
+
+ def test_readonlydictview_iter(self):
+ self.assertEqual(list(k for k in self.view),
+ list(k for k in self.sample))
+
+ # Assert the view is correct after modification
+ self.sample['new'] = 'value'
+ self.assertEqual(list(k for k in self.view),
+ list(k for k in self.sample))
+
+ def test_readonlydictview_len(self):
+ self.assertEqual(len(self.view), len(self.sample))
+
+ # Assert the view is correct after modification
+ self.sample['new'] = 'value'
+ self.assertEqual(len(self.view), len(self.sample))
+
+ def test_readonlydictview_get(self):
+ l = self.view.get('l')
+ self.assertEqual(l, self.sample.get('l'))
+
+ # Assert that modification changed only copied value
+ l.append(4)
+ self.assertNotEqual(l, self.sample.get('l'))
+
+ # Assert that viewed dict is not changed
+ self.assertEqual(self.sample, self.original_sample)
+
+ def test_readonlydict_items(self):
+ view_items = self.view.items()
+ sample_items = list(self.sample.items())
+ self.assertEqual(view_items, sample_items)
+
+ view_items.append('newkey')
+ self.assertNotEqual(view_items, sample_items)
+ self.assertEqual(self.sample, self.original_sample)
+
+ def test_readonlydict_values(self):
+ view_values = self.view.values()
+ sample_values = list(self.sample.values())
+ self.assertEqual(view_values, sample_values)
+
+ view_list_item = list(filter(lambda x: type(x) is list,
+ view_values))[0]
+ view_list_item.append(4)
+ self.assertNotEqual(view_values, sample_values)
+ self.assertEqual(self.sample, self.original_sample)