-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_field.py
193 lines (147 loc) · 6.33 KB
/
test_field.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
"""
A variety of tests to cover the majority of the functionality
in mago Fields.
"""
import unittest
from mago import Field, ReferenceField, connect, Model, EnumField, ConstantField
from mago.field import FieldError
import mago
class Base(object):
pass
class Sub(Base):
pass
class MagoFieldTests(unittest.TestCase):
def setUp(self):
super(MagoFieldTests, self).setUp()
self._mongo_connection = connect("__test_change_field_name")
def tearDown(self):
super(MagoFieldTests, self).tearDown()
self._mongo_connection.drop_database("__test_change_field_name")
self._mongo_connection.disconnect()
def test_field(self):
class MockModel(Model):
field = Field(str)
typeless = Field()
required = Field(required=True)
default = Field(int, default=4)
# reference = ReferenceField(Base)
mock = MockModel()
for name, field in mock._fields.items():
self.assertEqual(name, field.field_name)
# checks the unassigned values
self.assertEqual(mago.UnSet, mock.field)
self.assertEqual(mago.UnSet, mock.other)
self.assertEqual(mago.UnSet, mock["other"])
self.assertEqual(mago.UnSet, mock["field"])
# check setters
mock.field = "testing"
mock["other"] = "schemaless"
mock1 = MockModel(field="on_init")
# test getters
self.assertEqual("schemaless", mock["other"])
self.assertEqual("schemaless", mock.other)
self.assertEqual("testing", mock["field"])
self.assertEqual("testing", mock.field)
self.assertEqual("on_init", mock1.field)
self.assertEqual("on_init", mock1["field"])
# test default
self.assertEqual(mock.default, 4)
self.assertEqual(mock["default"], 4)
# test type constraint
self.assertRaises(FieldError, setattr, mock, "field", 5)
self.assertRaises(FieldError, mock.__setitem__, "field", 5)
self.assertRaises(FieldError, setattr, mock, "field", object())
self.assertRaises(FieldError, setattr, mock, "default", "object()")
self.assertRaises(FieldError, MockModel, **{"field" : 1})
# test typeless
mock.typeless = 5
mock.typeless = "string"
# test required
# print(mock.copy())
self.assertRaises(FieldError, mock.save)
def test_enum_field(self):
""" Test the enum field """
class EnumModel(Model):
field = EnumField((1, 3, "what"))
mock = EnumModel()
mock.field = 3
self.assertEqual(mock.field, 3)
self.assertRaises(FieldError, setattr, mock, "field", 2)
def test_const_field(self):
class ConstField(Model):
field = ConstantField(str)
mock = ConstField()
self.assertEqual(mock.field, mago.UnSet)
mock.field = "testing"
self.assertEqual(mock.field, "testing")
mock.field = "keep testing"
self.assertEqual(mock.field, "keep testing")
mock.save()
self.assertEqual(mock.field, "keep testing")
self.assertRaises(FieldError, setattr, mock, "field", "I'm jumping!")
self.assertRaises(FieldError, delattr, mock, "field")
mock = ConstField.find_one({"field": "keep testing"})
self.assertEqual(mock.field, "keep testing")
self.assertRaises(FieldError, setattr, mock, "field", "jumping again!")
self.assertRaises(FieldError, delattr, mock, "field")
# def test_change_field_name(self):
# """It should allow an override of a field's name."""
# class MockModel(Model):
# abbreviated = Field(str, field_name="abrv")
# long_name = Field(str, field_name="ln", required=True)
# regular = Field(str, required=True)
# model = MockModel(
# abbreviated=u"lorem ipsum", long_name=u"malarky", regular=u"meh.")
# # Check the model's dictionary.
# self.assertTrue("abrv" in model)
# # Access by friendly name.
# self.assertEqual(u"lorem ipsum", model.abbreviated)
# # No access by field_name.
# self.assertRaises(AttributeError, getattr, model, "abrv")
# # Test save.
# model.save(safe=True)
# # Test search.
# fetched = MockModel.search(abbreviated=u"lorem ipsum")
# self.assertIsNotNone(fetched)
# fetched = MockModel.search(long_name=u"malarky")
# self.assertIsNotNone(fetched)
# # Test updates with long names.
# model.update(abbreviated=u"dolor set")
# self.assertEqual(u"dolor set", model.abbreviated)
# fetched = MockModel.search(abbreviated=u"dolor set")
# self.assertEqual(1, fetched.count())
# model.update(long_name=u"foobar")
# self.assertEqual(u"foobar", model.long_name)
# fetched = MockModel.search(long_name=u"foobar")
# self.assertEqual(1, fetched.count())
# # Test updates with short names.
# MockModel.update({}, {"$set": {"abrv": u"revia"}})
# fetched = MockModel.find_one({"abrv": "revia"})
# self.assertEqual(fetched.abbreviated, "revia")
# # Test finds with short names.
# fetched = MockModel.find({"ln": "foobar"})
# self.assertEqual(1, fetched.count())
# fetched = fetched.first()
# self.assertEqual(u"revia", fetched.abbreviated)
# # Test search on regular fields.
# fetched = MockModel.search(regular=u"meh.")
# self.assertEqual(1, fetched.count())
# def test_default_field(self):
# """ Test that the default behavior works like you'd expect. """
# class TestDefaultModel(Model):
# field = Field() # i.e. no default value
# entry = TestDefaultModel()
# self.assertFalse("field" in entry)
# self.assertEqual(None, entry.field)
# self.assertFalse("field" in entry)
# class TestDefaultModel2(Model):
# field = Field(default=None)
# entry2 = TestDefaultModel2()
# self.assertTrue("field" in entry2)
# self.assertEqual(None, entry2.field)
# self.assertEqual(None, entry2["field"])
# entry3 = TestDefaultModel2(field="foobar")
# self.assertEqual("foobar", entry3.field)
# self.assertEqual("foobar", entry3["field"])
if __name__ == "__main__":
unittest.main()