Skip to content

Commit f516f26

Browse files
committed
tests/examples use store.box, added Box example to test_deprecated #44
1 parent c44c759 commit f516f26

10 files changed

Lines changed: 55 additions & 53 deletions

File tree

benchmark.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ class ObjectBoxPerf:
1010
"""
1111

1212
def __init__(self):
13-
self.ob = load_empty_test_default_store()
14-
self.box = objectbox.Box(self.ob, TestEntity)
13+
self.store = load_empty_test_default_store()
14+
self.box = store.box(TestEntity)
1515

1616
def remove_all(self):
1717
self.box.remove_all()

example/tasks/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def format_date(timestamp_ms: int) -> str:
1616
class TasklistCmd(Cmd):
1717
prompt = "> "
1818
_store = objectbox.Store(model=get_objectbox_model(), directory="tasklist-db")
19-
_box = objectbox.Box(_store, Task)
19+
_box = _store.box(Task)
2020

2121
def do_ls(self, _):
2222
"""list tasks"""

example/vectorsearch-cities/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def __init__(self, *args):
2424
dbdir = "cities-db"
2525
new_db = not os.path.exists(dbdir)
2626
self._store = objectbox.Store(model=get_objectbox_model(),directory=dbdir)
27-
self._box = objectbox.Box(self._store, City)
27+
self._box = _store.box(City)
2828
self._name_prop: Property = City.get_property("name")
2929
self._location_prop: Property = City.get_property("location")
3030
if new_db:

tests/test_box.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010

1111
def test_box_basics():
12-
ob = load_empty_test_default_store()
13-
box = objectbox.Box(ob, TestEntity)
12+
store = load_empty_test_default_store()
13+
box = store.box(TestEntity)
1414

1515
assert box.is_empty()
1616
assert box.count() == 0
@@ -89,12 +89,12 @@ def test_box_basics():
8989
assert box.get(object.id) == None
9090
assert box.get(1) == None
9191

92-
ob.close()
92+
store.close()
9393

9494

9595
def test_box_bulk():
96-
ob = load_empty_test_default_store()
97-
box = objectbox.Box(ob, TestEntity)
96+
store = load_empty_test_default_store()
97+
box = store.box(TestEntity)
9898

9999
box.put(TestEntity(str="first"))
100100

@@ -124,12 +124,12 @@ def test_box_bulk():
124124
assert removed == 4
125125
assert box.count() == 0
126126

127-
ob.close()
127+
store.close()
128128

129129

130130
def test_datetime():
131-
ob = load_empty_test_datetime_store()
132-
box = objectbox.Box(ob, TestEntityDatetime)
131+
store = load_empty_test_datetime_store()
132+
box = store.box(TestEntityDatetime)
133133

134134
assert box.is_empty()
135135
assert box.count() == 0
@@ -177,7 +177,7 @@ def test_datetime():
177177
assert box.get(object.id) == None
178178
assert box.get(1) == None
179179

180-
ob.close()
180+
store.close()
181181

182182

183183
def test_flex():
@@ -188,8 +188,8 @@ def test_put_get(object: TestEntity, box: objectbox.Box, property):
188188
read = box.get(object.id)
189189
assert read.flex == object.flex
190190

191-
ob = load_empty_test_default_store()
192-
box = objectbox.Box(ob, TestEntity)
191+
store = load_empty_test_default_store()
192+
box = store.box(TestEntity)
193193
object = TestEntity()
194194

195195
# Put an empty object
@@ -226,13 +226,13 @@ def test_put_get(object: TestEntity, box: objectbox.Box, property):
226226
# Update to list inside dict
227227
test_put_get(object, box, {"a": 1, "b": [1, 2, 3]})
228228

229-
ob.close()
229+
store.close()
230230

231231

232232
def test_flex_values():
233-
ob = create_test_store()
233+
store = create_test_store()
234234

235-
box = objectbox.Box(ob, TestEntityFlex)
235+
box = store.box(TestEntityFlex)
236236

237237
# Test empty object
238238
obj_id = box.put(TestEntityFlex())

tests/test_deprecated.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ def test_deprecated_ObjectBox():
1111
c_store = objectbox.c.obx_store_open(options._c_handle)
1212
with pytest.deprecated_call():
1313
ob = objectbox.objectbox.ObjectBox(c_store)
14+
box = objectbox.Box(ob, tests.model.TestEntity)
15+
assert box.count() == 0
1416

1517
def test_deprecated_Builder():
1618
model = tests.common.create_default_model()

tests/test_hnsw.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ def _test_random_points(
3434

3535
points = np.random.rand(num_points, 2).astype(np.float32)
3636

37-
db = create_test_store()
37+
store = create_test_store()
3838

3939
# Init and seed DB
40-
box = objectbox.Box(db, VectorEntity)
40+
box = store.box(VectorEntity)
4141

4242
print(f"Seeding DB with {num_points} points...")
4343
objects = []
@@ -94,9 +94,9 @@ def test_random_points():
9494

9595
def _test_combined_nn_search(distance_type: VectorDistanceType = VectorDistanceType.EUCLIDEAN):
9696

97-
db = create_test_store()
97+
store = create_test_store()
9898

99-
box = objectbox.Box(db, VectorEntity)
99+
box = store.box(VectorEntity)
100100

101101
vector_field_name = "vector_"+distance_type.name.lower()
102102

tests/test_index.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
@pytest.mark.skip(reason="Test indices implementation")
1313
def test_index_basics():
14-
ob = load_empty_test_default_store()
15-
box = objectbox.Box(ob, TestEntity)
14+
store = load_empty_test_default_store()
15+
box = store.box(TestEntity)
1616

1717
# create
1818
object = TestEntity()

tests/test_inmemory.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@
88
def test_inmemory():
99
# Use default path for persistent store
1010
db_name = "testdata"
11-
ob = load_empty_test_default_store()
12-
box = objectbox.Box(ob, TestEntity)
11+
store = load_empty_test_default_store()
12+
box = store.box(TestEntity)
1313
object = TestEntity()
1414
id = box.put(object)
1515
assert id == 1
1616
assert id == object.id
1717
assert os.path.exists(db_name)
1818
del box
19-
ob.close()
19+
store.close()
2020
shutil.rmtree(db_name)
2121

2222
# Expect no path for in-memory store
2323
db_name = "memory:testdata"
24-
ob = load_empty_test_default_store(db_name)
25-
box = objectbox.Box(ob, TestEntity)
24+
store = load_empty_test_default_store(db_name)
25+
box = store.box(TestEntity)
2626
object = TestEntity()
2727
id = box.put(object)
2828
assert id == 1

tests/test_query.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99

1010

1111
def test_basics():
12-
ob = create_test_store()
12+
store = create_test_store()
1313

14-
box_test_entity = objectbox.Box(ob, TestEntity)
14+
box_test_entity = store.box(TestEntity)
1515
box_test_entity.put(TestEntity(str="foo", int64=123))
1616
box_test_entity.put(TestEntity(str="bar", int64=456))
1717

18-
box_vector_entity = objectbox.Box(ob, VectorEntity)
18+
box_vector_entity = store.box(VectorEntity)
1919
box_vector_entity.put(VectorEntity(name="Object 1", vector_euclidean=[1, 1]))
2020
box_vector_entity.put(VectorEntity(name="Object 2", vector_euclidean=[2, 2]))
2121
box_vector_entity.put(VectorEntity(name="Object 3", vector_euclidean=[3, 3]))
@@ -104,13 +104,13 @@ def test_basics():
104104
assert query.count() == 2
105105
assert query.find_ids() == [2, 3]
106106

107-
ob.close()
107+
store.close()
108108

109109

110110
def test_flex_contains_key_value():
111-
ob = create_test_store()
111+
store = create_test_store()
112112

113-
box = objectbox.Box(ob, TestEntityFlex)
113+
box = store.box(TestEntityFlex)
114114
box.put(TestEntityFlex(flex={"k1": "String", "k2": 2, "k3": "string"}))
115115
box.put(TestEntityFlex(flex={"k1": "strinG", "k2": 3, "k3": 10, "k4": [1, "foo", 3]}))
116116
box.put(TestEntityFlex(flex={"k1": "buzz", "k2": 3, "k3": [2, 3], "k4": {"k1": "a", "k2": "inner text"}}))
@@ -156,9 +156,9 @@ def test_flex_contains_key_value():
156156

157157

158158
def test_offset_limit():
159-
ob = load_empty_test_default_store()
159+
store = load_empty_test_default_store()
160160

161-
box = objectbox.Box(ob, TestEntity)
161+
box = store.box(TestEntity)
162162
box.put(TestEntity())
163163
box.put(TestEntity(str="a"))
164164
box.put(TestEntity(str="b"))
@@ -185,9 +185,9 @@ def test_offset_limit():
185185

186186

187187
def test_any_all():
188-
db = create_test_store()
188+
store = create_test_store()
189189

190-
box = objectbox.Box(db, TestEntity)
190+
box = store.box(TestEntity)
191191

192192
box.put(TestEntity(str="Foo", int32=10, int8=2, float32=3.14, bool=True))
193193
box.put(TestEntity(str="FooBar", int32=100, int8=50, float32=2.0, bool=True))
@@ -247,17 +247,17 @@ def test_any_all():
247247

248248

249249
def test_set_parameter():
250-
db = create_test_store()
250+
store = create_test_store()
251251

252-
box_test_entity = objectbox.Box(db, TestEntity)
252+
box_test_entity = store.box(TestEntity)
253253
box_test_entity.put(TestEntity(str="Foo", int64=2, int32=703, int8=101))
254254
box_test_entity.put(TestEntity(str="FooBar", int64=10, int32=49, int8=45))
255255
box_test_entity.put(TestEntity(str="Bar", int64=10, int32=226, int8=126))
256256
box_test_entity.put(TestEntity(str="Foster", int64=2, int32=301, int8=42))
257257
box_test_entity.put(TestEntity(str="Fox", int64=10, int32=157, int8=11))
258258
box_test_entity.put(TestEntity(str="Barrakuda", int64=4, int32=386, int8=60))
259259

260-
box_vector_entity = objectbox.Box(db, VectorEntity)
260+
box_vector_entity = store.box(VectorEntity)
261261
box_vector_entity.put(VectorEntity(name="Object 1", vector_euclidean=[1, 1]))
262262
box_vector_entity.put(VectorEntity(name="Object 2", vector_euclidean=[2, 2]))
263263
box_vector_entity.put(VectorEntity(name="Object 3", vector_euclidean=[3, 3]))
@@ -300,13 +300,13 @@ def test_set_parameter():
300300

301301

302302
def test_set_parameter_alias():
303-
db = create_test_store()
304-
box = objectbox.Box(db, TestEntity)
303+
store = create_test_store()
304+
box = store.box(TestEntity)
305305

306306
box.put(TestEntity(str="Foo", int64=2, int32=703, int8=101))
307307
box.put(TestEntity(str="FooBar", int64=10, int32=49, int8=45))
308308

309-
box_vector = objectbox.Box(db, VectorEntity)
309+
box_vector = store.box(VectorEntity)
310310
box_vector.put(VectorEntity(name="Object 1", vector_euclidean=[1, 1]))
311311
box_vector.put(VectorEntity(name="Object 2", vector_euclidean=[2, 2]))
312312
box_vector.put(VectorEntity(name="Object 3", vector_euclidean=[3, 3]))
@@ -375,10 +375,10 @@ def test_set_parameter_alias():
375375

376376
def test_set_parameter_alias_advanced():
377377
""" Tests set_parameter_alias in a complex scenario (i.e. multiple query conditions/logical aggregations). """
378-
db = create_test_store()
378+
store = create_test_store()
379379

380380
# Setup 1
381-
box = objectbox.Box(db, TestEntity)
381+
box = store.box(TestEntity)
382382
box.put(TestEntity(str="Apple", bool=False, int64=47, int32=70))
383383
box.put(TestEntity(str="applE", bool=True, int64=253, int32=798))
384384
box.put(TestEntity(str="APPLE", bool=False, int64=3456, int32=123))

tests/test_transactions.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@
44

55

66
def test_transactions():
7-
ob = load_empty_test_default_store()
8-
box = objectbox.Box(ob, TestEntity)
7+
store = load_empty_test_default_store()
8+
box = store.box(TestEntity)
99

1010
assert box.is_empty()
1111

12-
with ob.write_tx():
12+
with store.write_tx():
1313
box.put(TestEntity(str="first"))
1414
box.put(TestEntity(str="second"))
1515

1616
assert box.count() == 2
1717

1818
try:
19-
with ob.write_tx():
19+
with store.write_tx():
2020
box.put(TestEntity(str="third"))
2121
box.put(TestEntity(str="fourth"))
2222
raise Exception("mission abort!")
@@ -31,12 +31,12 @@ def test_transactions():
3131

3232
# can't write in a read TX
3333
try:
34-
with ob.read_tx():
34+
with store.read_tx():
3535
box.put(TestEntity(str="third"))
3636

3737
# exception must be propagated so this line must not execute
3838
assert 0
3939
except Exception as err:
4040
assert "Cannot start a write transaction inside a read only transaction" in str(err)
4141
finally:
42-
ob.close()
42+
store.close()

0 commit comments

Comments
 (0)