Skip to content

Commit

Permalink
Convert unittest to pytest (#478)
Browse files Browse the repository at this point in the history
* Update PR template

* Update release-notes.rst

* Squash merge last in general

* climatologies.py pytest.mark.parametrize()

* test_fourier_filters into classes

* test_gradient unittest to pytest

* Upstream git reference

* Revert "Upstream git reference"

This reverts commit dd64679.

* function names reset

* update unittest to pytest

* update release-notes.rst

* remove unittest import reference

* remove unittest flag

* classmethod to pytest.fixture()

* pytest fixtures

* seperate file IO into fixtures

* 3.9 __name__ attribute

* cls -> self.__class__

* cls -> type(self)

* convert remaining setUpClass to type(self)

* rewrite fixtures to remove setup()

* reduce test_intput/output fixtures

* move release notes section to next release

---------

Co-authored-by: Anissa Zacharias <[email protected]>
  • Loading branch information
cyschneck and anissa111 authored Oct 26, 2023
1 parent c37bfc8 commit 57ca26a
Show file tree
Hide file tree
Showing 9 changed files with 1,144 additions and 1,210 deletions.
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ jobs:
uses: codecov/[email protected]
with:
file: ./coverage.xml
flags: unittests
env_vars: OS,PYTHON
name: codecov-umbrella
fail_ci_if_error: false
Expand Down
4 changes: 4 additions & 0 deletions docs/release-notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ Release Notes
v2023.10.1 (Unreleased)
-----------------------

Maintenance
^^^^^^^^^^^
* Convert Unittest to Pytest by `Cora Schneck`_ in (:pr:`462`)

Documentation
^^^^^^^^^^^^^
* Updated office hours link by `Anissa Zacharias`_ in (:pr:`495`)
Expand Down
251 changes: 134 additions & 117 deletions test/test_climatologies.py

Large diffs are not rendered by default.

351 changes: 182 additions & 169 deletions test/test_fourier_filters.py

Large diffs are not rendered by default.

260 changes: 83 additions & 177 deletions test/test_gradient.py
Original file line number Diff line number Diff line change
@@ -1,191 +1,97 @@
import sys
import unittest
import pytest

import numpy as np
import xarray as xr

from geocat.comp import gradient


class Test_Gradient(unittest.TestCase):
test_data_xr = None
test_data_np = None
test_data_dask = None
test_results_lon = None
test_results_lat = None
test_coords_1d_lon = None
test_coords_1d_lat = None
test_coords_2d_lon_np = None
test_coords_2d_lat_np = None
test_coords_1d_lat_np = None
test_coords_1d_lon_np = None
class Test_Gradient:

results = None
results_lon = None
results_lat = None

@classmethod
def setUpClass(cls):
cls.test_data_xr = xr.load_dataset(
@pytest.fixture(scope="class")
def test_data_xr(self):
return xr.load_dataset(
'test/gradient_test_data.nc').to_array().squeeze()
cls.test_data_xr_nocoords = xr.DataArray(cls.test_data_xr, coords={})
cls.test_data_np = cls.test_data_xr.values
cls.test_data_dask = cls.test_data_xr.chunk(10)
cls.test_results_lon = xr.load_dataset(
'test/gradient_test_results_longitude.nc').to_array().squeeze()
cls.test_results_lat = xr.load_dataset(
'test/gradient_test_results_latitude.nc').to_array().squeeze()
cls.test_coords_1d_lon = cls.test_data_xr.coords['lon']
cls.test_coords_1d_lat = cls.test_data_xr.coords['lat']
cls.test_coords_2d_lon_np, cls.test_coords_2d_lat_np = np.meshgrid(
cls.test_coords_1d_lon, cls.test_coords_1d_lat)
cls.test_data_xr_2d_coords = xr.DataArray(
cls.test_data_xr,
dims=['x', 'y'],
coords=dict(
lon=(['x', 'y'], cls.test_coords_2d_lon_np),
lat=(['x', 'y'], cls.test_coords_2d_lat_np),
),
)
cls.test_coords_1d_lon_np = cls.test_coords_1d_lon.values
cls.test_coords_1d_lat_np = cls.test_coords_1d_lat.values

def test_gradient_axis0_xr(self):
self.results = gradient(self.test_data_xr)
self.results_axis0 = self.results[0]
np.testing.assert_almost_equal(
self.results_axis0.values,
self.test_results_lon.values,
decimal=3,
)

def test_gradient_axis1_xr(self):
self.results = gradient(self.test_data_xr)
self.results_axis1 = self.results[1]
np.testing.assert_almost_equal(
self.results_axis1.values,
self.test_results_lat.values,
decimal=3,
)

def test_gradient_axis0_dask(self):
self.results = gradient(self.test_data_dask)
self.results_axis0 = self.results[0]
np.testing.assert_almost_equal(
self.results_axis0.values,
self.test_results_lon.values,
decimal=3,
)

def test_gradient_axis1_dask(self):
self.results = gradient(self.test_data_dask)
self.results_axis1 = self.results[1]
np.testing.assert_almost_equal(
self.results_axis1.values,
self.test_results_lat.values,
decimal=3,
)

def test_gradient_axis0_xr_1d_nocoords(self):
self.results = gradient(self.test_data_xr_nocoords,
lon=self.test_coords_1d_lon,
lat=self.test_coords_1d_lat)
self.results_axis0 = self.results[0]
np.testing.assert_almost_equal(
self.results_axis0.values,
self.test_results_lon.values,
decimal=3,
)

def test_gradient_axis1_xr_1d_nocoords(self):
self.results = gradient(self.test_data_xr_nocoords,
lon=self.test_coords_1d_lon,
lat=self.test_coords_1d_lat)
self.results_axis1 = self.results[1]
np.testing.assert_almost_equal(
self.results_axis1.values,
self.test_results_lat.values,
decimal=3,
)

def test_gradient_axis0_xr_2d_nocoords(self):
self.results = gradient(self.test_data_xr_nocoords,
self.test_coords_2d_lon_np,
self.test_coords_2d_lat_np)
self.results_axis0 = self.results[0]
np.testing.assert_almost_equal(
self.results_axis0.values,
self.test_results_lon.values,
decimal=3,
)

def test_gradient_axis1_xr_2d_nocoords(self):
self.results = gradient(self.test_data_xr_nocoords,
self.test_coords_2d_lon_np,
self.test_coords_2d_lat_np)
self.results_axis1 = self.results[1]
np.testing.assert_almost_equal(
self.results_axis1.values,
self.test_results_lat.values,
decimal=3,
)

def test_gradient_axis0_xr_2d_coords(self):
self.results = gradient(self.test_data_xr_2d_coords)
self.results_axis0 = self.results[0]
np.testing.assert_almost_equal(
self.results_axis0.values,
self.test_results_lon.values,
decimal=3,
)

def test_gradient_axis1_xr_2d_coords(self):
self.results = gradient(self.test_data_xr_2d_coords)
self.results_axis1 = self.results[1]
np.testing.assert_almost_equal(
self.results_axis1.values,
self.test_results_lat.values,
decimal=3,
@pytest.fixture(scope="class")
def expected_results(self):
return [
xr.load_dataset(
'test/gradient_test_results_longitude.nc').to_array().squeeze(),
xr.load_dataset(
'test/gradient_test_results_latitude.nc').to_array().squeeze()
]

@pytest.fixture(scope="class")
def lat_lon_meshgrid(self, test_data_xr):
return np.meshgrid(test_data_xr.coords["lon"],
test_data_xr.coords["lat"])

def test_gradient_xr(self, test_data_xr, expected_results) -> None:
actual_result = gradient(test_data_xr)
np.testing.assert_almost_equal(np.array(actual_result),
np.array(expected_results),
decimal=3)

def test_gradient_dask(self, test_data_xr, expected_results) -> None:
actual_result = gradient(test_data_xr.chunk(10))
np.testing.assert_almost_equal(np.array(actual_result),
np.array(expected_results),
decimal=3)

def test_gradient_xr_1d_nocoords(self, test_data_xr,
expected_results) -> None:
actual_result = gradient(xr.DataArray(test_data_xr, coords={}),
lon=test_data_xr.coords["lon"],
lat=test_data_xr.coords["lat"])
np.testing.assert_almost_equal(np.array(actual_result),
np.array(expected_results),
decimal=3)

def test_gradient_xr_2d_nocoords(self, test_data_xr, expected_results,
lat_lon_meshgrid) -> None:
(lon_2d, lat_2d) = lat_lon_meshgrid
actual_result = gradient(
xr.DataArray(test_data_xr, coords={}),
lon=lon_2d,
lat=lat_2d,
)

def test_gradient_axis0_np_1d_nocoords(self):
self.results = gradient(self.test_data_np,
lon=self.test_coords_1d_lon_np,
lat=self.test_coords_1d_lat_np)
self.results_axis0 = self.results[0]
np.testing.assert_almost_equal(
self.results_axis0,
self.test_results_lon.values,
decimal=3,
)

def test_gradient_axis1_np_1d_nocoords(self):
self.results = gradient(self.test_data_np,
lon=self.test_coords_1d_lon_np,
lat=self.test_coords_1d_lat_np)
self.results_axis1 = self.results[1]
np.testing.assert_almost_equal(
self.results_axis1,
self.test_results_lat.values,
decimal=3,
)

def test_gradient_axis0_np_2d_nocoords(self):
self.results = gradient(self.test_data_np, self.test_coords_2d_lon_np,
self.test_coords_2d_lat_np)
self.results_axis0 = self.results[0]
np.testing.assert_almost_equal(
self.results_axis0,
self.test_results_lon.values,
decimal=3,
np.testing.assert_almost_equal(np.array(actual_result),
np.array(expected_results),
decimal=3)

def test_gradient_xr_2d_coords(self, test_data_xr, expected_results,
lat_lon_meshgrid) -> None:
test_data_xr_2d_coords = xr.DataArray(
test_data_xr,
dims=["x", "y"],
coords=dict(
lon=(["x", "y"], lat_lon_meshgrid[0]),
lat=(["x", "y"], lat_lon_meshgrid[1]),
),
)

def test_gradient_axis1_np_2d_nocoords(self):
self.results = gradient(self.test_data_np, self.test_coords_2d_lon_np,
self.test_coords_2d_lat_np)
self.results_axis1 = self.results[1]
np.testing.assert_almost_equal(
self.results_axis1,
self.test_results_lat.values,
decimal=3,
actual_result = gradient(test_data_xr_2d_coords)
np.testing.assert_almost_equal(np.array(actual_result),
np.array(expected_results),
decimal=3)

def test_gradient_np_1d_nocoords(self, test_data_xr,
expected_results) -> None:
actual_result = gradient(
test_data_xr.values,
lon=test_data_xr.coords["lon"].values,
lat=test_data_xr.coords["lat"].values,
)
np.testing.assert_almost_equal(actual_result,
np.array(expected_results),
decimal=3)

def test_gradient_np_2d_nocoords(self, test_data_xr, expected_results,
lat_lon_meshgrid) -> None:
(lon_2d, lat_2d) = lat_lon_meshgrid
actual_result = gradient(test_data_xr.values, lon_2d, lat_2d)

np.testing.assert_almost_equal(actual_result,
np.array(expected_results),
decimal=3)
Loading

0 comments on commit 57ca26a

Please sign in to comment.