App Engine Python SDK version 1.7.4 (2)
[gae.git] / python / lib / django_1_4 / tests / regressiontests / expressions_regress / tests.py
blob80ddfadbe7fb01bdafaaf37917e7b42f34a8446b
1 """
2 Spanning tests for all the operations that F() expressions can perform.
3 """
4 from __future__ import absolute_import
6 import datetime
8 from django.db import connection
9 from django.db.models import F
10 from django.test import TestCase, Approximate, skipUnlessDBFeature
12 from .models import Number, Experiment
15 class ExpressionsRegressTests(TestCase):
17 def setUp(self):
18 Number(integer=-1).save()
19 Number(integer=42).save()
20 Number(integer=1337).save()
21 self.assertEqual(Number.objects.update(float=F('integer')), 3)
23 def test_fill_with_value_from_same_object(self):
24 """
25 We can fill a value in all objects with an other value of the
26 same object.
27 """
28 self.assertQuerysetEqual(
29 Number.objects.all(),
31 '<Number: -1, -1.000>',
32 '<Number: 42, 42.000>',
33 '<Number: 1337, 1337.000>'
37 def test_increment_value(self):
38 """
39 We can increment a value of all objects in a query set.
40 """
41 self.assertEqual(
42 Number.objects.filter(integer__gt=0)
43 .update(integer=F('integer') + 1),
46 self.assertQuerysetEqual(
47 Number.objects.all(),
49 '<Number: -1, -1.000>',
50 '<Number: 43, 42.000>',
51 '<Number: 1338, 1337.000>'
55 def test_filter_not_equals_other_field(self):
56 """
57 We can filter for objects, where a value is not equals the value
58 of an other field.
59 """
60 self.assertEqual(
61 Number.objects.filter(integer__gt=0)
62 .update(integer=F('integer') + 1),
64 self.assertQuerysetEqual(
65 Number.objects.exclude(float=F('integer')),
67 '<Number: 43, 42.000>',
68 '<Number: 1338, 1337.000>'
72 def test_complex_expressions(self):
73 """
74 Complex expressions of different connection types are possible.
75 """
76 n = Number.objects.create(integer=10, float=123.45)
77 self.assertEqual(Number.objects.filter(pk=n.pk)
78 .update(float=F('integer') + F('float') * 2),
81 self.assertEqual(Number.objects.get(pk=n.pk).integer, 10)
82 self.assertEqual(Number.objects.get(pk=n.pk).float, Approximate(256.900, places=3))
84 class ExpressionOperatorTests(TestCase):
85 def setUp(self):
86 self.n = Number.objects.create(integer=42, float=15.5)
88 def test_lefthand_addition(self):
89 # LH Addition of floats and integers
90 Number.objects.filter(pk=self.n.pk).update(
91 integer=F('integer') + 15,
92 float=F('float') + 42.7
95 self.assertEqual(Number.objects.get(pk=self.n.pk).integer, 57)
96 self.assertEqual(Number.objects.get(pk=self.n.pk).float, Approximate(58.200, places=3))
98 def test_lefthand_subtraction(self):
99 # LH Subtraction of floats and integers
100 Number.objects.filter(pk=self.n.pk).update(integer=F('integer') - 15,
101 float=F('float') - 42.7)
103 self.assertEqual(Number.objects.get(pk=self.n.pk).integer, 27)
104 self.assertEqual(Number.objects.get(pk=self.n.pk).float, Approximate(-27.200, places=3))
106 def test_lefthand_multiplication(self):
107 # Multiplication of floats and integers
108 Number.objects.filter(pk=self.n.pk).update(integer=F('integer') * 15,
109 float=F('float') * 42.7)
111 self.assertEqual(Number.objects.get(pk=self.n.pk).integer, 630)
112 self.assertEqual(Number.objects.get(pk=self.n.pk).float, Approximate(661.850, places=3))
114 def test_lefthand_division(self):
115 # LH Division of floats and integers
116 Number.objects.filter(pk=self.n.pk).update(integer=F('integer') / 2,
117 float=F('float') / 42.7)
119 self.assertEqual(Number.objects.get(pk=self.n.pk).integer, 21)
120 self.assertEqual(Number.objects.get(pk=self.n.pk).float, Approximate(0.363, places=3))
122 def test_lefthand_modulo(self):
123 # LH Modulo arithmetic on integers
124 Number.objects.filter(pk=self.n.pk).update(integer=F('integer') % 20)
126 self.assertEqual(Number.objects.get(pk=self.n.pk).integer, 2)
127 self.assertEqual(Number.objects.get(pk=self.n.pk).float, Approximate(15.500, places=3))
129 def test_lefthand_bitwise_and(self):
130 # LH Bitwise ands on integers
131 Number.objects.filter(pk=self.n.pk).update(integer=F('integer') & 56)
133 self.assertEqual(Number.objects.get(pk=self.n.pk).integer, 40)
134 self.assertEqual(Number.objects.get(pk=self.n.pk).float, Approximate(15.500, places=3))
136 @skipUnlessDBFeature('supports_bitwise_or')
137 def test_lefthand_bitwise_or(self):
138 # LH Bitwise or on integers
139 Number.objects.filter(pk=self.n.pk).update(integer=F('integer') | 48)
141 self.assertEqual(Number.objects.get(pk=self.n.pk).integer, 58)
142 self.assertEqual(Number.objects.get(pk=self.n.pk).float, Approximate(15.500, places=3))
144 def test_right_hand_addition(self):
145 # Right hand operators
146 Number.objects.filter(pk=self.n.pk).update(integer=15 + F('integer'),
147 float=42.7 + F('float'))
149 # RH Addition of floats and integers
150 self.assertEqual(Number.objects.get(pk=self.n.pk).integer, 57)
151 self.assertEqual(Number.objects.get(pk=self.n.pk).float, Approximate(58.200, places=3))
153 def test_right_hand_subtraction(self):
154 Number.objects.filter(pk=self.n.pk).update(integer=15 - F('integer'),
155 float=42.7 - F('float'))
157 # RH Subtraction of floats and integers
158 self.assertEqual(Number.objects.get(pk=self.n.pk).integer, -27)
159 self.assertEqual(Number.objects.get(pk=self.n.pk).float, Approximate(27.200, places=3))
161 def test_right_hand_multiplication(self):
162 # RH Multiplication of floats and integers
163 Number.objects.filter(pk=self.n.pk).update(integer=15 * F('integer'),
164 float=42.7 * F('float'))
166 self.assertEqual(Number.objects.get(pk=self.n.pk).integer, 630)
167 self.assertEqual(Number.objects.get(pk=self.n.pk).float, Approximate(661.850, places=3))
169 def test_right_hand_division(self):
170 # RH Division of floats and integers
171 Number.objects.filter(pk=self.n.pk).update(integer=640 / F('integer'),
172 float=42.7 / F('float'))
174 self.assertEqual(Number.objects.get(pk=self.n.pk).integer, 15)
175 self.assertEqual(Number.objects.get(pk=self.n.pk).float, Approximate(2.755, places=3))
177 def test_right_hand_modulo(self):
178 # RH Modulo arithmetic on integers
179 Number.objects.filter(pk=self.n.pk).update(integer=69 % F('integer'))
181 self.assertEqual(Number.objects.get(pk=self.n.pk).integer, 27)
182 self.assertEqual(Number.objects.get(pk=self.n.pk).float, Approximate(15.500, places=3))
184 def test_right_hand_bitwise_and(self):
185 # RH Bitwise ands on integers
186 Number.objects.filter(pk=self.n.pk).update(integer=15 & F('integer'))
188 self.assertEqual(Number.objects.get(pk=self.n.pk).integer, 10)
189 self.assertEqual(Number.objects.get(pk=self.n.pk).float, Approximate(15.500, places=3))
191 @skipUnlessDBFeature('supports_bitwise_or')
192 def test_right_hand_bitwise_or(self):
193 # RH Bitwise or on integers
194 Number.objects.filter(pk=self.n.pk).update(integer=15 | F('integer'))
196 self.assertEqual(Number.objects.get(pk=self.n.pk).integer, 47)
197 self.assertEqual(Number.objects.get(pk=self.n.pk).float, Approximate(15.500, places=3))
199 class FTimeDeltaTests(TestCase):
201 def setUp(self):
202 sday = datetime.date(2010, 6, 25)
203 stime = datetime.datetime(2010, 6, 25, 12, 15, 30, 747000)
204 midnight = datetime.time(0)
206 delta0 = datetime.timedelta(0)
207 delta1 = datetime.timedelta(microseconds=253000)
208 delta2 = datetime.timedelta(seconds=44)
209 delta3 = datetime.timedelta(hours=21, minutes=8)
210 delta4 = datetime.timedelta(days=10)
212 # Test data is set so that deltas and delays will be
213 # strictly increasing.
214 self.deltas = []
215 self.delays = []
216 self.days_long = []
218 # e0: started same day as assigned, zero duration
219 end = stime+delta0
220 e0 = Experiment.objects.create(name='e0', assigned=sday, start=stime,
221 end=end, completed=end.date())
222 self.deltas.append(delta0)
223 self.delays.append(e0.start-
224 datetime.datetime.combine(e0.assigned, midnight))
225 self.days_long.append(e0.completed-e0.assigned)
227 # e1: started one day after assigned, tiny duration, data
228 # set so that end time has no fractional seconds, which
229 # tests an edge case on sqlite. This Experiment is only
230 # included in the test data when the DB supports microsecond
231 # precision.
232 if connection.features.supports_microsecond_precision:
233 delay = datetime.timedelta(1)
234 end = stime + delay + delta1
235 e1 = Experiment.objects.create(name='e1', assigned=sday,
236 start=stime+delay, end=end, completed=end.date())
237 self.deltas.append(delta1)
238 self.delays.append(e1.start-
239 datetime.datetime.combine(e1.assigned, midnight))
240 self.days_long.append(e1.completed-e1.assigned)
242 # e2: started three days after assigned, small duration
243 end = stime+delta2
244 e2 = Experiment.objects.create(name='e2',
245 assigned=sday-datetime.timedelta(3), start=stime, end=end,
246 completed=end.date())
247 self.deltas.append(delta2)
248 self.delays.append(e2.start-
249 datetime.datetime.combine(e2.assigned, midnight))
250 self.days_long.append(e2.completed-e2.assigned)
252 # e3: started four days after assigned, medium duration
253 delay = datetime.timedelta(4)
254 end = stime + delay + delta3
255 e3 = Experiment.objects.create(name='e3',
256 assigned=sday, start=stime+delay, end=end, completed=end.date())
257 self.deltas.append(delta3)
258 self.delays.append(e3.start-
259 datetime.datetime.combine(e3.assigned, midnight))
260 self.days_long.append(e3.completed-e3.assigned)
262 # e4: started 10 days after assignment, long duration
263 end = stime + delta4
264 e4 = Experiment.objects.create(name='e4',
265 assigned=sday-datetime.timedelta(10), start=stime, end=end,
266 completed=end.date())
267 self.deltas.append(delta4)
268 self.delays.append(e4.start-
269 datetime.datetime.combine(e4.assigned, midnight))
270 self.days_long.append(e4.completed-e4.assigned)
271 self.expnames = [e.name for e in Experiment.objects.all()]
273 def test_delta_add(self):
274 for i in range(len(self.deltas)):
275 delta = self.deltas[i]
276 test_set = [e.name for e in
277 Experiment.objects.filter(end__lt=F('start')+delta)]
278 self.assertEqual(test_set, self.expnames[:i])
280 test_set = [e.name for e in
281 Experiment.objects.filter(end__lte=F('start')+delta)]
282 self.assertEqual(test_set, self.expnames[:i+1])
284 def test_delta_subtract(self):
285 for i in range(len(self.deltas)):
286 delta = self.deltas[i]
287 test_set = [e.name for e in
288 Experiment.objects.filter(start__gt=F('end')-delta)]
289 self.assertEqual(test_set, self.expnames[:i])
291 test_set = [e.name for e in
292 Experiment.objects.filter(start__gte=F('end')-delta)]
293 self.assertEqual(test_set, self.expnames[:i+1])
295 def test_exclude(self):
296 for i in range(len(self.deltas)):
297 delta = self.deltas[i]
298 test_set = [e.name for e in
299 Experiment.objects.exclude(end__lt=F('start')+delta)]
300 self.assertEqual(test_set, self.expnames[i:])
302 test_set = [e.name for e in
303 Experiment.objects.exclude(end__lte=F('start')+delta)]
304 self.assertEqual(test_set, self.expnames[i+1:])
306 def test_date_comparison(self):
307 for i in range(len(self.days_long)):
308 days = self.days_long[i]
309 test_set = [e.name for e in
310 Experiment.objects.filter(completed__lt=F('assigned')+days)]
311 self.assertEqual(test_set, self.expnames[:i])
313 test_set = [e.name for e in
314 Experiment.objects.filter(completed__lte=F('assigned')+days)]
315 self.assertEqual(test_set, self.expnames[:i+1])
317 @skipUnlessDBFeature("supports_mixed_date_datetime_comparisons")
318 def test_mixed_comparisons1(self):
319 for i in range(len(self.delays)):
320 delay = self.delays[i]
321 if not connection.features.supports_microsecond_precision:
322 delay = datetime.timedelta(delay.days, delay.seconds)
323 test_set = [e.name for e in
324 Experiment.objects.filter(assigned__gt=F('start')-delay)]
325 self.assertEqual(test_set, self.expnames[:i])
327 test_set = [e.name for e in
328 Experiment.objects.filter(assigned__gte=F('start')-delay)]
329 self.assertEqual(test_set, self.expnames[:i+1])
331 def test_mixed_comparisons2(self):
332 delays = [datetime.timedelta(delay.days) for delay in self.delays]
333 for i in range(len(delays)):
334 delay = delays[i]
335 test_set = [e.name for e in
336 Experiment.objects.filter(start__lt=F('assigned')+delay)]
337 self.assertEqual(test_set, self.expnames[:i])
339 test_set = [e.name for e in
340 Experiment.objects.filter(start__lte=F('assigned')+delay+
341 datetime.timedelta(1))]
342 self.assertEqual(test_set, self.expnames[:i+1])
344 def test_delta_update(self):
345 for i in range(len(self.deltas)):
346 delta = self.deltas[i]
347 exps = Experiment.objects.all()
348 expected_durations = [e.duration() for e in exps]
349 expected_starts = [e.start+delta for e in exps]
350 expected_ends = [e.end+delta for e in exps]
352 Experiment.objects.update(start=F('start')+delta, end=F('end')+delta)
353 exps = Experiment.objects.all()
354 new_starts = [e.start for e in exps]
355 new_ends = [e.end for e in exps]
356 new_durations = [e.duration() for e in exps]
357 self.assertEqual(expected_starts, new_starts)
358 self.assertEqual(expected_ends, new_ends)
359 self.assertEqual(expected_durations, new_durations)
361 def test_delta_invalid_op_mult(self):
362 raised = False
363 try:
364 r = repr(Experiment.objects.filter(end__lt=F('start')*self.deltas[0]))
365 except TypeError:
366 raised = True
367 self.assertTrue(raised, "TypeError not raised on attempt to multiply datetime by timedelta.")
369 def test_delta_invalid_op_div(self):
370 raised = False
371 try:
372 r = repr(Experiment.objects.filter(end__lt=F('start')/self.deltas[0]))
373 except TypeError:
374 raised = True
375 self.assertTrue(raised, "TypeError not raised on attempt to divide datetime by timedelta.")
377 def test_delta_invalid_op_mod(self):
378 raised = False
379 try:
380 r = repr(Experiment.objects.filter(end__lt=F('start')%self.deltas[0]))
381 except TypeError:
382 raised = True
383 self.assertTrue(raised, "TypeError not raised on attempt to modulo divide datetime by timedelta.")
385 def test_delta_invalid_op_and(self):
386 raised = False
387 try:
388 r = repr(Experiment.objects.filter(end__lt=F('start')&self.deltas[0]))
389 except TypeError:
390 raised = True
391 self.assertTrue(raised, "TypeError not raised on attempt to binary and a datetime with a timedelta.")
393 def test_delta_invalid_op_or(self):
394 raised = False
395 try:
396 r = repr(Experiment.objects.filter(end__lt=F('start')|self.deltas[0]))
397 except TypeError:
398 raised = True
399 self.assertTrue(raised, "TypeError not raised on attempt to binary or a datetime with a timedelta.")