Skip to content

Commit

Permalink
test: update the test to avoid error when data are regenerated from G…
Browse files Browse the repository at this point in the history
…oogle side (#255)
  • Loading branch information
12rambau committed Mar 24, 2024
2 parents 26ccfe8 + 5a5125c commit 7c771e2
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 1,275 deletions.
25 changes: 23 additions & 2 deletions geetools/Image/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""Toolbox for the ``ee.Image`` class."""
from __future__ import annotations

from typing import Optional
from datetime import datetime
from typing import Any, Optional

import ee
import ee_extra
Expand All @@ -20,6 +21,26 @@
)


def _add_tz(data: Any) -> Any:
"""Workaround for the lack of timezone info in date strings returned by the Earth Engine API.
related issue: https://issuetracker.google.com/issues/331016656
"""
if isinstance(data, dict):
return {key: _add_tz(value) for key, value in data.items()}
elif isinstance(data, list):
return [_add_tz(item) for item in data]
elif isinstance(data, str):
try:
datetime_obj = datetime.fromisoformat(data)
extra_tz = "Z" if datetime_obj.tzinfo is None else ""
return data + extra_tz # Add Z if no timezone info is present
except ValueError:
return data # Not a valid date string, return unchanged
else:
return data # Return unchanged for non-string values


@register_class_accessor(ee.Image, "geetools")
class ImageAccessor:
"""Toolbox for the ``ee.Image`` class."""
Expand Down Expand Up @@ -1007,7 +1028,7 @@ def getSTAC(self) -> dict:
if collection_stac is None:
raise ValueError(f"Collection {collection} not found in the {project} catalog")

return requests.get(collection_stac).json()
return _add_tz(requests.get(collection_stac).json())

def getDOI(self) -> str:
"""Gets the DOI of the image, if available.
Expand Down
23 changes: 22 additions & 1 deletion geetools/ImageCollection/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Toolbox for the ``ee.ImageCollection`` class."""
from __future__ import annotations

from datetime import datetime
from typing import Any, Optional, Tuple, Union

import ee
Expand All @@ -14,6 +15,26 @@
from geetools.types import ee_list, ee_number, number


def _add_tz(data: Any) -> Any:
"""Workaround for the lack of timezone info in date strings returned by the Earth Engine API.
related issue: https://issuetracker.google.com/issues/331016656
"""
if isinstance(data, dict):
return {key: _add_tz(value) for key, value in data.items()}
elif isinstance(data, list):
return [_add_tz(item) for item in data]
elif isinstance(data, str):
try:
datetime_obj = datetime.fromisoformat(data)
extra_tz = "Z" if datetime_obj.tzinfo is None else ""
return data + extra_tz # Add Z if no timezone info is present
except ValueError:
return data # Not a valid date string, return unchanged
else:
return data # Return unchanged for non-string values


@register_class_accessor(ee.ImageCollection, "geetools")
class ImageCollectionAccessor:
"""Toolbox for the ``ee.ImageCollection`` class."""
Expand Down Expand Up @@ -303,7 +324,7 @@ def getSTAC(self) -> dict:
if collection_stac is None:
raise ValueError(f"Collection {collection} not found in the {project} catalog")

return requests.get(collection_stac).json()
return _add_tz(requests.get(collection_stac).json())

def getDOI(self) -> str:
"""Gets the DOI of the collection, if available.
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Homepage = "https://github.com/gee-community/geetools"
"pytest-regressions",
"Pillow",
"pytest-gee",
"jsonschema",
]
doc = [
"sphinx>=6.2.1",
Expand Down
8 changes: 8 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import ee
import pytest
import pytest_gee
import requests


def pytest_configure() -> None:
Expand Down Expand Up @@ -227,3 +228,10 @@ def climSamp():
normClim = ee.ImageCollection("OREGONSTATE/PRISM/Norm81m").toBands()
region = ee.Geometry.Rectangle(-123.41, 40.43, -116.38, 45.14)
return normClim.sample(region, 5000)


@pytest.fixture(scope="session")
def stac_schema():
"""Return the STAC collection schema."""
url = "https://raw.githubusercontent.com/radiantearth/stac-spec/master/collection-spec/json-schema/collection.json"
return requests.get(url).json()
10 changes: 7 additions & 3 deletions tests/test_Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import ee
import numpy as np
import pytest
from jsonschema import validate

import geetools

Expand Down Expand Up @@ -636,10 +637,13 @@ def test_preprocess(self, vatican_buffer, s2_sr_vatican_2020, num_regression):
class TestGetSTAC:
"""Test the ``getSTAC`` method."""

def test_get_stac(self, s2_sr_vatican_2020, data_regression):
def test_get_stac_schema(self, s2_sr_vatican_2020, stac_schema):
stac = s2_sr_vatican_2020.geetools.getSTAC()
stac["extent"].pop("temporal") # it will change all the time
data_regression.check(stac)
validate(stac, stac_schema)

def test_get_stac(self, s2_sr_vatican_2020):
stac = s2_sr_vatican_2020.geetools.getSTAC()
assert stac["id"] == "COPERNICUS/S2_SR_HARMONIZED"


class TestGetDOI:
Expand Down

0 comments on commit 7c771e2

Please sign in to comment.