Skip to content

Commit 948c43e

Browse files
committed
tests: add VectorEntity objectbox#24
1 parent 9c53c92 commit 948c43e

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

tests/common.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import objectbox
22
import os
3+
from os import path
34
import shutil
45
import pytest
5-
from tests.model import TestEntity, TestEntityDatetime, TestEntityFlex
66
import numpy as np
7+
from typing import *
8+
from tests.model import *
9+
710

811
test_dir = 'testdata'
912

@@ -23,15 +26,13 @@ def autocleanup():
2326

2427
def load_empty_test_objectbox(db_name: str = test_dir) -> objectbox.ObjectBox:
2528
model = objectbox.Model()
26-
from objectbox.model import IdUid
2729
model.entity(TestEntity, last_property_id=IdUid(27, 1027))
2830
model.last_entity_id = IdUid(2, 2)
2931

3032
return objectbox.Builder().model(model).directory(db_name).build()
3133

3234
def load_empty_test_datetime(name: str = "") -> objectbox.ObjectBox:
3335
model = objectbox.Model()
34-
from objectbox.model import IdUid
3536
model.entity(TestEntityDatetime, last_property_id=IdUid(4, 2004))
3637
model.last_entity_id = IdUid(2, 2)
3738

@@ -42,7 +43,6 @@ def load_empty_test_datetime(name: str = "") -> objectbox.ObjectBox:
4243

4344
def load_empty_test_flex(name: str = "") -> objectbox.ObjectBox:
4445
model = objectbox.Model()
45-
from objectbox.model import IdUid
4646
model.entity(TestEntityFlex, last_property_id=IdUid(3, 3003))
4747
model.last_entity_id = IdUid(3, 3)
4848

@@ -51,6 +51,26 @@ def load_empty_test_flex(name: str = "") -> objectbox.ObjectBox:
5151
return objectbox.Builder().model(model).directory(db_name).build()
5252

5353

54+
def create_test_objectbox(db_name: Optional[str] = None, clear_db: bool = True) -> objectbox.ObjectBox:
55+
""" Creates an ObjectBox instance with all entities. """
56+
57+
db_path = test_dir if db_name is None else path.join(test_dir, db_name)
58+
print(f"DB path: \"{db_path}\"")
59+
60+
if clear_db and path.exists(db_path):
61+
shutil.rmtree(db_path)
62+
63+
model = objectbox.Model()
64+
model.entity(TestEntity, last_property_id=IdUid(27, 1027))
65+
model.entity(TestEntityDatetime, last_property_id=IdUid(4, 2004))
66+
model.entity(TestEntityFlex, last_property_id=IdUid(3, 3003))
67+
model.entity(VectorEntity, last_property_id=IdUid(3, 4003))
68+
model.last_entity_id = IdUid(4, 4)
69+
model.last_index_id = IdUid(3, 40001)
70+
71+
return objectbox.Builder().model(model).directory(db_path).build()
72+
73+
5474
def assert_equal_prop(actual, expected, default):
5575
assert actual == expected or (isinstance(
5676
expected, objectbox.model.Property) and actual == default)

tests/model.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,23 @@ class TestEntityDatetime:
5252
def __init__(self, string: str = ""):
5353
self.str = string
5454

55+
5556
@Entity(id=3, uid=3)
5657
class TestEntityFlex:
5758
id = Id(id=1, uid=3001)
5859
flex_dict = Property(Dict[str, Any], type=PropertyType.flex, id=2, uid=3002)
5960
flex_int = Property(int, type=PropertyType.flex, id=3, uid=3003)
6061

6162
def __init__(self, string: str = ""):
62-
self.str = string
63+
self.str = string
64+
65+
66+
@Entity(id=4, uid=4)
67+
class VectorEntity:
68+
id = Id(id=1, uid=4001)
69+
name = Property(str, type=PropertyType.string, id=2, uid=4002)
70+
vector = Property(np.ndarray, type=PropertyType.floatVector, id=3, uid=4003,
71+
index=HnswIndex(
72+
id=3, uid=40001,
73+
dimensions=2, distance_type=HnswDistanceType.EUCLIDEAN)
74+
)

0 commit comments

Comments
 (0)