Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# 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)
tasks = self.tw.tasks.filter(uuid=self.stored['uuid'])
assert self.lazy in tasks
assert type(self.lazy) is LazyUUIDTask
def test_lazy_saved(self):
assert self.lazy.saved is True
def test_lazy_modified(self):
assert self.lazy.modified is False
def test_lazy_modified_fields(self):
assert self.lazy._modified_fields == set()
class LazyUUIDTaskSetTest(TasklibTest):
def setUp(self):
super(LazyUUIDTaskSetTest, self).setUp()
self.task1 = Task(self.tw, description='task 1')
self.task2 = Task(self.tw, description='task 2')
self.task3 = Task(self.tw, description='task 3')
self.task1.save()
self.task2.save()
self.task3.save()
self.uuids = (
self.task1['uuid'],
self.task2['uuid'],
self.task3['uuid'],
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):
def test_create_unsaved_task(self):
# Make sure a new task is not saved unless explicitly called for
Task(self.tw, description='test task')
self.assertEqual(len(self.tw.tasks.all()), 0)
# TODO: once python 2.6 compatibility is over, use context managers here
# and in all subsequent tests for assertRaises
def test_delete_unsaved_task(self):
t = Task(self.tw, description='test task')
self.assertRaises(Task.NotSaved, t.delete)
def test_complete_unsaved_task(self):
t = Task(self.tw, description='test task')
self.assertRaises(Task.NotSaved, t.done)
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_unicode_task(self):
Task(self.tw, description='†åßk').save()
self.tw.tasks.get()
def test_filter_by_unicode_task(self):
Task(self.tw, description='†åßk').save()
tasks = self.tw.tasks.filter(description='†åßk')
self.assertEqual(len(tasks), 1)
def test_non_unicode_task(self):
Task(self.tw, description='test task').save()
self.tw.tasks.get()
class ReadOnlyDictViewTest(unittest.TestCase):
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 = StringIO(
'{"description":"Buy some milk",'
'"entry":"20141118T050231Z",'
'"status":"pending",'
'"start":"20141119T152233Z",'
'"uuid":"a360fc44-315c-4366-b70c-ea7e7520b749"}',
)
input_add_data_recurring = StringIO(
'{"description":"Mow the lawn",'
'"entry":"20160210T224304Z",'
'"parent":"62da6227-519c-42c2-915d-dccada926ad7",'
'"recur":"weekly",'
'"status":"pending",'
'"uuid":"81305335-0237-49ff-8e87-b3cdc2369cec"}',
tw_kwargs = dict(
data_location=self.tmp,
taskrc_location='/',
)
tw_kwargs.update(kwargs)
return TaskWarrior(**tw_kwargs)
def setUp(self):
self.tmp = tempfile.mkdtemp(dir='.')
self.tw = self.get_taskwarrior()
def tearDown(self):
shutil.rmtree(self.tmp)
class TaskWarriorTest(TasklibTest):
def test_custom_command(self):
# ensure that a custom command which contains multiple parts
# is properly split up
tw = self.get_taskwarrior(
task_command='wsl task',
# prevent `_get_version` from running as `wsl` may not exist
version_override=os.getenv('TASK_VERSION'),
)
self.assertEqual(tw._get_task_command(), ['wsl', 'task'])
class TaskFilterTest(TasklibTest):
def test_all_empty(self):
self.assertEqual(len(self.tw.tasks.all()), 0)
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)
class LazyUUIDTaskTest(TasklibTest):
def setUp(self):
super(LazyUUIDTaskTest, self).setUp()
self.stored = Task(self.tw, description='this is test task')
self.stored.save()
self.lazy = LazyUUIDTask(self.tw, self.stored['uuid'])
def test_uuid_non_conversion(self):
assert self.stored['uuid'] == self.lazy['uuid']
assert type(self.lazy) is LazyUUIDTask
def test_lazy_explicit_conversion(self):
assert type(self.lazy) is LazyUUIDTask
self.lazy.replace()
class TaskWarriorTest(TasklibTest):
def test_custom_command(self):
# ensure that a custom command which contains multiple parts
# is properly split up
tw = self.get_taskwarrior(
task_command='wsl task',
# prevent `_get_version` from running as `wsl` may not exist
version_override=os.getenv('TASK_VERSION'),
)
self.assertEqual(tw._get_task_command(), ['wsl', 'task'])
class TaskFilterTest(TasklibTest):
def test_all_empty(self):
self.assertEqual(len(self.tw.tasks.all()), 0)
def test_all_non_empty(self):
Task(self.tw, description='test task').save()
self.assertEqual(len(self.tw.tasks.all()), 1)
self.assertEqual(self.tw.tasks.all()[0]['description'], 'test task')
self.assertEqual(self.tw.tasks.all()[0]['status'], 'pending')
def test_pending_non_empty(self):
Task(self.tw, description='test task').save()
self.assertEqual(len(self.tw.tasks.pending()), 1)
self.assertEqual(
self.tw.tasks.pending()[0]['description'],
'test task',
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 < '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)
# 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)
class AnnotationTest(TasklibTest):
def setUp(self):
super(AnnotationTest, self).setUp()
Task(self.tw, description='test task').save()
def test_adding_annotation(self):
task = self.tw.tasks.get()
task.add_annotation('test annotation')
self.assertEqual(len(task['annotations']), 1)
ann = task['annotations'][0]
self.assertEqual(ann['description'], 'test annotation')
def test_removing_annotation(self):
task = self.tw.tasks.get()
task.add_annotation('test annotation')
ann = task['annotations'][0]