-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathtest_object.py
More file actions
400 lines (298 loc) · 9.84 KB
/
test_object.py
File metadata and controls
400 lines (298 loc) · 9.84 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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# coding: utf-8
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
from datetime import datetime
from dateutil import tz
from nose.tools import assert_equal # type: ignore
from nose.tools import assert_raises # type: ignore
from nose.tools import eq_ # type: ignore
from nose.tools import ok_ # type: ignore
from nose.tools import with_setup # type: ignore
import leancloud
from leancloud import Object
from leancloud import Query
__author__ = "asaka <lan@leancloud.rocks>"
def setup_func():
leancloud.client.USE_MASTER_KEY = None
leancloud.client.APP_ID = None
leancloud.client.APP_KEY = None
leancloud.client.MASTER_KEY = None
leancloud.init(os.environ["APP_ID"], os.environ["APP_KEY"])
class Album(Object):
pass
class Band(Object):
pass
def test_new(): # type: () -> None
album = Album()
assert album._class_name == "Album"
def test_class_equal(): # type: () -> None
AnotherAlbum = Object.extend("Album")
assert AnotherAlbum is Album
album = AnotherAlbum()
assert isinstance(album, AnotherAlbum)
def test_dirty(): # type: () -> None
album = Album()
assert album.is_dirty() is True
album.id = "123"
assert album.is_dirty() is False
album.set("foo", "bar")
assert album.is_dirty() is True
def test_find_unsaved_children(): # type: ignore
album = Album()
unsaved_children = []
unsaved_files = []
Object._find_unsaved_children(album, unsaved_children, unsaved_files)
assert unsaved_children == [album]
assert unsaved_files == []
def test_find_unsaved_children_2(): # type: ignore
album = Album()
band = Band()
album.set("band", band)
unsaved_children = []
unsaved_files = []
Object._find_unsaved_children(album, unsaved_children, unsaved_files)
assert unsaved_children == [band, album]
def test_set(): # type: () -> None
album = Album()
album.set("title", "Nightwish")
eq_(album._attributes, {"title": "Nightwish"})
album = Album(title="Nightwish")
eq_(album._attributes, {"title": "Nightwish"})
def test_get(): # type: () -> None
album = Album()
album.set("foo", "bar")
assert album.get("foo") == "bar"
def test_get_default(): # type: () -> None
album = Album()
assert album.get("foo", "bar") == "bar"
assert album.get("foo", default="bar") == "bar"
# for backward compatibility
assert album.get("foo", deafult="bar") == "bar"
assert album.get("foo", "bar", deafult="foobar") == "bar"
assert album.get("foo", deafult="foobar", default="bar") == "bar"
def test_unset(): # type: () -> None
album = Album()
album.set("foo", "bar")
album.unset("foo")
assert album.get("foo") is None
assert album.has("foo") is False
def test_increment(): # type: () -> None
album = Album()
album.set("foo", 1)
album.increment("foo", 1)
assert album.get("foo") == 2
@with_setup(setup_func)
def test_bit_operation(): # type: () -> None
album = Album()
album.set("flags", 0b0)
album.bit_and("flags", 0b1)
assert_equal(album.get("flags"), 0b0)
album.save()
assert_equal(album.get("flags"), 0b0)
album.bit_or("flags", 0b1)
assert_equal(album.get("flags"), 0b1)
album.save()
assert_equal(album.get("flags"), 0b1)
album.bit_xor("flags", 0b10)
assert_equal(album.get("flags"), 0b11)
album.save()
assert_equal(album.get("flags"), 0b11)
@with_setup(setup_func)
def test_increment_atfer_save(): # type: () -> None
album = Album()
album.set("foo", 1)
album.save()
album.increment("foo", 234)
assert album.get("foo") == 235
album.save()
assert album.get("foo") == 235
def test_add(): # type: () -> None
album = Album()
album.add("foo", 1)
eq_(album.get("foo"), [1])
album.add("foo", 2)
eq_(album.get("foo"), [1, 2])
def test_add_unique(): # type: () -> None
album = Album()
album.add_unique("foo", 1)
album.add_unique("foo", 1)
eq_(album.get("foo"), [1])
album.add_unique("foo", 2)
eq_(album.get("foo"), [1, 2])
def test_remove(): # type: () -> None
album = Album()
album.set("foo", ["bar", "baz"])
album.remove("foo", "bar")
eq_(album.get("foo"), ["baz"])
def test_clear(): # type: () -> None
album = Album(foo=1, bar=2, baz=3)
album.clear()
assert album.get("foo") is None
assert album.get("bar") is None
assert album.get("baz") is None
def test_full_dump(): # type: ignore
album = Album()
album.set("title", "Nightwish")
assert album._dump() == {
"className": "Album",
"__type": "Object",
"title": "Nightwish",
}
band = Band()
album.set("band", band)
assert album._dump() == {
"className": "Album",
"band": {"className": "Band", "__type": "Pointer", "objectId": None},
"__type": "Object",
"title": "Nightwish",
}
def test_dump_save(): # type: ignore
album = Album()
album.set("foo", "bar")
eq_(album._dump_save(), {"foo": "bar"})
def test_extend(): # type: () -> None
ok_(Object.extend("Album"))
def test_update_data(): # type: ignore
album = Album()
album._update_data({"title": "Once", "artist": "nightwish"})
eq_(album._attributes, {"title": "Once", "artist": "nightwish"})
def test_dump(): # type: () -> None
album = Album()
album.set("foo", "bar")
eq_(album.dump(), {"foo": "bar"})
def test_to_pointer(): # type: ignore
album = Album()
album.set("foo", "bar")
album._to_pointer()
@with_setup(setup_func)
def test_fetch(): # type: () -> None
album = Album(title="Once")
band = Band(name="Nightwish")
album.set("parent", band)
album.save()
album_1 = Album.create_without_data(album.id)
assert album_1.is_existed() is False
album_1.fetch(include=["parent"], select=["name", "parent"])
assert album_1.is_existed() is True
assert album_1.get("parent").get("name") == "Nightwish"
assert not album_1.has("title")
album.destroy()
band.destroy()
def test_has(): # type: () -> None
album = Album()
album.set("foo", "bar")
assert album.has("foo") is True
assert album.has("bar") is False
def test_existence(): # type: () -> None
album = Album()
assert album.is_existed() is False
def test_get_set_acl(): # type: () -> None
acl = leancloud.ACL()
album = Album()
album.set_acl(acl)
assert album.get_acl() == acl
def test_invalid_acl(): # type: () -> None
album = Album()
assert_raises(TypeError, album.set, "ACL", 1)
assert_raises(TypeError, album.set_acl, 1)
@with_setup(setup_func)
def test_relation(): # type: () -> None
album = Album()
band = Band()
band.save()
album.relation("band")
relation = album.relation("band")
relation.add(band)
album.save()
@with_setup(setup_func)
def test_pointer(): # type: () -> None
user = leancloud.User.create_without_data("555ed132e4b032867865884e")
score = leancloud.Object.extend("score")
s = score()
s.set("user", user)
s.save()
@with_setup(setup_func)
def test_save_and_destroy_all(): # type: () -> None
ObjToDelete = Object.extend("ObjToDelete")
objs = [ObjToDelete() for _ in range(3)]
already_saved_obj = ObjToDelete()
already_saved_obj.save()
objs.append(already_saved_obj)
Object.save_all(objs)
assert all(not x.is_new() for x in objs)
Object.destroy_all(objs)
for obj in objs:
try:
leancloud.Query(ObjToDelete).get(obj.id)
except leancloud.LeanCloudError as e:
assert e.code == 101
@with_setup(setup_func)
def test_fetch_when_save(): # type: () -> None
Foo = Object.extend("Foo")
foo = Foo()
foo.set("counter", 1)
foo.save()
assert foo.created_at == foo.updated_at
assert foo.get("counter") == 1
foo_from_other_thread = leancloud.Query(Foo).get(foo.id)
assert foo_from_other_thread.get("counter") == 1
foo_from_other_thread.set("counter", 100)
foo_from_other_thread.save()
foo.increment("counter", 3)
foo.save(fetch_when_save=True)
eq_(foo.get("counter"), 103)
foo.destroy()
@with_setup(setup_func)
def test_save_with_where(): # type: () -> None
Foo = Object.extend("Foo")
foo = Foo(aNumber=1)
assert_raises(TypeError, foo.save, where=Foo.query) # type: ignore
assert_raises(TypeError, foo.save, where=leancloud.Query("SomeClassNotEqualToFoo"))
foo.save()
foo.set("aNumber", 2)
try:
foo.save(where=leancloud.Query("Foo").equal_to("aNumber", 2))
except leancloud.LeanCloudError as e:
assert e.code == 305
foo.save(where=leancloud.Query("Foo").equal_to("aNumber", 1))
assert leancloud.Query("Foo").get(foo.id).get("aNumber") == 2
@with_setup(setup_func)
def test_modify_class_name(): # type: () -> None
class Philosopher(Object):
class_name = "Teacher"
@Object.as_class("Student")
class Physicist(Object):
pass
plato = Philosopher()
plato.save()
aristotle = Physicist()
aristotle.save()
assert Query("Teacher").get(plato.id)
assert Query("Student").get(aristotle.id)
plato.destroy()
aristotle.destroy()
@with_setup(setup_func)
def test_create_without_data(): # type: () -> None
Foo = Object.extend("Foo")
foo1 = Foo(aNumber=2)
foo1.save()
foo2 = Foo.create_without_data(foo1.id)
foo2.set("aNumber", 3)
foo2.save()
assert foo1.id == foo2.id
assert Foo.query.get(foo1.id).get("aNumber") == 3
foo1.destroy()
@with_setup(setup_func)
def test_time_zone():
TestTimeZone = Object.extend("TestTimeZone")
now = datetime.now()
obj = TestTimeZone()
obj.set("date", now)
obj.save()
obj = TestTimeZone.query.get(obj.id)
assert obj.created_at.tzinfo == tz.tzlocal()
assert obj.updated_at.tzinfo == tz.tzlocal()
assert obj.get("date").tzinfo == tz.tzlocal()
obj.destroy()