Ejemplo n.º 1
0
 def generate_items():  # <- Generator that reads from single iterator.
     shared = iter([
         'x',
         1,
         1,
         1,
         2,
         2,
         2,
         3,
         3,
         3,
         'y',
         4,
         4,
         4,
         5,
         5,
         5,
         6,
         6,
         6,
     ])
     yield next(shared), Result(islice(shared, 9), evaltype=list)
     yield next(shared), Result(islice(shared, 9), evaltype=list)
Ejemplo n.º 2
0
    def test_execute_other_source(self):
        query = Query.from_object([1, 3, 4, 2])
        result = query.execute()
        self.assertIsInstance(result, Result)
        self.assertEqual(result.fetch(), [1, 3, 4, 2])

        query = Query.from_object(iter([1, 3, 4, 2]))
        result = query.execute()
        self.assertIsInstance(result, Result)
        self.assertEqual(result.fetch(), [1, 3, 4, 2])

        query = Query.from_object(Result([1, 3, 4, 2], evaltype=list))
        result = query.execute()
        self.assertIsInstance(result, Result)
        self.assertEqual(result.fetch(), [1, 3, 4, 2])

        query = Query.from_object(Result({'a': 1, 'b': 2}, evaltype=dict))
        result = query.execute()
        self.assertIsInstance(result, Result)
        self.assertEqual(result.fetch(), {'a': 1, 'b': 2})

        query = Query.from_object(IterItems(iter([iter(['a', 1]), iter(['b', 2])])))
        result = query.execute()
        self.assertIsInstance(result, Result)
        self.assertEqual(result.fetch(), {'a': 1, 'b': 2})
Ejemplo n.º 3
0
    def test_nonmapping_iters(self):
        """Non-mapping iterables should be returned unchanged."""
        iterable = Result([-4, -1, 2, 3], list)
        result = _flatten_data(iterable)
        self.assertEqual(result.fetch(), [-4, -1, 2, 3])

        iterable = Result(set([1, 2, 3]), set)
        result = _flatten_data(iterable)
        self.assertEqual(result.fetch(), set([1, 2, 3]))
Ejemplo n.º 4
0
    def test_nonmappings(self):
        """Check collection types (i.e., sized, iterable containers)."""
        result = Result([1, 2, 3], list)
        self.assertEqual(result.fetch(), [1, 2, 3])

        result = Result([1, 2, 3], set)
        self.assertEqual(result.fetch(), set([1, 2, 3]))

        result = Result(iter([1, 2, 3]), set)
        self.assertEqual(result.fetch(), set([1, 2, 3]))
Ejemplo n.º 5
0
    def test_dataiter_dict_of_containers(self):
        iterable = Result({'a': [1, 2, 1, 2], 'b': [3, 4, 3]}, dict)
        result = _sqlite_distinct(iterable)
        self.assertIsInstance(result, Result)
        self.assertEqual(result.evaltype, dict)
        self.assertEqual(result.fetch(), {'a': [1, 2], 'b': [3, 4]})

        # Check tuple handling.
        iterable = Result({'a': [(1, 2), (1, 2)], 'b': (3, 4, 3)}, dict)
        result = _sqlite_distinct(iterable)
        self.assertIsInstance(result, Result)
        self.assertEqual(result.evaltype, dict)
        self.assertEqual(result.fetch(), {'a': [(1, 2)], 'b': (3, 4, 3)})
Ejemplo n.º 6
0
    def test_mappings(self):
        result = Result({'a': 1, 'b': 2}, dict)
        self.assertEqual(result.fetch(), {'a': 1, 'b': 2})

        result = Result(IterItems([('a', 1), ('b', 2)]), dict)
        self.assertEqual(result.fetch(), {'a': 1, 'b': 2})

        result = Result(iter([iter(['a', 1]), iter(['b', 2])]), dict)
        self.assertEqual(result.fetch(), {'a': 1, 'b': 2})

        with self.assertRaises(ValueError):
            result = Result([('a', 1), 'b'], dict)
            result.fetch()  # <- Fails late (on fetch, only)
Ejemplo n.º 7
0
    def test_multiple_item_containers(self):
        """Containers with multiple items should be returned unchanged."""
        result = _unwrap_data([-4, -1, 2, 3])
        self.assertEqual(result, [-4, -1, 2, 3])

        result = _unwrap_data(set([1, 2, 3]))
        self.assertEqual(result, set([1, 2, 3]))

        iterable = Result([-4, -1, 2, 3], list)
        result = _unwrap_data(iterable)
        self.assertEqual(result.fetch(), [-4, -1, 2, 3])

        iterable = Result(set([1, 2, 3]), set)
        result = _unwrap_data(iterable)
        self.assertEqual(result.fetch(), set([1, 2, 3]))
Ejemplo n.º 8
0
 def test_mapping_of_values(self):
     """A small integration test of mixed types and containers."""
     iterable = Result(
         {
             'a': 1,
             'b': [2],
             'c': [3, 4],
             'd': set([5]),
             'e': set([6, 7]),
             'f': 'abc',
             'g': ('def',),
             'h': ('ghi', 'jkl'),
             'i': {'x': 8},
         },
         dict,
     )
     result = _unwrap_data(iterable)
     expected = {
         'a': 1,
         'b': 2,      # <- unwrapped
         'c': [3, 4],
         'd': 5,      # <- unwrapped
         'e': set([6, 7]),
         'f': 'abc',
         'g': 'def',  # <- unwrapped
         'h': ('ghi', 'jkl'),
         'i': {'x': 8},
     }
     self.assertEqual(result.fetch(), expected)
Ejemplo n.º 9
0
    def test_dataiter_dict_of_ints(self):
        iterable = Result({'a': 2, 'b': 3}, dict)
        result = _sqlite_distinct(iterable)

        self.assertIsInstance(result, Result)
        self.assertEqual(result.evaltype, dict)
        self.assertEqual(result.fetch(), {'a': 2, 'b': 3})
Ejemplo n.º 10
0
    def test_dict_iter_of_lists(self):
        iterable = Result({'a': [1, None], 'b': ['x', None, 0]}, dict)
        result = _apply_to_data(_sqlite_count, iterable)

        self.assertIsInstance(result, Result)
        self.assertEqual(result.evaltype, dict)
        self.assertEqual(result.fetch(), {'a': 1, 'b': 2})
Ejemplo n.º 11
0
    def test_dict_iter_of_integers(self):
        iterable = Result({'a': 2, 'b': 3, 'c': None}, dict)
        result = _apply_to_data(_sqlite_max, iterable)

        self.assertIsInstance(result, Result)
        self.assertEqual(result.evaltype, dict)
        self.assertEqual(result.fetch(), {'a': 2, 'b': 3, 'c': None})
Ejemplo n.º 12
0
    def test_dict_iter_of_lists(self):
        iterable = Result({'a': [1, 2], 'b': [3, 4]}, dict)
        result = _apply_to_data(_sqlite_sum, iterable)

        self.assertIsInstance(result, Result)
        self.assertEqual(result.evaltype, dict)
        self.assertEqual(result.fetch(), {'a': 3, 'b': 7})
Ejemplo n.º 13
0
    def test_shared_iterator(self):
        """Dict result should not assume independent source iterators."""
        def generate_items():  # <- Generator that reads from single iterator.
            shared = iter([
                'x',
                1,
                1,
                1,
                2,
                2,
                2,
                3,
                3,
                3,
                'y',
                4,
                4,
                4,
                5,
                5,
                5,
                6,
                6,
                6,
            ])
            yield next(shared), Result(islice(shared, 9), evaltype=list)
            yield next(shared), Result(islice(shared, 9), evaltype=list)

        result = Result(generate_items(), evaltype=dict)

        expected = {
            'x': [1, 1, 1, 2, 2, 2, 3, 3, 3],
            'y': [4, 4, 4, 5, 5, 5, 6, 6, 6],
        }
        self.assertEqual(result.fetch(), expected)
Ejemplo n.º 14
0
    def test_single_item_containers(self):
        """Single item sequences and sets should be unwrapped."""
        result = _unwrap_data([1])
        self.assertEqual(result, 1)

        result = _unwrap_data(set(['abc']))
        self.assertEqual(result, 'abc')

        # Test result objects.
        iterable = Result([1], list)
        result = _unwrap_data(iterable)
        self.assertEqual(result, 1)

        iterable = Result(set(['abc']), set)
        result = _unwrap_data(iterable)
        self.assertEqual(result, 'abc')
Ejemplo n.º 15
0
    def test_dict_iter_of_integers(self):
        iterable = Result({'a': -5, 'b': None, 'c': 'xyz'}, dict)
        result = _apply_to_data(_sqlite_count, iterable)

        self.assertIsInstance(result, Result)
        self.assertEqual(result.evaltype, dict)
        self.assertEqual(result.fetch(), {'a': 1, 'b': 0, 'c': 1})
Ejemplo n.º 16
0
    def test_delete(self):
        """"Should call close() when object is garbage collected."""
        result = Result(iter([1, 2, 3]), set, closefunc=self.closefunc)
        self.assertEqual(self.log, [], msg='verify log is empty')

        result.__del__()  # Call __del__() directly.
        self.assertEqual(self.log, ['closed'])
Ejemplo n.º 17
0
    def test_stopiteration(self):
        """"Should call close() method when iterable is exhausted."""
        result = Result(iter([1, 2, 3]), set, closefunc=self.closefunc)
        self.assertEqual(self.log, [], msg='verify log is empty')

        list(result)  # Exhaust iterable.
        self.assertEqual(self.log, ['closed'])
Ejemplo n.º 18
0
    def test_empty_containers(self):
        """Single item sequences and sets should be unwrapped."""
        result = _unwrap_data([])
        self.assertEqual(result, [])

        iterable = Result(set(), set)
        result = _unwrap_data(iterable)
        self.assertEqual(result.fetch(), set())
Ejemplo n.º 19
0
    def test_dataiter_dict_of_ints(self):
        iterable = Result({'a': 2, 'b': 3}, dict)

        function = lambda x: x * 2
        result = _apply_data(function, iterable)

        self.assertIsInstance(result, Result)
        self.assertEqual(result.evaltype, dict)
        self.assertEqual(result.fetch(), {'a': 4, 'b': 6})
Ejemplo n.º 20
0
    def test_dataiter_dict_of_mixed_iterables(self):
        iterable = Result({'a': iter([1, 2]), 'b': (3, 4)}, dict)

        function = lambda itr: [x * 2 for x in itr]
        result = _apply_data(function, iterable)

        self.assertIsInstance(result, Result)
        self.assertEqual(result.evaltype, dict)
        self.assertEqual(result.fetch(), {'a': [2, 4], 'b': [6, 8]})
Ejemplo n.º 21
0
    def test_initializer(self):
        iterable = Result(['a', 'a', 'b', 'c', 'c', 'c'], list)

        def simplecount(acc, upd):
            acc[upd] = acc.get(upd, 0) + 1
            return acc

        result = _reduce_data(simplecount, iterable, initializer_factory=dict)
        self.assertEqual(result, {'a': 2, 'b': 1, 'c': 3})
Ejemplo n.º 22
0
    def test_dict_iter_of_integers(self):
        iterable = Result({'a': 2, 'b': 3}, dict)

        function = lambda x, y: x + y
        result = _reduce_data(function, iterable)

        self.assertIsInstance(result, Result)
        self.assertEqual(result.evaltype, dict)
        self.assertEqual(result.fetch(), {'a': 2, 'b': 3})
Ejemplo n.º 23
0
    def test_dataiter_dict_of_tuples(self):
        data = Result({'a': (1, 2), 'b': (3, 4)}, dict)

        function = lambda x, y: x + y
        result = _starmap_data(function, data)

        self.assertIsInstance(result, Result)
        self.assertEqual(result.evaltype, dict)
        self.assertEqual(result.fetch(), {'a': 3, 'b': 7})
Ejemplo n.º 24
0
    def test_dict_iter_of_lists(self):
        iterable = Result({'a': [1, 3], 'b': [4, 5, 6]}, dict)

        iseven = lambda x: x % 2 == 0
        result = _filter_data(iseven, iterable)

        self.assertIsInstance(result, Result)
        self.assertEqual(result.evaltype, dict)
        self.assertEqual(result.fetch(), {'a': [], 'b': [4, 6]})
Ejemplo n.º 25
0
    def test_dataiter_dict_of_noniters(self):
        data = Result({'a': 2, 'b': 3}, dict)

        function = lambda x: x * 2
        result = _starmap_data(function, data)

        self.assertIsInstance(result, Result)
        self.assertEqual(result.evaltype, dict)
        self.assertEqual(result.fetch(), {'a': 4, 'b': 6})
Ejemplo n.º 26
0
    def test_dataiter_dict_of_lists(self):
        data = Result({'a': [1, 2], 'b': [3, 4]}, dict)

        function = lambda x: x + 1
        result = _starmap_data(function, data)

        self.assertIsInstance(result, Result)
        self.assertEqual(result.evaltype, dict)
        self.assertEqual(result.fetch(), {'a': [2, 3], 'b': [4, 5]})
Ejemplo n.º 27
0
    def test_settype_to_list(self):
        data = Result([(1, 1), (2, 2)], set)  # <- Starts as 'set'.

        function = lambda x, y: x - y
        result = _starmap_data(function, data)

        self.assertIsInstance(result, Result)
        self.assertEqual(result.evaltype, list) # <- Now a 'list'.
        self.assertEqual(result.fetch(), [0, 0])
Ejemplo n.º 28
0
    def test_dict_iter_of_lists(self):
        iterable = Result({'a': [1, 2], 'b': [3, 4]}, dict)

        function = lambda x, y: x + y
        result = _reduce_data(function, iterable)

        self.assertIsInstance(result, Result)
        self.assertEqual(result.evaltype, dict)
        self.assertEqual(result.fetch(), {'a': 3, 'b': 7})
Ejemplo n.º 29
0
    def test_iter_of_noniters(self):
        data = Result([1, 2], list)

        function = lambda x: x + 1
        result = _starmap_data(function, data)

        self.assertIsInstance(result, Result)
        self.assertEqual(result.evaltype, list)
        self.assertEqual(result.fetch(), [2, 3])
Ejemplo n.º 30
0
    def test_dataiter_dict_of_lists_of_tuples(self):
        data = Result({'a': [(1, 2), (1, 2)], 'b': [(3, 4), (3, 4)]}, dict)

        function = lambda x, y: x + y
        result = _starmap_data(function, data)

        self.assertIsInstance(result, Result)
        self.assertEqual(result.evaltype, dict)
        self.assertEqual(result.fetch(), {'a': [3, 3], 'b': [7, 7]})