Skip to content

Commit 0ccfc0c

Browse files
authored
Merge pull request #181 from knutfrode/dev
Added mock-based test, made by Copilot
2 parents 0d1b2c7 + 9c0ca40 commit 0ccfc0c

16 files changed

+80
-39
lines changed

tests/opendrift/test_plot_opendrift.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ def test_opendrift(opendrift_sim, plot):
1010

1111
if plot:
1212
plt.show()
13+
else:
14+
plt.close()

tests/parcels/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def moving_eddies_fieldset(xdim=200, ydim=350, mesh='flat'):
2020
time = np.arange(0., 8. * 86400., 86400., dtype=np.float64)
2121

2222
# Coordinates of the test fieldset (on A-grid in m)
23-
if mesh is 'spherical':
23+
if mesh == 'spherical':
2424
lon = np.linspace(0, 4, xdim, dtype=np.float32)
2525
lat = np.linspace(45, 52, ydim, dtype=np.float32)
2626
else:
@@ -32,9 +32,9 @@ def cosd(x):
3232
return math.cos(math.radians(float(x)))
3333

3434
dx = (lon[1] - lon[0]) * 1852 * 60 * cosd(
35-
lat.mean()) if mesh is 'spherical' else lon[1] - lon[0]
35+
lat.mean()) if mesh == 'spherical' else lon[1] - lon[0]
3636
dy = (lat[1] -
37-
lat[0]) * 1852 * 60 if mesh is 'spherical' else lat[1] - lat[0]
37+
lat[0]) * 1852 * 60 if mesh == 'spherical' else lat[1] - lat[0]
3838

3939
# Define arrays U (zonal), V (meridional), and P (sea surface height) on A-grid
4040
U = np.zeros((lon.size, lat.size, time.size), dtype=np.float32)

tests/parcels/test_flatmesh.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ def test_parcels_flatmesh_animate(plot):
2020
ds = xr.open_dataset('tests/test_data/parcels.zarr', engine='zarr')
2121
ds = ds.traj.set_crs(None)
2222
print(ds)
23-
ds.traj.animate()
23+
anim = ds.traj.animate()
2424

2525
if plot:
2626
plt.show()
2727
else:
28+
anim._draw_was_started = True # avoiding warning
2829
plt.close()

tests/test_animate_drifters.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
import xarray as xr
33
import matplotlib.pyplot as plt
44

5-
def test_barents(barents, plot):
6-
print(barents)
5+
def test_animate_barents(barents, plot):
76

8-
barents.traj.animate()
7+
anim = barents.traj.animate()
98

109
if plot:
1110
plt.show()
12-
11+
else:
12+
anim._draw_was_started = True # To avoid warning
13+
plt.close('all')

tests/test_convert_datalayout.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def test_to2d(barents):
1717

1818
def test_to1d(barents):
1919
# print(barents)
20-
gr = barents.traj.gridtime('1H')
20+
gr = barents.traj.gridtime('1h')
2121
assert gr.traj.is_1d()
2222

2323
gr = gr.traj.to_1d()

tests/test_convert_ragged.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ def test_convert_ragged(test_data, plot):
1313

1414
if plot:
1515
plt.show()
16+
else:
17+
plt.close()
1618

1719
print("----------------------------------------")
1820
print("the raw ds:")

tests/test_crs.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
import trajan as ta
33
import xarray as xr
44
import cf_xarray as _
5-
import matplotlib.pyplot as plt
65

76

8-
def test_barents_detect_lonlat(barents, plot):
7+
def test_barents_detect_lonlat(barents):
98
print(barents.traj.crs)
109

1110
crs = barents.traj.crs
@@ -16,7 +15,7 @@ def test_barents_detect_lonlat(barents, plot):
1615
print(barents.traj.ccrs)
1716

1817

19-
def test_barents_set_crs(barents, plot):
18+
def test_barents_set_crs(barents):
2019
crs = barents.traj.crs
2120
barents = barents.traj.set_crs(crs)
2221

@@ -25,7 +24,7 @@ def test_barents_set_crs(barents, plot):
2524
assert barents.traj.crs == crs
2625

2726

28-
def test_barents_tlat_tlon(barents, plot):
27+
def test_barents_tlat_tlon(barents):
2928
np.testing.assert_array_equal(barents.lon, barents.traj.tlon)
3029
np.testing.assert_array_equal(barents.lat, barents.traj.tlat)
3130

tests/test_interpolate.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ def test_interpolate_barents(barents, plot):
3535

3636
plt.legend()
3737
plt.show()
38+
else:
39+
plt.close()
3840

3941

4042
def test_distance(barents):
@@ -50,7 +52,7 @@ def test_length(barents):
5052
l = barents.traj.length()
5153
print(l)
5254

53-
lg = barents.traj.gridtime('1H').traj.length()
55+
lg = barents.traj.gridtime('1h').traj.length()
5456
print(lg)
5557

5658
np.testing.assert_allclose(l, lg, atol=20000)
@@ -152,6 +154,8 @@ def test_speed(barents, plot):
152154
plt.xlabel('Drifter speed [m/s]')
153155
plt.ylabel('Number')
154156
plt.show()
157+
else:
158+
plt.close()
155159

156160
def test_speed_2d(barents):
157161
s = barents.traj.speed()
@@ -169,9 +173,11 @@ def test_insert_nan_where(barents, plot):
169173
barents.traj.plot(color='b', linewidth=2)
170174
b2.traj.plot(color='r')
171175
plt.show()
176+
else:
177+
plt.close()
172178

173179

174-
def test_drop_where(barents, plot):
180+
def test_drop_where(barents):
175181

176182
t2n = barents.traj.time_to_next() / np.timedelta64(1, 'm')
177183
assert_almost_equal(t2n.min(), 0.0166, 3)

tests/test_plot_drifters.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ def test_barents(barents, plot):
99

1010
if plot:
1111
plt.show()
12+
else:
13+
plt.close()
1214

1315
def test_barents_linecolor(barents, plot):
1416
speed = barents.traj.speed()
@@ -25,3 +27,5 @@ def test_barents_linecolor(barents, plot):
2527

2628
if plot:
2729
plt.show()
30+
else:
31+
plt.close()

tests/test_plot_landmask.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ def test_defaults(opendrift_sim, plot):
1111

1212
if plot:
1313
plt.show()
14+
else:
15+
plt.close()
1416

1517

1618
@pytest.mark.parametrize("land", ["auto", "c", "f", "mask"])
@@ -22,3 +24,5 @@ def test_land_specs(opendrift_sim, plot, land):
2224

2325
if plot:
2426
plt.show()
27+
else:
28+
plt.close()

0 commit comments

Comments
 (0)