Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Splitted expressions tests into smaller methods

  • Loading branch information...
commit c2532825dbe2a422bbce67285637febb0ef9c9f1 1 parent 28abf5f
Anssi Kääriäinen authored September 30, 2012

Showing 1 changed file with 28 additions and 15 deletions. Show diff stats Hide diff stats

  1. 43  tests/modeltests/expressions/tests.py
43  tests/modeltests/expressions/tests.py
@@ -9,7 +9,7 @@
9 9
 
10 10
 
11 11
 class ExpressionsTests(TestCase):
12  
-    def test_filter(self):
  12
+    def setUp(self):
13 13
         Company.objects.create(
14 14
             name="Example Inc.", num_employees=2300, num_chairs=5, is_large=False,
15 15
             ceo=Employee.objects.create(firstname="Joe", lastname="Smith")
@@ -23,6 +23,8 @@ def test_filter(self):
23 23
             ceo=Employee.objects.create(firstname="Max", lastname="Mustermann")
24 24
         )
25 25
 
  26
+
  27
+    def test_filter(self):
26 28
         company_query = Company.objects.values(
27 29
             "name", "num_employees", "num_chairs", "is_large"
28 30
         ).order_by(
@@ -158,6 +160,13 @@ def test_filter(self):
158 160
             ],
159 161
             lambda o: o,
160 162
         )
  163
+
  164
+    def test_comparisons(self):
  165
+        company_query = Company.objects.values(
  166
+            "name", "num_employees", "num_chairs", "is_large"
  167
+        ).order_by(
  168
+            "name", "num_employees", "num_chairs", "is_large"
  169
+        )
161 170
         # The comparison operators and the bitwise unary not can be used
162 171
         # to assign to boolean fields
163 172
         for expression in (
@@ -180,19 +189,19 @@ def test_filter(self):
180 189
             self.assertQuerysetEqual(
181 190
                 company_query, [
182 191
                     {
183  
-                        'num_chairs': 5294600,
  192
+                        'num_chairs': 5,
184 193
                         'name': 'Example Inc.',
185 194
                         'num_employees': 2300,
186 195
                         'is_large': True
187 196
                     },
188 197
                     {
189  
-                        'num_chairs': 15,
  198
+                        'num_chairs': 4,
190 199
                         'name': 'Foobar Ltd.',
191 200
                         'num_employees': 3,
192 201
                         'is_large': False
193 202
                     },
194 203
                     {
195  
-                        'num_chairs': 1088,
  204
+                        'num_chairs': 1,
196 205
                         'name': 'Test GmbH',
197 206
                         'num_employees': 32,
198 207
                         'is_large': False
@@ -230,28 +239,31 @@ def test_filter(self):
230 239
             lambda c: six.text_type(c.point_of_contact),
231 240
         )
232 241
 
  242
+    def test_joins(self):
233 243
         c = Company.objects.all()[0]
234  
-        c.point_of_contact = Employee.objects.create(firstname="Guido", lastname="van Rossum")
  244
+        c.point_of_contact = Employee.objects.create(
  245
+            firstname="Guido", lastname="van Rossum")
  246
+        old_ceo = c.ceo
  247
+        c.ceo = c.point_of_contact
235 248
         c.save()
236 249
 
237 250
         # F Expressions can also span joins
238 251
         self.assertQuerysetEqual(
239  
-            Company.objects.filter(ceo__firstname=F("point_of_contact__firstname")), [
240  
-                "Foobar Ltd.",
241  
-                "Test GmbH",
  252
+            Company.objects.filter(
  253
+                ceo__firstname=F("point_of_contact__firstname")),
  254
+            [
  255
+                "Example Inc.",
242 256
             ],
243 257
             lambda c: c.name
244 258
         )
245  
-
  259
+        c.ceo = old_ceo
  260
+        c.save()
  261
+        # Guido is point of contanct but not CEO. For the null cases we do
  262
+        # not generate a match.
246 263
         Company.objects.exclude(
247 264
             ceo__firstname=F("point_of_contact__firstname")
248 265
         ).update(name="foo")
249  
-        self.assertEqual(
250  
-            Company.objects.exclude(
251  
-                ceo__firstname=F('point_of_contact__firstname')
252  
-            ).get().name,
253  
-            "foo",
254  
-        )
  266
+        self.assertEqual(Company.objects.filter(name="foo").count(), 1)
255 267
 
256 268
         self.assertRaises(FieldError,
257 269
             lambda: Company.objects.exclude(
@@ -259,6 +271,7 @@ def test_filter(self):
259 271
             ).update(name=F('point_of_contact__lastname'))
260 272
         )
261 273
 
  274
+    def test_save(self):
262 275
         # F expressions can be used to update attributes on single objects
263 276
         test_gmbh = Company.objects.get(name="Test GmbH")
264 277
         self.assertEqual(test_gmbh.num_employees, 32)

0 notes on commit c253282

Please sign in to comment.
Something went wrong with that request. Please try again.