Skip to content
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

added test package and refactored logic for eligibility checker #516

Merged
merged 2 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
3 changes: 2 additions & 1 deletion ted_sws/core/model/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ class NormalisedMetadata(Metadata):
legal_basis_directive: str
form_number: str
eforms_subtype: str
xsd_version: str
xsd_version: Optional[str]
published_in_cellar_counter: int = Field(default=0)
is_eform: Optional[bool] = False
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to change this flag to a field:
notice_source: Optional[NoticeSource] = NoticeSource.STANDART_FORMS
and for NoticeSource as example:
`class NoticeSource(str, Enum):
STANDART_FORMS = "standart_forms"
ELECTRONIC_FORMS = "eforms"

def __str__(self):
    return self.value

`

eform_sdk_version: Optional[str]


class NormalisedMetadataView(Metadata):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
END_DATE_KEY = "end_date"
MIN_XSD_VERSION_KEY = "min_xsd_version"
MAX_XSD_VERSION_KEY = "max_xsd_version"
EFORMS_SDK_VERSIONS_KEY = "eforms_sdk_versions"
TITLE_KEY = "title"
CREATED_KEY = "created_at"
IDENTIFIER_KEY = "identifier"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
DEADLINE_DATE_KEY = "deadline_for_submission"
NOTICE_TYPE_KEY = "notice_type"
XSD_VERSION_KEY = "xsd_version"
EFORM_SDK_VERSION_KEY = "eform_sdk_version"
IS_EFORM_KEY = "is_eform"
ENGLISH_LANGUAGE_TAG = "EN"
mapping_registry = MappingFilesRegistry()
Expand Down Expand Up @@ -339,7 +340,7 @@ def normalise_metadata(self, extracted_metadata: ExtractedMetadata) -> Normalise
LEGAL_BASIS_DIRECTIVE_KEY: get_map_value(mapping=legal_basis_map,
value=legal_basis),
E_FORMS_SUBTYPE_KEY: extracted_metadata.extracted_notice_subtype,
XSD_VERSION_KEY: extracted_metadata.xml_schema_version,
EFORM_SDK_VERSION_KEY: extracted_metadata.xml_schema_version,
IS_EFORM_KEY: True
}

Expand Down
51 changes: 41 additions & 10 deletions ted_sws/notice_metadata_processor/services/notice_eligibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,31 @@
from ted_sws.core.model.transform import MappingSuite
from ted_sws.data_manager.adapters.repository_abc import MappingSuiteRepositoryABC, NoticeRepositoryABC
from ted_sws.mapping_suite_processor.services.conceptual_mapping_generate_metadata import START_DATE_KEY, END_DATE_KEY, \
MIN_XSD_VERSION_KEY, MAX_XSD_VERSION_KEY, E_FORMS_SUBTYPE_KEY
MIN_XSD_VERSION_KEY, MAX_XSD_VERSION_KEY, E_FORMS_SUBTYPE_KEY, EFORMS_SDK_VERSIONS_KEY


def format_version_with_zero_patch(version_string:str) -> semantic_version.Version:
"""
This will take a string version (1.7 or 1.7.6) and will transform it to a semantic version with 0 as patch
1.7 -> 1.7.0
1.7.6 -> 1.7.0
"""
parsed_version = semantic_version.Version.coerce(version_string)
return semantic_version.Version(major=parsed_version.major, minor=parsed_version.minor, patch=0)


def is_date_in_range(publication_date, constraint_start_date_value, constraint_end_date_value) -> bool:
"""
This will return True or False if publication_date is in range looking at the start and end date constraints in the
metadata of a mapping suite
"""
if not constraint_start_date_value and not constraint_end_date_value:
return True

start_date = datetime.datetime.fromisoformat(constraint_start_date_value[0])
end_date = datetime.datetime.fromisoformat(
constraint_end_date_value[0] if constraint_end_date_value else datetime.datetime.now().isoformat())
return start_date <= publication_date <= end_date


def check_package(mapping_suite: MappingSuite, notice_metadata: NormalisedMetadata):
Expand All @@ -18,22 +42,29 @@ def check_package(mapping_suite: MappingSuite, notice_metadata: NormalisedMetada
:param mapping_suite:
:return:
"""

constraints = mapping_suite.metadata_constraints.constraints

eform_subtype = notice_metadata.eforms_subtype
notice_publication_date = datetime.datetime.fromisoformat(notice_metadata.publication_date)
notice_xsd_version = notice_metadata.xsd_version

end_date = constraints[END_DATE_KEY][0] if constraints[END_DATE_KEY] else datetime.datetime.now().isoformat()
constraint_start_date = datetime.datetime.fromisoformat(constraints[START_DATE_KEY][0])
constraint_end_date = datetime.datetime.fromisoformat(end_date)
constraint_min_xsd_version = constraints[MIN_XSD_VERSION_KEY][0]
constraint_max_xsd_version = constraints[MAX_XSD_VERSION_KEY][0]
if notice_metadata.is_eform:
notice_xsd_version = notice_metadata.eform_sdk_version
eforms_sdk_version = notice_xsd_version.rsplit('-', 1)[1]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this split need to be explained, maybe we need to use a template or regex selector instead of unclear split.

constraint_version_range = [format_version_with_zero_patch(version) for version in
constraints[EFORMS_SDK_VERSIONS_KEY]]
in_version_range = format_version_with_zero_patch(eforms_sdk_version) in constraint_version_range
else:
notice_xsd_version = notice_metadata.xsd_version
constraint_min_xsd_version = constraints[MIN_XSD_VERSION_KEY][0]
constraint_max_xsd_version = constraints[MAX_XSD_VERSION_KEY][0]
in_version_range = constraint_min_xsd_version <= notice_xsd_version <= constraint_max_xsd_version

in_date_range = is_date_in_range(publication_date=notice_publication_date,
constraint_start_date_value=constraints[START_DATE_KEY],
constraint_end_date_value=constraints[END_DATE_KEY])
eform_subtype_constraint_values = [str(eforms_subtype_value) for eforms_subtype_value in
constraints[E_FORMS_SUBTYPE_KEY]]

in_date_range = constraint_start_date <= notice_publication_date <= constraint_end_date
in_version_range = constraint_min_xsd_version <= notice_xsd_version <= constraint_max_xsd_version
covered_eform_type = eform_subtype in eform_subtype_constraint_values

return True if in_date_range and in_version_range and covered_eform_type else False
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replace:
return True if in_date_range and in_version_range and covered_eform_type else False
with:
return in_date_range and in_version_range and covered_eform_type

Expand Down
30 changes: 30 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,36 @@ def normalised_metadata_object():

return NormalisedMetadata(**data)

@pytest.fixture
def eform_normalised_metadata_object():
data = {
"title": [LanguageTaggedString(text="Eteläisen Salon liikuntapaikkojen hoidon hankinta", language="FIN")],
"long_title": [
LanguageTaggedString(text="FIN :: Eteläisen Salon liikuntapaikkojen hoidon hankinta", language="FIN")],
"notice_publication_number": "00622690-2023",
"publication_date": "2023-10-13T00:00:00",
"ojs_issue_number": "198/2023",
"ojs_type": "S",
"city_of_buyer": None,
"name_of_buyer": None,
"original_language": None,
"country_of_buyer": None,
"eu_institution": None,
"document_sent_date": "2023-10-12T00:00:00",
"deadline_for_submission": None,
"notice_type": "http://publications.europa.eu/resource/authority/notice-type/cn-standard",
"form_type": "http://publications.europa.eu/resource/authority/form-type/competition",
"place_of_performance": ["http://data.europa.eu/nuts/code/FI1C1"],
"extracted_legal_basis_directive": "http://publications.europa.eu/resource/authority/legal-basis/32014L0024",
"legal_basis_directive": "http://publications.europa.eu/resource/authority/legal-basis/32014L0024",
"form_number": "",
"eforms_subtype": "16",
"eform_sdk_version": "eforms-sdk-1.7",
"published_in_cellar_counter": 0,
"is_eform": True
}

return NormalisedMetadata(**data)

@pytest.fixture
@mongomock.patch(servers=(('server.example.com', 27017),))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"identifier": "package_EF16",
"title": "Package EF16 v1.2",
"description": "This is the conceptual mapping for bla bla bla",
"mapping_version": "3.0.0-alpha.1",
"ontology_version": "4.0.0",
"metadata_constraints": {
"constraints": {
"min_xsd_version": ["R2.0.9.S04.E01"],
"max_xsd_version": ["R2.0.9.S04.E01"],
"eforms_subtype": [
"16",
"10",
"11",
"12",
"13",
"X1",
"T1"
],
"start_date": null,
"end_date": null,
"eforms_sdk_versions": [
"1.3",
"1.4",
"1.5",
"1.6",
"1.7",
"1.8",
"1.9",
"1.10"
]
}
}
}
Loading
Loading