Passed
Pull Request — develop (#77)
by Angeline
01:21
created

TestPyEnviron.teardown_method()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nop 1
1
import os
2
import sys
3
import pytest
4
5
6
@pytest.mark.xfail
7
class TestPyEnviron(object):
8
    """Unit tests for the AACGMV2 environment variables."""
9
    def setup_method(self):
10
        """Create a clean test environment."""
11
        self.igrf_path = os.path.join("aacgmv2", "aacgmv2",
12
                                      "magmodel_1590-2020.txt")
13
        self.aacgm_path = os.path.join("aacgmv2", "aacgmv2", "aacgm_coeffs",
14
                                       "aacgm_coeffs-13-")
15
16
    def teardown_method(self):
17
        """Clean up the test environment."""
18
        del self.igrf_path, self.aacgm_path
19
20
    def reset_evar(self, evar):
21
        """Reset the environment variables."""
22
23
        for coeff_key in evar:
24
            if coeff_key in os.environ.keys():
25
                del os.environ[coeff_key]
26
27
        for coeff_key in evar:
28
            assert coeff_key not in os.environ.keys()
29
30
    def test_good_coeff(self, aacgm_test=None, igrf_test=None):
31
        """Test the coefficient path/prefixes."""
32
33
        # Set the defaults
34
        if aacgm_test is None:
35
            aacgm_test = self.aacgm_path
36
        if igrf_test is None:
37
            igrf_test = self.igrf_path
38
39
        # Perform the test
40
        if aacgm_test.find(self.aacgm_path) < 0:
41
            raise AssertionError('BAD AACGMV PATH')
42
43
        if igrf_test.find(self.igrf_path) < 0:
44
            raise AssertionError('BAD IGRF PATH')
45
46
    @pytest.mark.parametrize("coeff", [("aacgm_test"), ("igrf_test")])
47
    def test_bad_coeff(self, coeff):
48
        """Test the failure of the class routine 'test_good_coeff'.
49
50
        Parameters
51
        ----------
52
        coeff : str
53
            Coefficient name
54
55
        """
56
        with pytest.raises(AssertionError, match="BAD"):
57
            self.test_good_coeff(**{coeff: "bad path"})
58
59
    def test_top_parameters_default(self):
60
        """Test default module coefficients."""
61
62
        # Import AACGMV2 after removing any possible preset env variables
63
        self.reset_evar(evar=['AACGM_v2_DAT_PREFIX', 'IGRF_COEFFS'])
64
        import aacgmv2
65
66
        self.test_good_coeff(aacgmv2.AACGM_v2_DAT_PREFIX, aacgmv2.IGRF_COEFFS)
67
68
        assert not aacgmv2.__reset_warn__
69
        del sys.modules["aacgmv2"]
70
        del aacgmv2
71
72
    @pytest.mark.parametrize("evars", [(["AACGM_v2_DAT_PREFIX"]),
73
                                       (["AACGM_v2_DAT_PREFIX", "IGRF_COEFFS"]),
74
                                       (["IGRF_COEFFS"])])
75
    def test_top_parameters_reset_evar_to_specified(self, evars):
76
        """Test module reset of AACGM environment variables.
77
78
        Parameters
79
        ----------
80
        evars : str
81
            Environmental variable name
82
83
        """
84
        self.reset_evar(evar=evars)
85
        for i, evar in enumerate(evars):
86
            os.environ[evar] = 'test_prefix{:d}'.format(i)
87
88
        import aacgmv2
89
90
        self.test_good_coeff(aacgmv2.AACGM_v2_DAT_PREFIX, aacgmv2.IGRF_COEFFS)
91
92
        assert aacgmv2.__reset_warn__
93
        del sys.modules["aacgmv2"]
94
        del aacgmv2
95
96
    def test_top_parameters_set_same(self):
97
        """Test module non-reset with outside def of both coefficient paths."""
98
99
        from aacgmv2 import __file__ as file_path
100
101
        coeff_path = os.path.realpath(os.path.dirname(file_path))
102
        os.environ['AACGM_v2_DAT_PREFIX'] = os.path.join(coeff_path,
103
                                                         self.aacgm_path)
104
        os.environ['IGRF_COEFFS'] = os.path.join(coeff_path, self.igrf_path)
105
106
        import aacgmv2
107
108
        self.test_good_coeff(aacgmv2.AACGM_v2_DAT_PREFIX, aacgmv2.IGRF_COEFFS)
109
110
        assert not aacgmv2.__reset_warn__
111
        del sys.modules["aacgmv2"]
112
        del aacgmv2
113