Skip to content

Commit

Permalink
Adjust tests and model logic to suport pydantic 2 version
Browse files Browse the repository at this point in the history
  • Loading branch information
CaptainOfHacks committed Sep 12, 2023
1 parent f0d8ebd commit 4ac8b8b
Show file tree
Hide file tree
Showing 13 changed files with 191 additions and 183 deletions.
26 changes: 13 additions & 13 deletions ted_sws/core/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,24 @@ class PropertyBaseModel(BaseModel):
https://github.com/samuelcolvin/pydantic/issues/935
is solved
"""

@classmethod
def get_properties(cls):
return [
prop for prop in dir(cls)
if isinstance(getattr(cls, prop), property) and prop not in ("__values__", "fields")
]

def dict(self, *args, **kwargs) -> 'DictStrAny':
self.__dict__.update({prop: getattr(self, prop) for prop in self.get_properties()})

return super().dict(*args, **kwargs)
#TODO : remove this code, because this implementation is not necessary anymore in pydantic 2
# @classmethod
# def get_properties(cls):
# return [
# prop for prop in dir(cls)
# if isinstance(getattr(cls, prop), property) and prop not in ("__values__", "fields")
# ]
#
#TODO : remove this code, because this implementation is not necessary anymore in pydantic 2
# def dict(self, *args, **kwargs) -> 'DictStrAny':
# self.__dict__.update({prop: getattr(self, prop) for prop in self.get_properties()})
# return super().dict(*args, **kwargs)

def __eq__(self, other) -> bool:
if not isinstance(other, self.__class__) or not other:
return False
# raise ValueError(f"Must compare objects of the same class {self.__class__}")
difference = DeepDiff(self.dict(), other.dict())
difference = DeepDiff(self.model_dump(), other.model_dump())
return not difference

def __ne__(self, other):
Expand Down
62 changes: 31 additions & 31 deletions ted_sws/core/model/manifestation.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class XPATHCoverageSummaryResult(PropertyBaseModel):


class XPATHCoverageSummaryReport(PropertyBaseModel):
mapping_suite_identifier: Optional[str]
mapping_suite_identifier: Optional[str] = None
validation_result: Optional[XPATHCoverageSummaryResult] = XPATHCoverageSummaryResult()


Expand All @@ -86,8 +86,8 @@ class SPARQLSummaryCountReport(PropertyBaseModel):


class SPARQLSummaryResult(PropertyBaseModel):
test_suite_identifier: Optional[str]
mapping_suite_identifier: Optional[str]
test_suite_identifier: Optional[str] = None
mapping_suite_identifier: Optional[str] = None
aggregate: Optional[SPARQLSummaryCountReport] = SPARQLSummaryCountReport()


Expand All @@ -107,8 +107,8 @@ class SHACLSummaryResultSeverityReport(PropertyBaseModel):


class SHACLSummaryResult(PropertyBaseModel):
test_suite_identifier: Optional[str]
mapping_suite_identifier: Optional[str]
test_suite_identifier: Optional[str] = None
mapping_suite_identifier: Optional[str] = None
result_severity: Optional[SHACLSummaryResultSeverityReport] = SHACLSummaryResultSeverityReport()


Expand Down Expand Up @@ -141,11 +141,11 @@ class XPATHCoverageValidationAssertion(PropertyBaseModel):
"""
"""
form_field: Optional[str]
xpath: Optional[str]
count: Optional[int]
notice_hit: Optional[Dict[str, int]]
query_result: Optional[bool]
form_field: Optional[str] = None
xpath: Optional[str] = None
count: Optional[int] = None
notice_hit: Optional[Dict[str, int]] = None
query_result: Optional[bool] = None


class XPATHCoverageValidationResultBase(PropertyBaseModel):
Expand All @@ -157,30 +157,30 @@ class XPATHCoverageValidationResultBase(PropertyBaseModel):
xpath_not_covered: Optional[List[str]] = []
xpath_extra: Optional[List[str]] = []
remarked_xpaths: Optional[List[str]] = []
coverage: Optional[float]
conceptual_coverage: Optional[float]
coverage: Optional[float] = None
conceptual_coverage: Optional[float] = None


class XPATHCoverageValidationResult(XPATHCoverageValidationResultBase):
"""
XPATHCoverageValidationResult for Notice
"""
notices: Optional[List[ReportNoticeData]]
notices: Optional[List[ReportNoticeData]] = None


class XPATHCoverageValidationReport(XMLValidationManifestation):
"""
This is the model structure for Notice(s) XPATHs Coverage Report
"""

validation_result: Optional[XPATHCoverageValidationResult]
validation_result: Optional[XPATHCoverageValidationResult] = None


class XMLManifestation(Manifestation):
"""
Original XML Notice manifestation as published on the TED website.
"""
xpath_coverage_validation: XPATHCoverageValidationReport = None
xpath_coverage_validation: Optional[XPATHCoverageValidationReport] = None

def add_validation(self, validation: Union[XPATHCoverageValidationReport]):
if type(validation) == XPATHCoverageValidationReport:
Expand All @@ -197,7 +197,7 @@ class METSManifestation(Manifestation):
"""
type: str = METS_TYPE_CREATE
package_name: str = None
package_name: Optional[str] = None


class RDFValidationManifestation(ValidationManifestation):
Expand All @@ -206,15 +206,15 @@ class RDFValidationManifestation(ValidationManifestation):
"""
mapping_suite_identifier: str
test_suite_identifier: Optional[str]
test_suite_identifier: Optional[str] = None


class SPARQLQuery(PropertyBaseModel):
"""
Stores SPARQL query details
"""
title: Optional[str]
description: Optional[str]
title: Optional[str] = None
description: Optional[str] = None
xpath: Optional[List[str]] = []
query: str

Expand All @@ -224,13 +224,13 @@ class SPARQLQueryResult(PropertyBaseModel):
Stores SPARQL query execution result
"""
query: SPARQLQuery
result: Optional[SPARQLQueryRefinedResultType]
query_result: Optional[str]
result: Optional[SPARQLQueryRefinedResultType] = None
query_result: Optional[str] = None
fields_covered: Optional[bool] = True
missing_fields: Optional[List[str]] = []
error: Optional[str]
message: Optional[str]
identifier: Optional[str]
error: Optional[str] = None
message: Optional[str] = None
identifier: Optional[str] = None

class Config:
use_enum_values = True
Expand All @@ -248,10 +248,10 @@ class QueriedSHACLShapeValidationResult(PropertyBaseModel):
Queried SHACL Validation Report which contains the following variables
?focusNode ?message ?resultPath ?resultSeverity ?sourceConstraintComponent ?sourceShape ?value
"""
conforms: Optional[str]
results_dict: Optional[dict]
error: Optional[str]
identifier: Optional[str]
conforms: Optional[str] = None
results_dict: Optional[dict] = None
error: Optional[str] = None
identifier: Optional[str] = None


class SHACLTestSuiteValidationReport(RDFValidationManifestation):
Expand All @@ -262,7 +262,7 @@ class SHACLTestSuiteValidationReport(RDFValidationManifestation):


class EntityDeduplicationReport(Manifestation):
object_data: Optional[str]
object_data: Optional[str] = None
number_of_duplicates: int
number_of_cets: int
uries: List[str]
Expand All @@ -272,10 +272,10 @@ class RDFManifestation(Manifestation):
"""
Transformed manifestation in RDF format
"""
mapping_suite_id = "unknown_mapping_suite_id"
mapping_suite_id: str = "unknown_mapping_suite_id"
shacl_validations: List[SHACLTestSuiteValidationReport] = []
sparql_validations: List[SPARQLTestSuiteValidationReport] = []
deduplication_report: Optional[EntityDeduplicationReport]
deduplication_report: Optional[EntityDeduplicationReport] = None

def validation_exists(self, validation, validations):
"""
Expand Down
99 changes: 49 additions & 50 deletions ted_sws/core/model/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
# Email: [email protected]

""" """
from typing import List, Optional
from typing import List, Optional, NamedTuple

from pydantic import Field
from pydantic.annotated_types import NamedTuple

from ted_sws.core.model import PropertyBaseModel

Expand Down Expand Up @@ -65,17 +64,17 @@ class NormalisedMetadata(Metadata):
publication_date: str
ojs_issue_number: str
ojs_type: str
city_of_buyer: Optional[List[LanguageTaggedString]]
name_of_buyer: Optional[List[LanguageTaggedString]]
original_language: Optional[str]
country_of_buyer: Optional[str]
eu_institution: Optional[bool]
document_sent_date: Optional[str]
deadline_for_submission: Optional[str]
city_of_buyer: Optional[List[LanguageTaggedString]] = None
name_of_buyer: Optional[List[LanguageTaggedString]] = None
original_language: Optional[str] = None
country_of_buyer: Optional[str] = None
eu_institution: Optional[bool] = None
document_sent_date: Optional[str] = None
deadline_for_submission: Optional[str] = None
notice_type: str
form_type: str
place_of_performance: Optional[List[str]]
extracted_legal_basis_directive: Optional[str]
place_of_performance: Optional[List[str]] = None
extracted_legal_basis_directive: Optional[str] = None
legal_basis_directive: str
form_number: str
eforms_subtype: str
Expand All @@ -90,17 +89,17 @@ class NormalisedMetadataView(Metadata):
publication_date: str
ojs_issue_number: str
ojs_type: str
city_of_buyer: Optional[str]
name_of_buyer: Optional[str]
original_language: Optional[str]
country_of_buyer: Optional[str]
eu_institution: Optional[bool]
document_sent_date: Optional[str]
deadline_for_submission: Optional[str]
city_of_buyer: Optional[str] = None
name_of_buyer: Optional[str] = None
original_language: Optional[str] = None
country_of_buyer: Optional[str] = None
eu_institution: Optional[bool] = None
document_sent_date: Optional[str] = None
deadline_for_submission: Optional[str] = None
notice_type: str
form_type: str
place_of_performance: Optional[List[str]]
extracted_legal_basis_directive: Optional[str]
place_of_performance: Optional[List[str]] = None
extracted_legal_basis_directive: Optional[str] = None
legal_basis_directive: str
form_number: str
eforms_subtype: str
Expand All @@ -113,34 +112,34 @@ class TEDMetadata(Metadata):
"""
Stores notice original metadata
"""
AA: List[str] = None
AC: str = None
CY: List[str] = None
DD: str = None
DI: str = None
DS: str = None
DT: List[str] = None
MA: List[str] = None
NC: List[str] = None
ND: str = None
NL: str = None
OC: List[str] = None
OJ: str = None
OL: str = None
OY: List[str] = None
PC: List[str] = None
PD: str = None
PR: str = None
RC: List[str] = None
RN: List[str] = None
RP: str = None
TD: str = None
TVH: str = None
TVL: str = None
TY: str = None
award_criterion_type: str = Field(default=None, alias='award-criterion-type')
AA: Optional[List[str]] = None
AC: Optional[str] = None
CY: Optional[List[str]] = None
DD: Optional[str] = None
DI: Optional[str] = None
DS: Optional[str] = None
DT: Optional[List[str]] = None
MA: Optional[List[str]] = None
NC: Optional[List[str]] = None
ND: Optional[str] = None
NL: Optional[str] = None
OC: Optional[List[str]] = None
OJ: Optional[str] = None
OL: Optional[str] = None
OY: Optional[List[str]] = None
PC: Optional[List[str]] = None
PD: Optional[str] = None
PR: Optional[str] = None
RC: Optional[List[str]] = None
RN: Optional[List[int]] = None #TODO check if this is a list of strings, now we change to list of ints to fix errors
RP: Optional[str] = None
TD: Optional[str] = None
TVH: Optional[str] = None
TVL: Optional[str] = None
TY: Optional[str] = None
award_criterion_type: Optional[str] = Field(default=None, alias='award-criterion-type')
corporate_body: List[str] = Field(default=None, alias='corporate-body')
funding: List[str] = None
notice_identifier: str = Field(default=None, alias='notice-identifier')
notice_type: str = Field(default=None, alias='notice-type')
notice_version: str = Field(default=None, alias='notice-version')
funding: Optional[List[str]] = None
notice_identifier: Optional[str] = Field(default=None, alias='notice-identifier')
notice_type: Optional[str] = Field(default=None, alias='notice-type')
notice_version: Optional[str] = Field(default=None, alias='notice-version')
Loading

0 comments on commit 4ac8b8b

Please sign in to comment.