Skip to content

Commit ebd2926

Browse files
committed
Merge branch '57-segfault-on-model-reuse-in-multiple-stores' into 'dev'
Resolve "Segfault on Model reuse in multiple Stores" Closes #57 See merge request objectbox/objectbox-python!42
2 parents 3e42a16 + d7918fd commit ebd2926

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

tests/test_model.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
from objectbox.model import *
2+
from objectbox import *
3+
from objectbox.model.idsync import sync_model
4+
import os
5+
import os.path
6+
7+
def test_reuse_model():
8+
@Entity()
9+
class MyEntity:
10+
id = Id()
11+
name = String()
12+
13+
model = Model()
14+
model.entity(MyEntity)
15+
model_filepath = "test-model.json"
16+
if os.path.exists(model_filepath):
17+
os.remove(model_filepath)
18+
sync_model(model, model_filepath)
19+
20+
db1path = "test-db1"
21+
db2path = "test-db2"
22+
Store.remove_db_files(db1path)
23+
Store.remove_db_files(db2path)
24+
25+
store1 = Store(model=model, directory=db1path)
26+
store2 = Store(model=model, directory=db2path)
27+
28+
store1.close()
29+
store2.close()
30+
31+
32+
def test_reuse_entity():
33+
@Entity()
34+
class MyEntity:
35+
id = Id()
36+
name = String()
37+
38+
m1 = Model()
39+
m1.entity(MyEntity)
40+
model_filepath = "test-model1.json"
41+
if os.path.exists(model_filepath):
42+
os.remove(model_filepath)
43+
sync_model(m1, model_filepath)
44+
45+
db1path = "test-db1"
46+
db2path = "test-db2"
47+
Store.remove_db_files(db1path)
48+
Store.remove_db_files(db2path)
49+
50+
store1 = Store(model=m1, directory=db1path)
51+
52+
box1 = store1.box(MyEntity)
53+
box1.put(MyEntity(name="foo"))
54+
assert box1.count() == 1
55+
56+
m2 = Model()
57+
58+
@Entity()
59+
class MyEntity2:
60+
id = Id()
61+
name = String()
62+
value = Int64()
63+
64+
m2.entity(MyEntity2)
65+
m2.entity(MyEntity)
66+
model_filepath = "test-model2.json"
67+
if os.path.exists(model_filepath):
68+
os.remove(model_filepath)
69+
sync_model(m2, model_filepath)
70+
71+
store2 = Store(model=m2, directory=db2path)
72+
box2 = store2.box(MyEntity)
73+
box2.put(MyEntity(name="bar"))
74+
box2.put(MyEntity(name="bar"))
75+
box2.put(MyEntity(name="bar"))
76+
assert box2.count() == 3
77+
78+
box1.put(MyEntity(name="foo"))
79+
box1.put(MyEntity(name="foo"))
80+
assert box1.count() == 3
81+
82+
store1.close()
83+
store2.close()

0 commit comments

Comments
 (0)