+ def test_saving_unmodified_task(self):
+ t = Task(self.tw, description="test task")
+ t.save()
+ t.save()
+
+ def test_adding_tag_by_appending(self):
+ t = Task(self.tw, description="test task", tags=['test1'])
+ t.save()
+ t['tags'].add('test2')
+ t.save()
+ self.assertEqual(t['tags'], set(['test1', 'test2']))
+
+ def test_adding_tag_twice(self):
+ t = Task(self.tw, description="test task", tags=['test1'])
+ t.save()
+ t['tags'].add('test2')
+ t['tags'].add('test2')
+ t.save()
+ self.assertEqual(t['tags'], set(['test1', 'test2']))
+
+ def test_adding_tag_by_appending_empty(self):
+ t = Task(self.tw, description="test task")
+ t.save()
+ t['tags'].add('test')
+ t.save()
+ self.assertEqual(t['tags'], set(['test']))
+
+ def test_serializers_returning_empty_string_for_none(self):
+ # Test that any serializer returns '' when passed None
+ t = Task(self.tw)
+ serializers = [getattr(t, serializer_name) for serializer_name in
+ filter(lambda x: x.startswith('serialize_'), dir(t))]
+ for serializer in serializers:
+ self.assertEqual(serializer(None), '')
+
+ def test_deserializer_returning_empty_value_for_empty_string(self):
+ # Test that any deserializer returns empty value when passed ''
+ t = Task(self.tw)
+ deserializers = [getattr(t, deserializer_name) for deserializer_name in
+ filter(lambda x: x.startswith('deserialize_'), dir(t))]
+ 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)
+
+ def test_spawned_task_parent(self):
+ today = datetime.date.today()
+ t = Task(self.tw, description="brush teeth",
+ due=today, recur="daily")
+ t.save()
+
+ spawned = self.tw.tasks.pending().get(due=today)
+ assert spawned['parent'] == t
+
+ def test_modify_number_of_tasks_at_once(self):
+ for i in range(1, 100):
+ Task(self.tw, description="test task %d" % i, tags=['test']).save()
+
+ self.tw.execute_command(['+test', 'mod', 'unified', 'description'])
+
+ def test_return_all_from_executed_command(self):
+ Task(self.tw, description="test task", tags=['test']).save()
+ out, err, rc = self.tw.execute_command(['count'], return_all=True)
+ self.assertEqual(rc, 0)
+
+ def test_return_all_from_failed_executed_command(self):
+ Task(self.tw, description="test task", tags=['test']).save()
+ out, err, rc = self.tw.execute_command(['countinvalid'],
+ return_all=True, allow_failure=False)
+ self.assertNotEqual(rc, 0)
+
+
+class TaskFromHookTest(TasklibTest):
+
+ input_add_data = six.StringIO(
+ '{"description":"Buy some milk",'
+ '"entry":"20141118T050231Z",'
+ '"status":"pending",'
+ '"start":"20141119T152233Z",'
+ '"uuid":"a360fc44-315c-4366-b70c-ea7e7520b749"}')
+
+ input_add_data_recurring = six.StringIO(
+ '{"description":"Mow the lawn",'
+ '"entry":"20160210T224304Z",'
+ '"parent":"62da6227-519c-42c2-915d-dccada926ad7",'
+ '"recur":"weekly",'
+ '"status":"pending",'
+ '"uuid":"81305335-0237-49ff-8e87-b3cdc2369cec"}')
+
+ input_modify_data = six.StringIO(input_add_data.getvalue() + '\n' +
+ '{"description":"Buy some milk finally",'
+ '"entry":"20141118T050231Z",'
+ '"status":"completed",'
+ '"uuid":"a360fc44-315c-4366-b70c-ea7e7520b749"}')
+
+ exported_raw_data = (
+ '{"project":"Home",'
+ '"due":"20150101T232323Z",'
+ '"description":"test task"}')
+
+ def test_setting_up_from_add_hook_input(self):
+ t = Task.from_input(input_file=self.input_add_data, backend=self.tw)
+ self.assertEqual(t['description'], "Buy some milk")
+ self.assertEqual(t.pending, True)
+
+ def test_setting_up_from_add_hook_input_recurring(self):
+ t = Task.from_input(input_file=self.input_add_data_recurring,
+ backend=self.tw)
+ self.assertEqual(t['description'], "Mow the lawn")
+ 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,
+ backend=self.tw)
+ self.assertEqual(t['description'], "Buy some milk finally")
+ self.assertEqual(t.pending, False)
+ self.assertEqual(t.completed, True)
+
+ self.assertEqual(t._original_data['status'], "pending")
+ self.assertEqual(t._original_data['description'], "Buy some milk")
+ self.assertEqual(set(t._modified_fields),
+ set(['status', 'description', 'start']))
+
+ def test_export_data(self):
+ t = Task(self.tw, description="test task",
+ 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"}
+ allowed_segments = self.exported_raw_data[1:-1].split(',')
+ allowed_output = [
+ '{' + ','.join(segments) + '}'
+ for segments in itertools.permutations(allowed_segments)
+ ]
+
+ 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 DatetimeStringTest(TasklibTest):
+
+ def test_simple_now_conversion(self):
+ if self.tw.version < six.text_type('2.4.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 task", due="now")
+ now = local_zone.localize(datetime.datetime.now())
+
+ # Assert that both times are not more than 5 seconds apart
+ if sys.version_info < (2,7):
+ self.assertTrue(total_seconds_2_6(now - t['due']) < 5)
+ self.assertTrue(total_seconds_2_6(t['due'] - now) < 5)
+ else:
+ self.assertTrue((now - t['due']).total_seconds() < 5)
+ self.assertTrue((t['due'] - now).total_seconds() < 5)
+
+ def test_simple_eoy_conversion(self):
+ if self.tw.version < six.text_type('2.4.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 task", due="eoy")
+ now = local_zone.localize(datetime.datetime.now())
+ eoy = local_zone.localize(datetime.datetime(
+ year=now.year,
+ month=12,
+ day=31,
+ hour=23,
+ minute=59,
+ second=59
+ ))
+ self.assertEqual(eoy, t['due'])
+
+ def test_complex_eoy_conversion(self):
+ if self.tw.version < six.text_type('2.4.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 task", due="eoy - 4 months")
+ now = local_zone.localize(datetime.datetime.now())
+ due_date = local_zone.localize(datetime.datetime(
+ year=now.year,
+ month=12,
+ day=31,
+ hour=23,
+ minute=59,
+ second=59
+ )) - datetime.timedelta(0,4 * 30 * 86400)
+ self.assertEqual(due_date, t['due'])
+
+ def test_filtering_with_string_datetime(self):
+ if self.tw.version < six.text_type('2.4.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 task",
+ due=datetime.datetime.now() - datetime.timedelta(0,2))
+ t.save()
+ self.assertEqual(len(self.tw.tasks.filter(due__before="now")), 1)