|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
from __future__ import division, absolute_import, unicode_literals |
|
3
|
|
|
|
|
4
|
|
|
import os |
|
5
|
|
|
import pytest |
|
6
|
|
|
|
|
7
|
|
|
@pytest.mark.skip(reason='only works for first import') |
|
8
|
|
|
class TestPyEnviron: |
|
9
|
|
|
def setup(self): |
|
10
|
|
|
self.igrf_path = os.path.join("aacgmv2", "aacgmv2", |
|
11
|
|
|
"magmodel_1590-2015.txt") |
|
12
|
|
|
self.aacgm_path = os.path.join("aacgmv2", "aacgmv2", "aacgm_coeffs", |
|
13
|
|
|
"aacgm_coeffs-12-") |
|
14
|
|
|
|
|
15
|
|
|
def teardown(self): |
|
16
|
|
|
del self.igrf_path, self.aacgm_path |
|
17
|
|
|
|
|
18
|
|
|
def reset_evar(self, evar): |
|
19
|
|
|
""" Reset the environment variables """ |
|
20
|
|
|
|
|
21
|
|
|
for coeff_key in evar: |
|
22
|
|
|
if coeff_key in os.environ.keys(): |
|
23
|
|
|
del os.environ[coeff_key] |
|
24
|
|
|
|
|
25
|
|
|
for coeff_key in evar: |
|
26
|
|
|
assert coeff_key not in os.environ.keys() |
|
27
|
|
|
|
|
28
|
|
|
def test_good_coeff(self, aacgm_test=None, igrf_test=None): |
|
29
|
|
|
""" Test the coefficient path/prefixes """ |
|
30
|
|
|
|
|
31
|
|
|
# Set the defaults |
|
32
|
|
|
if aacgm_test is None: |
|
33
|
|
|
aacgm_test = self.aacgm_path |
|
34
|
|
|
if igrf_test is None: |
|
35
|
|
|
igrf_test = self.igrf_path |
|
36
|
|
|
|
|
37
|
|
|
# Perform the test |
|
38
|
|
|
if aacgm_test.find(self.aacgm_path) < 0: |
|
39
|
|
|
raise AssertionError('BAD AACGMV PATH') |
|
40
|
|
|
|
|
41
|
|
|
if igrf_test.find(self.igrf_path) < 0: |
|
42
|
|
|
raise AssertionError('BAD IGRF PATH') |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
def test_top_parameters_default(self): |
|
46
|
|
|
"""Test default module coefficients""" |
|
47
|
|
|
|
|
48
|
|
|
# Import AACGMV2 after removing any possible preset env variables |
|
49
|
|
|
self.reset_evar(evar=['AACGM_v2_DAT_PREFIX', 'IGRF_COEFFS']) |
|
50
|
|
|
import aacgmv2 |
|
51
|
|
|
|
|
52
|
|
|
self.test_good_coeff(aacgmv2.AACGM_v2_DAT_PREFIX, aacgmv2.IGRF_COEFFS) |
|
53
|
|
|
|
|
54
|
|
|
assert not aacgmv2.__reset_warn__ |
|
55
|
|
|
del aacgmv2 |
|
56
|
|
|
|
|
57
|
|
|
def test_top_parameters_reset_aacgm(self): |
|
58
|
|
|
"""Test module reset of AACGM coefficient path""" |
|
59
|
|
|
|
|
60
|
|
|
self.reset_evar(evar=['AACGM_v2_DAT_PREFIX']) |
|
61
|
|
|
os.environ['AACGM_v2_DAT_PREFIX'] = 'test_prefix' |
|
62
|
|
|
|
|
63
|
|
|
import aacgmv2 |
|
64
|
|
|
|
|
65
|
|
|
self.test_good_coeff(aacgmv2.AACGM_v2_DAT_PREFIX, aacgmv2.IGRF_COEFFS) |
|
66
|
|
|
|
|
67
|
|
|
assert aacgmv2.__reset_warn__ |
|
68
|
|
|
del aacgmv2 |
|
69
|
|
|
|
|
70
|
|
|
def test_top_parameters_reset_igrf(self): |
|
71
|
|
|
"""Test module reset of IGRF coefficient path""" |
|
72
|
|
|
|
|
73
|
|
|
self.reset_evar(evar=['IGRF_COEFFS']) |
|
74
|
|
|
os.environ['IGRF_COEFFS'] = 'test_prefix' |
|
75
|
|
|
|
|
76
|
|
|
import aacgmv2 |
|
77
|
|
|
|
|
78
|
|
|
self.test_good_coeff(aacgmv2.AACGM_v2_DAT_PREFIX, aacgmv2.IGRF_COEFFS) |
|
79
|
|
|
|
|
80
|
|
|
assert aacgmv2.__reset_warn__ |
|
81
|
|
|
del aacgmv2 |
|
82
|
|
|
|
|
83
|
|
|
def test_top_parameters_reset_both(self): |
|
84
|
|
|
"""Test module reset of both coefficient paths""" |
|
85
|
|
|
|
|
86
|
|
|
os.environ['AACGM_v2_DAT_PREFIX'] = 'test_prefix1' |
|
87
|
|
|
os.environ['IGRF_COEFFS'] = 'test_prefix2' |
|
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 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 aacgmv2 |
|
112
|
|
|
|