Skip to content

Commit

Permalink
Allow population of SparqlResult pydantic models by name
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkleiven committed Dec 7, 2023
1 parent 504335c commit 84b515f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
14 changes: 9 additions & 5 deletions cimsparql/sparql_result_json.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
from polyfactory.decorators import post_generated
from polyfactory.factories import pydantic_factory
from pydantic import BaseModel, Field
from pydantic import BaseModel, ConfigDict, Field


class SparqlResultHead(BaseModel):
class CimsparqlBaseModel(BaseModel):
model_config = ConfigDict(populate_by_name=True)


class SparqlResultHead(CimsparqlBaseModel):
link: list[str] = Field(default_factory=list)
variables: list[str] = Field(default_factory=list, alias="vars")


class SparqlResultValue(BaseModel):
class SparqlResultValue(CimsparqlBaseModel):
value_type: str = Field(alias="type")
value: str
datatype: str = ""


class SparqlData(BaseModel):
class SparqlData(CimsparqlBaseModel):
bindings: list[dict[str, SparqlResultValue]]


class SparqlResultJson(BaseModel):
class SparqlResultJson(CimsparqlBaseModel):
"""
Data model for rest api resonse of MIME type
Expand Down
15 changes: 15 additions & 0 deletions tests/test_sparql_result_json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from cimsparql.sparql_result_json import SparqlResultHead, SparqlResultValue


def test_populate_sparql_result_head_by_name():
sparql_head_1 = SparqlResultHead(variables=["a", "b"], links=["l1", "l2"])

sparql_head_2 = SparqlResultHead(vars=["a", "b"], links=["l1", "l2"])
assert sparql_head_1 == sparql_head_2


def test_populate_sparql_result_value_by_name():
sparql_value_1 = SparqlResultValue(value_type="literal", value="value")

sparql_value_2 = SparqlResultValue(type="literal", value="value")
assert sparql_value_1 == sparql_value_2

0 comments on commit 84b515f

Please sign in to comment.