Skip to content

Commit

Permalink
Basic test (#33)
Browse files Browse the repository at this point in the history
    Created test framework
    Added test that runs sail_ship complete. No assertions yet; just to see if it runs.
    Seperated Argo simulation as a seperate module in instruments/argo. Created test for it; similarly without assertions.
    Added tests to CI. Full code coverage is not generated or uploaded anywhere yet. Just runs the tests and sees how that goes.
  • Loading branch information
surgura committed Aug 18, 2024
1 parent 6a0c463 commit 8a9dd21
Show file tree
Hide file tree
Showing 18 changed files with 479 additions and 233 deletions.
19 changes: 16 additions & 3 deletions .github/workflows/codetools.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on: [push, pull_request]

env:
PACKAGE: virtual_ship
TESTS: tests

jobs:
codetools:
Expand All @@ -19,7 +20,7 @@ jobs:
- name: install
run: pip install ".[dev]"
- name: flake8
run: flake8 ./$PACKAGE
run: flake8 ./$PACKAGE ./$TESTS
- name: pydocstyle
run: pydocstyle ./$PACKAGE
- name: sort-all
Expand All @@ -28,6 +29,18 @@ jobs:
[[ -z $(git status -s) ]]
git checkout -- .
- name: black
run: black --diff --check ./$PACKAGE
run: black --diff --check ./$PACKAGE ./$TESTS
- name: isort
run: isort --check-only --diff ./$PACKAGE
run: isort --check-only --diff ./$PACKAGE ./$TESTS

tests:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v4
- uses: actions/[email protected]
with:
python-version: 3.12
- name: install
run: pip install ".[dev]"
- name: run_tests
run: pytest --cov=virtual_ship tests
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,5 @@ cython_debug/

# Auto generated by setuptools scm
virtual_ship/_version_setup.py

.vscode/
7 changes: 4 additions & 3 deletions codetools.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ set -e
cd "$(dirname "$0")"

PACKAGE=virtual_ship
TESTS=tests

echo "--------------"
echo "flake8"
echo "--------------"
flake8 ./$PACKAGE
flake8 ./$PACKAGE ./$TESTS
# darglint is ran as a plugin for flake8.

echo "--------------"
Expand All @@ -29,10 +30,10 @@ find ./$PACKAGE -type f -name '__init__.py' -print0 | xargs -0 sort-all
echo "--------------"
echo "black"
echo "--------------"
black ./$PACKAGE
black ./$PACKAGE ./$TESTS

echo "--------------"
echo "isort"
echo "--------------"
isort ./$PACKAGE
isort ./$PACKAGE ./$TESTS

13 changes: 13 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ dependencies = [
"parcels >= 3, < 4",
"scipy >= 1, < 2",
"xarray >= 2023, < 2024",
# The packages below are parcels packages not included in their distribution requirements.
"numpy >= 1, < 2",
"cgen >= 2020, < 2021",
"dask >= 2023, < 2025",
"cftime >= 1, < 2",
"psutil >= 1, < 2",
"netCDF4 >= 1, < 2",
"zarr >= 2, < 3",
"tqdm >= 4, < 5",
"pymbolic >= 2022, < 2023",
]

[project.urls]
Expand All @@ -53,6 +63,9 @@ dev = [
"isort == 5.13.2",
"pydocstyle == 6.3.0",
"sort-all == 1.2.0",
"pytest == 8.2.0",
"pytest-cov == 5.0.0",
"codecov == 2.1.13",
]

[tool.isort]
Expand Down
5 changes: 5 additions & 0 deletions tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh

# Runs the tests and creates a code coverage report.

pytest --cov=virtual_ship --cov-report=html tests
7 changes: 7 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import pytest


# Set the working directory for each test to the directory of that test.
@pytest.fixture(autouse=True)
def change_test_dir(request, monkeypatch):
monkeypatch.chdir(request.fspath.dirname)
48 changes: 48 additions & 0 deletions tests/instruments/test_argo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""Test the simulation of Argo floats."""

from datetime import timedelta

import numpy as np
from parcels import FieldSet

from virtual_ship.instruments import Location
from virtual_ship.instruments.argo_float import ArgoFloat, simulate_argo_floats


def test_simulate_argo_floats() -> None:
DRIFT_DEPTH = -1000
MAX_DEPTH = -2000
VERTICLE_SPEED = -0.10
CYCLE_DAYS = 10
DRIFT_DAYS = 9

fieldset = FieldSet.from_data(
{"U": 0, "V": 0, "T": 0, "S": 0},
{
"lon": 0,
"lat": 0,
"time": [np.datetime64("1950-01-01") + np.timedelta64(632160, "h")],
},
)

min_depth = -fieldset.U.depth[0]

argo_floats = [
ArgoFloat(
location=Location(latitude=0, longitude=0),
deployment_time=0,
min_depth=min_depth,
max_depth=MAX_DEPTH,
drift_depth=DRIFT_DEPTH,
vertical_speed=VERTICLE_SPEED,
cycle_days=CYCLE_DAYS,
drift_days=DRIFT_DAYS,
)
]

simulate_argo_floats(
argo_floats=argo_floats,
fieldset=fieldset,
out_file_name="test",
outputdt=timedelta(minutes=5),
)
40 changes: 40 additions & 0 deletions tests/sailship_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"region_of_interest": {
"North": 64,
"East": -23,
"South": 59,
"West": -43
},
"requested_ship_time": {
"start": "2022-01-01T00:00:00",
"end": "2022-01-2T00:00:00"
},
"route_coordinates": [
[-23.071289, 63.743631], [-23.081289, 63.743631], [-23.091289, 63.743631]
],
"underway_data": true,
"ADCP_data": false,
"ADCP_settings": {
"max_depth": -1000,
"bin_size_m": 24
},
"CTD_locations": [
[-23.071289, 63.743631]
],
"CTD_settings": {
"max_depth": "max"
},
"drifter_deploylocations": [

],
"argo_deploylocations": [
[-23.081289, 63.743631]
],
"argo_characteristics": {
"driftdepth": -1000,
"maxdepth": -2000,
"vertical_speed": -0.10,
"cycle_days" : 10,
"drift_days": 9
}
}
43 changes: 43 additions & 0 deletions tests/test_sailship.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""Performs a complete cruise with virtual ship."""

import numpy as np
from parcels import FieldSet

from virtual_ship.sailship import sailship
from virtual_ship.virtual_ship_configuration import VirtualShipConfiguration


def test_sailship() -> None:
ctd_fieldset = FieldSet.from_data(
{"U": 0, "V": 0, "S": 0, "T": 0, "bathymetry": 0},
{"lon": 0, "lat": 0},
)
ctd_fieldset.add_constant("maxtime", ctd_fieldset.U.grid.time_full[-1])
ctd_fieldset.add_constant("mindepth", -ctd_fieldset.U.depth[0])
ctd_fieldset.add_constant("max_depth", -ctd_fieldset.U.depth[-1])

drifter_fieldset = FieldSet.from_data(
{
"U": 0,
"V": 0,
},
{"lon": 0, "lat": 0},
)

argo_float_fieldset = FieldSet.from_data(
{"U": 0, "V": 0, "T": 0, "S": 0},
{
"lon": 0,
"lat": 0,
"time": [np.datetime64("1950-01-01") + np.timedelta64(632160, "h")],
},
)

config = VirtualShipConfiguration(
"sailship_config.json",
ctd_fieldset=ctd_fieldset,
drifter_fieldset=drifter_fieldset,
argo_float_fieldset=argo_float_fieldset,
)

sailship(config)
4 changes: 4 additions & 0 deletions virtual_ship/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
"""Code for the Virtual Ship Classroom, where Marine Scientists can combine Copernicus Marine Data with an OceanParcels ship to go on a virtual expedition."""

from . import instruments, sailship

__all__ = ["instruments", "sailship"]
Loading

0 comments on commit 8a9dd21

Please sign in to comment.