Skip to content

Commit

Permalink
Allow use of pypgstac loader within same minor version
Browse files Browse the repository at this point in the history
  • Loading branch information
drnextgis committed Feb 14, 2023
1 parent f42e233 commit 7091654
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
8 changes: 7 additions & 1 deletion pypgstac/pypgstac/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import psycopg
from orjson import JSONDecodeError
from plpygis.geometry import Geometry
from pkg_resources import parse_version as V
from psycopg import sql
from psycopg.types.range import Range
from smart_open import open
Expand Down Expand Up @@ -161,7 +162,12 @@ def __init__(self, db: PgstacDB):
self._partition_cache: Dict[str, Partition] = {}

def check_version(self) -> None:
if self.db.version != __version__:
pypgstac_version = V(__version__)
db_version = V(self.db.version)
if (db_version.major, db_version.minor) != (
pypgstac_version.major,
pypgstac_version.minor,
):
raise Exception(
f"pypgstac version {__version__} is not compatible with the target"
f" database version {self.db.version}."
Expand Down
30 changes: 30 additions & 0 deletions pypgstac/tests/test_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
import json
from pathlib import Path
from unittest import mock
from pkg_resources import parse_version as V
from pypgstac.load import Methods, Loader, read_json
from psycopg.errors import UniqueViolation
import pytest
import pystac
import pypgstac.version

HERE = Path(__file__).parent
TEST_DATA_DIR = HERE.parent.parent / "test" / "testdata"
Expand Down Expand Up @@ -366,3 +368,31 @@ def test_load_items_incompatible_version(loader: Loader) -> None:
str(TEST_ITEMS),
insert_mode=Methods.insert,
)


@mock.patch(
pypgstac.version.__version__,
side_effect=lambda v: V(".".join(map(str, [v.major, v.minor + 1, v.micro]))),
)
def test_load_collections_compatible_major_minor_version(loader: Loader) -> None:
"""Test pypgstac collections loader doesn't raise an exception."""
with pytest.raises(Exception):
loader.load_collections(
str(TEST_COLLECTIONS_JSON),
insert_mode=Methods.insert,
)
assert pypgstac.version.__version__ != loader.db.version


@mock.patch(
pypgstac.version.__version__,
side_effect=lambda v: V(".".join(map(str, [v.major, v.minor + 1, v.micro]))),
)
def test_load_items_compatible_major_minor_version(loader: Loader) -> None:
"""Test pypgstac items loader doesn't raise an exception."""
with pytest.raises(Exception):
loader.load_items(
str(TEST_ITEMS),
insert_mode=Methods.insert,
)
assert pypgstac.version.__version__ != loader.db.version

0 comments on commit 7091654

Please sign in to comment.