-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathtest_util.py
More file actions
89 lines (66 loc) · 2.03 KB
/
Copy pathtest_util.py
File metadata and controls
89 lines (66 loc) · 2.03 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
# coding: utf-8
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import time
from nose.tools import eq_
from leancloud import ACL
from leancloud import GeoPoint
from leancloud import Object
from leancloud import utils
__author__ = "asaka <lan@leancloud.rocks>"
def test_encode(): # type: () -> None
Foo = Object.extend("Foo")
obj = Foo()
assert utils.encode(obj) == {
"className": "Foo",
"__type": "Pointer",
"objectId": None,
}
acl = ACL()
assert utils.encode(acl) == {}
acl.set_read_access("xxx", True)
assert utils.encode(acl) == {"xxx": {"read": True}}
point = GeoPoint()
assert utils.encode(point) == {
"__type": "GeoPoint",
"longitude": 0,
"latitude": 0,
}
assert utils.encode([obj, acl, point]) == [
{"className": "Foo", "__type": "Pointer", "objectId": None},
{"xxx": {"read": True}},
{"__type": "GeoPoint", "longitude": 0, "latitude": 0},
]
assert utils.encode({"a": obj, "b": acl}) == {
"a": {"className": "Foo", "__type": "Pointer", "objectId": None},
"b": {"xxx": {"read": True}},
}
def test_decode(): # type: () -> None
p = utils.decode("test_key", {"__type": "GeoPoint", "longitude": 0, "latitude": 0})
assert isinstance(p, GeoPoint)
assert p.latitude == 0
assert p.longitude == 0
def test_util(): # type: () -> None
obj = Object.extend("Foo")()
def callback(o):
callback.count += 1
if callback.count == 1:
assert o == {}
elif callback.count == 2:
assert o == obj
callback.count = 0
utils.traverse_object(obj, callback)
assert callback.count == 2
def test_throttle(): # type: () -> None
env = {"life": 0}
@utils.throttle(seconds=1)
def plus_one_second():
env["life"] += 1
plus_one_second()
plus_one_second()
plus_one_second()
eq_(env["life"], 1)
time.sleep(2)
plus_one_second()
eq_(env["life"], 2)