77import tarantool
88from .lib .tarantool_server import TarantoolServer
99
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+
1037class TestSuite_Schema_Abstract (unittest .TestCase ):
1138 # Define 'encoding' field in a concrete class.
1239
@@ -27,6 +54,25 @@ def setUp(self):
2754 if self .srv .is_started ():
2855 self .srv .touch_lock ()
2956
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+
3076 def test_00_authenticate (self ):
3177 self .assertIsNone (self .srv .admin ("box.schema.user.create('test', { password = 'test' })" ))
3278 self .assertIsNone (self .srv .admin ("box.schema.user.grant('test', 'read,write', 'space', '_space')" ))
@@ -105,6 +151,9 @@ def test_04_space_cached(self):
105151 self .assertEqual (space .name , '_index' )
106152 self .assertEqual (space .arity , 1 )
107153
154+ # Verify that no schema fetches occurs.
155+ self .assertEqual (self .fetch_count , 0 )
156+
108157 def test_05_01_index_name___name__ (self ):
109158 self .con .flush_schema ()
110159 index = self .sch .get_index ('_index' , 'primary' )
@@ -219,6 +268,9 @@ def test_06_index_cached(self):
219268 self .assertEqual (index .name , 'name' )
220269 self .assertEqual (len (index .parts ), 1 )
221270
271+ # Verify that no schema fetches occurs.
272+ self .assertEqual (self .fetch_count , 0 )
273+
222274 def test_07_schema_version_update (self ):
223275 _space_len = len (self .con .select ('_space' ))
224276 self .srv .admin ("box.schema.create_space('ttt22')" )
0 commit comments