Skip to content

Commit 118f868

Browse files
committed
IdSync: add more asserts and value checks #25
* syncing an emtpy model is illegal * UIDs in a JSON are unique * IDs are set on "on_sync()"
1 parent 5a96668 commit 118f868

File tree

4 files changed

+27
-26
lines changed

4 files changed

+27
-26
lines changed

objectbox/model/entity.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def has_uid(self) -> bool:
5555

5656
def on_sync(self):
5757
""" Method called once ID/UID are synced with the model file. """
58+
assert self.iduid.is_assigned()
5859
for prop in self.properties:
5960
prop.on_sync()
6061

objectbox/model/idsync.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class IdSync:
1818

1919
def __init__(self, model: Model, model_json_filepath: str):
2020
self.model = model
21+
if len(model.entities) == 0:
22+
raise ValueError("A valid model must have at least one entity")
2123

2224
self.model_filepath = model_json_filepath
2325
self.model_json = None
@@ -42,11 +44,22 @@ def _load_model_json(self):
4244

4345
def _load_assigned_uids(self):
4446
for entity_json in self.model_json["entities"]:
45-
self._assigned_uids.add(IdUid.from_str(entity_json["id"]).uid)
47+
entity_uid = IdUid.from_str(entity_json["id"]).uid
48+
if entity_uid in self._assigned_uids:
49+
raise ValueError(f"An entity's UID {entity_uid} has already been used elsewhere")
50+
self._assigned_uids.add(entity_uid)
51+
4652
for prop_json in entity_json["properties"]:
47-
self._assigned_uids.add(IdUid.from_str(prop_json["id"]).uid)
53+
prop_uid = IdUid.from_str(prop_json["id"]).uid
54+
if prop_uid in self._assigned_uids:
55+
raise ValueError(f"A property's UID {prop_uid} has already been used elsewhere")
56+
self._assigned_uids.add(prop_uid)
57+
4858
if "indexId" in prop_json:
49-
self._assigned_uids.add(IdUid.from_str(prop_json["indexId"]).uid)
59+
index_uid = IdUid.from_str(prop_json["indexId"]).uid
60+
if index_uid in self._assigned_uids:
61+
raise ValueError(f"An index's UID {index_uid} has already been used elsewhere")
62+
self._assigned_uids.add(index_uid)
5063

5164
def _save_model_json(self):
5265
""" Replaces model JSON with the serialized model whose ID/UIDs are assigned. """

objectbox/model/properties.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ def is_id(self) -> bool:
201201

202202
def on_sync(self):
203203
""" Method called once ID/UID are synced with the model file. """
204+
assert self.iduid.is_assigned()
204205
self._fb_slot = self.id - 1
205206
self._fb_v_offset = 4 + 2 * self._fb_slot
206207

tests/test_idsync.py

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ def test_empty_model(env):
5454

5555
# JSON file didn't exist, user syncs an empty model -> no JSON file is generated
5656
model = Model()
57-
assert not env.sync(model)
57+
with pytest.raises(ValueError):
58+
assert not env.sync(model)
5859
assert not path.exists(env.model_path)
5960

6061
# Init the JSON file with an entity
@@ -65,31 +66,14 @@ class MyEntity:
6566
model.entity(MyEntity)
6667
assert env.sync(model) # Model JSON written
6768

68-
# JSON file exists, user syncs an empty model -> JSON file is written but entities are cleared (last ID/UID
69-
# retained)
69+
# JSON file exists, user tries to sync an empty model: must fail with JSON file untouched
7070
model = Model()
71-
assert env.sync(model)
71+
with pytest.raises(ValueError):
72+
env.sync(model)
7273

7374
doc = env.json()
74-
assert doc['_note1']
75-
assert doc['_note2']
76-
assert doc['_note3']
77-
assert len(doc['entities']) == 0
78-
# Last entity ID/UID will still be set at MyEntity's ID/UID
79-
# assert doc['lastEntityId'] == '0:0'
80-
# assert doc['lastIndexId'] == '0:0' # NOTE: objectbox-generator outputs ""
81-
assert doc['modelVersionParserMinimum'] >= 5
82-
# debug: pprint(doc)
83-
#
84-
# TODO: sync with objectbox-generator empty fbs
85-
# assert doc['modelVersion'] == 5
86-
# assert doc['lastIndex'] == ""
87-
# assert doc['lastRelationId'] == ""
88-
# assert len(doc['retiredEntityUids']) == 0
89-
# assert len(doc['retiredIndexUids']) == 0
90-
# assert len(doc['retiredPropertyUids']) == 0
91-
# assert len(doc['retiredRelationUids']) == 0
92-
# assert len(doc['version']) == 1
75+
assert len(doc['entities']) == 1
76+
assert doc['entities'][0]['id'] == str(MyEntity.iduid)
9377

9478

9579
def test_json(env):
@@ -311,6 +295,8 @@ class Entity4:
311295
name = String() # Add one property also
312296

313297
model = Model()
298+
reset_ids(Entity1)
299+
reset_ids(Entity3)
314300
model.entity(Entity1)
315301
model.entity(Entity3)
316302
model.entity(Entity4)

0 commit comments

Comments
 (0)