Skip to content

Commit

Permalink
Added tests for both new and old instance formats (#730)
Browse files Browse the repository at this point in the history
# Description
Test that new and old instance formats are equivalent.

To support the test, also added a convenient `props` property to
metadata as well as equality comparition to properties and dimensions.

Closes #702 

## Type of change
- [x] Bug fix & code cleanup
- [ ] New feature
- [ ] Documentation update
- [x] Test update

## Checklist for the reviewer
This checklist should be used as a help for the reviewer.

- [ ] Is the change limited to one issue?
- [ ] Does this PR close the issue?
- [ ] Is the code easy to read and understand?
- [ ] Do all new feature have an accompanying new test?
- [ ] Has the documentation been updated as necessary?
  • Loading branch information
jesper-friis authored Dec 8, 2023
2 parents dfe09e0 + 7dedf91 commit d6ffb94
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 5 deletions.
20 changes: 15 additions & 5 deletions bindings/python/dlite-entity-python.i
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ class Metadata(Instance):
inst[k] = v
return inst

# For convenience. Easier to use than self.properties["properties"]
props = property(
fget=lambda self: {p.name: p for p in self.properties["properties"]},
doc="A dict mapping property name to the `Property` object for the "
"described metadata.",
)

def getprop(self, name):
"""Returns the metadata property object with the given name."""
if "properties" not in self.properties:
Expand Down Expand Up @@ -173,6 +180,10 @@ def get_instance(id: str, metaid: str = None, check_storages: bool = True) -> "I
return 'Dimension(name=%r, description=%r)' % (
self.name, self.description)

def __eq__(self, other):
"""Returns true if `other` equals self."""
return self.asdict() == other.asdict()

def asdict(self):
"""Returns a dict representation of self."""
d = {}
Expand Down Expand Up @@ -202,6 +213,10 @@ def get_instance(id: str, metaid: str = None, check_storages: bool = True) -> "I
return 'Property(%r, type=%r%s%s%s%s)' % (
self.name, self.type, ref, shape, unit, descr)

def __eq__(self, other):
"""Returns true if `other` equals self."""
return self.asdict() == other.asdict()

def asdict(self, soft7=True, exclude_name=False):
"""Returns a dict representation of self.
Expand Down Expand Up @@ -356,11 +371,6 @@ def get_instance(id: str, metaid: str = None, check_storages: bool = True) -> "I
is_meta = property(_is_meta, doc='Whether this is a metadata instance.')
is_metameta = property(_is_metameta,
doc='Whether this is a meta-metadata instance.')
namespace = property(
lambda self: Namespace(self.get_uri() + '#'),
doc='A tripper.Namespace reference to this instance. '
'Note, this requires tripper to be installed.'
)

@classmethod
def from_metaid(cls, metaid, dims, id=None):
Expand Down
23 changes: 23 additions & 0 deletions bindings/python/tests/entities/PersonNew.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"uri": "http://onto-ns.com/meta/0.1/PersonNew",
"description": "A person.",
"dimensions": {
"N": "Number of skills."
},
"properties": {
"name": {
"type": "string",
"description": "Full name."
},
"age": {
"type": "float",
"unit": "years",
"description": "Age of person."
},
"skills": {
"type": "string",
"shape": ["N"],
"description": "List of skills."
}
}
}
31 changes: 31 additions & 0 deletions bindings/python/tests/entities/PersonOld.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "PersonOld",
"version": "0.1",
"namespace": "http://onto-ns.com/meta",
"description": "A person.",
"dimensions": [
{
"name": "N",
"description": "Number of skills."
}
],
"properties": [
{
"name": "name",
"type": "string",
"description": "Full name."
},
{
"name": "age",
"type": "float",
"unit": "years",
"description": "Age of person."
},
{
"name": "skills",
"type": "string",
"dims": ["N"],
"description": "List of skills."
}
]
}
9 changes: 9 additions & 0 deletions bindings/python/tests/test_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@

thisdir = Path(__file__).absolute().parent
indir = thisdir / "input"
entitydir = thisdir / "entities"
dlite.storage_path.append(indir / "*.json")
dlite.storage_path.append(entitydir / "*.json")

url = f"json://{thisdir}/MyEntity.json"

Expand Down Expand Up @@ -325,3 +327,10 @@
# For issue #691
with raises(dlite.DLiteStorageOpenError):
Invalid3 = dlite.Instance.from_location("json", indir / "Invalid3.json")


# For issue #702
PersonOld = dlite.get_instance("http://onto-ns.com/meta/0.1/PersonOld")
PersonNew = dlite.get_instance("http://onto-ns.com/meta/0.1/PersonNew")
assert PersonOld.props == PersonNew.props
assert PersonOld.dimnames() == PersonNew.dimnames()

0 comments on commit d6ffb94

Please sign in to comment.