-
Notifications
You must be signed in to change notification settings - Fork 601
/
test_rest_object.py
63 lines (50 loc) · 1.85 KB
/
test_rest_object.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
import unittest
import sys
from tabpy.tabpy_tools.rest import RESTObject, RESTProperty, enum
class TestRESTObject(unittest.TestCase):
def test_new_class(self):
class FooObject(RESTObject):
f = RESTProperty(float)
i = RESTProperty(int)
s = RESTProperty(str)
e = RESTProperty(enum("a", "b"))
f = FooObject(f="6.0", i="3", s="hello!")
self.assertEqual(f.f, 6.0)
self.assertEqual(f.i, 3)
self.assertEqual(f.s, "hello!")
with self.assertRaises(AttributeError):
f.e
self.assertEqual(f["f"], 6.0)
self.assertEqual(f["i"], 3)
self.assertEqual(f["s"], "hello!")
with self.assertRaises(KeyError):
f["e"]
with self.assertRaises(KeyError):
f["cat"]
with self.assertRaises(KeyError):
f["cat"] = 5
self.assertEqual(len(f), 3)
self.assertEqual(set(f), set(["f", "i", "s"]))
self.assertEqual(set(f.keys()), set(["f", "i", "s"]))
self.assertEqual(set(f.values()), set([6.0, 3, "hello!"]))
self.assertEqual(set(f.items()), set([("f", 6.0), ("i", 3), ("s", "hello!")]))
f.e = "a"
self.assertEqual(f.e, "a")
self.assertEqual(f["e"], "a")
f["e"] = "b"
self.assertEqual(f.e, "b")
with self.assertRaises(ValueError):
f.e = "fubar"
f.f = sys.float_info.max
self.assertEqual(f.f, sys.float_info.max)
f.f = float("inf")
self.assertEqual(f.f, float("inf"))
f.f = None
self.assertEqual(f.f, None)
class BarObject(FooObject):
x = RESTProperty(str)
f = BarObject(f="6.0", i="3", s="hello!", x="5")
self.assertEqual(f.f, 6.0)
self.assertEqual(f.i, 3)
self.assertEqual(f.s, "hello!")
self.assertEqual(f.x, "5")