Passed
Pull Request — master (#60)
by Angeline
01:04
created

TestPyEnviron.test_bad_coeff()   A

Complexity

Conditions 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 2
nop 2
1
# -*- coding: utf-8 -*-
2
from __future__ import division, absolute_import, unicode_literals
3
4
import os
5
import sys
6
import pytest
7
8
9
@pytest.mark.xfail
10
class TestPyEnviron:
11
    def setup(self):
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(self):
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
        with pytest.raises(AssertionError, match="BAD"):
50
            self.test_good_coeff(**{coeff: "bad path"})
51
52
    def test_top_parameters_default(self):
53
        """Test default module coefficients"""
54
55
        # Import AACGMV2 after removing any possible preset env variables
56
        self.reset_evar(evar=['AACGM_v2_DAT_PREFIX', 'IGRF_COEFFS'])
57
        import aacgmv2
58
59
        self.test_good_coeff(aacgmv2.AACGM_v2_DAT_PREFIX, aacgmv2.IGRF_COEFFS)
60
61
        assert not aacgmv2.__reset_warn__
62
        del sys.modules["aacgmv2"]
63
        del aacgmv2
64
65
    @pytest.mark.parametrize("evars", [(["AACGM_v2_DAT_PREFIX"]),
66
                                       (["AACGM_v2_DAT_PREFIX", "IGRF_COEFFS"]),
67
                                       (["IGRF_COEFFS"])])
68
    def test_top_parameters_reset_evar_to_specified(self, evars):
69
        """Test module reset of AACGM environment variables"""
70
71
        self.reset_evar(evar=evars)
72
        for i, evar in enumerate(evars):
73
            os.environ[evar] = 'test_prefix{:d}'.format(i)
74
75
        import aacgmv2
76
77
        self.test_good_coeff(aacgmv2.AACGM_v2_DAT_PREFIX, aacgmv2.IGRF_COEFFS)
78
79
        assert aacgmv2.__reset_warn__
80
        del sys.modules["aacgmv2"]
81
        del aacgmv2
82
83
    def test_top_parameters_set_same(self):
84
        """Test module non-reset with outside def of both coefficient paths"""
85
86
        from aacgmv2 import __file__ as file_path
87
88
        coeff_path = os.path.realpath(os.path.dirname(file_path))
89
        os.environ['AACGM_v2_DAT_PREFIX'] = os.path.join(coeff_path,
90
                                                         self.aacgm_path)
91
        os.environ['IGRF_COEFFS'] = os.path.join(coeff_path, self.igrf_path)
92
93
        import aacgmv2
94
95
        self.test_good_coeff(aacgmv2.AACGM_v2_DAT_PREFIX, aacgmv2.IGRF_COEFFS)
96
97
        assert not aacgmv2.__reset_warn__
98
        del sys.modules["aacgmv2"]
99
        del aacgmv2
100