8000 Refs #18586 -- Refactored expressions tests. · alex-python/django@43041ee · GitHub
[go: up one dir, main page]

Skip to content

Commit 43041ee

Browse files
shabdatimgraham
authored andcommitted
Refs django#18586 -- Refactored expressions tests.
1 parent 079ee1f commit 43041ee

File tree

1 file changed

+34
-20
lines changed

1 file changed

+34
-20
lines changed

tests/expressions/tests.py

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
from .models import Company, Employee, Number, Experiment
1414

1515

16-
class ExpressionsTests(TestCase):
17-
18-
def test_filter(self):
16+
class BasicExpressionsTests(TestCase):
17+
@classmethod
18+
def setUpTestData(cls):
1919
Company.objects.create(
2020
name="Example Inc.", num_employees=2300, num_chairs=5,
2121
ceo=Employee.objects.create(firstname="Joe", lastname="Smith")
@@ -29,16 +29,19 @@ def test_filter(self):
2929
ceo=Employee.objects.create(firstname="Max", lastname="Mustermann")
3030
)
3131

32-
company_query = Company.objects.values(
32+
def setUp(self):
33+
self.company_query = Company.objects.values(
3334
"name", "num_employees", "num_chairs"
3435
).order_by(
3536
"name", "num_employees", "num_chairs"
3637
)
3738

38-
# We can filter for companies where the number of employees is greater
39+
def test_filter_inter_attribute(self):
40+
# We can filter on attribute relationships on same model obj, e.g.
41+
# find companies where the number of employees is greater
3942
# than the number of chairs.
4043
self.assertQuerysetEqual(
41-
company_query.filter(num_employees__gt=F("num_chairs")), [
44+
self.company_query.filter(num_employees__gt=F("num_chairs")), [
4245
{
4346
"num_chairs": 5,
4447
"name": "Example Inc.",
@@ -53,11 +56,12 @@ def test_filter(self):
5356
lambda o: o
5457
)
5558

59+
def test_update(self):
5660
# We can set one field to have the value of another field
5761
# Make sure we have enough chairs
58-
company_query.update(num_chairs=F("num_employees"))
62+
self.company_query.update(num_chairs=F("num_employees"))
5963
self.assertQuerysetEqual(
60-
company_query, [
64+
self.company_query, [
6165
{
6266
"num_chairs": 2300,
6367
"name": "Example Inc.",
@@ -77,11 +81,12 @@ def test_filter(self):
7781
lambda o: o
7882
)
7983

84+
def test_arithmetic(self):
8085
# We can perform arithmetic operations in expressions
8186
# Make sure we have 2 spare chairs
82-
company_query.update(num_chairs=F("num_employees") + 2)
87+
self.company_query.update(num_chairs=F("num_employees") + 2)
8388
self.assertQuerysetEqual(
84-
company_query, [
89+
self.company_query, [
8590
{
8691
'num_chairs': 2302,
8792
'name': 'Example Inc.',
@@ -101,12 +106,13 @@ def test_filter(self):
101106
lambda o: o,
102107
)
103108

109+
def test_order_of_operations(self):
104110
# Law of order of operations is followed
105-
company_query.update(
111+
self. company_query.update(
106112
num_chairs=F('num_employees') + 2 * F('num_employees')
107113
)
108114
self.assertQuerysetEqual(
109-
company_query, [
115+
self.company_query, [
110116
{
111117
'num_chairs': 6900,
112118
'name': 'Example Inc.',
@@ -126,12 +132,13 @@ def test_filter(self):
126132
lambda o: o,
127133
)
128134

135+
def test_parenthesis_priority(self):
129136
# Law of order of operations can be overridden by parentheses
130-
company_query.update(
137+
self.company_query.update(
131138
num_chairs=((F('num_employees') + 2) * F('num_employees'))
132139
)
133140
self.assertQuerysetEqual(
134-
company_query, [
141+
self.company_query, [
135142
{
136143
'num_chairs': 5294600,
137144
'name': 'Example Inc.',
@@ -151,8 +158,8 @@ def test_filter(self):
151158
lambda o: o,
152159
)
153160

154-
# The relation of a foreign key can become copied over to an other
155-
# foreign key.
161+
def test_update_with_fk(self):
162+
# ForeignKey can become updated with the value of another ForeignKey.
156163
self.assertEqual(
157164
Company.objects.update(point_of_contact=F('ceo')),
158165
3
@@ -167,11 +174,13 @@ def test_filter(self):
167174
ordered=False
168175
)
169176

177+
def test_filter_with_join(self):
178+
# F Expressions can also span joins
179+
Company.objects.update(point_of_contact=F('ceo'))
170180
c = Company.objects.all()[0]
171181
c.point_of_contact = Employee.objects.create(firstname="Guido", lastname="van Rossum")
172182
c.save()
173183

174-
# F Expressions can also span joins
175184
self.assertQuerysetEqual(
176185
Company.objects.filter(ceo__firstname=F("point_of_contact__firstname")), [
177186
"Foobar Ltd.",
@@ -197,6 +206,7 @@ def test_filter(self):
197206
ceo__firstname=F('point_of_contact__firstname')
198207
).update(name=F('point_of_contact__lastname'))
199208

209+
def test_object_update(self):
200210
# F expressions can be used to update attributes on single objects
201211
test_gmbh = Company.objects.get(name="Test GmbH")
202212
self.assertEqual(test_gmbh.num_employees, 32)
@@ -205,11 +215,10 @@ def test_filter(self):
205215
test_gmbh = Company.objects.get(pk=test_gmbh.pk)
206216
self.assertEqual(test_gmbh.num_employees, 36)
207217

218+
def test_object_update_fk(self):
208219
# F expressions cannot be used to update attributes which are foreign
209220
# keys, or attributes which involve joins.
210-
test_gmbh.point_of_contact = None
211-
test_gmbh.save()
212-
self.assertIsNone(test_gmbh.point_of_contact)
221+
test_gmbh = Company.objects.get(name="Test GmbH")
213222

214223
def test():
215224
test_gmbh.point_of_contact = F("ceo")
@@ -220,8 +229,10 @@ def test():
220229
test_gmbh.name = F("ceo__last_name")
221230
self.assertRaises(FieldError, test_gmbh.save)
222231

232+
def test_object_update_unsaved_objects(self):
223233
# F expressions cannot be used to update attributes on objects which do
224234
# not yet exist in the database
235+
test_gmbh = Company.objects.get(name="Test GmbH")
225236
acme = Company(
226237
name="The Acme Widget Co.", num_employees=12, num_chairs=5,
227238
ceo=test_gmbh.ceo
@@ -287,6 +298,9 @@ def test_ticket_18375_chained_filters(self):
287298
)
288299
self.assertEqual(str(qs.query).count('JOIN'), 2)
289300

301+
302+
class ExpressionsTests(TestCase):
303+
290304
def test_F_object_deepcopy(self):
291305
"""
292306
Make sure F objects can be deepcopied (#23492)

0 commit comments

Comments
 (0)
0