Exemple #1
0
    def test_setitem(self):
        q = IndexedList(['a', 'b'], {'a': 123}, 'a', 123, ['a', 'b', 'c'],
                        ['b', 123], ['a'], {'b': 321})
        q[0] = 2
        self.assertEqual(
            q._directory, {
                'b': SortedList([5, 7]),
                'a': SortedList([1, 4, 6]),
                str: SortedList([0, 2, 3])
            })

        self.assertTrue(q.is_consistent())

        q[2] = ['b', 'c', 'd']
        self.assertEqual(
            q._directory, {
                'b': SortedList([2, 5, 7]),
                'a': SortedList([1, 4, 6]),
                str: SortedList([0, 3])
            })
        self.assertTrue(q.is_consistent())

        q[1] = ['cd', 'efg', 12]
        self.assertEqual(
            q._directory, {
                'a': SortedList([4, 6]),
                'b': SortedList([2, 5, 7]),
                'cd': SortedList([1]),
                str: SortedList([0, 3])
            })
        self.assertTrue(q.is_consistent())
def gte_indexed_integers():
    """ IndexedList of integers with a gte filter """

    values = itertools.cycle(range(1, 8))
    data = [next(values) for _ in range(0, 20)]

    ilist = IndexedList(data)
    ilist.create_lookup(ilist.item >= 5)

    return ilist
def partially_indexed_integers():
    """ IndexedList of integers with only certain values indexed """

    values = itertools.cycle(range(1, 8))
    data = [next(values) for _ in range(0, 20)]

    ilist = IndexedList(data)
    ilist.create_lookup(ilist.item.in_(4, 5, 6))

    return ilist
def indexed_dicts():
    """ IndexedList of dicts indexed by 'a' """

    a_values = itertools.cycle([1, 2, 3])
    b_values = itertools.cycle([3, 4, 5, 6])

    data = [{"a": next(a_values), "b": next(b_values)} for _ in range(0, 10)]

    del a_values
    del b_values

    ilist = IndexedList(data)
    ilist.create_lookup(ilist.item["a"])

    return ilist
Exemple #5
0
 def test_creating_indexedlist2(self):
     q = IndexedList(['a', 'b'], {'a': 123}, 'a', 123, ['a', 'b', 'c'],
                     ['b', 123], ['a'], {'b': 321})
     self.assertEqual(
         repr(q),
         "IndexedList([['a', 'b'], {'a': 123}, 'a', 123, ['a', 'b', 'c'], ['b', 123], ['a'], {'b': 321}])"
     )
     self.assertEqual(eval(repr(q)), q)
     self.assertEqual(
         q._directory, {
             'b': SortedList([5, 7]),
             'a': SortedList([0, 1, 4, 6]),
             str: SortedList([2, 3])
         })
     self.assertTrue(q.is_consistent())
Exemple #6
0
    def test_init_with_data(self, ints):
        """ Test creating an IndexedList with data provided """

        expected = [1, 2, 3, 4]

        found = IndexedList(ints)._data

        assert expected == found, "Data in list does not match"
Exemple #7
0
    def test_delitem(self):
        q = IndexedList(['a', 'b'], {'a': 123}, 'a', 123, ['a', 'b', 'c'],
                        ['b', 123], ['a'], {'b': 321})

        del q[0]
        self.assertEqual(
            q._directory, {
                'b': SortedList([4, 6]),
                'a': SortedList([0, 3, 5]),
                str: SortedList([1, 2])
            })
        self.assertTrue(q.is_consistent())

        del q[2]
        self.assertEqual(
            q._directory, {
                'b': SortedList([3, 5]),
                'a': SortedList([0, 2, 4]),
                str: SortedList([1])
            })
        self.assertTrue(q.is_consistent())

        del q[3]
        self.assertEqual(q._directory, {
            'b': SortedList([4]),
            'a': SortedList([0, 2, 3]),
            str: SortedList([1])
        })

        self.assertTrue(q.is_consistent())

        del q[0]
        self.assertTrue(q.is_consistent())

        del q[3]
        self.assertTrue(q.is_consistent())

        del q[0]
        self.assertTrue(q.is_consistent())

        del q[1]
        self.assertTrue(q.is_consistent())

        del q[0]
        self.assertTrue(q.is_consistent())
        self.assertEqual(q, [])
Exemple #8
0
def repeating_integers():
    """ IndexedList of integers in a repeating pattern """

    values = itertools.cycle(range(1, 8))
    data = [next(values) for _ in range(0, 20)]

    ilist = IndexedList(data)

    return ilist
Exemple #9
0
def inconsistent_dicts_list():

    data = [
        {"a": 1, "b": 2},
        {"b": 3},
        {"a": 2, "b": 4},
        {"a": 3, "b": 5}
    ]

    return IndexedList(data)
Exemple #10
0
def dicts():
    """ IndexedList of dicts """

    a_values = itertools.cycle([1, 2, 3])
    b_values = itertools.cycle([3, 4, 5, 6])

    data = [{"a": next(a_values), "b": next(b_values)} for _ in range(0, 10)]

    del a_values
    del b_values

    return IndexedList(data)
Exemple #11
0
def list_with_lookups():

    ilist = IndexedList(range(95, 100))

    ilist.create_lookup(name="basic")
    ilist.create_lookup(ilist.item > 97, name="filtered")

    return ilist
def function_indexed_integers():
    """ IndexedList of integers with a function-based index """

    values = itertools.cycle(range(1, 8))
    data = [next(values) for _ in range(0, 20)]

    ilist = IndexedList(data)
    ilist.create_lookup(double(ilist.item))
    ilist.create_lookup(triple(triple(ilist.item)))

    return ilist
Exemple #13
0
    def test_insert(self):
        q = IndexedList(['a', 'b'], {'a': 123}, 'a', 123, ['a', 'b', 'c'],
                        ['b', 123], ['a'], {'b': 321})
        q.insert(2, 'b')
        self.assertEqual(
            q._directory, {
                'a': SortedList([0, 1, 5, 7]),
                'b': SortedList([6, 8]),
                str: SortedList([2, 3, 4])
            })
        self.assertTrue(q.is_consistent())

        q.insert(0, ['b', 123])
        self.assertEqual(
            q._directory, {
                'a': SortedList([1, 2, 6, 8]),
                'b': SortedList([0, 7, 9]),
                str: SortedList([3, 4, 5])
            })
        self.assertTrue(q.is_consistent())
Exemple #14
0
def test_create_lookup_before_data():
    """ Test creating a lookup before putting any data in the indexed list """

    indexed_list = IndexedList()
    indexed_list.create_lookup(name="sample")

    indexed_list.extend(range(1, 5))

    expected = [
        (1, [0]),
        (2, [1]),
        (3, [2]),
        (4, [3])
    ]

    found = [
        (key, sorted(value))
        for key, value
        in indexed_list.lookups["sample"].mapping.items()
    ]

    assert expected == found, "Data in mapping is not as expected"
Exemple #15
0
def simple_dicts_list():

    data = [{"a": i, "b": i + 1} for i in range(1, 6)]

    return IndexedList(data)
Exemple #16
0
def unique_integer_list():

    return IndexedList(range(0, 10))
Exemple #17
0
def integers_with_nones():
    """ IndexedList of integers with Nones included """

    return IndexedList([1, 2, None, 3, None, 2, 2, None])
Exemple #18
0
def unique_strings():
    """ IndexedList of strings of unique lengths """

    strings = ("z" * i for i in range(1, 101))

    return IndexedList(strings)
Exemple #19
0
def unique_integers():
    """ IndexedList of integers between 1 and 100 """

    return IndexedList(range(1, 101))
Exemple #20
0
def small_indexed_list():

    values = itertools.cycle(range(1, 4))
    data = [next(values) for _ in range(0, 15)]

    return IndexedList(data)
Exemple #21
0
 def test_creating_indexedlist1(self):
     q = IndexedList([['a', 'b'], ['a', 'd']])
     self.assertEqual(list(q._directory['a']), [0, 1])
     self.assertTrue(q.is_consistent())
Exemple #22
0
def basic_indexed_list():

    return IndexedList(range(1, 5))