All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
13 from .task import TaskWarrior, Task, ReadOnlyDictView, local_zone, DATE_FORMAT
15 # http://taskwarrior.org/docs/design/task.html , Section: The Attributes
16 TASK_STANDARD_ATTRS = (
39 class TasklibTest(unittest.TestCase):
42 self.tmp = tempfile.mkdtemp(dir='.')
43 self.tw = TaskWarrior(data_location=self.tmp)
46 shutil.rmtree(self.tmp)
49 class TaskFilterTest(TasklibTest):
51 def test_all_empty(self):
52 self.assertEqual(len(self.tw.tasks.all()), 0)
54 def test_all_non_empty(self):
55 Task(self.tw, description="test task").save()
56 self.assertEqual(len(self.tw.tasks.all()), 1)
57 self.assertEqual(self.tw.tasks.all()[0]['description'], 'test task')
58 self.assertEqual(self.tw.tasks.all()[0]['status'], 'pending')
60 def test_pending_non_empty(self):
61 Task(self.tw, description="test task").save()
62 self.assertEqual(len(self.tw.tasks.pending()), 1)
63 self.assertEqual(self.tw.tasks.pending()[0]['description'],
65 self.assertEqual(self.tw.tasks.pending()[0]['status'], 'pending')
67 def test_completed_empty(self):
68 Task(self.tw, description="test task").save()
69 self.assertEqual(len(self.tw.tasks.completed()), 0)
71 def test_completed_non_empty(self):
72 Task(self.tw, description="test task").save()
73 self.assertEqual(len(self.tw.tasks.completed()), 0)
74 self.tw.tasks.all()[0].done()
75 self.assertEqual(len(self.tw.tasks.completed()), 1)
77 def test_filtering_by_attribute(self):
78 Task(self.tw, description="no priority task").save()
79 Task(self.tw, priority="H", description="high priority task").save()
80 self.assertEqual(len(self.tw.tasks.all()), 2)
82 # Assert that the correct number of tasks is returned
83 self.assertEqual(len(self.tw.tasks.filter(priority="H")), 1)
85 # Assert that the correct tasks are returned
86 high_priority_task = self.tw.tasks.get(priority="H")
87 self.assertEqual(high_priority_task['description'], "high priority task")
89 def test_filtering_by_empty_attribute(self):
90 Task(self.tw, description="no priority task").save()
91 Task(self.tw, priority="H", description="high priority task").save()
92 self.assertEqual(len(self.tw.tasks.all()), 2)
94 # Assert that the correct number of tasks is returned
95 self.assertEqual(len(self.tw.tasks.filter(priority=None)), 1)
97 # Assert that the correct tasks are returned
98 no_priority_task = self.tw.tasks.get(priority=None)
99 self.assertEqual(no_priority_task['description'], "no priority task")
101 def test_filter_for_task_with_space_in_descripition(self):
102 task = Task(self.tw, description="test task")
105 filtered_task = self.tw.tasks.get(description="test task")
106 self.assertEqual(filtered_task['description'], "test task")
108 def test_filter_for_task_without_space_in_descripition(self):
109 task = Task(self.tw, description="test")
112 filtered_task = self.tw.tasks.get(description="test")
113 self.assertEqual(filtered_task['description'], "test")
115 def test_filter_for_task_with_space_in_project(self):
116 task = Task(self.tw, description="test", project="random project")
119 filtered_task = self.tw.tasks.get(project="random project")
120 self.assertEqual(filtered_task['project'], "random project")
122 def test_filter_for_task_without_space_in_project(self):
123 task = Task(self.tw, description="test", project="random")
126 filtered_task = self.tw.tasks.get(project="random")
127 self.assertEqual(filtered_task['project'], "random")
129 def test_filter_with_empty_uuid(self):
130 self.assertRaises(ValueError, lambda: self.tw.tasks.get(uuid=''))
132 def test_filter_dummy_by_status(self):
133 t = Task(self.tw, description="test")
136 tasks = self.tw.tasks.filter(status=t['status'])
137 self.assertEqual(list(tasks), [t])
139 def test_filter_dummy_by_uuid(self):
140 t = Task(self.tw, description="test")
143 tasks = self.tw.tasks.filter(uuid=t['uuid'])
144 self.assertEqual(list(tasks), [t])
146 def test_filter_dummy_by_entry(self):
147 t = Task(self.tw, description="test")
150 tasks = self.tw.tasks.filter(entry=t['entry'])
151 self.assertEqual(list(tasks), [t])
153 def test_filter_dummy_by_description(self):
154 t = Task(self.tw, description="test")
157 tasks = self.tw.tasks.filter(description=t['description'])
158 self.assertEqual(list(tasks), [t])
160 def test_filter_dummy_by_start(self):
161 t = Task(self.tw, description="test")
165 tasks = self.tw.tasks.filter(start=t['start'])
166 self.assertEqual(list(tasks), [t])
168 def test_filter_dummy_by_end(self):
169 t = Task(self.tw, description="test")
173 tasks = self.tw.tasks.filter(end=t['end'])
174 self.assertEqual(list(tasks), [t])
176 def test_filter_dummy_by_due(self):
177 t = Task(self.tw, description="test", due=datetime.datetime.now())
180 tasks = self.tw.tasks.filter(due=t['due'])
181 self.assertEqual(list(tasks), [t])
183 def test_filter_dummy_by_until(self):
184 t = Task(self.tw, description="test")
187 tasks = self.tw.tasks.filter(until=t['until'])
188 self.assertEqual(list(tasks), [t])
190 def test_filter_dummy_by_modified(self):
191 t = Task(self.tw, description="test")
194 tasks = self.tw.tasks.filter(modified=t['modified'])
195 self.assertEqual(list(tasks), [t])
197 def test_filter_dummy_by_scheduled(self):
198 t = Task(self.tw, description="test")
201 tasks = self.tw.tasks.filter(scheduled=t['scheduled'])
202 self.assertEqual(list(tasks), [t])
204 def test_filter_dummy_by_tags(self):
205 t = Task(self.tw, description="test", tags=["home"])
208 tasks = self.tw.tasks.filter(tags=t['tags'])
209 self.assertEqual(list(tasks), [t])
211 def test_filter_dummy_by_projects(self):
212 t = Task(self.tw, description="test", project="random")
215 tasks = self.tw.tasks.filter(project=t['project'])
216 self.assertEqual(list(tasks), [t])
218 def test_filter_by_priority(self):
219 t = Task(self.tw, description="test", priority="H")
222 tasks = self.tw.tasks.filter(priority=t['priority'])
223 self.assertEqual(list(tasks), [t])
226 class TaskTest(TasklibTest):
228 def test_create_unsaved_task(self):
229 # Make sure a new task is not saved unless explicitly called for
230 t = Task(self.tw, description="test task")
231 self.assertEqual(len(self.tw.tasks.all()), 0)
233 # TODO: once python 2.6 compatiblity is over, use context managers here
234 # and in all subsequent tests for assertRaises
236 def test_delete_unsaved_task(self):
237 t = Task(self.tw, description="test task")
238 self.assertRaises(Task.NotSaved, t.delete)
240 def test_complete_unsaved_task(self):
241 t = Task(self.tw, description="test task")
242 self.assertRaises(Task.NotSaved, t.done)
244 def test_refresh_unsaved_task(self):
245 t = Task(self.tw, description="test task")
246 self.assertRaises(Task.NotSaved, t.refresh)
248 def test_delete_deleted_task(self):
249 t = Task(self.tw, description="test task")
253 self.assertRaises(Task.DeletedTask, t.delete)
255 def test_complete_completed_task(self):
256 t = Task(self.tw, description="test task")
260 self.assertRaises(Task.CompletedTask, t.done)
262 def test_complete_deleted_task(self):
263 t = Task(self.tw, description="test task")
267 self.assertRaises(Task.DeletedTask, t.done)
269 def test_modify_simple_attribute_without_space(self):
270 t = Task(self.tw, description="test")
273 self.assertEquals(t['description'], "test")
275 t['description'] = "test-modified"
278 self.assertEquals(t['description'], "test-modified")
280 def test_modify_simple_attribute_with_space(self):
281 # Space can pose problems with parsing
282 t = Task(self.tw, description="test task")
285 self.assertEquals(t['description'], "test task")
287 t['description'] = "test task modified"
290 self.assertEquals(t['description'], "test task modified")
292 def test_empty_dependency_set_of_unsaved_task(self):
293 t = Task(self.tw, description="test task")
294 self.assertEqual(t['depends'], set())
296 def test_empty_dependency_set_of_saved_task(self):
297 t = Task(self.tw, description="test task")
299 self.assertEqual(t['depends'], set())
301 def test_set_unsaved_task_as_dependency(self):
302 # Adds only one dependency to task with no dependencies
303 t = Task(self.tw, description="test task")
304 dependency = Task(self.tw, description="needs to be done first")
306 # We only save the parent task, dependency task is unsaved
308 t['depends'] = set([dependency])
310 self.assertRaises(Task.NotSaved, t.save)
312 def test_set_simple_dependency_set(self):
313 # Adds only one dependency to task with no dependencies
314 t = Task(self.tw, description="test task")
315 dependency = Task(self.tw, description="needs to be done first")
320 t['depends'] = set([dependency])
322 self.assertEqual(t['depends'], set([dependency]))
324 def test_set_complex_dependency_set(self):
325 # Adds two dependencies to task with no dependencies
326 t = Task(self.tw, description="test task")
327 dependency1 = Task(self.tw, description="needs to be done first")
328 dependency2 = Task(self.tw, description="needs to be done second")
334 t['depends'] = set([dependency1, dependency2])
336 self.assertEqual(t['depends'], set([dependency1, dependency2]))
338 def test_remove_from_dependency_set(self):
339 # Removes dependency from task with two dependencies
340 t = Task(self.tw, description="test task")
341 dependency1 = Task(self.tw, description="needs to be done first")
342 dependency2 = Task(self.tw, description="needs to be done second")
347 t['depends'] = set([dependency1, dependency2])
350 t['depends'].remove(dependency2)
353 self.assertEqual(t['depends'], set([dependency1]))
355 def test_add_to_dependency_set(self):
356 # Adds dependency to task with one dependencies
357 t = Task(self.tw, description="test task")
358 dependency1 = Task(self.tw, description="needs to be done first")
359 dependency2 = Task(self.tw, description="needs to be done second")
364 t['depends'] = set([dependency1])
367 t['depends'].add(dependency2)
370 self.assertEqual(t['depends'], set([dependency1, dependency2]))
372 def test_add_to_empty_dependency_set(self):
373 # Adds dependency to task with one dependencies
374 t = Task(self.tw, description="test task")
375 dependency = Task(self.tw, description="needs to be done first")
379 t['depends'].add(dependency)
382 self.assertEqual(t['depends'], set([dependency]))
384 def test_simple_dependency_set_save_repeatedly(self):
385 # Adds only one dependency to task with no dependencies
386 t = Task(self.tw, description="test task")
387 dependency = Task(self.tw, description="needs to be done first")
390 t['depends'] = set([dependency])
393 # We taint the task, but keep depends intact
394 t['description'] = "test task modified"
397 self.assertEqual(t['depends'], set([dependency]))
399 # We taint the task, but assign the same set to the depends
400 t['depends'] = set([dependency])
401 t['description'] = "test task modified again"
404 self.assertEqual(t['depends'], set([dependency]))
406 def test_compare_different_tasks(self):
407 # Negative: compare two different tasks
408 t1 = Task(self.tw, description="test task")
409 t2 = Task(self.tw, description="test task")
414 self.assertEqual(t1 == t2, False)
416 def test_compare_same_task_object(self):
417 # Compare Task object wit itself
418 t = Task(self.tw, description="test task")
421 self.assertEqual(t == t, True)
423 def test_compare_same_task(self):
424 # Compare the same task using two different objects
425 t1 = Task(self.tw, description="test task")
428 t2 = self.tw.tasks.get(uuid=t1['uuid'])
429 self.assertEqual(t1 == t2, True)
431 def test_compare_unsaved_tasks(self):
432 # t1 and t2 are unsaved tasks, considered to be unequal
433 # despite the content of data
434 t1 = Task(self.tw, description="test task")
435 t2 = Task(self.tw, description="test task")
437 self.assertEqual(t1 == t2, False)
439 def test_hash_unsaved_tasks(self):
440 # Considered equal, it's the same object
441 t1 = Task(self.tw, description="test task")
443 self.assertEqual(hash(t1) == hash(t2), True)
445 def test_hash_same_task(self):
446 # Compare the hash of the task using two different objects
447 t1 = Task(self.tw, description="test task")
450 t2 = self.tw.tasks.get(uuid=t1['uuid'])
451 self.assertEqual(t1.__hash__(), t2.__hash__())
453 def test_adding_task_with_priority(self):
454 t = Task(self.tw, description="test task", priority="M")
457 def test_removing_priority_with_none(self):
458 t = Task(self.tw, description="test task", priority="L")
461 # Remove the priority mark
465 # Assert that priority is not there after saving
466 self.assertEqual(t['priority'], None)
468 def test_adding_task_with_due_time(self):
469 t = Task(self.tw, description="test task", due=datetime.datetime.now())
472 def test_removing_due_time_with_none(self):
473 t = Task(self.tw, description="test task", due=datetime.datetime.now())
476 # Remove the due timestamp
480 # Assert that due timestamp is no longer there
481 self.assertEqual(t['due'], None)
483 def test_modified_fields_new_task(self):
486 # This should be empty with new task
487 self.assertEqual(set(t._modified_fields), set())
490 t['description'] = "test task"
491 self.assertEqual(set(t._modified_fields), set(['description']))
493 t['due'] = datetime.datetime(2014, 2, 14, 14, 14, 14) # <3
494 self.assertEqual(set(t._modified_fields), set(['description', 'due']))
496 t['project'] = "test project"
497 self.assertEqual(set(t._modified_fields),
498 set(['description', 'due', 'project']))
500 # List of modified fields should clear out when saved
502 self.assertEqual(set(t._modified_fields), set())
504 # Reassigning the fields with the same values now should not produce
506 t['description'] = "test task"
507 t['due'] = datetime.datetime(2014, 2, 14, 14, 14, 14) # <3
508 t['project'] = "test project"
509 self.assertEqual(set(t._modified_fields), set())
511 def test_modified_fields_loaded_task(self):
515 t['description'] = "test task"
516 t['due'] = datetime.datetime(2014, 2, 14, 14, 14, 14) # <3
517 t['project'] = "test project"
519 dependency = Task(self.tw, description="dependency")
521 t['depends'] = set([dependency])
523 # List of modified fields should clear out when saved
525 self.assertEqual(set(t._modified_fields), set())
527 # Get the task by using a filter by UUID
528 t2 = self.tw.tasks.get(uuid=t['uuid'])
530 # Reassigning the fields with the same values now should not produce
532 t['description'] = "test task"
533 t['due'] = datetime.datetime(2014, 2, 14, 14, 14, 14) # <3
534 t['project'] = "test project"
535 t['depends'] = set([dependency])
536 self.assertEqual(set(t._modified_fields), set())
538 def test_modified_fields_not_affected_by_reading(self):
541 for field in TASK_STANDARD_ATTRS:
544 self.assertEqual(set(t._modified_fields), set())
546 def test_setting_read_only_attrs_through_init(self):
547 # Test that we are unable to set readonly attrs through __init__
548 for readonly_key in Task.read_only_fields:
549 kwargs = {'description': 'test task', readonly_key: 'value'}
550 self.assertRaises(RuntimeError,
551 lambda: Task(self.tw, **kwargs))
553 def test_setting_read_only_attrs_through_setitem(self):
554 # Test that we are unable to set readonly attrs through __init__
555 for readonly_key in Task.read_only_fields:
556 t = Task(self.tw, description='test task')
557 self.assertRaises(RuntimeError,
558 lambda: t.__setitem__(readonly_key, 'value'))
560 def test_saving_unmodified_task(self):
561 t = Task(self.tw, description="test task")
565 def test_adding_tag_by_appending(self):
566 t = Task(self.tw, description="test task", tags=['test1'])
568 t['tags'].append('test2')
570 self.assertEqual(t['tags'], ['test1', 'test2'])
572 def test_adding_tag_by_appending_empty(self):
573 t = Task(self.tw, description="test task")
575 t['tags'].append('test')
577 self.assertEqual(t['tags'], ['test'])
579 def test_serializers_returning_empty_string_for_none(self):
580 # Test that any serializer returns '' when passed None
582 serializers = [getattr(t, serializer_name) for serializer_name in
583 filter(lambda x: x.startswith('serialize_'), dir(t))]
584 for serializer in serializers:
585 self.assertEqual(serializer(None), '')
587 def test_deserializer_returning_empty_value_for_empty_string(self):
588 # Test that any deserializer returns empty value when passed ''
590 deserializers = [getattr(t, deserializer_name) for deserializer_name in
591 filter(lambda x: x.startswith('deserialize_'), dir(t))]
592 for deserializer in deserializers:
593 self.assertTrue(deserializer('') in (None, [], set()))
595 def test_normalizers_handling_none(self):
596 # Test that any normalizer can handle None as a valid value
599 for key in TASK_STANDARD_ATTRS:
600 t._normalize(key, None)
602 def test_recurrent_task_generation(self):
603 today = datetime.date.today()
604 t = Task(self.tw, description="brush teeth",
605 due=today, recur="daily")
607 self.assertEqual(len(self.tw.tasks.pending()), 2)
609 class TaskFromHookTest(TasklibTest):
611 input_add_data = six.StringIO(
612 '{"description":"Buy some milk",'
613 '"entry":"20141118T050231Z",'
614 '"status":"pending",'
615 '"uuid":"a360fc44-315c-4366-b70c-ea7e7520b749"}')
617 input_modify_data = six.StringIO(input_add_data.getvalue() + '\n' +
618 '{"description":"Buy some milk finally",'
619 '"entry":"20141118T050231Z",'
620 '"status":"completed",'
621 '"uuid":"a360fc44-315c-4366-b70c-ea7e7520b749"}')
623 exported_raw_data = (
625 '"due":"20150101T232323Z",'
626 '"description":"test task"}')
628 def test_setting_up_from_add_hook_input(self):
629 t = Task.from_input(input_file=self.input_add_data, warrior=self.tw)
630 self.assertEqual(t['description'], "Buy some milk")
631 self.assertEqual(t.pending, True)
633 def test_setting_up_from_modified_hook_input(self):
634 t = Task.from_input(input_file=self.input_modify_data, modify=True,
636 self.assertEqual(t['description'], "Buy some milk finally")
637 self.assertEqual(t.pending, False)
638 self.assertEqual(t.completed, True)
640 self.assertEqual(t._original_data['status'], "pending")
641 self.assertEqual(t._original_data['description'], "Buy some milk")
642 self.assertEqual(set(t._modified_fields),
643 set(['status', 'description']))
645 def test_export_data(self):
646 t = Task(self.tw, description="test task",
648 due=pytz.utc.localize(datetime.datetime(2015,1,1,23,23,23)))
650 # Check that the output is a permutation of:
651 # {"project":"Home","description":"test task","due":"20150101232323Z"}
652 allowed_segments = self.exported_raw_data[1:-1].split(',')
654 '{' + ','.join(segments) + '}'
655 for segments in itertools.permutations(allowed_segments)
658 self.assertTrue(any(t.export_data() == expected
659 for expected in allowed_output))
661 class TimezoneAwareDatetimeTest(TasklibTest):
664 super(TimezoneAwareDatetimeTest, self).setUp()
665 self.zone = local_zone
666 self.localdate_naive = datetime.datetime(2015,2,2)
667 self.localtime_naive = datetime.datetime(2015,2,2,0,0,0)
668 self.localtime_aware = self.zone.localize(self.localtime_naive)
669 self.utctime_aware = self.localtime_aware.astimezone(pytz.utc)
671 def test_timezone_naive_datetime_setitem(self):
672 t = Task(self.tw, description="test task")
673 t['due'] = self.localtime_naive
674 self.assertEqual(t['due'], self.localtime_aware)
676 def test_timezone_naive_datetime_using_init(self):
677 t = Task(self.tw, description="test task", due=self.localtime_naive)
678 self.assertEqual(t['due'], self.localtime_aware)
680 def test_filter_by_naive_datetime(self):
681 t = Task(self.tw, description="task1", due=self.localtime_naive)
683 matching_tasks = self.tw.tasks.filter(due=self.localtime_naive)
684 self.assertEqual(len(matching_tasks), 1)
686 def test_serialize_naive_datetime(self):
687 t = Task(self.tw, description="task1", due=self.localtime_naive)
688 self.assertEqual(json.loads(t.export_data())['due'],
689 self.utctime_aware.strftime(DATE_FORMAT))
691 def test_timezone_naive_date_setitem(self):
692 t = Task(self.tw, description="test task")
693 t['due'] = self.localdate_naive
694 self.assertEqual(t['due'], self.localtime_aware)
696 def test_timezone_naive_date_using_init(self):
697 t = Task(self.tw, description="test task", due=self.localdate_naive)
698 self.assertEqual(t['due'], self.localtime_aware)
700 def test_filter_by_naive_date(self):
701 t = Task(self.tw, description="task1", due=self.localdate_naive)
703 matching_tasks = self.tw.tasks.filter(due=self.localdate_naive)
704 self.assertEqual(len(matching_tasks), 1)
706 def test_serialize_naive_date(self):
707 t = Task(self.tw, description="task1", due=self.localdate_naive)
708 self.assertEqual(json.loads(t.export_data())['due'],
709 self.utctime_aware.strftime(DATE_FORMAT))
711 def test_timezone_aware_datetime_setitem(self):
712 t = Task(self.tw, description="test task")
713 t['due'] = self.localtime_aware
714 self.assertEqual(t['due'], self.localtime_aware)
716 def test_timezone_aware_datetime_using_init(self):
717 t = Task(self.tw, description="test task", due=self.localtime_aware)
718 self.assertEqual(t['due'], self.localtime_aware)
720 def test_filter_by_aware_datetime(self):
721 t = Task(self.tw, description="task1", due=self.localtime_aware)
723 matching_tasks = self.tw.tasks.filter(due=self.localtime_aware)
724 self.assertEqual(len(matching_tasks), 1)
726 def test_serialize_aware_datetime(self):
727 t = Task(self.tw, description="task1", due=self.localtime_aware)
728 self.assertEqual(json.loads(t.export_data())['due'],
729 self.utctime_aware.strftime(DATE_FORMAT))
731 class AnnotationTest(TasklibTest):
734 super(AnnotationTest, self).setUp()
735 Task(self.tw, description="test task").save()
737 def test_adding_annotation(self):
738 task = self.tw.tasks.get()
739 task.add_annotation('test annotation')
740 self.assertEqual(len(task['annotations']), 1)
741 ann = task['annotations'][0]
742 self.assertEqual(ann['description'], 'test annotation')
744 def test_removing_annotation(self):
745 task = self.tw.tasks.get()
746 task.add_annotation('test annotation')
747 ann = task['annotations'][0]
749 self.assertEqual(len(task['annotations']), 0)
751 def test_removing_annotation_by_description(self):
752 task = self.tw.tasks.get()
753 task.add_annotation('test annotation')
754 task.remove_annotation('test annotation')
755 self.assertEqual(len(task['annotations']), 0)
757 def test_removing_annotation_by_obj(self):
758 task = self.tw.tasks.get()
759 task.add_annotation('test annotation')
760 ann = task['annotations'][0]
761 task.remove_annotation(ann)
762 self.assertEqual(len(task['annotations']), 0)
764 def test_annotation_after_modification(self):
765 task = self.tw.tasks.get()
766 task['project'] = 'test'
767 task.add_annotation('I should really do this task')
768 self.assertEqual(task['project'], 'test')
770 self.assertEqual(task['project'], 'test')
772 def test_serialize_annotations(self):
773 # Test that serializing annotations is possible
774 t = Task(self.tw, description="test")
777 t.add_annotation("annotation1")
778 t.add_annotation("annotation2")
780 data = t._serialize('annotations', t._data['annotations'])
782 self.assertEqual(len(data), 2)
783 self.assertEqual(type(data[0]), dict)
784 self.assertEqual(type(data[1]), dict)
786 self.assertEqual(data[0]['description'], "annotation1")
787 self.assertEqual(data[1]['description'], "annotation2")
790 class UnicodeTest(TasklibTest):
792 def test_unicode_task(self):
793 Task(self.tw, description="†åßk").save()
796 def test_non_unicode_task(self):
797 Task(self.tw, description="test task").save()
800 class ReadOnlyDictViewTest(unittest.TestCase):
803 self.sample = dict(l=[1,2,3], d={'k':'v'})
804 self.original_sample = copy.deepcopy(self.sample)
805 self.view = ReadOnlyDictView(self.sample)
807 def test_readonlydictview_getitem(self):
809 self.assertEqual(l, self.sample['l'])
811 # Assert that modification changed only copied value
813 self.assertNotEqual(l, self.sample['l'])
815 # Assert that viewed dict is not changed
816 self.assertEqual(self.sample, self.original_sample)
818 def test_readonlydictview_contains(self):
819 self.assertEqual('l' in self.view, 'l' in self.sample)
820 self.assertEqual('d' in self.view, 'd' in self.sample)
821 self.assertEqual('k' in self.view, 'k' in self.sample)
823 # Assert that viewed dict is not changed
824 self.assertEqual(self.sample, self.original_sample)
826 def test_readonlydictview_iter(self):
827 self.assertEqual(list(k for k in self.view),
828 list(k for k in self.sample))
830 # Assert the view is correct after modification
831 self.sample['new'] = 'value'
832 self.assertEqual(list(k for k in self.view),
833 list(k for k in self.sample))
835 def test_readonlydictview_len(self):
836 self.assertEqual(len(self.view), len(self.sample))
838 # Assert the view is correct after modification
839 self.sample['new'] = 'value'
840 self.assertEqual(len(self.view), len(self.sample))
842 def test_readonlydictview_get(self):
843 l = self.view.get('l')
844 self.assertEqual(l, self.sample.get('l'))
846 # Assert that modification changed only copied value
848 self.assertNotEqual(l, self.sample.get('l'))
850 # Assert that viewed dict is not changed
851 self.assertEqual(self.sample, self.original_sample)
853 def test_readonlydict_items(self):
854 view_items = self.view.items()
855 sample_items = list(self.sample.items())
856 self.assertEqual(view_items, sample_items)
858 view_items.append('newkey')
859 self.assertNotEqual(view_items, sample_items)
860 self.assertEqual(self.sample, self.original_sample)
862 def test_readonlydict_values(self):
863 view_values = self.view.values()
864 sample_values = list(self.sample.values())
865 self.assertEqual(view_values, sample_values)
867 view_list_item = list(filter(lambda x: type(x) is list,
869 view_list_item.append(4)
870 self.assertNotEqual(view_values, sample_values)
871 self.assertEqual(self.sample, self.original_sample)