Completed
Pull Request — master (#377)
by Ryan
01:32
created

test_good_units()   A

Complexity

Conditions 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 4
rs 10
1
# Copyright (c) 2017 MetPy Developers.
2
# Distributed under the terms of the BSD 3-Clause License.
3
# SPDX-License-Identifier: BSD-3-Clause
4
r"""Tests the operation of MetPy's unit support code."""
5
6
import sys
7
8
import matplotlib.pyplot as plt
9
import pytest
10
11
from metpy.testing import set_agg_backend  # noqa: F401
12
from metpy.units import check_units, units
13
14
15
@pytest.mark.mpl_image_compare(tolerance=0, remove_text=True)
16
def test_axhline():
17
    r"""Ensure that passing a quantity to axhline does not error."""
18
    fig, ax = plt.subplots()
19
    ax.axhline(930 * units('mbar'))
20
    ax.set_ylim(900, 950)
21
    return fig
22
23
24
@pytest.mark.mpl_image_compare(tolerance=0, remove_text=True)
25
def test_axvline():
26
    r"""Ensure that passing a quantity to axvline does not error."""
27
    fig, ax = plt.subplots()
28
    ax.axvline(0 * units('degC'))
29
    ax.set_xlim(-1, 1)
30
    return fig
31
32
#
33
# Tests for unit-checking decorator
34
#
35
36
37
def unit_calc(temp, press, dens, mixing, unitless_const):
38
    r"""Stub calculation for testing unit checking."""
39
    pass
40
41
42
test_funcs = [
43
    check_units('[temperature]', '[pressure]', dens='[mass]/[volume]',
44
                mixing='[dimensionless]')(unit_calc),
45
    check_units(temp='[temperature]', press='[pressure]', dens='[mass]/[volume]',
46
                mixing='[dimensionless]')(unit_calc),
47
    check_units('[temperature]', '[pressure]', '[mass]/[volume]',
48
                '[dimensionless]')(unit_calc)]
49
50
51
@pytest.mark.parametrize('func', test_funcs, ids=['some kwargs', 'all kwargs', 'all pos'])
52
def test_good_units(func):
53
    r"""Test that unit checking passes good units regardless."""
54
    func(30 * units.degC, 1000 * units.mbar, 1.0 * units('kg/m^3'), 1, 5.)
55
56
57
test_params = [((30 * units.degC, 1000 * units.mb, 1 * units('kg/m^3'), 1, 5 * units('J/kg')),
58
                {}, [('press', '[pressure]', 'millibarn')]),
59
               ((30, 1000, 1.0, 1, 5.), {}, [('press', '[pressure]', 'none'),
60
                                             ('temp', '[temperature]', 'none'),
61
                                             ('dens', '[mass]/[volume]', 'none')]),
62
               ((30, 1000 * units.mbar),
63
                {'dens': 1.0 * units('kg / m'), 'mixing': 5 * units.m, 'unitless_const': 2},
64
                [('temp', '[temperature]', 'none'),
65
                 ('dens', '[mass]/[volume]', 'kilogram / meter'),
66
                 ('mixing', '[dimensionless]', 'meter')])]
67
68
69
@pytest.mark.skipif(sys.version_info < (3, 3), reason='Unit checking requires Python >= 3.3')
70
@pytest.mark.parametrize('func', test_funcs, ids=['some kwargs', 'all kwargs', 'all pos'])
71
@pytest.mark.parametrize('args,kwargs,bad_parts', test_params,
72
                         ids=['one bad arg', 'all args no units', 'mixed args'])
73
def test_bad(func, args, kwargs, bad_parts):
74
    r"""Test that unit checking flags appropriate arguments."""
75
    with pytest.raises(ValueError) as exc:
76
        func(*args, **kwargs)
77
78
    message = str(exc.value)
79
    assert func.__name__ in message
80
    for param in bad_parts:
81
        assert '`{}` requires "{}" but given "{}"'.format(*param) in message
82
83
        # Should never complain about the const argument
84
        assert 'unitless_const' not in message
85