Skip to content

Commit

Permalink
Adjust moto and lazy fixture after recent updates (#786)
Browse files Browse the repository at this point in the history
* general changes

* fix remaining problems
  • Loading branch information
zigaLuksic authored Jan 29, 2024
1 parent 43545cf commit 88ce0bb
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 64 deletions.
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,11 @@ dev = [
"build",
"eo-learn[FULL]",
"hypothesis",
"moto",
"moto[s3]>=5.0.0",
"mypy>=0.990",
"pylint>=2.14.0",
"pytest>=7.0.0",
"pytest-cov",
"pytest-lazy-fixture",
"pytest-mock",
"twine",
"types-python-dateutil",
Expand Down
27 changes: 0 additions & 27 deletions tests/core/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@
from __future__ import annotations

import os
from typing import Callable

import boto3
import pytest
from fs_s3fs import S3FS
from moto import mock_s3

from eolearn.core import EOPatch

Expand All @@ -28,26 +24,3 @@ def test_eopatch_path_fixture() -> str:
@pytest.fixture(name="test_eopatch")
def test_eopatch_fixture(test_eopatch_path) -> EOPatch:
return EOPatch.load(test_eopatch_path)


@pytest.fixture(name="create_mocked_s3fs", scope="session")
def s3_mocking_fixture() -> Callable[[str], S3FS]:
"""Provides a function for mocking S3 buckets"""

@mock_s3
def create_mocked_s3fs(bucket_name: str = "mocked-test-bucket") -> S3FS:
"""Creates a new empty mocked s3 bucket. If one such bucket already exists it deletes it first."""
s3resource = boto3.resource("s3", region_name="eu-central-1")

bucket = s3resource.Bucket(bucket_name)

if bucket.creation_date: # If bucket already exists
for key in bucket.objects.all():
key.delete()
bucket.delete()

s3resource.create_bucket(Bucket=bucket_name, CreateBucketConfiguration={"LocationConstraint": "eu-central-1"})

return S3FS(bucket_name=bucket_name)

return create_mocked_s3fs
7 changes: 6 additions & 1 deletion tests/core/test_eodata.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,12 @@ def test_get_spatial_dimension(feature: Feature, expected_dim: tuple[int, int],
("patch", "expected_features"),
[
(
pytest.lazy_fixture("mini_eopatch"),
generate_eopatch({
FeatureType.DATA: ["A", "B"],
FeatureType.MASK: ["C", "D"],
FeatureType.MASK_TIMELESS: ["E"],
FeatureType.META_INFO: ["beep"],
}),
[
(FeatureType.DATA, "A"),
(FeatureType.DATA, "B"),
Expand Down
80 changes: 50 additions & 30 deletions tests/core/test_eodata_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@
import warnings
from typing import Any

import boto3
import fs
import geopandas as gpd
import numpy as np
import pytest
from fs.base import FS
from fs.errors import CreateFailed, ResourceNotFound
from fs.tempfs import TempFS
from fs_s3fs import S3FS
from geopandas import GeoDataFrame
from moto import mock_s3
from moto import mock_aws
from numpy.testing import assert_array_equal
from shapely.geometry import Point

Expand All @@ -42,7 +44,25 @@
from eolearn.core.utils.parsing import FeatureParser
from eolearn.core.utils.testing import assert_feature_data_equal, generate_eopatch

FS_LOADERS = [TempFS, pytest.lazy_fixture("create_mocked_s3fs")]

@mock_aws
def create_mocked_s3fs(bucket_name: str = "mocked-test-bucket") -> S3FS:
"""Creates a new empty mocked s3 bucket. If one such bucket already exists it deletes it first."""
s3resource = boto3.resource("s3", region_name="eu-central-1")

bucket = s3resource.Bucket(bucket_name)

if bucket.creation_date: # If bucket already exists
for key in bucket.objects.all():
key.delete()
bucket.delete()

s3resource.create_bucket(Bucket=bucket_name, CreateBucketConfiguration={"LocationConstraint": "eu-central-1"})

return S3FS(bucket_name=bucket_name)


FS_LOADERS = [TempFS, create_mocked_s3fs]

DUMMY_BBOX = BBox((0, 0, 1, 1), CRS.WGS84)

Expand Down Expand Up @@ -88,7 +108,7 @@ def eopatch_fixture():
return eopatch


@mock_s3
@mock_aws
@pytest.mark.parametrize("fs_loader", FS_LOADERS)
@pytest.mark.parametrize("use_zarr", [True, False])
def test_saving_in_empty_folder(eopatch, fs_loader, use_zarr: bool):
Expand All @@ -105,7 +125,7 @@ def test_saving_in_empty_folder(eopatch, fs_loader, use_zarr: bool):
assert temp_fs.exists(f"/{subfolder}/bbox.geojson")


@mock_s3
@mock_aws
@pytest.mark.parametrize("fs_loader", FS_LOADERS)
@pytest.mark.parametrize("use_zarr", [True, False])
@pytest.mark.usefixtures("_silence_warnings")
Expand All @@ -126,7 +146,7 @@ def test_saving_in_non_empty_folder(eopatch, fs_loader, use_zarr: bool):
assert not temp_fs.exists(empty_file)


@mock_s3
@mock_aws
@pytest.mark.parametrize("fs_loader", FS_LOADERS)
@pytest.mark.parametrize("use_zarr", [True, False])
@pytest.mark.usefixtures("_silence_warnings")
Expand All @@ -153,7 +173,7 @@ def test_overwriting_non_empty_folder(eopatch, fs_loader, use_zarr: bool):
assert new_eopatch == merge_eopatches(eopatch, add_eopatch)


@mock_s3
@mock_aws
@pytest.mark.parametrize("fs_loader", FS_LOADERS)
@pytest.mark.parametrize("use_zarr", [True, False])
@pytest.mark.parametrize(
Expand Down Expand Up @@ -188,7 +208,7 @@ def test_save_load_partial(
assert feature not in loaded_eopatch


@mock_s3
@mock_aws
@pytest.mark.parametrize("fs_loader", FS_LOADERS)
@pytest.mark.parametrize("use_zarr", [True, False])
def test_save_add_only_features(eopatch, fs_loader, use_zarr: bool):
Expand All @@ -211,15 +231,15 @@ def test_save_add_only_features(eopatch, fs_loader, use_zarr: bool):
)


@mock_s3
@mock_aws
@pytest.mark.parametrize("fs_loader", FS_LOADERS)
def test_bbox_always_saved(eopatch, fs_loader):
with fs_loader() as temp_fs:
eopatch.save("/", filesystem=temp_fs, features=[FeatureType.DATA])
assert temp_fs.exists("/bbox.geojson")


@mock_s3
@mock_aws
@pytest.mark.parametrize("fs_loader", FS_LOADERS)
@pytest.mark.parametrize(
("save_timestamps", "features", "should_save"),
Expand Down Expand Up @@ -249,7 +269,7 @@ def test_auto_save_load_timestamps(eopatch):
assert EOPatch.load("/", filesystem=temp_fs).timestamps is not None


@mock_s3
@mock_aws
@pytest.mark.parametrize("fs_loader", FS_LOADERS)
@pytest.mark.parametrize(
("load_timestamps", "features", "should_load"),
Expand Down Expand Up @@ -282,7 +302,7 @@ def test_load_timestamps_when_nonexistant(eopatch, features):
loaded_patch = EOPatch.load("/", filesystem=temp_fs, features=features, load_timestamps=True)


@mock_s3
@mock_aws
@pytest.mark.parametrize("fs_loader", FS_LOADERS)
@pytest.mark.parametrize("use_zarr", [True, False])
def test_temporally_empty_patch_io(fs_loader, use_zarr: bool):
Expand All @@ -293,7 +313,7 @@ def test_temporally_empty_patch_io(fs_loader, use_zarr: bool):
assert eopatch == EOPatch.load("/", filesystem=temp_fs)


@mock_s3
@mock_aws
@pytest.mark.parametrize("fs_loader", FS_LOADERS)
@pytest.mark.parametrize("use_zarr", [True, False])
@pytest.mark.usefixtures("_silence_warnings")
Expand Down Expand Up @@ -326,7 +346,7 @@ def test_overwrite_failure(fs_loader, use_zarr: bool):
)


@mock_s3
@mock_aws
@pytest.mark.parametrize("fs_loader", FS_LOADERS)
@pytest.mark.parametrize("compress_level", [0, 1])
def test_compression_deprecation(eopatch, fs_loader, compress_level: int | None):
Expand All @@ -338,14 +358,14 @@ def test_compression_deprecation(eopatch, fs_loader, compress_level: int | None)
with fs_loader() as temp_fs:
SaveTask(folder, filesystem=temp_fs)

with fs_loader() as temp_fs, pytest.warns(EODeprecationWarning):
SaveTask(folder, filesystem=temp_fs, compress_level=compress_level)
with fs_loader() as temp_fs, pytest.warns(EODeprecationWarning):
SaveTask(folder, filesystem=temp_fs, compress_level=compress_level)

with fs_loader() as temp_fs, pytest.warns(EODeprecationWarning):
eopatch.save(folder, filesystem=temp_fs, compress_level=compress_level)
with fs_loader() as temp_fs, pytest.warns(EODeprecationWarning):
eopatch.save(folder, filesystem=temp_fs, compress_level=compress_level)


@mock_s3
@mock_aws
@pytest.mark.parametrize("fs_loader", FS_LOADERS)
@pytest.mark.parametrize("use_zarr", [True, False])
def test_save_and_load_tasks(eopatch, fs_loader, use_zarr: bool):
Expand All @@ -367,23 +387,23 @@ def test_save_and_load_tasks(eopatch, fs_loader, use_zarr: bool):
assert eop == eopatch


@mock_s3
@mock_aws
@pytest.mark.parametrize("fs_loader", FS_LOADERS)
def test_fail_saving_nonexistent_feature(eopatch, fs_loader):
features = [(FeatureType.DATA, "nonexistent")]
with fs_loader() as temp_fs, pytest.raises(ValueError):
eopatch.save("/", filesystem=temp_fs, features=features)


@mock_s3
@mock_aws
@pytest.mark.parametrize("fs_loader", FS_LOADERS)
def test_fail_loading_nonexistent_feature(fs_loader):
for features in [[(FeatureType.DATA, "nonexistent")], [(FeatureType.META_INFO, "nonexistent")]]:
with fs_loader() as temp_fs, pytest.raises(OSError):
EOPatch.load("/", filesystem=temp_fs, features=features)


@mock_s3
@mock_aws
@pytest.mark.parametrize("fs_loader", FS_LOADERS)
@pytest.mark.parametrize("use_zarr", [True, False])
def test_nonexistent_location(fs_loader, use_zarr: bool):
Expand Down Expand Up @@ -419,7 +439,7 @@ def test_nonexistent_location(fs_loader, use_zarr: bool):
assert os.path.exists(full_path)


@mock_s3
@mock_aws
@pytest.mark.parametrize("fs_loader", FS_LOADERS)
def test_cleanup_different_compression(fs_loader, eopatch):
folder = "foo-folder"
Expand Down Expand Up @@ -453,7 +473,7 @@ def test_cleanup_different_compression(fs_loader, eopatch):
assert not temp_fs.exists(mask_timeless_path)


@mock_s3
@mock_aws
@pytest.mark.parametrize("fs_loader", FS_LOADERS)
@pytest.mark.parametrize("use_zarr", [True, False])
@pytest.mark.parametrize("folder_name", ["/", "foo", "foo/bar"])
Expand Down Expand Up @@ -563,7 +583,7 @@ def test_zarr_and_numpy_combined_loading(eopatch):
assert EOPatch.load("/", filesystem=temp_fs) == eopatch


@mock_s3
@mock_aws
@pytest.mark.parametrize("fs_loader", FS_LOADERS)
@pytest.mark.parametrize(
"temporal_selection",
Expand Down Expand Up @@ -591,7 +611,7 @@ def test_partial_temporal_loading(fs_loader: type[FS], eopatch: EOPatch, tempora
assert_array_equal(np.array(full_patch.timestamps)[adjusted_selection], partial_patch.timestamps)


@mock_s3
@mock_aws
@pytest.mark.parametrize("fs_loader", FS_LOADERS)
def test_partial_temporal_loading_fails_for_numpy(fs_loader: type[FS], eopatch: EOPatch):
_skip_when_appropriate(fs_loader, True)
Expand All @@ -602,7 +622,7 @@ def test_partial_temporal_loading_fails_for_numpy(fs_loader: type[FS], eopatch:
EOPatch.load(path="patch-folder", filesystem=temp_fs, temporal_selection=[0])


@mock_s3
@mock_aws
@pytest.mark.parametrize("fs_loader", FS_LOADERS)
@pytest.mark.parametrize("temporal_selection", [[3, 4, 10]])
def test_partial_temporal_loading_fails_bad_selection(fs_loader: type[FS], eopatch: EOPatch, temporal_selection):
Expand All @@ -614,7 +634,7 @@ def test_partial_temporal_loading_fails_bad_selection(fs_loader: type[FS], eopat
EOPatch.load(path="patch-folder", filesystem=temp_fs, temporal_selection=temporal_selection)


@mock_s3
@mock_aws
@pytest.mark.parametrize("temporal_selection", [None, slice(None, 3), slice(2, 4, 2), [3, 4]])
def test_partial_temporal_saving_into_existing(eopatch: EOPatch, temporal_selection):
_skip_when_appropriate(TempFS, True)
Expand All @@ -635,7 +655,7 @@ def test_partial_temporal_saving_into_existing(eopatch: EOPatch, temporal_select
assert_array_equal(loaded_patch.timestamps, eopatch.timestamps)


@mock_s3
@mock_aws
@pytest.mark.parametrize("dtype", [float, np.float32, np.int8])
def test_partial_temporal_saving_just_timestamps(dtype):
_skip_when_appropriate(TempFS, True)
Expand All @@ -659,7 +679,7 @@ def test_partial_temporal_saving_just_timestamps(dtype):
assert_array_equal(loaded_patch.timestamps, patch_skeleton.timestamps)


@mock_s3
@mock_aws
def test_partial_temporal_saving_infer(eopatch: EOPatch):
_skip_when_appropriate(TempFS, True)
with TempFS() as temp_fs:
Expand All @@ -679,7 +699,7 @@ def test_partial_temporal_saving_infer(eopatch: EOPatch):
assert_array_equal(loaded_patch.timestamps, eopatch.timestamps)


@mock_s3
@mock_aws
def test_partial_temporal_saving_fails(eopatch: EOPatch):
_skip_when_appropriate(TempFS, True)
with TempFS() as temp_fs:
Expand Down
Loading

0 comments on commit 88ce0bb

Please sign in to comment.