This repository has been archived by the owner on Apr 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_queryset.py
225 lines (175 loc) · 7.02 KB
/
test_queryset.py
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
from plume import *
from plume.plume import QuerySet
from utils import DB_NAME, Pokemon, Trainer
import pytest
class Base:
TRAINERS = {
'Giovanni': {
'name': 'Giovanni',
'age': 42
},
'James': {
'name': 'James',
'age': 21
},
'Jessie': {
'name': 'Jessie',
'age': 17
},
}
POKEMONS = {
'Kangaskhan': {
'name': 'Kangaskhan',
'level': 29,
'trainer': 1
},
'Koffing': {
'name': 'Koffing',
'level': 9,
'trainer': 2
},
'Wobbuffet': {
'name': 'Wobbuffet',
'age': 19,
'trainer': 3
},
}
def setup_method(self):
db = Database(DB_NAME)
db.register(Trainer, Pokemon)
def add_trainer(self, names):
try:
names = names.split()
except:
pass
for name in names:
Trainer.objects.create(**self.TRAINERS[name])
def add_pokemon(self, names):
try:
names = names.split()
except:
pass
for name in names:
Pokemon.objects.create(**self.POKEMONS[name])
class TestQuerySetAPI(Base):
def test_output_queryset_as_string(self):
result = str(Trainer.objects.filter(Trainer.age > 18, Trainer.name != 'Giovanni'))
expected = "(SELECT * FROM trainer WHERE name != 'Giovanni' AND age > 18)"
assert result == expected
def test_is_slotted(self):
with pytest.raises(AttributeError):
QuerySet(Model).__dict__
class TestQuerySetSlice(Base):
def test_indexed_access_to_first_element_returns_a_model_instance(self):
self.add_trainer('Giovanni')
trainer = Trainer.objects.filter()[0]
assert trainer.name == 'Giovanni'
assert trainer.age == 42
def test_indexed_access_to_random_element_returns_a_model_instance(self):
self.add_trainer(['Giovanni', 'James', 'Jessie'])
trainer = Trainer.objects.filter()[2]
assert trainer.name == 'Jessie'
assert trainer.age == 17
def test_slice_access_with_start_and_stop_value_returns_a_model_instance_list(self):
self.add_trainer(['Giovanni', 'James', 'Jessie'])
trainers = Trainer.objects.filter()[1:3]
assert len(trainers) == 2
assert trainers[0].name == 'James'
assert trainers[0].age == 21
assert trainers[1].name == 'Jessie'
assert trainers[1].age == 17
def test_slice_access_with_offset_only_returns_a_model_instance_list(self):
self.add_trainer(['Giovanni', 'James', 'Jessie'])
trainers = Trainer.objects.filter()[1:3]
assert len(trainers) == 2
assert trainers[0].name == 'James'
assert trainers[0].age == 21
assert trainers[1].name == 'Jessie'
assert trainers[1].age == 17
def test_slice_access_with_offset_only_returns_a_pokemon_list(self):
self.add_trainer(['Giovanni', 'James', 'Jessie'])
trainers = Trainer.objects.filter()[:2]
assert len(trainers) == 2
assert trainers[0].name == 'Giovanni'
assert trainers[0].age == 42
assert trainers[1].name == 'James'
assert trainers[1].age == 21
class TestQuerySetResults(Base):
def test_select_from_one_table_with_all_fields(self):
self.add_trainer(['Giovanni', 'James', 'Jessie'])
result = list(Trainer.objects.filter())
assert len(result) == 3
giovanni, james, jessie = result
assert giovanni.name == 'Giovanni'
assert giovanni.age == 42
assert james.name == 'James'
assert james.age == 21
assert jessie.name == 'Jessie'
assert jessie.age == 17
def test_select_from_one_table_with_one_criterion(self):
self.add_trainer(['Giovanni', 'James', 'Jessie'])
result = list(Trainer.objects.filter(Trainer.age > 18))
assert len(result) == 2
giovanni, james = result
assert giovanni.name == 'Giovanni'
assert giovanni.age == 42
assert james.name == 'James'
assert james.age == 21
def test_select_from_one_table_with_ANDs_criteria_operator(self):
self.add_trainer(['Giovanni', 'James', 'Jessie'])
result = list(Trainer.objects.filter((Trainer.age > 18) & (Trainer.name != 'Giovanni')))
assert len(result) == 1
james = result[0]
assert james.name == 'James'
assert james.age == 21
def test_select_from_one_table_with_ANDs_criteria_list(self):
self.add_trainer(['Giovanni', 'James', 'Jessie'])
result = list(Trainer.objects.filter(Trainer.age > 18, Trainer.name != 'Giovanni'))
assert len(result) == 1
james = result[0]
assert james.name == 'James'
assert james.age == 21
def test_select_from_one_table_with_chained_filters(self):
self.add_trainer(['Giovanni', 'James', 'Jessie'])
queryset = Trainer.objects.filter(Trainer.age > 18)
queryset.filter(Trainer.name != 'Giovanni')
result = list(queryset)
assert len(result) == 1
james = result[0]
assert james.name == 'James'
assert james.age == 21
def test_select_from_one_table_with_in_operator(self):
self.add_trainer(['Giovanni', 'James', 'Jessie'])
result = list(Trainer.objects.filter(Trainer.age << [17, 21]))
assert len(result) == 2
james, jessie = result
assert james.name == 'James'
assert james.age == 21
assert jessie.name == 'Jessie'
assert jessie.age == 17
def test_filter_on_one_field_must_returns_a_list_of_field_values(self):
self.add_trainer(['Giovanni', 'James', 'Jessie'])
result = list(Trainer.objects.select('name'))
expected = ['Giovanni', 'James', 'Jessie']
assert result == expected
def test_filter_on_several_fields_must_returns_a_list_of_namedtuples(self):
self.add_trainer(['Giovanni', 'James', 'Jessie'])
result = list(Trainer.objects.select('name', 'age'))
assert result[0][0] == 'Giovanni'
assert result[0][1] == 42
assert result[1][0] == 'James'
assert result[1][1] == 21
assert result[2][0] == 'Jessie'
assert result[2][1] == 17
def test_filter_on_table_with_related_field(self):
self.add_trainer('Giovanni')
self.add_pokemon('Kangaskhan')
result = list(Pokemon.objects.filter())
assert len(result) == 1
pokemon = result[0]
assert pokemon.name == 'Kangaskhan'
assert pokemon.level == 29
assert isinstance(result[0].trainer, Trainer) is True
trainer = pokemon.trainer
assert trainer.name == 'Giovanni'
assert trainer.age == 42