Example #1
0
    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)
Example #2
0
    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')]})
Example #3
0
    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'})
Example #4
0
 def test_requirement(self):
     result = Result(DictItems([('a', 1), ('b', 2)]), evaluation_type=dict)
     normalized = _normalize_lazy(result)
     self.assertIsInstance(normalized, IterItems)
Example #5
0
 def test_dictitems(self):
     items_iter = DictItems([('a', 1), ('b', 2)])
     self.assertTrue(_is_collection_of_items(items_iter))
Example #6
0
 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)])
Example #7
0
 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)])
Example #8
0
 def test_empty_iterable(self):
     items = DictItems(iter([]))
     self.assertEqual(list(items), [])
Example #9
0
 def test_dict(self):
     items = DictItems({'a': 1, 'b': 2})
     self.assertEqual(set(items), set([('a', 1), ('b', 2)]))
Example #10
0
 def test_iter_of_items(self):
     items = DictItems(iter([('a', 1), ('b', 2)]))
     self.assertEqual(list(items), [('a', 1), ('b', 2)])