Skip to content

Commit 8308a48

Browse files
Repeating load of object with key causes failure. (#389)
* Fix repeated key change behaviour * Defeat the linter
1 parent d6195f3 commit 8308a48

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

terminusdb_client/schema/schema.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,10 @@ def __setattr__(self, name, value):
203203
and hasattr(self, "_key")
204204
and hasattr(self._key, "_keys")
205205
and name in self._key._keys
206+
and value != getattr(self, name)
206207
):
207208
raise ValueError(
208-
f"{name} has been used to generated id hance cannot be changed."
209+
f"{name} has been used to generate the id, hence cannot be changed."
209210
)
210211
super().__setattr__(name, value)
211212

terminusdb_client/tests/integration_tests/test_schema.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def test_insert_cheuk_again(docker_url, test_schema):
155155
uk.name = "United Kingdom of Great Britain and Northern Ireland"
156156
assert (
157157
str(error.value)
158-
== "name has been used to generated id hance cannot be changed."
158+
== "name has been used to generate the id, hence cannot be changed."
159159
)
160160

161161
cheuk = Employee()
@@ -302,3 +302,37 @@ def test_compress_data(docker_url):
302302
client.insert_document(test_obj, compress=0)
303303
test_obj2 = client.get_all_documents(as_list=True)
304304
assert len(test_obj2) == 10
305+
306+
307+
def test_repeated_object_load(docker_url, test_schema):
308+
schema = test_schema
309+
client = Client(docker_url, user_agent=test_user_agent)
310+
client.connect()
311+
client.create_database("test_repeated_load")
312+
client.insert_document(
313+
schema, commit_msg="I am checking in the schema", graph_type="schema"
314+
)
315+
[country_id] = client.insert_document({"@type" : "Country",
316+
"name" : "Romania",
317+
"perimeter" : []})
318+
obj = client.get_document(country_id)
319+
schema.import_objects(obj)
320+
obj2 = client.get_document(country_id)
321+
schema.import_objects(obj2)
322+
323+
324+
def test_key_change_raises_exception(docker_url, test_schema):
325+
schema = test_schema
326+
client = Client(docker_url, user_agent=test_user_agent)
327+
client.connect()
328+
client.create_database("test_repeated_load_fails")
329+
client.insert_document(
330+
schema, commit_msg="I am checking in the schema", graph_type="schema"
331+
)
332+
[country_id] = client.insert_document({"@type" : "Country",
333+
"name" : "Romania",
334+
"perimeter" : []})
335+
obj = client.get_document(country_id)
336+
local_obj = schema.import_objects(obj)
337+
with pytest.raises(ValueError, match=r"name has been used to generate the id, hence cannot be changed."):
338+
local_obj.name = "France"

0 commit comments

Comments
 (0)