"""Tests for :mod:`esmvalcore.iris_helpers`."""
import datetime

import iris
import numpy as np
import pytest
from cf_units import Unit

from esmvalcore.iris_helpers import date2num, var_name_constraint


@pytest.fixture
def cubes():
    """Test cubes."""
    cubes = iris.cube.CubeList([
        iris.cube.Cube(0.0, var_name='a', long_name='a'),
        iris.cube.Cube(0.0, var_name='a', long_name='b'),
        iris.cube.Cube(0.0, var_name='c', long_name='d'),
    ])
    return cubes


@pytest.fixture
def units():
    return Unit('days since 0001-01-01', calendar='proleptic_gregorian')


@pytest.mark.parametrize("date, dtype, expected", [
    (datetime.datetime(1, 1, 1), np.float64, 0.0),
    (datetime.datetime(1, 1, 1), int, 0.0),
    (datetime.datetime(1, 1, 2, 12), np.float64, 1.5),
])
def test_date2num_scalar(date, dtype, expected, units):
    num = date2num(date, units, dtype=dtype)
    assert num == expected
    assert num.dtype == dtype


def test_var_name_constraint(cubes):
    """Test :func:`esmvalcore.iris_helpers.var_name_constraint`."""
    out_cubes = cubes.extract(var_name_constraint('a'))
    assert out_cubes == iris.cube.CubeList([
        iris.cube.Cube(0.0, var_name='a', long_name='a'),
        iris.cube.Cube(0.0, var_name='a', long_name='b'),
    ])
    out_cubes = cubes.extract(var_name_constraint('b'))
    assert out_cubes == iris.cube.CubeList([])
    out_cubes = cubes.extract(var_name_constraint('c'))
    assert out_cubes == iris.cube.CubeList([
        iris.cube.Cube(0.0, var_name='c', long_name='d'),
    ])
    with pytest.raises(iris.exceptions.ConstraintMismatchError):
        cubes.extract_cube(var_name_constraint('a'))
    with pytest.raises(iris.exceptions.ConstraintMismatchError):
        cubes.extract_cube(var_name_constraint('b'))
    out_cube = cubes.extract_cube(var_name_constraint('c'))
    assert out_cube == iris.cube.Cube(0.0, var_name='c', long_name='d')

[(25, 'cf_units.Unit', 'Unit', False, 'from cf_units import Unit\n'), (34, 'esmvalcore.iris_helpers.date2num', 'date2num', False, 'from esmvalcore.iris_helpers import date2num, var_name_constraint\n'), (41, 'esmvalcore.iris_helpers.var_name_constraint', 'var_name_constraint', False, 'from esmvalcore.iris_helpers import date2num, var_name_constraint\n'), (46, 'esmvalcore.iris_helpers.var_name_constraint', 'var_name_constraint', False, 'from esmvalcore.iris_helpers import date2num, var_name_constraint\n'), (47, 'iris.cube.CubeList', 'iris.cube.CubeList', False, 'import iris\n'), (48, 'esmvalcore.iris_helpers.var_name_constraint', 'var_name_constraint', False, 'from esmvalcore.iris_helpers import date2num, var_name_constraint\n'), (52, 'pytest.raises', 'pytest.raises', False, 'import pytest\n'), (54, 'pytest.raises', 'pytest.raises', False, 'import pytest\n'), (56, 'esmvalcore.iris_helpers.var_name_constraint', 'var_name_constraint', False, 'from esmvalcore.iris_helpers import date2num, var_name_constraint\n'), (57, 'iris.cube.Cube', 'iris.cube.Cube', False, 'import iris\n'), (16, 'iris.cube.Cube', 'iris.cube.Cube', False, 'import iris\n'), (17, 'iris.cube.Cube', 'iris.cube.Cube', False, 'import iris\n'), (18, 'iris.cube.Cube', 'iris.cube.Cube', False, 'import iris\n'), (29, 'datetime.datetime', 'datetime.datetime', False, 'import datetime\n'), (30, 'datetime.datetime', 'datetime.datetime', False, 'import datetime\n'), (31, 'datetime.datetime', 'datetime.datetime', False, 'import datetime\n'), (53, 'esmvalcore.iris_helpers.var_name_constraint', 'var_name_constraint', False, 'from esmvalcore.iris_helpers import date2num, var_name_constraint\n'), (55, 'esmvalcore.iris_helpers.var_name_constraint', 'var_name_constraint', False, 'from esmvalcore.iris_helpers import date2num, var_name_constraint\n'), (43, 'iris.cube.Cube', 'iris.cube.Cube', False, 'import iris\n'), (44, 'iris.cube.Cube', 'iris.cube.Cube', False, 'import iris\n'), (50, 'iris.cube.Cube', 'iris.cube.Cube', False, 'import iris\n')]