Skip to content

Commit

Permalink
Merge pull request #169 from simleo/fix_mutable_id
Browse files Browse the repository at this point in the history
Make Entity id a readonly property
  • Loading branch information
simleo authored Dec 21, 2023
2 parents fd5b75e + 54cff7e commit 7364e13
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
8 changes: 6 additions & 2 deletions rocrate/model/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,20 @@ class Entity(MutableMapping):
def __init__(self, crate, identifier=None, properties=None):
self.crate = crate
if identifier:
self.id = self.format_id(identifier)
self.__id = self.format_id(identifier)
else:
self.id = f"#{uuid.uuid4()}"
self.__id = f"#{uuid.uuid4()}"
if properties:
empty = self._empty()
empty.update(properties)
self._jsonld = empty
else:
self._jsonld = self._empty()

@property
def id(self):
return self.__id

# Format the given ID with rules appropriate for this type.
# For example, Dataset (directory) data entities SHOULD end with /
def format_id(self, identifier):
Expand Down
7 changes: 7 additions & 0 deletions test/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,3 +505,10 @@ def test_add_no_duplicates(test_data_dir, tmpdir):
assert ret["name"] == "Jim"
assert ret in crate.get_entities()
assert crate.contextual_entities == [jim]


def test_immutable_id():
crate = ROCrate()
p = crate.add(Person(crate, "#foo"))
with pytest.raises(AttributeError):
p.id = "#bar"

0 comments on commit 7364e13

Please sign in to comment.