Skip to content

Commit 1d419ab

Browse files
committed
Allow specifying properties via classes #59
Also, add test for named models
1 parent 036e5b1 commit 1d419ab

File tree

2 files changed

+68
-6
lines changed

2 files changed

+68
-6
lines changed

objectbox/model/entity.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
import inspect
1415

1516
import flatbuffers
1617
import flatbuffers.flexbuffers
@@ -280,6 +281,13 @@ def Entity(uid: int = 0, model: str = "default") -> Callable[[Type], _Entity]:
280281
""" Entity decorator that wraps _Entity to allow @Entity(id=, uid=); i.e. no class arguments. """
281282

282283
def wrapper(class_):
284+
# Also allow defining properties as class members; we'll instantiate them here
285+
class_members = inspect.getmembers(class_, lambda a: (inspect.isclass(a) and issubclass(a, Property)))
286+
for class_member in class_members:
287+
assert issubclass(class_member[1], Property)
288+
obj = class_member[1]()
289+
setattr(class_, class_member[0], obj)
290+
283291
metadata_set = obx_models_by_name.get(model)
284292
if metadata_set is None:
285293
metadata_set = set()

tests/test_idsync.py

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
from typing import *
1+
import json
2+
import pytest
3+
import os
4+
from numpy.testing import assert_approx_equal
25
from objectbox import *
36
from objectbox.model import *
47
from objectbox.model.entity import _Entity
58
from objectbox.model.idsync import sync_model
69
from objectbox.c import CoreException
7-
import json
8-
from pprint import pprint
9-
import os
1010
from os import path
11-
import tests.model
12-
import pytest
11+
12+
from tests.common import remove_json_model_file
13+
1314

1415
class _TestEnv:
1516
"""
@@ -539,3 +540,56 @@ class EntityA:
539540
with pytest.raises(ValueError) as e:
540541
env.sync(model)
541542
assert f"User supplied UID {entitya_uid} is already assigned elsewhere" == str(e.value)
543+
544+
545+
def test_models_named(env):
546+
@Entity(model="modelA")
547+
class EntityA:
548+
id = Id
549+
text_a = String
550+
551+
@Entity(model="modelB")
552+
class EntityB:
553+
id = Id
554+
int_b = Int64
555+
556+
@Entity(model="modelB")
557+
class EntityB2:
558+
id = Id()
559+
float_b = Float64
560+
561+
Store.remove_db_files("test-db-model-a")
562+
Store.remove_db_files("test-db-model-b")
563+
remove_json_model_file()
564+
store_a = Store(model="modelA", directory="test-db-model-a")
565+
remove_json_model_file()
566+
store_b = Store(model="modelB", directory="test-db-model-b")
567+
568+
box_a = store_a.box(EntityA)
569+
id = box_a.put(EntityA(text_a="ah"))
570+
assert id != 0
571+
assert box_a.get(id).text_a == "ah"
572+
573+
# TODO to make this work we Store/Box to check if the type is actually registered.
574+
# This might require to store the (Python) model in the Store.
575+
# with pytest.raises(ValueError):
576+
# store_a.box(EntityB)
577+
578+
# TODO XXX this should never fail, but is flaky
579+
#with pytest.raises(CoreException):
580+
# store_a.box(EntityB2)
581+
582+
box_b = store_b.box(EntityB)
583+
id = box_b.put(EntityB(int_b=42))
584+
assert id != 0
585+
assert box_b.get(id).int_b == 42
586+
587+
box_b2 = store_b.box(EntityB2)
588+
id = box_b2.put(EntityB2(float_b=3.141))
589+
assert id != 0
590+
assert_approx_equal(box_b2.get(id).float_b, 3.141)
591+
592+
# TODO to make this work we Store/Box to check if the type is actually registered.
593+
# This might require to store the (Python) model in the Store.
594+
# with pytest.raises(ValueError):
595+
# store_b.box(EntityA)

0 commit comments

Comments
 (0)