def test_pop_does_nothing_and_returns_the_provided_default_value_when_provided_key_doesnt_exist(self): d = ImageDict() obj1 = cv2.imread(path.join(_this_path, 'data', 'object.png')) default_value = 1 popped_value = d.pop(obj1, default_value) #pop a non-existent key self.assertEqual(popped_value, default_value) self.assertEqual(len(d), 0) #should still be empty
def test_a_copy_has_same_keypackages_as_original(self): d = ImageDict() obj = cv2.imread(path.join(_this_path, 'data', 'object.png')) value = 1 d[obj] = value d_copy = d.copy() self.assertEqual(d._keypackages, d_copy._keypackages)
def test_ImageDict_returns_true_if_key_exists(self): d = ImageDict() obj = cv2.imread(path.join(_this_path, 'data', 'object.png')) mask = cv2.imread(path.join(_this_path, 'data', 'object_mask.png')) d[obj, mask] = 1 self.assertTrue(d.__contains__((obj, mask))) self.assertTrue((obj, mask) in d) self.assertTrue(d.has_key((obj, mask)))
def test_setdefault_with_an_existing_key_returns_the_existing_keys_value(self): d = ImageDict() obj = cv2.imread(path.join(_this_path, 'data', 'object.png')) original_value = 1 new_value = 2 d[obj] = original_value d.setdefault(obj, new_value) self.assertEqual(d._keypackages[0].value, original_value)
def test_pop_removes_the_provided_key_when_it_exists_and_returns_its_value(self): d = ImageDict() obj1 = cv2.imread(path.join(_this_path, 'data', 'object.png')) value_1 = 1 d[obj1] = value_1 popped_value = d.pop(obj1) self.assertEqual(popped_value, value_1) self.assertEqual(len(d), 0) #should be empty after pop
def test_popitem_removes_some_item_and_returns_it_as_a_key_value_pair(self): d = ImageDict() obj1 = cv2.imread(path.join(_this_path, 'data', 'object.png')) value_1 = 1 d[obj1] = value_1 item_specification = ((obj1, None), value_1) popped_item = d.popitem() self.assertEqual(popped_item, item_specification) self.assertEqual(len(d), 0) #item should have been removed
def test_lookup_with_get_method_has_optional_default(self): d = ImageDict() obj1 = cv2.imread(path.join(_this_path, 'data', 'object.png')) obj2 = cv2.imread(path.join(_this_path, 'data', 'different_object.png')) lookup = cv2.imread(path.join(_this_path, 'data', 'lookup.png')) some_value = 1 d[obj1] = some_value #use the same image in a lookup self.assertEqual(d.get(lookup), some_value) self.assertEqual(d.get(obj2, None), None)
def test_user_gets_a_confirmation_image_with_value(self): d = ImageDict() obj = cv2.imread(path.join(_this_path, 'data', 'object.png')) lookup = cv2.imread(path.join(_this_path, 'data', 'lookup.png')) value_spec = 1 d[obj] = value_spec value, confirmation_image = d.get(lookup, return_confirmation = True) self.assertEqual(value, value_spec) self.assertTrue(hasattr(confirmation_image, 'shape'), 'Looks like the confirmation image is not a cv numpy image.') self.assertTrue(hasattr(confirmation_image, 'dtype'), 'Looks like the confirmation image is not a cv numpy image.')
def test_ImageDict_provides_values_of_each_key_value_pair(self): d = ImageDict() obj1 = cv2.imread(path.join(_this_path, 'data', 'object.png')) obj2 = cv2.imread(path.join(_this_path, 'data', 'different_object.png')) d[obj1] = 1 d[obj2] = 2 values_specification = (1, 2) values_by_itervalues = d.itervalues() values_by_values = d.values() self.assertItemsEqual(list(values_by_itervalues), values_specification) self.assertItemsEqual(values_by_values, values_specification)
def test_arg_with_keys_sets_each_arg_item(self): obj1 = cv2.imread(path.join(_this_path, 'data', 'object.png')) obj2 = cv2.imread(path.join(_this_path, 'data', 'different_object.png')) value_1 = 1 value_2 = 2 d_content = ImageDict() d_content[obj1] = value_1 d_content[obj2] = value_2 #create and try to update a blank ImageDict d = ImageDict() d.update(d_content) image_mask_values = ((kp.image, kp.mask, kp.value) for kp in d._keypackages) image_mask_values_spec = ((obj1, None, value_1), (obj2, None, value_2)) self.assertItemsEqual(image_mask_values, image_mask_values_spec)
def test_iterating_over_an_ImageDict_provides_a_generator_of_keys_as_image_mask_pairs(self): d = ImageDict() obj1 = cv2.imread(path.join(_this_path, 'data', 'object.png')) mask1 = cv2.imread(path.join(_this_path, 'data', 'object_mask.png')) obj2 = cv2.imread(path.join(_this_path, 'data', 'different_object.png')) d[obj1, mask1] = 1 d[obj2] = 2 keys_specification = [(obj1, mask1), (obj2, None)] keys_by_iter = (key for key in d) keys_by_iterkeys = d.iterkeys() keys_by_keys = d.keys() self.assertItemsEqual(list(keys_by_iter), keys_specification) self.assertItemsEqual(list(keys_by_iterkeys), keys_specification) self.assertItemsEqual(keys_by_keys, keys_specification)
def test_ImageDict_provides_key_value_pairs(self): d = ImageDict() obj1 = cv2.imread(path.join(_this_path, 'data', 'object.png')) mask1 = cv2.imread(path.join(_this_path, 'data', 'object_mask.png')) obj2 = cv2.imread(path.join(_this_path, 'data', 'different_object.png')) value_1 = 1 value_2 = 2 d[obj1, mask1] = value_1 d[obj2] = value_2 key1_spec = (obj1, mask1) key2_spec = (obj2, None) items_specification = ((key1_spec, value_1), (key2_spec, value_2)) items_by_iteritems = d.iteritems() items_by_items = d.items() self.assertItemsEqual(list(items_by_iteritems), items_specification) self.assertItemsEqual(items_by_items, items_specification)
def test_arg_without_keys_sets_sequence_of_key_value_pairs(self): obj1 = cv2.imread(path.join(_this_path, 'data', 'object.png')) obj2 = cv2.imread(path.join(_this_path, 'data', 'different_object.png')) value_1 = 1 value_2 = 2 key_value_pairs = ((obj1, value_1), (obj2, value_2)) #create and try to update a blank ImageDict d = ImageDict() d.update(key_value_pairs) items = [[kp.image, kp.mask, kp.value] for kp in d._keypackages] items_spec = ((obj1, None, value_1), (obj2, None, value_2)) #below works like assertSequenceEqual but gets around numpy's demand for unambiguous equality operator #wish I could use ItemsEqual but I believe it has a problem even though it is supposed to have a non-hashable code path for (image_actual, mask_actual, value_actual), (image_spec, mask_spec, value_spec) in zip(items, items_spec): self.assertTrue(np.all(image_actual == image_spec)) self.assertTrue(np.all(mask_actual == mask_spec)) self.assertEqual(value_actual, value_spec)
def test_kwarg_sets_as_with_an_arg_with_keys(self): obj1 = cv2.imread(path.join(_this_path, 'data', 'object.png')) obj2 = cv2.imread(path.join(_this_path, 'data', 'different_object.png')) value_1 = 1 value_2 = 2 d_content = ImageDict() d_content[obj1] = value_1 d_content[obj2] = value_2 #create and try to update a blank ImageDict d = ImageDict() d.update(**d_content) #todo: this doesn't work. maybe can work around hash requirement? items = ((kp.image, kp.mask, kp.value) for kp in d._keypackages) items_spec = ((obj1, None, value_1), (obj2, None, value_2)) #below works like assertSequenceEqual but gets around numpy's demand for unambiguous equality operator #wish I could use ItemsEqual but I believe it has a problem even though it is supposed to have a non-hashable code path for (image_actual, mask_actual, value_actual), (image_spec, mask_spec, value_spec) in zip(items, items_spec): self.assertTrue(np.all(image_actual == image_spec)) self.assertTrue(np.all(mask_actual == mask_spec)) self.assertEqual(value_actual, value_spec)
def test_copy_is_a_new_object(self): d = ImageDict() d_copy = d.copy() self.assertIsNot(d_copy, d)
def test_opencv_numpy_images_are_on_the_whitelist_of_types(self): color_image = cv2.imread(path.join(_this_path, 'data', 'object.png')) gray_image = cv2.cvtColor(color_image, cv2.COLOR_BGR2GRAY) d = ImageDict() self.assertTrue(d._is_on_whitelist(color_image)) self.assertTrue(d._is_on_whitelist(gray_image))
def test_setdefault_with_a_key_that_doesnt_exist_sets_the_new_item_and_returns_the_new_value(self): d = ImageDict() obj = cv2.imread(path.join(_this_path, 'data', 'object.png')) new_value = 2 d.setdefault(obj, new_value) self.assertEqual(d._keypackages[0].value, new_value)
def test_ImageDict_removes_all_items(self): d = ImageDict() obj = cv2.imread(path.join(_this_path, 'data', 'object.png')) d[obj] = 1 d.clear() self.assertEqual(len(d), 0)