1
|
|
|
"""Test model properties.""" |
2
|
|
|
|
3
|
|
|
import pytest |
4
|
|
|
from gremlin_python.statics import long |
5
|
|
|
|
6
|
|
|
from goblin import element, exception, manager, properties |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
def test_set_change_property(person, lives_in): |
10
|
|
|
# vertex |
11
|
|
|
assert not person.name |
12
|
|
|
person.name = 'leif' |
13
|
|
|
assert person.name == 'leif' |
14
|
|
|
person.name = 'leifur' |
15
|
|
|
assert person.name == 'leifur' |
16
|
|
|
# edge |
17
|
|
|
assert not lives_in.notes |
18
|
|
|
lives_in.notes = 'notable' |
19
|
|
|
assert lives_in.notes == 'notable' |
20
|
|
|
lives_in.notes = 'more notable' |
21
|
|
|
assert lives_in.notes == 'more notable' |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
def test_property_default(knows): |
25
|
|
|
assert knows.notes == 'N/A' |
26
|
|
|
knows.notes = 'notable' |
27
|
|
|
assert knows.notes == 'notable' |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
def test_false_bool_default(place): |
31
|
|
|
assert place.incorporated.value is False |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
def test_validation(person): |
35
|
|
|
person.age = 10 |
36
|
|
|
with pytest.raises(Exception): |
37
|
|
|
person.age = 'hello' |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
def test_setattr_validation(person): |
41
|
|
|
setattr(person, 'age', 10) |
42
|
|
|
assert person.age == 10 |
43
|
|
|
with pytest.raises(Exception): |
44
|
|
|
setattr(person, 'age', 'hello') |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
def test_set_id_long(person): |
48
|
|
|
person.id = 1 |
49
|
|
|
assert isinstance(person.id, long) |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
def test_id_class_attr_throws(person_class): |
53
|
|
|
with pytest.raises(exception.ElementError): |
54
|
|
|
person_class.id |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
# Vertex properties |
58
|
|
|
def test_set_change_vertex_property(person): |
59
|
|
|
assert not person.birthplace |
60
|
|
|
person.birthplace = 'Iowa City' |
61
|
|
|
assert isinstance(person.birthplace, element.VertexProperty) |
62
|
|
|
assert person.birthplace.value == 'Iowa City' |
63
|
|
|
person.birthplace = 'U of I Hospital' |
64
|
|
|
assert person.birthplace.value == 'U of I Hospital' |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
def test_vertex_property_default(): |
68
|
|
|
"""Makes sure that a brand new VertexProperty (i.e., with no value set) is |
69
|
|
|
still representable. Addresses issue #52. |
70
|
|
|
""" |
71
|
|
|
vp = element.VertexProperty(int) |
72
|
|
|
assert repr(vp) == "<VertexProperty(type=0, value=None)" |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
def test_validate_vertex_prop(person): |
76
|
|
|
assert not person.birthplace |
77
|
|
|
person.birthplace = 1 |
78
|
|
|
assert person.birthplace.value == '1' |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
def test_set_change_list_card_vertex_property(person): |
82
|
|
|
assert not person.nicknames |
83
|
|
|
person.nicknames = 'sly' |
84
|
|
|
assert isinstance(person.nicknames, list) |
85
|
|
|
assert isinstance(person.nicknames, manager.ListVertexPropertyManager) |
86
|
|
|
assert isinstance(person.nicknames[0], element.VertexProperty) |
87
|
|
|
assert person.nicknames[0].value == 'sly' |
88
|
|
|
assert person.nicknames('sly') == person.nicknames[0] |
89
|
|
|
person.nicknames = set(['sly', 'guy']) |
90
|
|
|
assert isinstance(person.nicknames, list) |
91
|
|
|
assert person.nicknames('sly').value == 'sly' |
92
|
|
|
assert person.nicknames('guy').value == 'guy' |
93
|
|
|
person.nicknames = ('sly', 'big', 'guy') |
94
|
|
|
assert isinstance(person.nicknames, list) |
95
|
|
|
assert [v.value for v in person.nicknames] == ['sly', 'big', 'guy'] |
96
|
|
|
person.nicknames = ['sly', 'big', 'guy', 'guy'] |
97
|
|
|
assert isinstance(person.nicknames, list) |
98
|
|
|
assert len(person.nicknames('guy')) == 2 |
99
|
|
|
assert [v.value for v in person.nicknames] == ['sly', 'big', 'guy', 'guy'] |
100
|
|
|
person.nicknames.append(1) |
101
|
|
|
assert person.nicknames('1').value == '1' |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
def test_list_card_vertex_property_validation(person): |
105
|
|
|
person.nicknames = [1, 1.5, 2] |
106
|
|
|
assert [v.value for v in person.nicknames] == ['1', '1.5', '2'] |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
def test_set_change_set_card_vertex_property(place): |
110
|
|
|
assert not place.important_numbers |
111
|
|
|
place.important_numbers = 1 |
112
|
|
|
assert isinstance(place.important_numbers, set) |
113
|
|
|
assert isinstance(place.important_numbers, |
114
|
|
|
manager.SetVertexPropertyManager) |
115
|
|
|
number_one, = place.important_numbers |
116
|
|
|
assert isinstance(number_one, element.VertexProperty) |
117
|
|
|
assert number_one.value == 1 |
118
|
|
|
assert place.important_numbers(1) == number_one |
119
|
|
|
place.important_numbers = [1, 2] |
120
|
|
|
assert isinstance(place.important_numbers, set) |
121
|
|
|
assert {v.value for v in place.important_numbers} == set([1, 2]) |
122
|
|
|
place.important_numbers.add(3) |
123
|
|
|
assert {v.value for v in place.important_numbers} == set([1, 2, 3]) |
124
|
|
|
place.important_numbers = (1, 2, 3, 4) |
125
|
|
|
assert isinstance(place.important_numbers, set) |
126
|
|
|
assert {v.value for v in place.important_numbers} == set([1, 2, 3, 4]) |
127
|
|
|
place.important_numbers = set([1, 2, 3]) |
128
|
|
|
assert isinstance(place.important_numbers, set) |
129
|
|
|
assert {v.value for v in place.important_numbers} == set([1, 2, 3]) |
130
|
|
|
with pytest.raises(exception.ValidationError): |
131
|
|
|
place.important_numbers.add('dude') |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
def test_set_card_union(place): |
135
|
|
|
place.important_numbers = set([1, 2, 3]) |
136
|
|
|
place.important_numbers = place.important_numbers.union({3, 4, 5}) |
137
|
|
|
|
138
|
|
|
|
139
|
|
|
def test_set_card_64bit_integer(place): |
140
|
|
|
place.important_numbers = set([long(1), long(2), long(3)]) |
141
|
|
|
assert all(isinstance(i.value, long) for i in place.important_numbers) |
142
|
|
|
|
143
|
|
|
|
144
|
|
|
def test_set_card_validation_vertex_property(place): |
145
|
|
|
with pytest.raises(exception.ValidationError): |
146
|
|
|
place.important_numbers = set(['hello', 2, 3]) |
147
|
|
|
|
148
|
|
|
|
149
|
|
|
def test_cant_set_vertex_prop_on_edge(): |
150
|
|
|
with pytest.raises(exception.MappingError): |
151
|
|
|
|
152
|
|
|
class MyEdge(element.Edge): |
153
|
|
|
vert_prop = element.VertexProperty(properties.String) |
154
|
|
|
|
155
|
|
|
|
156
|
|
|
def test_meta_property_set_update(place): |
157
|
|
|
assert not place.historical_name |
158
|
|
|
place.historical_name = ['hispania', 'al-andalus'] |
159
|
|
|
place.historical_name('hispania').notes = 'roman rule' |
160
|
|
|
assert place.historical_name('hispania').notes == 'roman rule' |
161
|
|
|
place.historical_name('hispania').year = 300 |
162
|
|
|
assert place.historical_name('hispania').year == 300 |
163
|
|
|
place.historical_name('al-andalus').notes = 'muslim rule' |
164
|
|
|
assert place.historical_name('al-andalus').notes == 'muslim rule' |
165
|
|
|
place.historical_name('al-andalus').year = 700 |
166
|
|
|
assert place.historical_name('al-andalus').year == 700 |
167
|
|
|
|
168
|
|
|
|
169
|
|
|
def test_meta_property_validation(place): |
170
|
|
|
assert not place.historical_name |
171
|
|
|
place.historical_name = ['spain'] |
172
|
|
|
with pytest.raises(exception.ValidationError): |
173
|
|
|
place.historical_name('spain').year = 'hello' |
174
|
|
|
|
175
|
|
|
|
176
|
|
|
class TestString: |
177
|
|
|
def test_validation(self, string): |
178
|
|
|
assert string.validate(1) == '1' |
179
|
|
|
|
180
|
|
|
def test_to_db(self, string): |
181
|
|
|
assert string.to_db('hello') == 'hello' |
182
|
|
|
|
183
|
|
|
def test_to_ogm(self, string): |
184
|
|
|
assert string.to_ogm('hello') == 'hello' |
185
|
|
|
|
186
|
|
|
def test_initval_to_db(self, string_class): |
187
|
|
|
string = string_class('hello') |
188
|
|
|
assert string.to_db() == 'hello' |
189
|
|
|
|
190
|
|
|
|
191
|
|
|
class TestInteger: |
192
|
|
|
def test_validation(self, integer): |
193
|
|
|
assert integer.validate('1') == 1 |
194
|
|
|
with pytest.raises(Exception): |
195
|
|
|
integer.validate('hello') |
196
|
|
|
|
197
|
|
|
def test_to_db(self, integer): |
198
|
|
|
assert integer.to_db(1) == 1 |
199
|
|
|
|
200
|
|
|
def test_to_ogm(self, integer): |
201
|
|
|
assert integer.to_db(1) == 1 |
202
|
|
|
|
203
|
|
|
def test_initval_to_db(self, integer_class): |
204
|
|
|
integer = integer_class(1) |
205
|
|
|
assert integer.to_db() == 1 |
206
|
|
|
|
207
|
|
|
|
208
|
|
|
class TestFloat: |
209
|
|
|
def test_validation(self, flt): |
210
|
|
|
assert flt.validate(1.2) == 1.2 |
211
|
|
|
with pytest.raises(Exception): |
212
|
|
|
flt.validate('hello') |
213
|
|
|
|
214
|
|
|
def test_to_db(self, flt): |
215
|
|
|
assert flt.to_db(1.2) == 1.2 |
216
|
|
|
|
217
|
|
|
def test_to_ogm(self, flt): |
218
|
|
|
assert flt.to_db(1.2) == 1.2 |
219
|
|
|
|
220
|
|
|
def test_initval_to_db(self, flt_class): |
221
|
|
|
flt = flt_class(1.2) |
222
|
|
|
assert flt.to_db() == 1.2 |
223
|
|
|
|
224
|
|
|
|
225
|
|
|
class TestBoolean: |
226
|
|
|
def test_validation_true(self, boolean): |
227
|
|
|
assert boolean.validate(True) |
228
|
|
|
|
229
|
|
|
def test_validation_false(self, boolean): |
230
|
|
|
assert not boolean.validate(False) |
231
|
|
|
|
232
|
|
|
def test_to_db_true(self, boolean): |
233
|
|
|
assert boolean.to_db(True) |
234
|
|
|
|
235
|
|
|
def test_to_db_false(self, boolean): |
236
|
|
|
assert not boolean.to_db(False) |
237
|
|
|
|
238
|
|
|
def test_to_ogm_true(self, boolean): |
239
|
|
|
assert boolean.to_ogm(True) |
240
|
|
|
|
241
|
|
|
def test_to_ogm_false(self, boolean): |
242
|
|
|
assert not boolean.to_ogm(False) |
243
|
|
|
|
244
|
|
|
def test_initval_to_db_true(self, boolean_class): |
245
|
|
|
boolean = boolean_class(True) |
246
|
|
|
assert boolean.to_db() |
247
|
|
|
|
248
|
|
|
def test_initval_to_db_true(self, boolean_class): |
249
|
|
|
boolean = boolean_class(False) |
250
|
|
|
assert not boolean.to_db() |
251
|
|
|
|