-
Notifications
You must be signed in to change notification settings - Fork 13
/
test_ltree.py
173 lines (138 loc) · 5.32 KB
/
test_ltree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import pytest
from django.db import IntegrityError
from demo.categories.models import Category
pytestmark = pytest.mark.django_db
def test_create_category():
category = Category.objects.create(name='Foo', code='bar')
# we need to do a full refresh to get the value of the path
category.refresh_from_db()
assert category.id > 0
assert category.name == 'Foo'
assert category.code == 'bar'
assert category.path == 'bar'
def test_direct_children():
top = Category.objects.create(code='top')
science = Category.objects.create(code='science', parent=top)
sport = Category.objects.create(code='sport', parent=top)
news = Category.objects.create(code='news', parent=top)
Category.objects.create(code='politics', parent=news)
# we can acess direct children using the `children` property
assert list(top.children.order_by('code')) == [news, science, sport]
def test_descendants():
top = Category.objects.create(code='top')
top.refresh_from_db()
science = Category.objects.create(code='science', parent=top)
Category.objects.create(code='maths', parent=science)
biology = Category.objects.create(code='biology', parent=science)
Category.objects.create(code='genetics', parent=biology)
Category.objects.create(code='neuroscience', parent=biology)
sport = Category.objects.create(code='sport', parent=top)
Category.objects.create(code='rugby', parent=sport)
football = Category.objects.create(code='football', parent=sport)
Category.objects.create(code='champions_league', parent=football)
Category.objects.create(code='world_cup', parent=football)
# we can get all the ancestors of a category (including itself)
assert list(
Category.objects
.filter(path__descendant=top.path)
.values_list('path', flat=True)
.order_by('path')
) == [
'top',
'top.science',
'top.science.biology',
'top.science.biology.genetics',
'top.science.biology.neuroscience',
'top.science.maths',
'top.sport',
'top.sport.football',
'top.sport.football.champions_league',
'top.sport.football.world_cup',
'top.sport.rugby',
]
def test_ancestors():
top = Category.objects.create(code='top')
top.refresh_from_db()
Category.objects.create(code='sport', parent=top)
science = Category.objects.create(code='science', parent=top)
Category.objects.create(code='maths', parent=science)
biology = Category.objects.create(code='biology', parent=science)
Category.objects.create(code='genetics', parent=biology)
neuroscience = Category.objects.create(code='neuroscience', parent=biology)
neuroscience.refresh_from_db()
# we can get all the ancestors of a category (including itself)
assert list(
Category.objects
.filter(path__ancestor=neuroscience.path)
.values_list('path', flat=True)
.order_by('path')
) == [
'top',
'top.science',
'top.science.biology',
'top.science.biology.neuroscience',
]
def test_update_code():
top = Category.objects.create(code='top')
top.refresh_from_db()
Category.objects.create(code='sport', parent=top)
science = Category.objects.create(code='science', parent=top)
biology = Category.objects.create(code='biology', parent=science)
Category.objects.create(code='genetics', parent=biology)
Category.objects.create(code='neuroscience', parent=biology)
# update the code of a category, it should update its path as well as
# the path of all of its descendants
science.code = 'magic'
science.save()
assert list(
Category.objects
.filter(path__descendant=top.path)
.values_list('path', flat=True)
.order_by('path')
) == [
'top',
'top.magic',
'top.magic.biology',
'top.magic.biology.genetics',
'top.magic.biology.neuroscience',
'top.sport',
]
def test_update_parent():
top = Category.objects.create(code='top')
top.refresh_from_db()
Category.objects.create(code='sport', parent=top)
science = Category.objects.create(code='science', parent=top)
biology = Category.objects.create(code='biology', parent=science)
Category.objects.create(code='genetics', parent=biology)
Category.objects.create(code='neuroscience', parent=biology)
# update the parent of a category, it should update its path as well as
# the path of all of its descendants
biology.parent = top
biology.save()
assert list(
Category.objects
.filter(path__descendant=top.path)
.values_list('path', flat=True)
.order_by('path')
) == [
'top',
'top.biology',
'top.biology.genetics',
'top.biology.neuroscience',
'top.science',
'top.sport',
]
def test_simple_recursion():
foo = Category.objects.create(code='foo')
# we cannot be our own parent...
foo.parent = foo
with pytest.raises(IntegrityError):
foo.save()
def test_nested_recursion():
foo = Category.objects.create(code='foo')
bar = Category.objects.create(code='bar', parent=foo)
baz = Category.objects.create(code='baz', parent=bar)
# we cannot be the descendant of one of our parent
foo.parent = baz
with pytest.raises(IntegrityError):
foo.save()