-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathtest_schema.py
202 lines (166 loc) · 6.97 KB
/
test_schema.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
from django.apps import apps
from django.contrib.auth.models import Group, User
from django.test import TestCase
from djangoql.exceptions import DjangoQLSchemaError
from djangoql.parser import DjangoQLParser
from djangoql.schema import DjangoQLSchema, IntField
from ..models import Book
class ExcludeUserSchema(DjangoQLSchema):
exclude = (User,)
class IncludeUserGroupSchema(DjangoQLSchema):
include = (Group, User)
class IncludeExcludeSchema(DjangoQLSchema):
include = (Group,)
exclude = (Book,)
class BookCustomFieldsSchema(DjangoQLSchema):
def get_fields(self, model):
if model == Book:
return ['name', 'is_published']
return super(BookCustomFieldsSchema, self).get_fields(model)
class WrittenInYearField(IntField):
model = Book
name = 'written_in_year'
def get_lookup_name(self):
return 'written__year'
class BookCustomSearchSchema(DjangoQLSchema):
def get_fields(self, model):
if model == Book:
return [
WrittenInYearField(),
]
class DjangoQLSchemaTest(TestCase):
def all_models(self):
models = []
for app_label in apps.app_configs:
models.extend(apps.get_app_config(app_label).get_models())
return models
def test_default(self):
schema_dict = DjangoQLSchema(Book).as_dict()
self.assertIsInstance(schema_dict, dict)
self.assertEqual('core.book', schema_dict.get('current_model'))
models = schema_dict.get('models')
self.assertIsInstance(models, dict)
all_model_labels = sorted([str(m._meta) for m in self.all_models()])
session_model = all_model_labels.pop()
self.assertEqual('sessions.session', session_model)
self.assertListEqual(all_model_labels, sorted(models.keys()))
def test_exclude(self):
schema_dict = ExcludeUserSchema(Book).as_dict()
self.assertEqual('core.book', schema_dict['current_model'])
self.assertListEqual(sorted(schema_dict['models'].keys()), [
'admin.logentry',
'auth.group',
'auth.permission',
'contenttypes.contenttype',
'core.book'
])
def test_include(self):
schema_dict = IncludeUserGroupSchema(User).as_dict()
self.assertEqual('auth.user', schema_dict['current_model'])
self.assertListEqual(sorted(schema_dict['models'].keys()), [
'auth.group',
'auth.user',
])
def test_get_fields(self):
default = DjangoQLSchema(Book).as_dict()['models']['core.book']
custom = BookCustomFieldsSchema(Book).as_dict()['models']['core.book']
self.assertListEqual(list(default.keys()), [
'author',
'content_type',
'genre',
'id',
'is_published',
'name',
'object_id',
'price',
'rating',
'similar_books',
'written',
])
self.assertListEqual(list(custom.keys()), ['name', 'is_published'])
def test_circular_references(self):
models = DjangoQLSchema(Book).as_dict()['models']
# If Book references Author then Author shouldn't reference Book back
book_author_field = models['core.book'].get('author')
self.assertIsNotNone(book_author_field)
self.assertEqual('relation', book_author_field['type'])
self.assertEqual('auth.user', book_author_field['relation'])
self.assertNotIn('book', models['auth.user'])
def test_custom_search(self):
custom = BookCustomSearchSchema(Book).as_dict()['models']['core.book']
self.assertListEqual(list(custom.keys()), ['written_in_year'])
def test_invalid_config(self):
try:
IncludeExcludeSchema(Group)
self.fail('Invalid schema with include & exclude raises no error')
except DjangoQLSchemaError:
pass
try:
IncludeUserGroupSchema(Book)
self.fail('Schema was initialized with a model excluded from it')
except DjangoQLSchemaError:
pass
try:
IncludeUserGroupSchema(User())
self.fail('Schema was initialized with an instance of a model')
except DjangoQLSchemaError:
pass
def test_validation_pass(self):
samples = [
'first_name = "Lolita"',
'groups.id < 42',
'groups = None', # that's ok to compare a model to None
'groups != None',
'groups.name in ("Stoics") and is_staff = False',
'date_joined > "1753-01-01"',
'date_joined > "1753-01-01 01:24"',
'date_joined > "1753-01-01 01:24:42"',
]
for query in samples:
ast = DjangoQLParser().parse(query)
try:
IncludeUserGroupSchema(User).validate(ast)
except DjangoQLSchemaError as e:
self.fail(e)
def test_validation_fail(self):
samples = [
'gav = 1', # unknown field
'groups.gav > 1', # unknown related field
'groups = "lol"', # can't compare model to a value
'groups.name != 1', # bad value type
'is_staff = True and gav < 2', # complex expression with valid part
'date_joined < "1753-30-01"', # bad timestamps
'date_joined < "1753-01-01 12"',
'date_joined < "1753-01-01 12AM"',
]
for query in samples:
ast = DjangoQLParser().parse(query)
try:
IncludeUserGroupSchema(User).validate(ast)
self.fail('This query should\'t pass validation: %s' % query)
except DjangoQLSchemaError as e:
pass
class UserQLSchema(DjangoQLSchema):
include = (User,)
suggestions_limit = 30
class BookQLSchema(DjangoQLSchema):
include = (Book,)
suggestions_limit = 2
class DjangoQLFieldTest(TestCase):
def setUp(self):
for i in range(2, 70):
User.objects.create(username='a' * i)
def test_get_suggestions_limit_doesnt_affect_choices(self):
result = BookQLSchema(Book).get_field_instance(Book, 'genre').get_sugestions('blah')
self.assertEqual(len(result), len(Book.GENRES))
def test_get_suggestions_with_string_field_should_be_limited(self):
suggestions = IncludeUserGroupSchema(User).get_field_instance(User, 'username').get_sugestions('aaa')
self.assertEqual(len(suggestions), 50)
def test_get_suggestions_with_custom_limit(self):
suggestions = UserQLSchema(User).get_field_instance(User, 'username').get_sugestions('aaa')
self.assertEqual(len(suggestions), UserQLSchema.suggestions_limit)
def test_suggestions_should_contain_query_string(self):
query_string = 'aaa'
suggestions = UserQLSchema(User).get_field_instance(User, 'username').get_sugestions(query_string)
for suggestion in suggestions:
self.assertIn(query_string, suggestion)