| 1 | from django.db import models
|
|---|
| 2 | from django.test import TestCase
|
|---|
| 3 | from wrong_or.wrong.models import BaseModel, First, Second, Third
|
|---|
| 4 |
|
|---|
| 5 | class 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 |
|
|---|