-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathintegration_test_relations.py
More file actions
99 lines (72 loc) · 4 KB
/
integration_test_relations.py
File metadata and controls
99 lines (72 loc) · 4 KB
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
# -*- coding: utf-8 -*-
from syncano.models import Class
from tests.integration_test import InstanceMixin, IntegrationTest
class RelationApiTest(InstanceMixin, IntegrationTest):
@classmethod
def setUpClass(cls):
super(RelationApiTest, cls).setUpClass()
# prapare data
cls.author = Class.please.create(name="author", schema=[
{"name": "name", "type": "string", "filter_index": True},
{"name": "birthday", "type": "integer"},
])
cls.book = Class.please.create(name="book", schema=[
{"name": "title", "type": "string", "filter_index": True},
{"name": "authors", "type": "relation", "target": "author", "filter_index": True},
])
cls.prus = cls.author.objects.create(name='Bolesław Prus', birthday=1847)
cls.lem = cls.author.objects.create(name='Stanisław Lem', birthday=1921)
cls.coehlo = cls.author.objects.create(name='Paulo Coehlo', birthday=1947)
cls.lalka = cls.book.objects.create(authors=[cls.prus.id], title='Lalka')
cls.niezwyciezony = cls.book.objects.create(authors=[cls.lem.id], title='Niezwyciężony')
cls.brida = cls.book.objects.create(authors=[cls.coehlo.id], title='Brida')
def test_integers_list(self):
authors_list_ids = [self.prus.id, self.coehlo.id]
book = self.book.objects.create(authors=authors_list_ids, title='Strange title')
self.assertListEqual(sorted(book.authors), sorted(authors_list_ids))
book.delete()
def test_object_list(self):
authors_list_ids = [self.prus.id, self.coehlo.id]
book = self.book.objects.create(authors=authors_list_ids, title='Strange title')
self.assertListEqual(sorted(book.authors), sorted(authors_list_ids))
book.delete()
def test_object_assign(self):
self.lalka.authors = [self.lem, self.coehlo]
self.lalka.save()
self.assertListEqual(sorted(self.lalka.authors), sorted([self.lem.id, self.coehlo.id]))
self.lalka.authors = [self.prus]
self.lalka.save()
def test_related_field_add(self):
self.niezwyciezony.authors_set.add(self.coehlo)
self.assertListEqual(sorted(self.niezwyciezony.authors), sorted([self.lem.id, self.coehlo.id]))
self.niezwyciezony.authors_set.add(self.prus.id, self.coehlo.id)
self.assertListEqual(sorted(self.niezwyciezony.authors), sorted([self.lem.id, self.prus.id, self.coehlo.id]))
self.niezwyciezony.authors = [self.lem]
self.niezwyciezony.save()
def test_related_field_remove(self):
self.brida.authors_set.remove(self.coehlo)
self.assertEqual(self.brida.authors, None)
self.niezwyciezony.authors_set.remove(self.prus, self.lem, self.coehlo)
self.assertEqual(self.niezwyciezony.authors, None)
self.niezwyciezony.authors = [self.lem]
self.niezwyciezony.save()
self.brida.authors = [self.coehlo]
self.brida.save()
def test_related_field_lookup_contains(self):
filtered_books = self.book.objects.list().filter(authors__contains=[self.prus])
self.assertEqual(len(list(filtered_books)), 1)
for book in filtered_books:
self.assertEqual(book.title, self.lalka.title)
def test_related_field_lookup_contains_fail(self):
filtered_books = self.book.objects.list().filter(authors__contains=[self.prus, self.lem])
self.assertEqual(len(list(filtered_books)), 0)
def test_related_field_lookup_is(self):
filtered_books = self.book.objects.list().filter(authors__name__startswith='Stan')
self.assertEqual(len(list(filtered_books)), 1)
for book in filtered_books:
self.assertEqual(book.title, self.niezwyciezony.title)
def test_multiple_lookups(self):
filtered_books = self.book.objects.list().filter(authors__id__in=[self.prus.id], title__eq='Lalka')
self.assertEqual(len(list(filtered_books)), 1)
for book in filtered_books:
self.assertEqual(book.title, self.lalka.title)