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