-
Notifications
You must be signed in to change notification settings - Fork 8
/
test_manager.py
108 lines (80 loc) · 2.78 KB
/
test_manager.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
import unittest
from datetime import datetime
from reobject.models.fields import Field, ForeignKey
from reobject.models.manager import Manager
from reobject.models.model import Model
class SomeModel(Model):
p = Field()
q = Field()
r = Field()
class TestQuery(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
SomeModel.objects.all().delete()
def test_descriptor_cls(self):
try:
SomeModel.objects
SomeModel.objects.filter
except AttributeError:
self.assertFalse(True)
else:
pass # AttributeError was not raised
def test_descriptor_instance(self):
obj = SomeModel(1, 2, 3)
with self.assertRaises(AttributeError):
obj.objects
obj.objects.foo
def test_create(self):
obj = SomeModel(p=1, q=2, r=1)
self.assertEqual(obj.p, 1)
self.assertEqual(obj.q, 2)
self.assertEqual(obj.r, 1)
self.assertEqual(obj.id, id(obj))
self.assertLess(obj.created, datetime.utcnow())
self.assertEqual(obj.created, obj.updated)
def test_none(self):
self.assertFalse(SomeModel.objects.none())
def test_all(self):
SomeModel(p=0, q=0, r=1)
SomeModel(p=0, q=1, r=0)
SomeModel(p=1, q=0, r=0)
self.assertEqual(len(SomeModel.objects.all()), 3)
def test_manager_model(self):
self.assertEqual(SomeModel.objects.model, SomeModel)
class Teacher(Model):
pass
class Student(Model):
teacher = ForeignKey(Teacher)
class TestRelatedManager(unittest.TestCase):
def setUp(self):
self.teacher_a = Teacher()
self.teacher_b = Teacher()
Student(teacher=self.teacher_a)
Student(teacher=self.teacher_a)
Student(teacher=self.teacher_a)
Student(teacher=self.teacher_b)
Student(teacher=self.teacher_b)
def tearDown(self):
Teacher.objects.all().delete()
Student.objects.all().delete()
def test_manager_cls(self):
self.assertIsInstance(Teacher.objects, Manager)
self.assertIsInstance(Student.objects, Manager)
self.assertEqual(self.teacher_a.student_set.__class__.__name__, 'RelatedManager')
self.assertEqual(self.teacher_a.student_set.model, Student)
def test_many_to_many_relationship(self):
self.assertEqual(
self.teacher_a.student_set.all(),
Student.objects.filter(teacher__pk=self.teacher_a.pk)
)
self.assertEqual(
self.teacher_a.student_set.count(), 3
)
self.assertEqual(
self.teacher_b.student_set.all(),
Student.objects.filter(teacher__pk=self.teacher_b.pk)
)
self.assertEqual(
self.teacher_b.student_set.count(), 2
)