Ticket #15009: tests.py

File tests.py, 1.1 KB (added by Saverio Trioni <strioni@…>, 13 years ago)
Line 
1from django.db import models
2from django.test import TestCase
3from wrong_or.wrong.models import BaseModel, First, Second, Third
4
5class SimpleTest(TestCase):
6 def test_wrong(self):
7
8 bo = BaseModel.objects.create()
9
10 f = First.objects.create(condition=True)
11 bo.firsts.add(f)
12
13 s1 = Second.objects.create()
14 t1 = Third.objects.create(second=s1, condition=False)
15 s2 = Second.objects.create()
16 t2 = Third.objects.create(second=s2, condition=False)
17
18 bo.seconds.add(s1, s2)
19
20 q1 = models.Q(firsts__condition=True) # match the only BaseModel
21 q2 = models.Q(seconds__thirds__condition=True) # match nothing
22
23 self.assertTrue(BaseModel.objects.filter(q1).exists())
24 self.assertEqual(1, BaseModel.objects.filter(q1).count())
25 self.assertFalse(BaseModel.objects.filter(q2).exists())
26
27 # A filter should not duplicate records.
28 # This assertion will fail, because there are two copies of
29 # the 'bo' record.
30 self.assertEqual(1, BaseModel.objects.filter(q1|q2).count())
31
32
33
Back to Top