@@ -21,21 +21,29 @@ def __init__(self):
2121 self .model_path = 'test.json'
2222 if path .exists (self .model_path ):
2323 os .remove (self .model_path )
24- self .model = None
24+ self ._model = None
2525 self .db_path = 'testdb'
2626 Store .remove_db_files (self .db_path )
27+ self ._store = None # Last created store
2728
2829 def sync (self , model : Model ) -> bool :
2930 """ Returns True if changes were made and the model JSON was written. """
30- self .model = model
31- return sync_model (self .model , self .model_path )
31+ self ._model = model
32+ return sync_model (self ._model , self .model_path )
3233
3334 def json (self ):
3435 return json .load (open (self .model_path ))
3536
36- def store (self ):
37- assert self .model is not None
38- return Store (model = self .model , directory = self .db_path )
37+ def create_store (self ):
38+ assert self ._model is not None , "Model must be set before creating store"
39+ if self ._store is not None :
40+ self ._store .close ()
41+ self ._store = Store (model = self ._model , directory = self .db_path )
42+ return self ._store
43+
44+ def close (self ):
45+ if self ._store is not None :
46+ self ._store .close ()
3947
4048
4149def reset_ids (entity : _Entity ):
@@ -48,7 +56,9 @@ def reset_ids(entity: _Entity):
4856
4957@pytest .fixture
5058def env ():
51- return _TestEnv ()
59+ env_ = _TestEnv ()
60+ yield env_
61+ env_ .close ()
5262
5363
5464def test_empty_model (env ):
@@ -136,13 +146,11 @@ class MyEntity:
136146 entity_ids = str (MyEntity ._iduid )
137147
138148 # create new database and populate with two objects
139- store = env .store ()
149+ store = env .create_store ()
140150 entityBox = store .box (MyEntity )
141151 entityBox .put (MyEntity (name = "foo" ),MyEntity (name = "bar" ))
142152 assert entityBox .count () == 2
143153 del entityBox
144- store .close ()
145- del store
146154
147155 # recreate model using existing model json and open existing database
148156 model = Model ()
@@ -156,7 +164,7 @@ class MyEntity:
156164 assert str (model .entities [0 ]._iduid ) == entity_ids
157165
158166 # open existing database
159- store = env .store ()
167+ store = env .create_store ()
160168 entityBox = store .box (MyEntity )
161169 assert entityBox .count () == 2
162170
@@ -169,12 +177,10 @@ class MyEntity1:
169177 model .entity (MyEntity1 )
170178 env .sync (model )
171179 e0_iduid = IdUid (MyEntity1 ._id , MyEntity1 ._uid )
172- store = env .store ()
180+ store = env .create_store ()
173181 box = store .box (MyEntity1 )
174182 box .put ( MyEntity1 (name = "foo" ), MyEntity1 (name = "bar" ))
175183 assert box .count () == 2
176- store .close ()
177- del store
178184
179185 @Entity ()
180186 class MyEntity2 :
@@ -188,7 +194,7 @@ class MyEntity2:
188194 assert str (model .entities [0 ]._iduid ) == "0:0"
189195 env .sync (model )
190196 assert model .entities [0 ]._iduid == e0_iduid
191- store = env .store ()
197+ store = env .create_store ()
192198 box1 = store .box (MyEntity1 )
193199 assert box1 .count () == 2
194200 box2 = store .box (MyEntity2 )
@@ -209,24 +215,21 @@ class MyEntity2:
209215 model .entity (MyEntity1 )
210216 model .entity (MyEntity2 )
211217 env .sync (model )
212- store = env .store ()
218+ store = env .create_store ()
213219 box1 = store .box (MyEntity1 )
214220 box1 .put ( MyEntity1 (name = "foo" ), MyEntity1 (name = "bar" ))
215221 box2 = store .box (MyEntity2 )
216222 box2 .put ( MyEntity2 (name = "foo" ), MyEntity2 (name = "bar" ))
217223 assert box1 .count () == 2
218224 assert box2 .count () == 2
219225
220- store .close ()
221- del store
222-
223226 # Re-create a model without MyEntity2
224227
225228 model = Model ()
226229 reset_ids (MyEntity1 )
227230 model .entity (MyEntity1 )
228231 env .sync (model )
229- store = env .store ()
232+ store = env .create_store ()
230233 box1 = store .box (MyEntity1 )
231234 assert box1 .count () == 2
232235
@@ -248,13 +251,11 @@ class MyEntity:
248251 assert uid != 0
249252 # Debug: print("UID: "+ str(uid))
250253
251- store = env .store ()
254+ store = env .create_store ()
252255 box = store .box (MyEntity )
253256 box .put (MyEntity (name = "foo" ),MyEntity (name = "bar" ))
254257 assert box .count () == 2
255258 del box
256- store .close ()
257- del store
258259
259260 @Entity (uid = uid )
260261 class MyRenamedEntity :
@@ -264,7 +265,7 @@ class MyRenamedEntity:
264265 model = Model ()
265266 model .entity (MyRenamedEntity )
266267 env .sync (model )
267- store = env .store ()
268+ store = env .create_store ()
268269 box = store .box (MyRenamedEntity )
269270 assert box .count () == 2
270271
@@ -316,12 +317,10 @@ class MyEntity:
316317 model = Model ()
317318 model .entity (MyEntity )
318319 env .sync (model )
319- store = env .store ()
320+ store = env .create_store ()
320321 box = store .box (MyEntity )
321322 box .put ( MyEntity (name = "foo" ), MyEntity (name = "bar" ))
322323 del box
323- store .close ()
324- del store
325324
326325 @Entity ()
327326 class MyEntity :
@@ -332,7 +331,7 @@ class MyEntity:
332331 model = Model ()
333332 model .entity (MyEntity )
334333 env .sync (model )
335- store = env .store ()
334+ store = env .create_store ()
336335 box = store .box (MyEntity )
337336
338337 assert box .count () == 2
@@ -348,12 +347,10 @@ class MyEntity:
348347 model = Model ()
349348 model .entity (MyEntity )
350349 env .sync (model )
351- store = env .store ()
350+ store = env .create_store ()
352351 box = store .box (MyEntity )
353352 box .put ( MyEntity (name = "foo" ), MyEntity (name = "bar" ))
354353 del box
355- store .close ()
356- del store
357354
358355 @Entity ()
359356 class MyEntity :
@@ -363,7 +360,7 @@ class MyEntity:
363360 model = Model ()
364361 model .entity (MyEntity )
365362 env .sync (model )
366- store = env .store ()
363+ store = env .create_store ()
367364 box = store .box (MyEntity )
368365 assert box .count () == 2
369366
@@ -377,7 +374,7 @@ class EntityA:
377374 model = Model ()
378375 model .entity (EntityA )
379376 env .sync (model )
380- store = env .store ()
377+ store = env .create_store ()
381378 box = store .box (EntityA )
382379 box .put (EntityA (name = "Luca" ))
383380 assert box .count () == 1
@@ -390,8 +387,6 @@ class EntityA:
390387 print (f"Entity.name ID/UID: { name .iduid } " )
391388
392389 del box # Close store
393- store .close ()
394- del store
395390
396391 # *** Rename ***
397392
@@ -403,7 +398,7 @@ class EntityA:
403398 model = Model ()
404399 model .entity (EntityA )
405400 env .sync (model )
406- store = env .store ()
401+ store = env .create_store ()
407402
408403 # Check ID/UID(s) are preserved after renaming
409404 entity2_iduid = EntityA ._iduid
0 commit comments