2015-07-10 15:52:22 +08:00
|
|
|
from django.test import TestCase
|
|
|
|
|
|
|
|
from .models import SlugPage
|
|
|
|
|
|
|
|
|
|
|
|
class RestrictedConditionsTests(TestCase):
|
2018-11-24 09:59:38 +08:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
2015-07-10 15:52:22 +08:00
|
|
|
slugs = [
|
|
|
|
'a',
|
|
|
|
'a/a',
|
|
|
|
'a/b',
|
|
|
|
'a/b/a',
|
|
|
|
'x',
|
|
|
|
'x/y/z',
|
|
|
|
]
|
|
|
|
SlugPage.objects.bulk_create([SlugPage(slug=slug) for slug in slugs])
|
|
|
|
|
|
|
|
def test_restrictions_with_no_joining_columns(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
It's possible to create a working related field that doesn't
|
2015-07-10 15:52:22 +08:00
|
|
|
use any joining columns, as long as an extra restriction is supplied.
|
|
|
|
"""
|
|
|
|
a = SlugPage.objects.get(slug='a')
|
2017-03-17 19:51:48 +08:00
|
|
|
self.assertEqual(
|
2015-07-10 15:52:22 +08:00
|
|
|
[p.slug for p in SlugPage.objects.filter(ascendants=a)],
|
|
|
|
['a', 'a/a', 'a/b', 'a/b/a'],
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
[p.slug for p in a.descendants.all()],
|
|
|
|
['a', 'a/a', 'a/b', 'a/b/a'],
|
|
|
|
)
|
|
|
|
|
|
|
|
aba = SlugPage.objects.get(slug='a/b/a')
|
2017-03-17 19:51:48 +08:00
|
|
|
self.assertEqual(
|
2015-07-10 15:52:22 +08:00
|
|
|
[p.slug for p in SlugPage.objects.filter(descendants__in=[aba])],
|
|
|
|
['a', 'a/b', 'a/b/a'],
|
|
|
|
)
|
2017-03-17 19:51:48 +08:00
|
|
|
self.assertEqual(
|
2015-07-10 15:52:22 +08:00
|
|
|
[p.slug for p in aba.ascendants.all()],
|
|
|
|
['a', 'a/b', 'a/b/a'],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_empty_join_conditions(self):
|
|
|
|
x = SlugPage.objects.get(slug='x')
|
|
|
|
message = "Join generated an empty ON clause."
|
|
|
|
with self.assertRaisesMessage(ValueError, message):
|
|
|
|
list(SlugPage.objects.filter(containers=x))
|