-
Notifications
You must be signed in to change notification settings - Fork 2
/
tests.py
107 lines (84 loc) · 3.63 KB
/
tests.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
import unittest
from classproperties import *
def get_cls_definition():
class Cls(object):
@classproperty
def prop(cls):
return 1000
cached_prop_exec_count = 0
@cached_classproperty
def cached_prop(cls):
cls.cached_prop_exec_count += 1
return 2000
return Cls
class TestClassProperty(unittest.TestCase):
"""This class contains all tests of the library"""
def test_classproperty(self):
Cls = get_cls_definition()
self.assertEqual(Cls.prop, 1000)
def test_classproperty_instance(self):
Cls = get_cls_definition()
self.assertEqual(Cls().prop, 1000)
class TestCachedClassProperty(unittest.TestCase):
def test_cached_classproperty(self):
Cls = get_cls_definition()
self.assertEqual(Cls.cached_prop_exec_count, 0)
self.assertEqual(Cls.cached_prop, 2000)
self.assertEqual(Cls.cached_prop_exec_count, 1)
def test_cached_classproperty_executed_once(self):
Cls = get_cls_definition()
self.assertEqual(Cls.cached_prop_exec_count, 0)
self.assertEqual(Cls.cached_prop, 2000)
self.assertEqual(Cls.cached_prop_exec_count, 1)
self.assertEqual(Cls.cached_prop, 2000)
self.assertEqual(Cls.cached_prop_exec_count, 1)
def test_cached_classproperty_delete_cache(self):
Cls = get_cls_definition()
self.assertEqual(Cls.cached_prop_exec_count, 0)
self.assertEqual(Cls.cached_prop, 2000)
self.assertEqual(Cls.cached_prop_exec_count, 1)
cached_classproperty.remove_cached_value(Cls, 'cached_prop')
self.assertEqual(Cls.cached_prop, 2000)
self.assertEqual(Cls.cached_prop_exec_count, 2)
def test_cached_classproperty_instance(self):
Cls = get_cls_definition()
inst = Cls()
self.assertEqual(inst.cached_prop_exec_count, 0)
self.assertEqual(inst.cached_prop, 2000)
self.assertEqual(inst.cached_prop_exec_count, 1)
def test_cached_classproperty_instance_with_deletion(self):
Cls = get_cls_definition()
inst = Cls()
self.assertEqual(inst.cached_prop_exec_count, 0)
self.assertEqual(inst.cached_prop, 2000)
inst.cached_prop = 3000
self.assertEqual(Cls.cached_prop, 2000)
self.assertEqual(inst.cached_prop, 3000)
del inst.cached_prop
self.assertEqual(Cls.cached_prop, 2000)
self.assertEqual(Cls.cached_prop_exec_count, 1)
def test_cached_classproperty_executed_once_instance(self):
Cls = get_cls_definition()
inst = Cls()
self.assertEqual(inst.cached_prop_exec_count, 0)
self.assertEqual(inst.cached_prop, 2000)
self.assertEqual(inst.cached_prop_exec_count, 1)
self.assertEqual(inst.cached_prop, 2000)
self.assertEqual(inst.cached_prop_exec_count, 1)
def test_cached_classproperty_executed_once_instance_cls_share_same_cached_val(self):
Cls = get_cls_definition()
inst = Cls()
self.assertEqual(inst.cached_prop_exec_count, 0)
self.assertEqual(inst.cached_prop, 2000)
self.assertEqual(inst.cached_prop_exec_count, 1)
self.assertEqual(inst.cached_prop, 2000)
self.assertEqual(inst.cached_prop_exec_count, 1)
self.assertEqual(Cls.cached_prop_exec_count, 1)
self.assertEqual(Cls.cached_prop, 2000)
self.assertEqual(Cls.cached_prop_exec_count, 1)
inst = Cls()
self.assertEqual(inst.cached_prop_exec_count, 1)
self.assertEqual(inst.cached_prop, 2000)
self.assertEqual(inst.cached_prop_exec_count, 1)
if __name__ == '__main__':
unittest.main()