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