7
7
import tarantool
8
8
from .lib .tarantool_server import TarantoolServer
9
9
10
+
11
+ # FIXME: I'm quite sure that there is a simpler way to count
12
+ # a method calls, but I failed to find any. It seems, I should
13
+ # look at unittest.mock more thoroughly.
14
+ class MethodCallCounter :
15
+ def __init__ (self , obj , method_name ):
16
+ self ._call_count = 0
17
+ self ._bind (obj , method_name )
18
+
19
+ def _bind (self , obj , method_name ):
20
+ self ._obj = obj
21
+ self ._method_name = method_name
22
+ self ._saved_method = getattr (obj , method_name )
23
+ def wrapper (_ , * args , ** kwargs ):
24
+ self ._call_count += 1
25
+ return self ._saved_method (* args , ** kwargs )
26
+ bound_wrapper = wrapper .__get__ (obj .__class__ , obj )
27
+ setattr (obj , method_name , bound_wrapper )
28
+
29
+ def unbind (self ):
30
+ if self ._saved_method is not None :
31
+ setattr (self ._obj , self ._method_name , self ._saved_method )
32
+
33
+ def call_count (self ):
34
+ return self ._call_count
35
+
36
+
10
37
class TestSuite_Schema_Abstract (unittest .TestCase ):
11
38
# Define 'encoding' field in a concrete class.
12
39
@@ -27,6 +54,25 @@ def setUp(self):
27
54
if self .srv .is_started ():
28
55
self .srv .touch_lock ()
29
56
57
+ # Count calls of fetch methods. See <fetch_count>.
58
+ self .fetch_space_counter = MethodCallCounter (self .sch , 'fetch_space' )
59
+ self .fetch_index_counter = MethodCallCounter (self .sch , 'fetch_index' )
60
+
61
+ def tearDown (self ):
62
+ self .fetch_space_counter .unbind ()
63
+ self .fetch_index_counter .unbind ()
64
+
65
+ @property
66
+ def fetch_count (self ):
67
+ """Amount of fetch_{space,index}() calls.
68
+
69
+ It is initialized to zero before each test case.
70
+ """
71
+ res = 0
72
+ res += self .fetch_space_counter .call_count ()
73
+ res += self .fetch_index_counter .call_count ()
74
+ return res
75
+
30
76
def test_00_authenticate (self ):
31
77
self .assertIsNone (self .srv .admin ("box.schema.user.create('test', { password = 'test' })" ))
32
78
self .assertIsNone (self .srv .admin ("box.schema.user.grant('test', 'read,write', 'space', '_space')" ))
@@ -105,6 +151,9 @@ def test_04_space_cached(self):
105
151
self .assertEqual (space .name , '_index' )
106
152
self .assertEqual (space .arity , 1 )
107
153
154
+ # Verify that no schema fetches occurs.
155
+ self .assertEqual (self .fetch_count , 0 )
156
+
108
157
def test_05_01_index_name___name__ (self ):
109
158
self .con .flush_schema ()
110
159
index = self .sch .get_index ('_index' , 'primary' )
@@ -219,6 +268,9 @@ def test_06_index_cached(self):
219
268
self .assertEqual (index .name , 'name' )
220
269
self .assertEqual (len (index .parts ), 1 )
221
270
271
+ # Verify that no schema fetches occurs.
272
+ self .assertEqual (self .fetch_count , 0 )
273
+
222
274
def test_07_schema_version_update (self ):
223
275
_space_len = len (self .con .select ('_space' ))
224
276
self .srv .admin ("box.schema.create_space('ttt22')" )
0 commit comments