def test_invalid_input(self): source = ['x', 1, 'y', 2] with self.assertRaises(TypeError): normalized = DictItems(source) source = [{'x': 1}, {'y': 2}] with self.assertRaises(TypeError): normalized = DictItems(source)
def test_dictitems_data(self): """"When *data* is an exhaustible iterator of dict-items and *requirement* is a non-mapping. """ items = DictItems(iter([('a', 'x'), ('b', 'y')])) x_or_y = lambda value: value == 'x' or value == 'y' result = _get_invalid_info(items, x_or_y) self.assertIsNone(result) items = DictItems(iter([('a', 'x'), ('b', 'y')])) msg, diffs = _get_invalid_info(items, 'x') # <- string self.assertTrue(exhaustible(diffs)) self.assertEqual(dict(diffs), {'b': Invalid('y')}) items = DictItems(iter([('a', 'x'), ('b', 'y')])) msg, diffs = _get_invalid_info(items, set('x')) # <- set self.assertTrue(exhaustible(diffs)) self.assertEqual(dict(diffs), {'b': [Missing('x'), Extra('y')]})
def test_normalize_requirement(self): requirement = [1, 2, 3] self.assertIs(_normalize_requirement(requirement), requirement, msg='should return original object') with self.assertRaises(TypeError, msg='cannot use generic iter'): _normalize_requirement(iter([1, 2, 3])) result_obj = Result(iter([1, 2, 3]), evaluation_type=tuple) output = _normalize_requirement(result_obj) self.assertIsInstance(output, tuple) self.assertEqual(output, (1, 2, 3)) items = DictItems(iter([(0, 'x'), (1, 'y'), (2, 'z')])) output = _normalize_requirement(items) self.assertIsInstance(output, dict) self.assertEqual(output, {0: 'x', 1: 'y', 2: 'z'})
def test_requirement(self): result = Result(DictItems([('a', 1), ('b', 2)]), evaluation_type=dict) normalized = _normalize_lazy(result) self.assertIsInstance(normalized, IterItems)
def test_dictitems(self): items_iter = DictItems([('a', 1), ('b', 2)]) self.assertTrue(_is_collection_of_items(items_iter))
def test_Query(self): source = Selector([('A', 'B'), ('x', 1), ('y', 2)]) query = source({'A': 'B'}).apply(lambda x: next(x)) normalized = DictItems(query) self.assertEqual(list(normalized), [('x', 1), ('y', 2)])
def test_Result(self): result = Result(DictItems([('a', 1), ('b', 2)]), evaluation_type=dict) normalized = DictItems(result) self.assertEqual(list(normalized), [('a', 1), ('b', 2)])
def test_empty_iterable(self): items = DictItems(iter([])) self.assertEqual(list(items), [])
def test_dict(self): items = DictItems({'a': 1, 'b': 2}) self.assertEqual(set(items), set([('a', 1), ('b', 2)]))
def test_iter_of_items(self): items = DictItems(iter([('a', 1), ('b', 2)])) self.assertEqual(list(items), [('a', 1), ('b', 2)])