-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a error count to latest dataset object in gtfs-feeds (#396)
- Loading branch information
1 parent
c8b520a
commit a50ec2c
Showing
6 changed files
with
137 additions
and
29 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from functools import reduce | ||
|
||
from packaging.version import Version | ||
|
||
from database_gen.sqlacodegen_models import Gtfsdataset | ||
from feeds.impl.models.bounding_box_impl import BoundingBoxImpl | ||
from feeds.impl.models.validation_report_impl import ValidationReportImpl | ||
from feeds_gen.models.latest_dataset import LatestDataset | ||
from feeds_gen.models.latest_dataset_validation_report import LatestDatasetValidationReport | ||
|
||
|
||
class LatestDatasetImpl(LatestDataset): | ||
"""Implementation of the `LatestDataset` model. | ||
This class converts a SQLAlchemy row DB object to a Pydantic model. | ||
""" | ||
|
||
class Config: | ||
"""Pydantic configuration. | ||
Enabling `from_orm` method to create a model instance from a SQLAlchemy row object.""" | ||
|
||
from_attributes = True | ||
orm_mode = True | ||
|
||
@classmethod | ||
def from_orm(cls, dataset: Gtfsdataset | None) -> LatestDataset | None: | ||
"""Create a model instance from a SQLAlchemy a Latest Dataset row object.""" | ||
if not dataset: | ||
return None | ||
validation_report: LatestDatasetValidationReport | None = None | ||
if dataset.validation_reports: | ||
latest_report = reduce( | ||
lambda a, b: a if Version(a.validator_version) > Version(b.validator_version) else b, | ||
dataset.validation_reports, | ||
) | ||
total_error, total_info, total_warning = ValidationReportImpl.compute_totals(latest_report) | ||
validation_report = LatestDatasetValidationReport( | ||
total_error=total_error, | ||
total_warning=total_warning, | ||
total_info=total_info, | ||
) | ||
return cls( | ||
id=dataset.stable_id, | ||
hosted_url=dataset.hosted_url, | ||
bounding_box=BoundingBoxImpl.from_orm(dataset.bounding_box), | ||
downloaded_at=dataset.downloaded_at, | ||
hash=dataset.hash, | ||
validation_report=validation_report, | ||
) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import unittest | ||
from datetime import datetime | ||
|
||
from geoalchemy2 import WKTElement | ||
|
||
from database_gen.sqlacodegen_models import Gtfsdataset, Feed, Validationreport, Notice | ||
from feeds.impl.models.latest_dataset_impl import LatestDatasetImpl | ||
from feeds_gen.models.latest_dataset import LatestDataset | ||
|
||
POLYGON = "POLYGON ((3.0 1.0, 4.0 1.0, 4.0 2.0, 3.0 2.0, 3.0 1.0))" | ||
|
||
|
||
class TestLatestDatasetImpl(unittest.TestCase): | ||
def test_from_orm(self): | ||
now = datetime.now() | ||
assert LatestDatasetImpl.from_orm( | ||
Gtfsdataset( | ||
id="10", | ||
stable_id="stable_id", | ||
feed=Feed(stable_id="feed_stable_id"), | ||
hosted_url="http://example.com", | ||
note="note", | ||
downloaded_at=now, | ||
hash="hash", | ||
bounding_box=WKTElement(POLYGON, srid=4326), | ||
validation_reports=[ | ||
Validationreport(validator_version="1.0.0"), | ||
Validationreport( | ||
validator_version="1.2.0", | ||
notices=[ | ||
Notice(severity="INFO", total_notices=1), | ||
Notice(severity="ERROR", total_notices=2), | ||
Notice(severity="WARNING", total_notices=3), | ||
], | ||
), | ||
Validationreport(validator_version="1.1.1"), | ||
], | ||
) | ||
) == LatestDataset( | ||
id="stable_id", | ||
feed_id="feed_stable_id", | ||
hosted_url="http://example.com", | ||
note="note", | ||
downloaded_at=now, | ||
hash="hash", | ||
bounding_box={ | ||
"minimum_latitude": 1.0, | ||
"maximum_latitude": 2.0, | ||
"minimum_longitude": 3.0, | ||
"maximum_longitude": 4.0, | ||
}, | ||
validation_report={"validator_version": "1.2.0", "total_error": 2, "total_info": 1, "total_warning": 3}, | ||
) | ||
|
||
assert LatestDatasetImpl.from_orm(None) is None |
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