Skip to content

Commit d9aabee

Browse files
committed
Test flex property with concrete types #10
1 parent 990ccb9 commit d9aabee

File tree

4 files changed

+70
-24
lines changed

4 files changed

+70
-24
lines changed

objectbox/model/entity.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,9 @@ def marshal(self, object, id: int) -> bytearray:
128128
elif prop._ob_type == OBXPropertyType_DoubleVector:
129129
offsets[prop._id] = builder.CreateNumpyVector(np.array(val, dtype=np.float64))
130130
elif prop._ob_type == OBXPropertyType_Flex:
131-
buffer = flatbuffers.flexbuffers.Dumps(val)
131+
flex_builder = flatbuffers.flexbuffers.Builder()
132+
flex_builder.Add(val)
133+
buffer = flex_builder.Finish()
132134
offsets[prop._id] = builder.CreateByteVector(bytes(buffer))
133135
else:
134136
assert False, "programming error - invalid type OB & FB type combination"

tests/common.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import os
33
import shutil
44
import pytest
5-
from tests.model import TestEntity, TestEntityDatetime
5+
from tests.model import TestEntity, TestEntityDatetime, TestEntityFlex
66
import numpy as np
77

88
test_dir = 'testdata'
@@ -35,14 +35,25 @@ def load_empty_test_objectbox(name: str = "") -> objectbox.ObjectBox:
3535
def load_empty_test_datetime(name: str = "") -> objectbox.ObjectBox:
3636
model = objectbox.Model()
3737
from objectbox.model import IdUid
38-
model.entity(TestEntityDatetime, last_property_id=IdUid(3, 2003))
38+
model.entity(TestEntityDatetime, last_property_id=IdUid(4, 2004))
3939
model.last_entity_id = IdUid(2, 2)
4040

4141
db_name = test_dir if len(name) == 0 else test_dir + "/" + name
4242

4343
return objectbox.Builder().model(model).directory(db_name).build()
4444

4545

46+
def load_empty_test_flex(name: str = "") -> objectbox.ObjectBox:
47+
model = objectbox.Model()
48+
from objectbox.model import IdUid
49+
model.entity(TestEntityFlex, last_property_id=IdUid(3, 3003))
50+
model.last_entity_id = IdUid(3, 3)
51+
52+
db_name = test_dir if len(name) == 0 else test_dir + "/" + name
53+
54+
return objectbox.Builder().model(model).directory(db_name).build()
55+
56+
4657
def assert_equal_prop(actual, expected, default):
4758
assert actual == expected or (isinstance(
4859
expected, objectbox.model.Property) and actual == default)
@@ -77,11 +88,3 @@ def assert_equal(actual: TestEntity, expected: TestEntity):
7788
assert_equal_prop_approx(actual.date, expected.date, 0)
7889
assert_equal_prop(actual.date_nano, expected.date_nano, 0)
7990
assert_equal_prop(actual.flex, expected.flex, None)
80-
81-
82-
def put_flex(object, box, property):
83-
object.flex = property
84-
id = box.put(object)
85-
assert id == object.id
86-
read = box.get(object.id)
87-
assert read.flex == object.flex

tests/model.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from objectbox.model import *
22
import numpy as np
33
from datetime import datetime
4-
from typing import Generic
4+
from typing import Generic, Dict, Any
55

66

77
@Entity(id=1, uid=1)
@@ -40,3 +40,12 @@ class TestEntityDatetime:
4040

4141
def __init__(self, string: str = ""):
4242
self.str = string
43+
44+
@Entity(id=3, uid=3)
45+
class TestEntityFlex:
46+
id = Id(id=1, uid=3001)
47+
flex_dict = Property(Dict[str, Any], type=PropertyType.flex, id=2, uid=3002)
48+
flex_int = Property(int, type=PropertyType.flex, id=3, uid=3003)
49+
50+
def __init__(self, string: str = ""):
51+
self.str = string

tests/test_box.py

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import pytest
22
import objectbox
3-
from tests.model import TestEntity, TestEntityDatetime
3+
from tests.model import TestEntity, TestEntityDatetime, TestEntityFlex
44
from tests.common import (
55
autocleanup,
66
load_empty_test_objectbox,
77
load_empty_test_datetime,
8+
load_empty_test_flex,
89
assert_equal,
9-
put_flex,
1010
)
1111
import numpy as np
1212
from datetime import datetime
@@ -179,32 +179,64 @@ def test_datetime():
179179

180180

181181
def test_flex():
182+
183+
def test_put_get(object: TestEntity, box: objectbox.Box, property):
184+
object.flex = property
185+
id = box.put(object)
186+
assert id == object.id
187+
read = box.get(object.id)
188+
assert read.flex == object.flex
189+
182190
ob = load_empty_test_objectbox()
183191
box = objectbox.Box(ob, TestEntity)
184192
object = TestEntity()
185193

186-
# Put a None type first
187-
put_flex(object, box, None)
194+
# Put an empty object
195+
id = box.put(object)
196+
assert id == object.id
197+
198+
# Put a None type object
199+
test_put_get(object, box, None)
188200

189201
# Update to int
190-
put_flex(object, box, 1)
202+
test_put_get(object, box, 1)
191203

192204
# Update to float
193-
put_flex(object, box, 1.2)
205+
test_put_get(object, box, 1.2)
194206

195207
# Update to string
196-
put_flex(object, box, "foo")
208+
test_put_get(object, box, "foo")
197209

198210
# Update to int list
199-
put_flex(object, box, [1, 2, 3])
211+
test_put_get(object, box, [1, 2, 3])
200212

201213
# Update to float list
202-
put_flex(object, box, [1.1, 2.2, 3.3])
214+
test_put_get(object, box, [1.1, 2.2, 3.3])
203215

204216
# Update to dict
205-
put_flex(object, box, {"a": 1, "b": 2})
217+
test_put_get(object, box, {"a": 1, "b": 2})
206218

207219
# Update to bool
208-
put_flex(object, box, True)
220+
test_put_get(object, box, True)
221+
222+
# Update to dict inside dict
223+
test_put_get(object, box, {"a": 1, "b": {"c": 2}})
224+
225+
# Update to list inside dict
226+
test_put_get(object, box, {"a": 1, "b": [1, 2, 3]})
209227

210-
ob.close()
228+
ob.close()
229+
230+
231+
def test_flex_dict():
232+
ob = load_empty_test_flex()
233+
box = objectbox.Box(ob, TestEntityFlex)
234+
object = TestEntityFlex()
235+
236+
object.flex_dict = {"a": 1, "b": 2}
237+
object.flex_int = 25
238+
id = box.put(object)
239+
assert id == object.id
240+
read = box.get(object.id)
241+
assert read.flex_dict == object.flex_dict
242+
assert read.flex_int == object.flex_int

0 commit comments

Comments
 (0)