Commit 2f63ff44 authored by BorjaEst's avatar BorjaEst
Browse files

Extend and reconfigure tests

Add skimed file testing
Split o3skim tests into multiple files
parent e65b9455
"""Pytest configuration file."""
import os
import o3skim
import pytest
import tests.mockup as mockup_data
import xarray
# configurations ----------------------------------------------------
year_line = range(2000, 2022)
sources = ["SourceSplit", "SourceMerged"]
sources_example = "tests/sources_example.yaml"
sources_err = "tests/sources_err.yaml"
models = ["ModelTCO3", "ModelVMRO3", "ModelALL"]
models_tco3 = ['ModelTCO3', 'ModelALL']
models_vmro3 = ['ModelVMRO3', 'ModelALL']
# session fixtures ---------------------------------------------------
@pytest.fixture(scope='session')
def data_dir(tmpdir_factory):
data_dir = tmpdir_factory.mktemp("data")
for source in sources:
source_dir = data_dir.join(source)
os.mkdir(source_dir)
with o3skim.utils.cd(source_dir):
if source == 'SourceMerged':
mockup_data.combined(year_line)
mockup_data.noise(name='merged_noise.nc')
if source == 'SourceSplit':
mockup_data.tco3(year_line)
mockup_data.noise(name='tco3_noise.nc')
mockup_data.vmro3(year_line)
mockup_data.noise(name='vmro3_noise.nc')
return data_dir
@pytest.fixture(scope='session')
def config_file(request):
return request.param
@pytest.fixture(scope='session')
def config_dict(config_file):
return o3skim.utils.load(config_file)
# package fixtures --------------------------------------------------
# module fixtures ---------------------------------------------------
@pytest.fixture(scope='module')
def output_dir(tmpdir_factory):
return tmpdir_factory.mktemp("output")
@pytest.fixture(scope='module')
def groupby(request):
return request.param
@pytest.fixture(scope='module')
def source_name(request):
return request.param
@pytest.fixture(scope='module')
def source(config_dict, source_name, data_dir):
with o3skim.utils.cd(data_dir):
source = o3skim.Source(source_name, config_dict[source_name])
return source
@pytest.fixture(scope='module')
def skimmed(groupby, source, output_dir):
with o3skim.utils.cd(output_dir):
source.skim(groupby=groupby)
yield groupby, source.name
# class fixtures --------------------------------------------------
# function fixtures -------------------------------------------------
@pytest.fixture()
def model_name(request):
return request.param
@pytest.fixture()
def model(source, model_name):
return source[model_name]
@pytest.fixture()
def year(request):
return request.param
@pytest.fixture()
def variable(request):
return request.param
@pytest.fixture()
def skimed_file(skimmed, model_name, variable, year):
groupby, source_name = skimmed
with o3skim.utils.cd("{}_{}".format(source_name, model_name)):
if not groupby:
yield "{}.nc".format(variable)
if groupby == 'year':
yield "{}_{}-{}.nc".format(variable, year, year + 1)
if groupby == 'decade':
yield "{}_{}-{}.nc".format(variable, year, year + 10)
@pytest.fixture()
def skimmed_model(skimed_file):
with xarray.open_dataset(skimed_file) as model:
yield model
"""Unittest module template."""
import os
import pytest
import o3skim
import xarray
import tests.mockup as mockup_data
# Test configurations ----------------------------------------------
configuration_file = "tests/sources_err.yaml"
year_line = range(2000, 2022)
sources = ["SourceSplit"]
# models = ["ModelBadName", "ModelBadPath", "ModelMissingCoords",
# "ModelBadCoords", "ModelExtraCoords"]
config = o3skim.utils.load(configuration_file)
# Module fixtures ---------------------------------------------------
@ pytest.fixture(scope='module')
def data_dir(tmpdir_factory):
data_dir = tmpdir_factory.mktemp("data")
for source in sources:
source_dir = data_dir.join(source)
os.mkdir(source_dir)
with o3skim.utils.cd(source_dir):
if source == 'SourceMerged':
mockup_data.combined(year_line)
mockup_data.noise(name='merged_noise.nc')
if source == 'SourceSplit':
mockup_data.tco3(year_line)
mockup_data.noise(name='tco3_noise.nc')
mockup_data.vmro3(year_line)
mockup_data.noise(name='vmro3_noise.nc')
with o3skim.utils.cd(data_dir):
yield data_dir
# Source Tests ------------------------------------------------------
@ pytest.fixture(scope='module')
def source_config(request, data_dir):
source_name = request.param
yield source_name, config[source_name]
@ pytest.fixture(scope='class')
def source(source_config):
source_name, collections = source_config
yield o3skim.Source(source_name, collections)
@ pytest.mark.parametrize('source_config', sources, indirect=True)
class Test_Exceptions:
def test_constructor(self, source_config):
source_name, collections = source_config
source = o3skim.Source(source_name, collections)
assert source.name == source_name
assert source.models == []
assert True
def test_skimming(self, source):
assert None == source.skim(groupby=None)
assert None == source.skim(groupby='year')
assert None == source.skim(groupby='decade')
assert True
"""Unittest module template."""
import os
import pytest
import o3skim
import xarray
import tests.mockup as mockup_data
# Test configurations ----------------------------------------------
configuration_file = "tests/sources_example.yaml"
year_line = range(2000, 2022)
sources = ["SourceSplit", "SourceMerged"]
models = ["ModelTCO3", "ModelVMRO3", "ModelALL"]
models_tco3 = ['ModelTCO3', 'ModelALL']
models_vmro = ['ModelVMRO3', 'ModelALL']
config = o3skim.utils.load(configuration_file)
# Module fixtures ---------------------------------------------------
@pytest.fixture(scope='module')
def data_dir(tmpdir_factory):
data_dir = tmpdir_factory.mktemp("data")
for source in sources:
source_dir = data_dir.join(source)
os.mkdir(source_dir)
with o3skim.utils.cd(source_dir):
if source == 'SourceMerged':
mockup_data.combined(year_line)
mockup_data.noise(name='merged_noise.nc')
if source == 'SourceSplit':
mockup_data.tco3(year_line)
mockup_data.noise(name='tco3_noise.nc')
mockup_data.vmro3(year_line)
mockup_data.noise(name='vmro3_noise.nc')
with o3skim.utils.cd(data_dir):
yield data_dir
# Source Tests ------------------------------------------------------
@pytest.fixture(scope='module')
def source_name(request):
source_name = request.param
yield source_name
@pytest.fixture(scope='class')
def source(source_name, data_dir):
yield o3skim.Source(source_name, config[source_name])
@pytest.mark.parametrize('source_name', sources, indirect=True)
class TestSource:
def test_constructor(self, source_name, data_dir):
source = o3skim.Source(source_name, config[source_name])
assert type(source.name) is str
assert type(source.models) is list
def test_skimming(self, source):
assert None == source.skim(groupby=None)
assert None == source.skim(groupby='year')
assert None == source.skim(groupby='decade')
# Model Tests ------------------------------------------------------
@pytest.fixture(scope='class')
def model_name(request):
model_name = request.param
yield model_name
@pytest.fixture(scope='class')
def model(source, model_name):
yield source[model_name]
@pytest.mark.parametrize('source_name', sources, indirect=True)
class TestModel:
@pytest.mark.parametrize('model_name', models, indirect=True)
def test_model_properties(self, model):
assert isinstance(model, xarray.Dataset)
assert model.time.size != 0
@pytest.mark.parametrize('model_name', ['ModelTCO3'], indirect=True)
def test_tco3_coordinates(self, model):
assert 'time' in model.coords
assert not 'plev' in model.coords
assert 'lat' in model.coords
assert 'lon' in model.coords
@pytest.mark.parametrize('model_name', ['ModelVMRO3'], indirect=True)
def test_vmro3_coordinates(self, model):
assert 'time' in model.coords
assert 'plev' in model.coords
assert 'lat' in model.coords
assert 'lon' in model.coords
@pytest.mark.parametrize('model_name', ['ModelALL'], indirect=True)
def test_all_coordinates(self, model):
assert 'time' in model.coords
assert 'plev' in model.coords
assert 'lat' in model.coords
assert 'lon' in model.coords
# Skimming Tests ----------------------------------------------------
@pytest.fixture(scope='class')
def groupby(request):
return request.param
@pytest.fixture(scope='class')
def skimmed(groupby, source):
output_dir = "{}_groupby_{}".format(source.name, groupby)
os.mkdir(output_dir)
with o3skim.utils.cd(output_dir):
source.skim(groupby=groupby)
yield source.name
@pytest.fixture(scope='class')
def skimmed_model(model_name, skimmed):
source_name = skimmed
with o3skim.utils.cd(source_name + "_" + model_name):
yield source_name, model_name
@pytest.mark.parametrize('source_name', sources, indirect=True)
@pytest.mark.parametrize('groupby', [None], indirect=True)
class TestSkimming_NoGroup:
@pytest.mark.parametrize('model_name', models_tco3, indirect=True)
def test_skimmed_tco3(self, skimmed_model):
assert os.path.isfile("tco3_zm.nc")
@pytest.mark.parametrize('model_name', models_vmro, indirect=True)
def test_skimmed_vmro3(self, skimmed_model):
assert os.path.isfile("vmro3_zm.nc")
@pytest.mark.parametrize('source_name', sources, indirect=True)
@pytest.mark.parametrize('groupby', ['year'], indirect=True)
class TestSkimming_ByYear:
@pytest.mark.parametrize('model_name', models_tco3, indirect=True)
def test_skimmed_tco3(self, skimmed_model):
for y in year_line:
assert os.path.isfile("tco3_zm_{}-{}.nc".format(y, y + 1))
@pytest.mark.parametrize('model_name', models_vmro, indirect=True)
def test_skimmed_vmro3(self, skimmed_model):
for y in year_line:
assert os.path.isfile("vmro3_zm_{}-{}.nc".format(y, y + 1))
@pytest.mark.parametrize('source_name', sources, indirect=True)
@pytest.mark.parametrize('groupby', ['decade'], indirect=True)
class TestSkimming_ByDecade:
@pytest.mark.parametrize('model_name', models_tco3, indirect=True)
def test_skimmed_tco3(self, skimmed_model):
for y in year_line:
if y % 10 == 0:
assert os.path.isfile("tco3_zm_{}-{}.nc".format(y, y + 10))
else:
assert not os.path.isfile("tco3_zm_{}-{}.nc".format(y, y + 10))
@pytest.mark.parametrize('model_name', models_vmro, indirect=True)
def test_skimmed_vmro3(self, skimmed_model):
for y in year_line:
if y % 10 == 0:
assert os.path.isfile("vmro3_zm_{}-{}.nc".format(y, y + 10))
else:
assert not os.path.isfile("vmro3_zm_{}-{}.nc".format(y, y + 10))
"""Pytest module to test sources as blackbox."""
import os
import o3skim
import pytest
import tests.conftest as conftest
import xarray
@pytest.mark.parametrize('config_file', [conftest.sources_example], indirect=True)
@pytest.mark.parametrize('source_name', conftest.sources, indirect=True)
class TestSource:
def test_constructor(self, config_dict, source_name, data_dir):
with o3skim.utils.cd(data_dir):
source = o3skim.Source(source_name, config_dict[source_name])
assert type(source.name) is str
assert type(source.models) is list
assert source.models != []
def test_skimming(self, source, output_dir):
with o3skim.utils.cd(output_dir):
assert None == source.skim(groupby=None)
assert None == source.skim(groupby='year')
assert None == source.skim(groupby='decade')
@pytest.mark.parametrize('model_name', conftest.models, indirect=True)
def test_model_properties(self, model):
assert isinstance(model, xarray.Dataset)
assert model.time.size != 0
@pytest.mark.parametrize('config_file', [conftest.sources_err], indirect=True)
@pytest.mark.parametrize('source_name', ["SourceSplit"], indirect=True)
class TestExceptions:
def test_constructor(self, config_dict, source_name, data_dir):
with o3skim.utils.cd(data_dir):
source = o3skim.Source(source_name, config_dict[source_name])
assert source.name == source_name
assert source.models == []
assert True
def test_skimming(self, source):
assert None == source.skim(groupby=None)
assert None == source.skim(groupby='year')
assert None == source.skim(groupby='decade')
assert True
"""Pytest module to test tco3 data as blackbox."""
import os
import o3skim
import pytest
import tests.conftest as conftest
import xarray
def pytest_generate_tests(metafunc):
config_file = conftest.sources_example
metafunc.parametrize("config_file", [config_file], indirect=True)
metafunc.parametrize("source_name", conftest.sources, indirect=True)
metafunc.parametrize("variable", ["tco3_zm"], indirect=True)
class TestModel:
@pytest.mark.parametrize('model_name', conftest.models_tco3, indirect=True)
def test_tco3_coordinates(self, model, variable):
assert 'time' in model.coords
assert 'lat' in model.coords
assert 'lon' in model.coords
@pytest.mark.parametrize('model_name', ["ModelTCO3"], indirect=True)
def test_tco3_no_extra_coordinates(self, model, variable):
assert len(model.coords) == 3
@pytest.mark.parametrize('groupby', [None], indirect=True)
class TestSkimming_NoGroup:
@pytest.mark.parametrize('model_name', conftest.models_tco3, indirect=True)
@pytest.mark.parametrize('year', [None], indirect=True)
def test_isTCO3file(self, skimed_file):
assert os.path.isfile(skimed_file)
@pytest.mark.parametrize('model_name', conftest.models_tco3, indirect=True)
@pytest.mark.parametrize('year', [None], indirect=True)
def test_skimmed_tco3(self, skimmed_model):
assert 'tco3_zm' in skimmed_model.var()
assert 'time' in skimmed_model.coords
assert not 'plev' in skimmed_model.coords
assert 'lat' in skimmed_model.coords
assert not 'lon' in skimmed_model.coords
@pytest.mark.parametrize('groupby', ['year'], indirect=True)
class TestSkimming_ByYear:
@pytest.mark.parametrize('model_name', conftest.models_tco3, indirect=True)
@pytest.mark.parametrize('year', list(conftest.year_line), indirect=True)
def test_isTCO3file(self, skimed_file):
assert os.path.isfile(skimed_file)
@pytest.mark.parametrize('model_name', conftest.models_tco3, indirect=True)
@pytest.mark.parametrize('year', list(conftest.year_line), indirect=True)
def test_skimmed_tco3(self, skimmed_model):
assert 'tco3_zm' in skimmed_model.var()
assert 'time' in skimmed_model.coords
assert not 'plev' in skimmed_model.coords
assert 'lat' in skimmed_model.coords
assert not 'lon' in skimmed_model.coords
@pytest.mark.parametrize('groupby', ['decade'], indirect=True)
class TestSkimming_ByDecade:
@pytest.mark.parametrize('model_name', conftest.models_tco3, indirect=True)
@pytest.mark.parametrize('year', [y for y in conftest.year_line if y % 10 == 0], indirect=True)
def test_isTCO3file(self, skimed_file):
assert os.path.isfile(skimed_file)
@pytest.mark.parametrize('model_name', conftest.models_tco3, indirect=True)
@pytest.mark.parametrize('year', [y for y in conftest.year_line if y % 10 != 0], indirect=True)
def test_isNotTCO3file(self, skimed_file, year):
assert not os.path.isfile(skimed_file)
@pytest.mark.parametrize('model_name', conftest.models_tco3, indirect=True)
@pytest.mark.parametrize('year', [y for y in conftest.year_line if y % 10 == 0], indirect=True)
def test_skimmed_tco3(self, skimmed_model):
assert 'tco3_zm' in skimmed_model.var()
assert 'time' in skimmed_model.coords
assert not 'plev' in skimmed_model.coords
assert 'lat' in skimmed_model.coords
assert not 'lon' in skimmed_model.coords
"""Pytest module to test vmro3 data as blackbox."""
import os
import o3skim
import pytest
import tests.conftest as conftest
import xarray
def pytest_generate_tests(metafunc):
config_file = conftest.sources_example
metafunc.parametrize("config_file", [config_file], indirect=True)
metafunc.parametrize("source_name", conftest.sources, indirect=True)
metafunc.parametrize("variable", ["vmro3_zm"], indirect=True)
class TestModel:
@pytest.mark.parametrize('model_name', conftest.models_vmro3, indirect=True)
def test_vmro3_coordinates(self, model, variable):
assert 'time' in model.coords
assert 'plev' in model.coords
assert 'lat' in model.coords
assert 'lon' in model.coords
@pytest.mark.parametrize('model_name', ["ModelVMRO3"], indirect=True)
def test_vmro3_no_extra_coordinates(self, model, variable):
assert len(model.coords) == 4
@pytest.mark.parametrize('groupby', [None], indirect=True)
class TestSkimming_NoGroup:
@pytest.mark.parametrize('model_name', conftest.models_vmro3, indirect=True)
@pytest.mark.parametrize('year', [None], indirect=True)
def test_isVMRO3file(self, skimed_file):
assert os.path.isfile(skimed_file)
@pytest.mark.parametrize('model_name', conftest.models_vmro3, indirect=True)
@pytest.mark.parametrize('year', [None], indirect=True)
def test_skimmed_vmro3(self, skimmed_model):
assert 'vmro3_zm' in skimmed_model.var()
assert 'time' in skimmed_model.coords
assert 'plev' in skimmed_model.coords
assert 'lat' in skimmed_model.coords
assert not 'lon' in skimmed_model.coords
@pytest.mark.parametrize('groupby', ['year'], indirect=True)
class TestSkimming_ByYear:
@pytest.mark.parametrize('model_name', conftest.models_vmro3, indirect=True)
@pytest.mark.parametrize('year', list(conftest.year_line), indirect=True)
def test_isVMRO3file(self, skimed_file):
assert os.path.isfile(skimed_file)
@pytest.mark.parametrize('model_name', conftest.models_vmro3, indirect=True)
@pytest.mark.parametrize('year', list(conftest.year_line), indirect=True)
def test_skimmed_vmro3(self, skimmed_model):
assert 'vmro3_zm' in skimmed_model.var()
assert 'time' in skimmed_model.coords
assert 'plev' in skimmed_model.coords
assert 'lat' in skimmed_model.coords
assert not 'lon' in skimmed_model.coords
@pytest.mark.parametrize('groupby', ['decade'], indirect=True)
class TestSkimming_ByDecade:
@pytest.mark.parametrize('model_name', conftest.models_vmro3, indirect=True)
@pytest.mark.parametrize('year', [y for y in conftest.year_line if y % 10 == 0], indirect=True)
def test_isVMRO3file(self, skimed_file):
assert os.path.isfile(skimed_file)
@pytest.mark.parametrize('model_name', conftest.models_vmro3, indirect=True)
@pytest.mark.parametrize('year', [y for y in conftest.year_line if y % 10 != 0], indirect=True)
def test_isNotVMRO3file(self, skimed_file, year):
assert not os.path.isfile(skimed_file)
@pytest.mark.parametrize('model_name', conftest.models_vmro3, indirect=True)
@pytest.mark.parametrize('year', [y for y in conftest.year_line if y % 10 == 0], indirect=True)
def test_skimmed_vmro3(self, skimmed_model):
assert 'vmro3_zm' in skimmed_model.var()
assert 'time' in skimmed_model.coords
assert 'plev' in skimmed_model.coords
assert 'lat' in skimmed_model.coords