Skip to content

Commit

Permalink
Additional unit tests (#406)
Browse files Browse the repository at this point in the history
* code: unit tests for name, module and crs

* code: fix unit test on name

* code: add unit test on shape and extent

* code:add unit tests for extent, bounds, transform and transform_r

* code: reformat code

* code: restore cutout.py

* code: override __eq__ method

* code: add equals method

* code: add unit test for equals method

* Update atlite/cutout.py

* Update atlite/cutout.py

* Update atlite/cutout.py

---------

Co-authored-by: Fabrizio Finozzi <[email protected]>
Co-authored-by: Lukas Trippe <[email protected]>
  • Loading branch information
3 people authored Nov 21, 2024
1 parent cf3a07b commit 1f994fd
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
9 changes: 9 additions & 0 deletions atlite/cutout.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,15 @@ def uniform_density_layout(self, capacity_density, crs=None):
"""
return capacity_density * self.area(crs)

def equals(self, other):
"""
It overrides xarray.Dataset.equals and ignores the path attribute in the comparison
"""
if not isinstance(other, Cutout):
return NotImplemented
# Compare cutouts data attributes
return self.data.equals(other.data)

def layout_from_capacity_list(self, data, col="Capacity"):
"""
Get a capacity layout aligned to the cutout based on a capacity list.
Expand Down
48 changes: 48 additions & 0 deletions test/test_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import numpy as np
import pytest
import rasterio as rio
from xarray.testing import assert_equal

from atlite import Cutout
Expand All @@ -31,6 +32,53 @@ def ref():
)


def test_name(ref):
assert ref.name == "creation_ref"


def test_module(ref):
assert ref.module == "era5"


def test_crs(ref):
assert ref.crs == "EPSG:4326"


def test_shape(ref):
assert ref.shape == (21, 23)


def test_extent(ref):
reference_extent = [-4.125, 1.625, 55.875, 61.125]
assert all([x == y for x, y in zip(ref.extent, reference_extent)])


def test_bounds(ref):
reference_extent = [-4.125, 55.875, 1.625, 61.125]
assert all([x == y for x, y in zip(ref.bounds, reference_extent)])


def test_transform(ref):
reference_affine = rio.Affine(
0.25, 0.00, -4.125, 0.00, 0.25, 55.875, 0.00, 0.00, 1.00
)
assert reference_affine == ref.transform


def test_equals(ref):
test_cutout = Cutout(
path="creation_ref", module="era5", bounds=(X0, Y0, X1, Y1), time=TIME
)
assert ref.equals(test_cutout)


def test_transform_r(ref):
reference_affine = rio.Affine(
0.25, 0.00, -4.125, 0.00, -0.25, 61.125, 0.00, 0.00, 1.00
)
assert reference_affine == ref.transform_r


def test_odd_bounds_coords(ref):
cutout = Cutout(
path="odd_bounds",
Expand Down

0 comments on commit 1f994fd

Please sign in to comment.