Skip to content

Commit

Permalink
Formatter pass on tests
Browse files Browse the repository at this point in the history
  • Loading branch information
leroyvn committed Dec 14, 2023
1 parent 6241815 commit dd5ca86
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 106 deletions.
60 changes: 29 additions & 31 deletions tests/profiles/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,44 @@
import pint
import pytest
import xarray as xr
from numpy.testing import assert_approx_equal, assert_allclose
from numpy.testing import assert_allclose, assert_approx_equal

from joseki import unit_registry as ureg
from joseki.core import make
from joseki.profiles.core import (
rescale_to_column,
interp,
extrapolate,
interp,
regularize,
rescale_to_column,
select_molecules,
)
from joseki.units import to_quantity


@pytest.fixture
def test_data_set() -> xr.Dataset:
"""Test dataset fixture."""
return make(identifier="afgl_1986-tropical")


def test_rescale_to_column_factor_0():
ds = make(identifier="afgl_1986-tropical")
with xr.set_options(keep_attrs=True):
reference = ds.copy(deep=True).assign({
"x_H2O": ds.x_H2O * 0
})
reference = ds.copy(deep=True).assign({"x_H2O": ds.x_H2O * 0})

rescaled = rescale_to_column(
reference=reference,
ds=ds
)
rescaled = rescale_to_column(reference=reference, ds=ds)

assert_allclose(rescaled.x_H2O, 0)


def test_rescale_to_column_impossible():
reference = make(identifier="afgl_1986-tropical")
with xr.set_options(keep_attrs=True):
ds = reference.copy(deep=True).assign({
"x_H2O": reference.x_H2O * 0
})
ds = reference.copy(deep=True).assign({"x_H2O": reference.x_H2O * 0})

with pytest.raises(ValueError):
rescale_to_column(
reference=reference,
ds=ds
)
rescale_to_column(reference=reference, ds=ds)


def test_interp_returns_data_set(test_data_set: xr.Dataset):
"""Returns an xarray.Dataset."""
Expand All @@ -65,12 +59,11 @@ def test_interp_out_of_bound(test_data_set: xr.Dataset):
def test_interp_extrapolate(test_data_set: xr.Dataset):
"""Key-word argument 'fill_value' is processed."""
interpolated = interp(
ds=test_data_set,
z_new=np.linspace(0, 150) * ureg.km,
fill_value="extrapolate"
ds=test_data_set, z_new=np.linspace(0, 150) * ureg.km, fill_value="extrapolate"
)
assert interpolated.joseki.is_valid


def test_interp_bounds_error(test_data_set: xr.Dataset):
"""Key-word argument 'fill_value' is processed."""
interpolated = interp(
Expand All @@ -86,7 +79,7 @@ def test_interp_bounds_error(test_data_set: xr.Dataset):
[
-0.1 * ureg.km,
np.linspace(-0.5, -0.1, 5) * ureg.km,
]
],
)
def test_extrapolate_down(test_data_set: xr.Dataset, z_down: pint.Quantity):
"""Extrapolate down to 100 m."""
Expand All @@ -104,12 +97,13 @@ def test_extrapolate_down(test_data_set: xr.Dataset, z_down: pint.Quantity):
)
assert extrapolated.joseki.is_valid


@pytest.mark.parametrize(
"z_up",
[
130 * ureg.km,
np.linspace(130, 180, 6) * ureg.km,
]
],
)
def test_extrapolate_up(test_data_set: xr.Dataset, z_up: pint.Quantity):
"""Extrapolate up to 130 km."""
Expand Down Expand Up @@ -137,14 +131,15 @@ def test_extrapolate_invalid_altitude(test_data_set: xr.Dataset):
z_extra=10 * ureg.km,
direction="down",
)

with pytest.raises(ValueError):
extrapolate(
ds=test_data_set,
z_extra=60 * ureg.km,
direction="up",
)


def test_extrapolate_invalid_direction(test_data_set: xr.Dataset):
"""Raises when invalid direction is provided."""
with pytest.raises(ValueError):
Expand All @@ -154,6 +149,7 @@ def test_extrapolate_invalid_direction(test_data_set: xr.Dataset):
direction="invalid",
)


def test_regularize(test_data_set: xr.Dataset):
"""Regularize the profile."""
regularized = regularize(ds=test_data_set)
Expand All @@ -162,46 +158,47 @@ def test_regularize(test_data_set: xr.Dataset):
# grid is regular
assert np.all(np.diff(zgrid) == zgrid[1] - zgrid[0])


def test_regularize_zstep(test_data_set: xr.Dataset):
"""Regularize the profile."""
zstep = 0.5 * ureg.km
regularized = regularize(
ds=test_data_set,
options={
"zstep": zstep,
}
},
)

zgrid = to_quantity(regularized.z)
assert np.all(np.diff(zgrid) == zstep)


def test_regularize_zstep_str_invalid(test_data_set: xr.Dataset):
"""Invalid zstep raises."""
with pytest.raises(ValueError):
regularize(
ds=test_data_set,
options={
"zstep": "invalid",
}
},
)


def test_regularize_zstep_invalid_type(test_data_set: xr.Dataset):
"""Invalid zstep raises."""
with pytest.raises(ValueError):
regularize(
ds=test_data_set,
options={
"zstep": ["cannot be a list"],
}
},
)


def test_regularize_options_invalid(test_data_set: xr.Dataset):
"""Invalid options raises."""
with pytest.raises(ValueError):
regularize(
ds=test_data_set,
options={}
)
regularize(ds=test_data_set, options={})


def test_regularize_num(test_data_set: xr.Dataset):
Expand All @@ -211,7 +208,7 @@ def test_regularize_num(test_data_set: xr.Dataset):
ds=test_data_set,
options={
"num": num,
}
},
)

assert regularized.z.size == num
Expand All @@ -228,6 +225,7 @@ def test_select_molecules(test_data_set: xr.Dataset):
assert "CO2" in selected.joseki.molecules
assert "O3" not in selected.joseki.molecules


def test_select_molecules_invalid(test_data_set: xr.Dataset):
"""Raise when selected molecules are not available."""
molecules = ["SO2", "NO2"]
Expand Down
2 changes: 1 addition & 1 deletion tests/profiles/test_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@

def test_create():
with pytest.raises(ValueError):
factory.create(identifier="invalid")
factory.create(identifier="invalid")
44 changes: 25 additions & 19 deletions tests/profiles/test_mipas_2007.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@
import numpy as np
import pytest

from joseki.profiles.mipas_2007 import parse_content
from joseki.profiles.mipas_2007 import parse_units
from joseki.profiles.mipas_2007 import parse_values_line
from joseki.profiles.mipas_2007 import parse_var_line
from joseki.profiles.mipas_2007 import parse_var_name
from joseki.profiles.mipas_2007 import Identifier
from joseki.profiles.mipas_2007 import read_file_content
from joseki.profiles.mipas_2007 import translate_cfc, to_chemical_formula
from joseki.profiles.mipas_2007 import to_dataset
from joseki.units import ureg
from joseki.profiles.mipas_2007 import (
MIPASMidlatitudeNight,
Identifier,
MIPASMidlatitudeDay,
MIPASMidlatitudeNight,
MIPASPolarSummer,
MIPASPolarWinter,
MIPASTropical,
parse_content,
parse_units,
parse_values_line,
parse_var_line,
parse_var_name,
read_file_content,
to_chemical_formula,
to_dataset,
translate_cfc,
)
from joseki.units import ureg


def test_parse_unit():
Expand Down Expand Up @@ -174,21 +175,26 @@ def test_to_chemical_formula_h2o():
"""Returns non-chlorofulorocarbon name unchanged."""
assert to_chemical_formula("H2O") == "H2O"


def test_to_dataset_z():
ds = to_dataset(
identifier=Identifier.TROPICAL,
z=np.linspace(0, 10, 11) * ureg.km,
)
assert ds.joseki.is_valid

@pytest.mark.parametrize("profile", [
MIPASMidlatitudeNight(),
MIPASMidlatitudeDay(),
MIPASPolarSummer(),
MIPASPolarWinter(),
MIPASTropical(),
])

@pytest.mark.parametrize(
"profile",
[
MIPASMidlatitudeNight(),
MIPASMidlatitudeDay(),
MIPASPolarSummer(),
MIPASPolarWinter(),
MIPASTropical(),
],
)
def test_profile_to_dataset(profile):
"""Returns a dataset."""
ds = profile.to_dataset()
assert ds.joseki.is_valid
assert ds.joseki.is_valid
19 changes: 10 additions & 9 deletions tests/profiles/test_schema.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from joseki import unit_registry as ureg
from joseki.profiles.schema import schema


def test_convert_no_n():
"""convert() adds 'n' data variable if missing in input"""
converted = schema.convert(
Expand All @@ -13,14 +14,14 @@ def test_convert_no_n():
"z": [0, 10, 20] * ureg.km,
},
attrs={
"Conventions": "CF-1.10",
"title": "test",
"institution": "test",
"source": "test",
"history": "test",
"references": "test",
"url": "test",
"urldate": "test",
}
"Conventions": "CF-1.10",
"title": "test",
"institution": "test",
"source": "test",
"history": "test",
"references": "test",
"url": "test",
"urldate": "test",
},
)
assert "n" in converted
1 change: 1 addition & 0 deletions tests/profiles/test_ussa_1976.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Test cases for the ussa_1976 module."""
from joseki.profiles.ussa_1976 import USSA1976


def test_ussa_1976():
"""Returns a valid dataset."""
profile = USSA1976()
Expand Down
8 changes: 5 additions & 3 deletions tests/profiles/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import pytest
import xarray as xr

from joseki import unit_registry as ureg
from joseki.profiles.util import (
air_molar_mass_from_mass_fraction,
molar_mass,
number_density,
)
from joseki.units import to_quantity
from joseki import unit_registry as ureg


@pytest.fixture
def test_y():
Expand All @@ -25,7 +26,7 @@ def test_y():
coords={
"m": ("m", ["N2", "O2"]),
"z": ("z", [0, 50, 100], {"units": "km"}),
}
},
)


Expand All @@ -35,6 +36,7 @@ def test_air_molar_mass_from_mass_fraction(test_y):
assert q.check("[mass] / [substance]")
assert np.allclose(air_mm.values, 29.0, rtol=1e-2)


def test_number_density():
p = 101325 * ureg.Pa
t = 273.15 * ureg.K
Expand All @@ -46,10 +48,10 @@ def test_number_density():
rtol=1e-5,
)


def test_molar_mass():
molecules = ["H2O", "CO2"]
da = molar_mass(molecules)
assert isinstance(da, xr.DataArray)
q = to_quantity(da)
assert q.check("[mass] / [substance]")

Loading

0 comments on commit dd5ca86

Please sign in to comment.