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

fix: java version comparison fails #889

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions api/src/feeds/impl/models/gtfs_dataset_impl.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from functools import reduce
from typing import List

from packaging.version import Version

from database_gen.sqlacodegen_models import Gtfsdataset, Validationreport
from feeds.impl.models.bounding_box_impl import BoundingBoxImpl
from feeds.impl.models.validation_report_impl import ValidationReportImpl
from feeds_gen.models.gtfs_dataset import GtfsDataset
from utils.model_utils import compare_java_versions


class GtfsDatasetImpl(GtfsDataset):
Expand All @@ -30,7 +29,8 @@ def from_orm_latest_validation_report(
"""
if validation_reports:
latest_report = reduce(
lambda a, b: a if Version(a.validator_version) > Version(b.validator_version) else b, validation_reports
lambda a, b: a if compare_java_versions(a.validator_version, b.validator_version) == 1 else b,
validation_reports,
)
return ValidationReportImpl.from_orm(latest_report)
return None
Expand Down
5 changes: 2 additions & 3 deletions api/src/feeds/impl/models/latest_dataset_impl.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
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
from utils.model_utils import compare_java_versions


class LatestDatasetImpl(LatestDataset):
Expand All @@ -28,7 +27,7 @@ def from_orm(cls, dataset: Gtfsdataset | None) -> LatestDataset | 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,
lambda a, b: a if compare_java_versions(a.validator_version, b.validator_version) == 1 else b,
dataset.validation_reports,
)
(
Expand Down
26 changes: 26 additions & 0 deletions api/src/utils/model_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from packaging.version import Version


def compare_java_versions(v1: str | None, v2: str | None):
"""
Compare two version strings v1 and v2.
Returns 1 if v1 > v2, -1 if v1 < v2,
otherwise 0.
The version strings are expected to be in the format of
major.minor.patch[-SNAPSHOT]
"""
if v1 is None and v2 is None:
return 0
if v1 is None:
return -1
if v2 is None:
return 1
# clean version strings replacing the SNAPSHOT suffix with .dev0
v1 = v1.replace("-SNAPSHOT", ".dev0")
v2 = v2.replace("-SNAPSHOT", ".dev0")
if Version(v1) > Version(v2):
return 1
elif Version(v1) < Version(v2):
return -1
else:
return 0
27 changes: 27 additions & 0 deletions api/tests/utils/test_compare_java_versions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import unittest
from utils.model_utils import compare_java_versions


class TestCompareJavaVersions(unittest.TestCase):
def test_compare_versions_equal(self):
self.assertEqual(compare_java_versions("1.0.0", "1.0.0"), 0)
self.assertEqual(compare_java_versions("1.0.0-SNAPSHOT", "1.0.0-SNAPSHOT"), 0)

def test_compare_versions_v1_greater(self):
self.assertEqual(compare_java_versions("1.0.1", "1.0.0"), 1)
self.assertEqual(compare_java_versions("1.0.0", "0.9.9"), 1)
self.assertEqual(compare_java_versions("1.0.0", "1.0.0-SNAPSHOT"), 1)

def test_compare_versions_v2_greater(self):
self.assertEqual(compare_java_versions("1.0.0", "1.0.1"), -1)
self.assertEqual(compare_java_versions("0.9.9", "1.0.0"), -1)
self.assertEqual(compare_java_versions("1.0.0-SNAPSHOT", "1.0.0"), -1)

def test_compare_versions_with_none(self):
self.assertEqual(compare_java_versions(None, None), 0)
self.assertEqual(compare_java_versions(None, "1.0.0"), -1)
self.assertEqual(compare_java_versions("1.0.0", None), 1)


if __name__ == "__main__":
unittest.main()
Loading