Skip to content

Commit

Permalink
Rename sdf.SDF to sdf.Document
Browse files Browse the repository at this point in the history
  • Loading branch information
christiansandberg committed Oct 1, 2024
1 parent 6186daa commit b8d992d
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Creating a new document:
```python
from onedm import sdf

doc = sdf.SDF()
doc = sdf.Document()

doc.info.title = "Generic switch document"
doc.things["switch"] = sdf.Thing(label="Generic switch")
Expand Down
4 changes: 2 additions & 2 deletions onedm/sdf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
StringProperty,
Thing,
)
from .document import SDF, Information
from .document import Document, Information
from .loader import SDFLoader

__all__ = [
"SDFLoader",
"SDF",
"Document",
"Thing",
"Object",
"Property",
Expand Down
10 changes: 7 additions & 3 deletions onedm/sdf/document.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from datetime import datetime
from typing import Annotated

from pydantic import BaseModel, Field
from pydantic import BaseModel, ConfigDict, Field
from pydantic.alias_generators import to_camel

from . import definitions

Expand Down Expand Up @@ -45,7 +46,11 @@ class Information(BaseModel):
]


class SDF(BaseModel):
class Document(BaseModel):
model_config = ConfigDict(
extra="allow", alias_generator=to_camel, populate_by_name=True
)

info: Annotated[
Information,
Field(default_factory=Information),
Expand All @@ -63,7 +68,6 @@ class SDF(BaseModel):
default_namespace: Annotated[
str | None,
Field(
alias="defaultNamespace",
description=(
"Identifies one of the prefixes in the namespace map "
"to be used as a default in resolving identifiers"
Expand Down
6 changes: 3 additions & 3 deletions onedm/sdf/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import urllib.parse
from typing import Any

from .document import SDF
from .document import Document


class SDFLoader:
Expand All @@ -38,8 +38,8 @@ def load(self, url: str):
else:
raise NotImplementedError("Not supported yet")

def to_sdf(self) -> SDF:
return SDF.model_validate(self.root)
def to_sdf(self) -> Document:
return Document.model_validate(self.root)

def _dereference(self, definition: dict[str, Any]) -> dict[str, Any]:
if "sdfRef" in definition:
Expand Down
18 changes: 9 additions & 9 deletions tests/sdf/test_datatypes.py
Original file line number Diff line number Diff line change
@@ -1,59 +1,59 @@
from onedm import sdf


def test_integer_data(test_model: sdf.SDF):
def test_integer_data(test_model: sdf.Document):
assert isinstance(test_model.data["Integer"], sdf.IntegerData)
assert test_model.data["Integer"].minimum == -2
assert test_model.data["Integer"].maximum == 2
assert test_model.data["Integer"].multiple_of == 2


def test_number_data(test_model: sdf.SDF):
def test_number_data(test_model: sdf.Document):
assert isinstance(test_model.data["Number"], sdf.NumberData)
assert test_model.data["Number"].minimum == -1.5
assert test_model.data["Number"].maximum == 1.5
assert test_model.data["Number"].multiple_of == 0.5


def test_integer_choices(test_model: sdf.SDF):
def test_integer_choices(test_model: sdf.Document):
assert isinstance(test_model.data["Enum"], sdf.IntegerData)
assert isinstance(test_model.data["Enum"].choices["One"], sdf.IntegerData)
assert test_model.data["Enum"].choices["One"].const == 1


def test_boolean_data(test_model: sdf.SDF):
def test_boolean_data(test_model: sdf.Document):
assert isinstance(test_model.data["Boolean"], sdf.BooleanData)
assert test_model.data["Boolean"].const == True


def test_string_data(test_model: sdf.SDF):
def test_string_data(test_model: sdf.Document):
assert isinstance(test_model.data["String"], sdf.StringData)
assert test_model.data["String"].min_length == 10
assert test_model.data["String"].max_length == 100
assert test_model.data["String"].pattern == ".*"


def test_bytestring_data(test_model: sdf.SDF):
def test_bytestring_data(test_model: sdf.Document):
assert isinstance(test_model.data["ByteString"], sdf.StringData)
assert test_model.data["ByteString"].sdf_type == "byte-string"


def test_array_data(test_model: sdf.SDF):
def test_array_data(test_model: sdf.Document):
assert isinstance(test_model.data["Array"], sdf.ArrayData)
assert test_model.data["Array"].min_items == 1
assert test_model.data["Array"].max_items == 5

assert isinstance(test_model.data["Array"].items, sdf.IntegerData)


def test_object_data(test_model: sdf.SDF):
def test_object_data(test_model: sdf.Document):
assert isinstance(test_model.data["Object"], sdf.ObjectData)

assert isinstance(test_model.data["Object"].properties["prop1"], sdf.NumberData)
assert test_model.data["Object"].required == ["prop1"]


def test_unknown_data(test_model: sdf.SDF):
def test_unknown_data(test_model: sdf.Document):
assert isinstance(test_model.data["Unknown"], sdf.AnyData)


Expand Down
14 changes: 7 additions & 7 deletions tests/sdf/test_properties.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
from onedm import sdf


def test_integer_property(test_model: sdf.SDF):
def test_integer_property(test_model: sdf.Document):
assert isinstance(test_model.properties["IntegerProperty"], sdf.IntegerProperty)
assert test_model.properties["IntegerProperty"].label == "Example integer"


def test_number_property(test_model: sdf.SDF):
def test_number_property(test_model: sdf.Document):
assert isinstance(test_model.properties["NumberProperty"], sdf.NumberProperty)
assert test_model.properties["NumberProperty"].label == "Example number"


def test_boolean_property(test_model: sdf.SDF):
def test_boolean_property(test_model: sdf.Document):
assert isinstance(test_model.properties["BooleanProperty"], sdf.BooleanProperty)
assert test_model.properties["BooleanProperty"].label == "Example boolean"


def test_string_property(test_model: sdf.SDF):
def test_string_property(test_model: sdf.Document):
assert isinstance(test_model.properties["StringProperty"], sdf.StringProperty)
assert test_model.properties["StringProperty"].label == "Example string"


def test_array_property(test_model: sdf.SDF):
def test_array_property(test_model: sdf.Document):
assert isinstance(test_model.properties["ArrayProperty"], sdf.ArrayProperty)
assert test_model.properties["ArrayProperty"].label == "Example array"


def test_object_property(test_model: sdf.SDF):
def test_object_property(test_model: sdf.Document):
assert isinstance(test_model.properties["ObjectProperty"], sdf.ObjectProperty)
assert test_model.properties["ObjectProperty"].label == "Example object"


def test_unknown_property(test_model: sdf.SDF):
def test_unknown_property(test_model: sdf.Document):
assert isinstance(test_model.properties["UnknownProperty"], sdf.AnyProperty)
2 changes: 1 addition & 1 deletion tests/sdf/test_references.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from onedm import sdf


def test_reference(test_model: sdf.SDF):
def test_reference(test_model: sdf.Document):
assert isinstance(test_model.data["Reference"], sdf.IntegerData)
assert test_model.data["Reference"].const == 0
4 changes: 2 additions & 2 deletions tests/sdf/test_value_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def test_int_enum_non_const_value():
assert value == 2


def test_number_validation(test_model: sdf.SDF):
def test_number_validation(test_model: sdf.Document):
assert test_model.data["Number"].validate_input(0.5) == 0.5
assert test_model.data["Number"].validate_input(1) == 1.0

Expand All @@ -54,7 +54,7 @@ def test_number_validation(test_model: sdf.SDF):
test_model.data["Number"].validate_input(0.1)


def test_string_validation(test_model: sdf.SDF):
def test_string_validation(test_model: sdf.Document):
assert test_model.data["String"].validate_input("0123456789") == "0123456789"

# Invalid length
Expand Down

0 comments on commit b8d992d

Please sign in to comment.