Ejemplo n.º 1
0
    def test_flat_attribute(self):
        input_items = [Asset(id=i, sid=i) for i in range(10)]
        output_items = [Asset(id=i, sid=i, tid=i + i) for i in range(10)]

        assert (list(
            ObjectQuery(input_items).annotate(
                tid=lambda obj: obj.id + obj.sid)) == output_items)
Ejemplo n.º 2
0
    def test_ascending(self):
        input_items = [Asset(id=i) for i in range(10)]
        output_items = [Asset(id=i) for i in range(10)]

        shuffle(input_items)

        assert list(ObjectQuery(input_items).order_by("id")) == output_items
Ejemplo n.º 3
0
    def test_no_lookup(self):
        items = [
            Asset(id=0),
            Asset(id=1),
            Asset(id=0),
        ]

        assert list(ObjectQuery(items).filter()) == items
Ejemplo n.º 4
0
    def test_nested(self):
        input_items = [Asset(nested=Asset(id=i)) for i in range(10)]
        output_items = [Asset(nested=Asset(id=i)) for i in range(10)]

        shuffle(input_items)

        assert list(
            ObjectQuery(input_items).order_by("nested__id")) == output_items
Ejemplo n.º 5
0
    def test_multiple_fields(self):
        input_items = [Asset(id=0, sid=i) for i in range(10)]
        output_items = [Asset(id=0, sid=i) for i in range(10)]

        shuffle(input_items)

        assert list(ObjectQuery(input_items).order_by("id",
                                                      "sid")) == output_items
Ejemplo n.º 6
0
    def test_len(self):
        listt = [1]
        list_iter = iter(listt)
        query = ObjectQuery(list_iter)

        assert len(query) == len(listt)

        with pytest.raises(StopIteration):
            next(list_iter)
Ejemplo n.º 7
0
    def test_not_suppported_attribute(self):
        items = [
            Asset(id=0),
            Asset(id=1),
            Asset(id=0),
        ]

        with pytest.raises(AttributeError):
            list(ObjectQuery(items).exclude(not_suppported_attribute=0))
Ejemplo n.º 8
0
    def test_in(self):
        items = [
            Asset(id=0),
            Asset(id=1),
            Asset(id=2),
        ]

        assert list(ObjectQuery(items).exclude(id__in=[1, 2])) == [
            items[0],
        ]
Ejemplo n.º 9
0
    def test_gte(self):
        items = [
            Asset(id=0),
            Asset(id=1),
            Asset(id=2),
        ]

        assert list(ObjectQuery(items).exclude(id__gte=1)) == [
            items[0],
        ]
Ejemplo n.º 10
0
    def test_exact(self):
        items = [
            Asset(id=0),
            Asset(id=1),
            Asset(id=0),
        ]

        assert list(ObjectQuery(items).exclude(id__exact=0)) == [
            items[1],
        ]
Ejemplo n.º 11
0
    def test_implicit_eq(self):
        items = [
            Asset(id=0),
            Asset(id=1),
            Asset(id=0),
        ]

        assert list(ObjectQuery(items).exclude(id=0)) == [
            items[1],
        ]
Ejemplo n.º 12
0
    def test_contains(self):
        items = [
            Asset(id=0, seq=[0]),
            Asset(id=1, seq=tuple(range(8))),
            Asset(id=2, seq=[0]),
        ]

        assert list(ObjectQuery(items).filter(seq__contains=3)) == [
            items[1],
        ]
Ejemplo n.º 13
0
    def test_gt(self):
        items = [
            Asset(id=0),
            Asset(id=1),
            Asset(id=2),
        ]

        assert list(ObjectQuery(items).filter(id__gt=1)) == [
            items[2],
        ]
Ejemplo n.º 14
0
    def test_combining_lookups(self):
        items = [
            Asset(id=0, seq=[0]),
            Asset(id=1, seq=(0, 0, 0xFF, 0, 0, 0, 0, 0)),
            Asset(id=1, seq=[0]),
        ]

        assert list(ObjectQuery(items).exclude(id=1, seq__contains=0xFF)) == [
            items[0],
        ]
Ejemplo n.º 15
0
    def test_in(self):
        items = [
            Asset(id=0),
            Asset(id=1),
            Asset(id=2),
        ]

        assert list(ObjectQuery(items).filter(id__in=[1, 2])) == [
            items[1],
            items[2],
        ]
Ejemplo n.º 16
0
    def test_exact(self):
        items = [
            Asset(id=0),
            Asset(id=1),
            Asset(id=0),
        ]

        assert list(ObjectQuery(items).filter(id__exact=0)) == [
            items[0],
            items[2],
        ]
Ejemplo n.º 17
0
    def test_implicit_eq(self):
        items = [
            Asset(id=0),
            Asset(id=1),
            Asset(id=0),
        ]

        assert list(ObjectQuery(items).filter(id=0)) == [
            items[0],
            items[2],
        ]
Ejemplo n.º 18
0
    def test_contains(self):
        items = [
            Asset(id=0, seq=[0]),
            Asset(id=1, seq=tuple(range(8))),
            Asset(id=1, seq=[0]),
        ]

        assert list(ObjectQuery(items).exclude(seq__contains=3)) == [
            items[0],
            items[2],
        ]
Ejemplo n.º 19
0
    def test_lt(self):
        items = [
            Asset(id=0),
            Asset(id=1),
            Asset(id=2),
        ]

        assert list(ObjectQuery(items).exclude(id__lt=1)) == [
            items[1],
            items[2],
        ]
Ejemplo n.º 20
0
    def test_nested_attribute(self):
        input_items = [
            Asset(id=i, sid=i, nested=Asset(tid=0)) for i in range(10)
        ]
        output_items = [
            Asset(id=i, sid=i, nested=Asset(tid=i + i)) for i in range(10)
        ]

        assert (list(
            ObjectQuery(input_items).annotate(
                nested__tid=lambda obj: obj.id + obj.sid)) == output_items)
Ejemplo n.º 21
0
    def test_lte(self):
        items = [
            Asset(id=0),
            Asset(id=1),
            Asset(id=2),
        ]

        assert list(ObjectQuery(items).filter(id__lte=1)) == [
            items[0],
            items[1],
        ]
Ejemplo n.º 22
0
    def test_union(self):
        q1 = ObjectQuery(range(10))
        q2 = ObjectQuery(range(10))

        assert list(q1.union(q2)) == list(range(10)) + list(range(10))
Ejemplo n.º 23
0
    def test_all(self):
        query1 = ObjectQuery(range(10))
        query2 = query1.all()

        assert list(query1) == list(query2)
Ejemplo n.º 24
0
    def test_slice(self):
        query = ObjectQuery(range(10))

        assert list(query[5:0:-1]) == list(range(10))[5:0:-1]
Ejemplo n.º 25
0
    def test_get_item(self):
        query = ObjectQuery(range(10))

        assert query[5] == 5
Ejemplo n.º 26
0
    def test_iteration(self):
        oq = ObjectQuery(range(10))

        for x, y in zip(oq, range(10)):
            assert x == y
Ejemplo n.º 27
0
    def test_or(self):
        q1 = ObjectQuery(range(10))
        q2 = ObjectQuery(range(10))

        assert list(q1 | q2) == list(range(10)) + list(range(10))
Ejemplo n.º 28
0
    def test_iterator(self):
        oq = ObjectQuery([])

        assert isinstance(oq, Iterator)
Ejemplo n.º 29
0
 def test_type_error(self):
     with pytest.raises(TypeError):
         ObjectQuery(object())
Ejemplo n.º 30
0
    def test_reversed(self):
        query = ObjectQuery(range(10))

        assert list(query.reverse()) == list(reversed(range(10)))