-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_relations.py
executable file
·82 lines (61 loc) · 2.09 KB
/
test_relations.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
#!/usr/bin/python3.2
from mago import Field, Model, connect, UnSet, OneToMany, ManyToOne
from bson import ObjectId
import unittest
import mago
_mongo_connection = connect("__test_model")
# 1 to *
class User(Model):
name = Field()
addresses = OneToMany("Address", backref="user")
# * to 1
class Address(Model):
email = Field()
user = ManyToOne("User", backref="addresses")
class RelationsTest(unittest.TestCase):
def tearDown(self):
super().tearDown()
_mongo_connection.drop_database("__test_model")
_mongo_connection.disconnect()
def test_relations(self):
self.assertEqual(User.addresses.field_name, "addresses")
self.assertEqual(User.addresses.model, Address)
self.assertEqual(Address.user.field_name, "user")
self.assertEqual(Address.user.model, User)
u = User(name="injaon")
hugo = User(name="hugo")
home = Address(email="[email protected]")
work = Address(email="[email protected]")
# 1 to *
# get
self.assertEqual(dict.get(u, "addresses", None), None)
self.assertEqual(u["addresses"], [])
first = u["addresses"]
# set
u["addresses"] = [home, work]
second = u["addresses"]
self.assertEqual(id(first), id(second))
self.assertEqual(u["addresses"], [home, work])
self.assertIs(home["user"], u)
self.assertIs(work["user"], u)
# del
del u["addresses"]
self.assertEqual(u["addresses"], [])
self.assertIs(work["user"], None)
self.assertIs(home["user"], None)
# * to 1
# set
home["user"] = u
self.assertIs(home["user"], u)
self.assertIs(u["addresses"][0], home)
home["user"] = hugo
self.assertIs(home["user"], hugo)
self.assertIs(hugo["addresses"][0], home)
self.assertEqual(u["addresses"], [])
# del
del home["user"]
self.assertEqual(hugo["addresses"], [])
self.assertIs(home["user"], None)
# TODO: test pesistance
if __name__ == "__main__":
unittest.main()