Skip to content

Commit

Permalink
@classmethod to @pytest.fixture()
Browse files Browse the repository at this point in the history
  • Loading branch information
cyschneck committed Sep 25, 2023
1 parent 65985f5 commit 495130c
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 61 deletions.
1 change: 0 additions & 1 deletion test/test_fourier_filters.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import math as m
import sys
import pytest
import numpy as np
import xarray as xr

Expand Down
5 changes: 3 additions & 2 deletions test/test_gradient.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sys
import pytest

import numpy as np
import xarray as xr
Expand All @@ -23,8 +24,8 @@ class Test_Gradient:
results_lon = None
results_lat = None

@classmethod
def test_setUpClass(cls) -> None:
@pytest.fixture(autouse=True)
def setUpClass(cls) -> None:
cls.test_data_xr = xr.load_dataset(
'test/gradient_test_data.nc').to_array().squeeze()
cls.test_data_xr_nocoords = xr.DataArray(cls.test_data_xr, coords={})
Expand Down
8 changes: 4 additions & 4 deletions test/test_interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,8 @@ def test_interp_sigma_to_hybrid_wrong_method(self) -> None:

class Test_interp_manually_calc:

@classmethod
def test_setUpClass(cls) -> None:
@pytest.fixture(autouse=True)
def setUpClass(cls) -> None:
cls.test_input = xr.load_dataset(
gdf.get("netcdf_files/interpolation_test_input_data.nc"))

Expand Down Expand Up @@ -454,8 +454,8 @@ class Test_interp_larger_dataset:
test_lon_output = None
test_data_chunked = None

@classmethod
def test_setUpClass(cls) -> None:
@pytest.fixture(autouse=True)
def setUpClass(cls) -> None:
cls.test_input = xr.load_dataset(
gdf.get("netcdf_files/spherical_noise_input.nc"))['spherical_noise']

Expand Down
84 changes: 34 additions & 50 deletions test/test_meteorology.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

class Test_dewtemp:

@classmethod
def test_setUpClass(cls) -> None:
@pytest.fixture(scope="function", autouse=True)
def setUpClass(cls) -> None:
# set up ground truths
cls.t_def = [
29.3, 28.1, 23.5, 20.9, 18.4, 15.9, 13.1, 10.1, 6.7, 3.1, -0.5,
Expand All @@ -41,6 +41,8 @@ def test_setUpClass(cls) -> None:

# make dask client to reference in subsequent tests
cls.client = dd.Client()
yield cls.client
cls.client.close()

def test_float_input(self) -> None:
tk = 18. + 273.15
Expand Down Expand Up @@ -85,15 +87,11 @@ def test_dask_lazy(self) -> None:

assert isinstance((dewtemp(tk, rh) - 273.15).data, dask.array.Array)

@classmethod
def test_closeClient(cls) -> None:
cls.client.close()


class Test_heat_index:

@classmethod
def test_setUpClass(cls) -> None:
@pytest.fixture(scope="function", autouse=True)
def setUpClass(cls) -> None:
# set up ground truths
cls.ncl_gt_1 = [
137.36142, 135.86795, 104.684456, 131.25621, 105.39449, 79.78999,
Expand All @@ -112,6 +110,8 @@ def test_setUpClass(cls) -> None:

# make client to reference in subsequent tests
cls.client = dd.Client()
yield cls.client
cls.client.close()

def test_numpy_input(self) -> None:
assert np.allclose(heat_index(self.t1, self.rh1, False),
Expand Down Expand Up @@ -192,15 +192,11 @@ def test_dask_lazy(self) -> None:

assert isinstance((heat_index(t, rh)).data, dask.array.Array)

@classmethod
def test_closeClient(cls) -> None:
cls.client.close()


class Test_relhum:

@classmethod
def test_setUpClass(cls) -> None:
@pytest.fixture(scope="function", autouse=True)
def setUpClass(cls) -> None:
# set up ground truths
cls.p_def = [
100800, 100000, 95000, 90000, 85000, 80000, 75000, 70000, 65000,
Expand Down Expand Up @@ -232,6 +228,8 @@ def test_setUpClass(cls) -> None:

# make dask client to reference in subsequent tests
cls.client = dd.Client()
yield cls.client
cls.client.close()

def test_float_input(self) -> None:
p = 1000. * 100
Expand Down Expand Up @@ -275,10 +273,6 @@ def test_dask_lazy(self) -> None:

assert isinstance(relhum(t, q, p).data, dask.array.Array)

@classmethod
def test_closeClient(cls) -> None:
cls.client.close()


class Test_relhum_water:

Expand Down Expand Up @@ -307,8 +301,8 @@ def test_float_input(self) -> None:

class Test_actual_saturation_vapor_pressure:

@classmethod
def test_setUpClass(cls) -> None:
@pytest.fixture(scope="function", autouse=True)
def setUpClass(cls) -> None:

# get ground truth from ncl run netcdf file
try:
Expand All @@ -325,6 +319,8 @@ def test_setUpClass(cls) -> None:

# make client to reference in subsequent tests
cls.client = dd.Client()
yield cls.client
cls.client.close()

def test_numpy_input(self) -> None:
assert np.allclose(actual_saturation_vapor_pressure(
Expand Down Expand Up @@ -375,15 +371,11 @@ def test_dask_lazy(self) -> None:
(actual_saturation_vapor_pressure(tempf, tfill=1.0000000e+20)).data,
dask.array.Array)

@classmethod
def test_closeClient(cls) -> None:
cls.client.close()


class Test_max_daylight:

@classmethod
def test_setUpClass(cls) -> None:
@pytest.fixture(scope="function", autouse=True)
def setUpClass(cls) -> None:

# get ground truth from ncl run netcdf file
try:
Expand All @@ -401,6 +393,8 @@ def test_setUpClass(cls) -> None:

# make client to reference in subsequent tests
cls.client = dd.Client()
yield cls.client
cls.client.close()

def test_numpy_input(self) -> None:
assert np.allclose(max_daylight(self.jday_gt, self.lat_gt),
Expand Down Expand Up @@ -450,15 +444,11 @@ def test_lat_bound_second_warning(self) -> None:
with pytest.warns(UserWarning):
max_daylight(10, 67)

@classmethod
def test_closeClient(cls) -> None:
cls.client.close()


class Test_psychrometric_constant:

@classmethod
def test_setUpClass(cls) -> None:
@pytest.fixture(scope="function", autouse=True)
def setUpClass(cls) -> None:

# get ground truth from ncl run netcdf file
try:
Expand All @@ -475,6 +465,8 @@ def test_setUpClass(cls) -> None:

# make client to reference in subsequent tests
cls.client = dd.Client()
yield cls.client
cls.client.close()

def test_numpy_input(self) -> None:
assert np.allclose(psychrometric_constant(self.pressure_gt),
Expand Down Expand Up @@ -520,15 +512,11 @@ def test_dask_lazy(self) -> None:
assert isinstance((psychrometric_constant(pressure)).data,
dask.array.Array)

@classmethod
def test_closeClient(cls) -> None:
cls.client.close()


class Test_saturation_vapor_pressure:

@classmethod
def test_setUpClass(cls) -> None:
@pytest.fixture(scope="function", autouse=True)
def setUpClass(cls) -> None:

# get ground truth from ncl run netcdf file
try:
Expand All @@ -545,6 +533,8 @@ def test_setUpClass(cls) -> None:

# make client to reference in subsequent tests
cls.client = dd.Client()
yield cls.client
cls.client.close()

def test_numpy_input(self) -> None:
assert np.allclose(saturation_vapor_pressure(self.temp_gt,
Expand Down Expand Up @@ -596,15 +586,11 @@ def test_dask_lazy(self) -> None:
tfill=1.0000000e+20)).data,
dask.array.Array)

@classmethod
def test_closeClient(cls) -> None:
cls.client.close()


class Test_saturation_vapor_pressure_slope:

@classmethod
def test_setUpClass(cls) -> None:
@pytest.fixture(scope="function", autouse=True)
def setUpClass(cls) -> None:

# get ground truth from ncl run netcdf file
try:
Expand All @@ -621,6 +607,8 @@ def test_setUpClass(cls) -> None:

# make client to reference in subsequent tests
cls.client = dd.Client()
yield cls.client
cls.client.close()

def test_numpy_input(self) -> None:
assert np.allclose(saturation_vapor_pressure_slope(self.temp_gt),
Expand Down Expand Up @@ -670,15 +658,11 @@ def test_dask_lazy(self) -> None:
assert isinstance((saturation_vapor_pressure_slope(tempf)).data,
dask.array.Array)

@classmethod
def test_closeClient(cls) -> None:
cls.client.close()


class Test_Delta_Pressure:

@classmethod
def test_setUpClass(cls) -> None:
@pytest.fixture(scope="function", autouse=True)
def setUpClass(cls) -> None:
cls.pressure_lev = np.array([1, 5, 100, 1000])
cls.pressure_lev_da = xr.DataArray(cls.pressure_lev)
cls.pressure_lev_da.attrs = {
Expand Down
5 changes: 3 additions & 2 deletions test/test_spherical.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import math as ma
import sys
import pytest

import numpy as np
import scipy.special as ss
Expand All @@ -10,8 +11,8 @@

class Test_Spherical:

@classmethod
def test_setUpClass(cls) -> None:
@pytest.fixture(autouse=True)
def setUpClass(cls) -> None:
max_harm = 23
num_phi = 90
num_theta = 180
Expand Down
4 changes: 2 additions & 2 deletions test/test_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,8 @@ def test_02(self) -> None:

class Test_pearson_r:

@classmethod
def test_setUpClass(cls) -> None:
@pytest.fixture(autouse=True)
def setUpClass(cls) -> None:
# Coordinates
times = xr.cftime_range(start='2022-08-01', end='2022-08-05', freq='D')
lats = np.linspace(start=-45, stop=45, num=3, dtype='float32')
Expand Down

0 comments on commit 495130c

Please sign in to comment.