|
1 | | -from datetime import timedelta |
| 1 | +from contextlib import nullcontext as does_not_raise |
| 2 | +from datetime import datetime, timedelta |
2 | 3 |
|
3 | 4 | import numpy as np |
4 | 5 | import pytest |
|
11 | 12 | ParticleSet, |
12 | 13 | UXPiecewiseConstantFace, |
13 | 14 | VectorField, |
| 15 | + xgcm, |
14 | 16 | ) |
| 17 | +from parcels._datasets.structured.generic import datasets as datasets_structured |
15 | 18 | from parcels._datasets.unstructured.generic import datasets as datasets_unstructured |
16 | 19 | from parcels.uxgrid import UxGrid |
| 20 | +from parcels.xgrid import XGrid |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture |
| 24 | +def fieldset() -> FieldSet: |
| 25 | + ds = datasets_structured["ds_2d_left"] |
| 26 | + grid = XGrid(xgcm.Grid(ds)) |
| 27 | + U = Field("U", ds["U (A grid)"], grid, mesh_type="flat") |
| 28 | + V = Field("V", ds["V (A grid)"], grid, mesh_type="flat") |
| 29 | + return FieldSet([U, V]) |
| 30 | + |
| 31 | + |
| 32 | +def DoNothing(particle, fieldset, time): |
| 33 | + pass |
| 34 | + |
| 35 | + |
| 36 | +@pytest.mark.parametrize( |
| 37 | + "time, expectation", |
| 38 | + [ |
| 39 | + (np.timedelta64(0, "s"), does_not_raise()), |
| 40 | + (np.datetime64("2000-01-02T00:00:00"), does_not_raise()), |
| 41 | + (0.0, pytest.raises(TypeError)), |
| 42 | + (timedelta(seconds=0), pytest.raises(TypeError)), |
| 43 | + (datetime(2023, 1, 1, 0, 0, 0), pytest.raises(TypeError)), |
| 44 | + ], |
| 45 | +) |
| 46 | +def test_particleset_init_time_type(fieldset, time, expectation): |
| 47 | + with expectation: |
| 48 | + ParticleSet(fieldset, lon=[0.2], lat=[5.0], time=[time], pclass=Particle) |
| 49 | + |
| 50 | + |
| 51 | +@pytest.mark.parametrize( |
| 52 | + "dt, expectation", |
| 53 | + [ |
| 54 | + (np.timedelta64(5, "s"), does_not_raise()), |
| 55 | + (5.0, pytest.raises(TypeError)), |
| 56 | + (np.datetime64("2000-01-02T00:00:00"), pytest.raises(TypeError)), |
| 57 | + (timedelta(seconds=2), pytest.raises(TypeError)), |
| 58 | + ], |
| 59 | +) |
| 60 | +def test_particleset_dt_type(fieldset, dt, expectation): |
| 61 | + pset = ParticleSet(fieldset, lon=[0.2], lat=[5.0], depth=[50.0], pclass=Particle) |
| 62 | + with expectation: |
| 63 | + pset.execute(runtime=np.timedelta64(10, "s"), dt=dt, pyfunc=DoNothing) |
| 64 | + |
| 65 | + |
| 66 | +@pytest.mark.parametrize( |
| 67 | + "runtime, expectation", |
| 68 | + [ |
| 69 | + (np.timedelta64(5, "s"), does_not_raise()), |
| 70 | + (5.0, pytest.raises(TypeError)), |
| 71 | + (timedelta(seconds=2), pytest.raises(TypeError)), |
| 72 | + (np.datetime64("2001-01-02T00:00:00"), pytest.raises(TypeError)), |
| 73 | + (datetime(2000, 1, 2, 0, 0, 0), pytest.raises(TypeError)), |
| 74 | + ], |
| 75 | +) |
| 76 | +def test_particleset_runtime_type(fieldset, runtime, expectation): |
| 77 | + pset = ParticleSet(fieldset, lon=[0.2], lat=[5.0], depth=[50.0], pclass=Particle) |
| 78 | + with expectation: |
| 79 | + pset.execute(runtime=runtime, dt=np.timedelta64(10, "s"), pyfunc=DoNothing) |
| 80 | + |
| 81 | + |
| 82 | +@pytest.mark.parametrize( |
| 83 | + "endtime, expectation", |
| 84 | + [ |
| 85 | + (np.datetime64("2000-01-02T00:00:00"), does_not_raise()), |
| 86 | + (5.0, pytest.raises(TypeError)), |
| 87 | + (np.timedelta64(5, "s"), pytest.raises(TypeError)), |
| 88 | + (timedelta(seconds=2), pytest.raises(TypeError)), |
| 89 | + (datetime(2000, 1, 2, 0, 0, 0), pytest.raises(TypeError)), |
| 90 | + ], |
| 91 | +) |
| 92 | +def test_particleset_endtime_type(fieldset, endtime, expectation): |
| 93 | + pset = ParticleSet(fieldset, lon=[0.2], lat=[5.0], depth=[50.0], pclass=Particle) |
| 94 | + with expectation: |
| 95 | + pset.execute(endtime=endtime, dt=np.timedelta64(10, "m"), pyfunc=DoNothing) |
17 | 96 |
|
18 | 97 |
|
19 | 98 | @pytest.mark.parametrize("verbose_progress", [True, False]) |
|
0 commit comments