-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix utils.parse_object() #46
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4871132
Improved generality of parse_literal() and parse_object() fixing fail…
jesper-friis 766041f
Added __hash__() and __eq__() methods to Literal and fixed some tests.
jesper-friis f0e5b9a
Added __hash__() and __eq__() to Namespace as well.
jesper-friis 1fbcafd
Added test to infer IRI from pydantic datamodel
jesper-friis 2980910
Added a pylint disable because of yet another bug in pylint
jesper-friis 2b8f679
Changed `uri` to `identity` to match soft7 datamodels
jesper-friis eb1c9fa
Added comment
jesper-friis 8c5985c
Also handle blank nodes correctly.
jesper-friis 5e0cc4c
Merge branch 'main' into 45-update-parse_object
jesper-friis 89fcdd5
Merge branch 'main' into 45-update-parse_object
jesper-friis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
"""Test utils""" | ||
# pylint: disable=invalid-name | ||
# pylint: disable=invalid-name,too-few-public-methods | ||
import dlite | ||
import pytest | ||
|
||
|
@@ -20,6 +20,51 @@ | |
assert infer_iri(coll.meta) == coll.meta.uri | ||
assert infer_iri(coll) == coll.uuid | ||
|
||
# We have no dependencies on pydantic, hence don't assume that it is installed. | ||
# But if it is, infer_iri() should be able to infer IRIs from SOFT7 datamodels. | ||
try: | ||
from pydantic import AnyUrl, BaseModel, Field | ||
except ImportError: | ||
pass | ||
else: | ||
from typing import Any, Optional | ||
|
||
class Property(BaseModel): | ||
"""A property.""" | ||
|
||
# pylint: disable=unsubscriptable-object | ||
# Yet another pylint bug, see https://github.com/PyCQA/pylint/issues/1498 | ||
type: Any = Field(..., description="Valid type name.") | ||
shape: Optional[list[str]] = Field( | ||
None, description="List of dimension expressions." | ||
) | ||
unit: Optional[str] = Field(None, description="Unit of a property.") | ||
description: Optional[str] = Field( | ||
None, description="A human description of the property." | ||
) | ||
|
||
class Entity(BaseModel): | ||
"""An entity.""" | ||
|
||
# pylint: disable=unsubscriptable-object | ||
identity: AnyUrl = Field(..., description="Unique URI identifying the entity.") | ||
description: str = Field("", description="A description of the entity.") | ||
dimensions: Optional[dict[str, str]] = Field( | ||
None, description="Dict mapping dimension names to descriptions." | ||
) | ||
properties: dict[str, Property] = Field(..., description="Dict of properties.") | ||
|
||
user = Entity( | ||
identity="http://onto-ns.com/meta/0.1/User", | ||
properties={ | ||
"username": Property(type=str, description="username"), | ||
"quota": Property(type=float, unit="GB", description="User quota"), | ||
}, | ||
) | ||
|
||
assert infer_iri(user) == "http://onto-ns.com/meta/0.1/User" | ||
|
||
|
||
# Test split_iri() | ||
rdfs = str(RDFS) | ||
assert split_iri(RDFS.subClassOf) == (rdfs, "subClassOf") | ||
|
@@ -65,15 +110,15 @@ def h(): | |
# test parse_literal() | ||
assert parse_literal("abc") == Literal("abc", datatype=XSD.string) | ||
assert parse_literal(True) == Literal("True", datatype=XSD.boolean) | ||
assert parse_literal(1) == Literal("1", datatype=XSD.inteter) | ||
assert parse_literal(1) == Literal("1", datatype=XSD.integer) | ||
assert parse_literal(3.14) == Literal("3.14", datatype=XSD.double) | ||
assert parse_literal(f'"3.14"^^{XSD.double}') == Literal("3.14", datatype=XSD.double) | ||
|
||
|
||
# test parse_object() | ||
assert parse_object("True") == Literal("True", datatype=XSD.boolean) | ||
assert parse_object("False") == Literal("False", datatype=XSD.boolean) | ||
assert parse_object("true") == Literal("true", datatype=XSD.string) | ||
assert parse_object("true") == Literal("true", datatype=XSD.boolean) | ||
assert parse_object("false") == Literal("false", datatype=XSD.boolean) | ||
assert parse_object("True") == Literal("True", datatype=XSD.string) | ||
assert parse_object("0") == Literal("0", datatype=XSD.integer) | ||
assert parse_object("1") == Literal("1", datatype=XSD.integer) | ||
assert parse_object("-1") == Literal("-1", datatype=XSD.integer) | ||
|
@@ -97,12 +142,13 @@ def h(): | |
assert parse_object("2022-12-01T12:30:30") == Literal( | ||
"2022-12-01T12:30:30", datatype=XSD.dateTime | ||
) | ||
assert parse_object("2022-12-01 12:30:30.50") == Literal( | ||
"2022-12-01 12:30:30.50", datatype=XSD.dateTime | ||
) | ||
assert parse_object("2022-12-01 12:30:30Z") == Literal( | ||
"2022-12-01 12:30:30Z", datatype=XSD.dateTime | ||
assert parse_object("2022-12-01 12:30:30.500") == Literal( | ||
"2022-12-01 12:30:30.500", datatype=XSD.dateTime | ||
) | ||
# Format not supported in Python < 3.11 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove commented-out code |
||
# assert parse_object("2022-12-01 12:30:30Z") == Literal( | ||
# "2022-12-01 12:30:30Z", datatype=XSD.dateTime | ||
# ) | ||
assert parse_object("2022-12-01 12:30:30+01:00") == Literal( | ||
"2022-12-01 12:30:30+01:00", datatype=XSD.dateTime | ||
) | ||
|
@@ -112,7 +158,5 @@ def h(): | |
assert parse_object(XSD.int) == XSD.int | ||
assert parse_object(f'"42"^^{XSD.integer}') == Literal("42", datatype=XSD.integer) | ||
assert parse_object(f'"4.2"^^{XSD.double}') == Literal("4.2", datatype=XSD.double) | ||
|
||
# __FIXME__: parse_object() currently fails for the following cases: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove commented-out code |
||
# assert parse_object(f'"42"^^{XSD.double}') == Literal("42", datatype=XSD.double) | ||
# assert parse_object(f'"42"^^{XSD.int}') == Literal("42", datatype=XSD.int) | ||
assert parse_object(f'"42"^^{XSD.double}') == Literal("42.0", datatype=XSD.double) | ||
assert parse_object(f'"42"^^{XSD.int}') == Literal("42", datatype=XSD.int) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try to avoid except pass - they are notoriously difficult to reason about