File: test_lookups.py

package info (click to toggle)
python-django-postgres-extra 2.0.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,096 kB
  • sloc: python: 9,057; makefile: 17; sh: 7; sql: 1
file content (102 lines) | stat: -rw-r--r-- 2,611 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
from django.db import models

from .fake_model import get_fake_model


def test_invalues_lookup_text_field():
    model = get_fake_model({"name": models.TextField()})
    [a, b] = model.objects.bulk_create(
        [
            model(name="a"),
            model(name="b"),
        ]
    )

    results = list(model.objects.filter(name__invalues=[a.name, b.name, "c"]))
    assert results == [a, b]


def test_invalues_lookup_integer_field():
    model = get_fake_model({"number": models.IntegerField()})
    [a, b] = model.objects.bulk_create(
        [
            model(number=1),
            model(number=2),
        ]
    )

    results = list(
        model.objects.filter(number__invalues=[a.number, b.number, 3])
    )
    assert results == [a, b]


def test_invalues_lookup_uuid_field():
    model = get_fake_model({"value": models.UUIDField()})
    [a, b] = model.objects.bulk_create(
        [
            model(value="f8fe0431-29f8-4c4c-839c-8a6bf29f95d5"),
            model(value="2fb0f45b-afaf-4e24-8637-2d81ded997bb"),
        ]
    )

    results = list(
        model.objects.filter(
            value__invalues=[
                a.value,
                b.value,
                "d7a8df83-f3f8-487b-b982-547c8f22b0bb",
            ]
        )
    )
    assert results == [a, b]


def test_invalues_lookup_related_field():
    model_1 = get_fake_model({"name": models.TextField()})
    model_2 = get_fake_model(
        {"relation": models.ForeignKey(model_1, on_delete=models.CASCADE)}
    )

    [a_relation, b_relation] = model_1.objects.bulk_create(
        [
            model_1(name="a"),
            model_1(name="b"),
        ]
    )

    [a, b] = model_2.objects.bulk_create(
        [model_2(relation=a_relation), model_2(relation=b_relation)]
    )

    results = list(
        model_2.objects.filter(relation__invalues=[a_relation, b_relation])
    )
    assert results == [a, b]


def test_invalues_lookup_related_field_subquery():
    model_1 = get_fake_model({"name": models.TextField()})
    model_2 = get_fake_model(
        {"relation": models.ForeignKey(model_1, on_delete=models.CASCADE)}
    )

    [a_relation, b_relation] = model_1.objects.bulk_create(
        [
            model_1(name="a"),
            model_1(name="b"),
        ]
    )

    [a, b] = model_2.objects.bulk_create(
        [model_2(relation=a_relation), model_2(relation=b_relation)]
    )

    results = list(
        model_2.objects.filter(
            relation__invalues=model_1.objects.all().values_list(
                "id", flat=True
            )
        )
    )
    assert results == [a, b]