Passed
Pull Request — main (#81)
by Angeline
01:42
created

TestTopStructure.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 logging
2
import numpy as np
3
import os
4
import pkgutil
5
import pytest
6
7
import aacgmv2
8
9
10
class TestModuleStructure(object):
11
    """Class for testing the module structure."""
12
13
    def setup_method(self):
14
        """Create a clean test environment."""
15
        # Define the acceptable output
16
        if not hasattr(self, "reference_list"):
17
            self.reference_list = list()
18
        if not hasattr(self, "module_name"):
19
            self.module_name = None
20
21
    def teardown_method(self):
22
        """Clean up the test environment."""
23
        del self.reference_list, self.module_name
24
25
    def test_module_existence(self):
26
        """Test the module existence."""
27
28
        # Get the dictionary of functions for the specified module
29
        retrieved_dict = aacgmv2.__dict__
30
31
        # Submodules only go one level down
32
        if self.module_name is None:
33
            assert True
34
        elif self.module_name != "aacgmv2":
35
            assert self.module_name in retrieved_dict.keys()
36
        else:
37
            assert isinstance(retrieved_dict, dict)
38
39
    def test_module_functions(self):
40
        """Test module function structure."""
41
42
        # Get the dictionary of functions for the specified module
43
        retrieved_dict = aacgmv2.__dict__
44
45
        if self.module_name is None:
46
            assert True
47
        else:
48
            if self.module_name != "aacgmv2":
49
                assert self.module_name in retrieved_dict.keys()
50
                retrieved_dict = retrieved_dict[self.module_name].__dict__
51
52
            # Get the functions attached to this module and make sure they
53
            # are supposed to be there
54
            retrieved_list = list()
55
            for name in retrieved_dict.keys():
56
                if callable(retrieved_dict[name]):
57
                    assert name in self.reference_list
58
                    retrieved_list.append(name)
59
60
            # Test to see if all of the modules match
61
            assert len(retrieved_list) == len(self.reference_list)
62
63
    def test_modules(self):
64
        """Test module submodule structure."""
65
66
        if self.module_name is None:
67
            assert True
68
        else:
69
            # Get the submodules and make sure they are supposed to be there
70
            retrieved_list = list()
71
            for imp, name, ispkg in pkgutil.iter_modules(path=aacgmv2.__path__):
72
                assert name in self.reference_list
73
                retrieved_list.append(name)
74
75
            # Test to see if all of the modules match
76
            assert len(retrieved_list) == len(self.reference_list)
77
78
79
class TestDepStructure(TestModuleStructure):
80
    """Test the deprecated structure."""
81
82
    def setup_method(self):
83
        """Create a clean test environment."""
84
        self.module_name = None
85
        self.reference_list = ["subsol", "igrf_dipole_axis", "gc2gd_lat"]
86
87
    def teardown_method(self):
88
        """Clean up the test environment."""
89
        del self.module_name, self.reference_list
90
91
    def test_dep_existence(self):
92
        """Test the deprecated functions."""
93
        self.module_name = "deprecated"
94
        self.test_module_existence()
95
96
    def test_dep_functions(self):
97
        """Test the deprecated functions."""
98
        self.module_name = "deprecated"
99
        self.test_module_functions()
100
101
102
class TestUtilsStructure(TestModuleStructure):
103
    """Test the utility structure."""
104
105
    def setup_method(self):
106
        """Create a clean test environment."""
107
        self.module_name = None
108
        self.reference_list = ["subsol", "igrf_dipole_axis", "gc2gd_lat"]
109
110
    def teardown_method(self):
111
        """Clean up the test environment."""
112
        del self.module_name, self.reference_list
113
114
    def test_dep_existence(self):
115
        """Test the utility functions."""
116
        self.module_name = "utils"
117
        self.test_module_existence()
118
119
    def test_dep_functions(self):
120
        """Test the utility functions."""
121
        self.module_name = "utils"
122
        self.test_module_functions()
123
124
125
class TestCStructure(TestModuleStructure):
126
    """Test the C structure."""
127
128
    def setup_method(self):
129
        """Create a clean test environment."""
130
        self.module_name = None
131
        self.reference_list = ["set_datetime", "convert", "inv_mlt_convert",
132
                               "inv_mlt_convert_yrsec", "mlt_convert",
133
                               "mlt_convert_yrsec", "inv_mlt_convert_arr",
134
                               "mlt_convert_arr", "convert_arr"]
135
136
    def teardown_method(self):
137
        """Clean up the test environment."""
138
        del self.module_name, self.reference_list
139
140
    def test_c_existence(self):
141
        """Test the C module existence."""
142
        self.module_name = "_aacgmv2"
143
        self.test_module_existence()
144
145
    def test_c_functions(self):
146
        """Test the C functions."""
147
        self.module_name = "_aacgmv2"
148
        self.test_module_functions()
149
150
151
class TestPyStructure(TestModuleStructure):
152
    """Test the Python structure."""
153
154
    def setup_method(self):
155
        """Create a clean test environment."""
156
        self.module_name = None
157
        self.reference_list = ["convert_bool_to_bit", "convert_str_to_bit",
158
                               "convert_mlt", "convert_latlon", "test_height",
159
                               "convert_latlon_arr", "get_aacgm_coord",
160
                               "get_aacgm_coord_arr", "set_coeff_path",
161
                               "test_time"]
162
163
    def teardown_method(self):
164
        """Clean up the test environment."""
165
        del self.module_name, self.reference_list
166
167
    def test_py_existence(self):
168
        """Test the python module existence."""
169
        self.module_name = "wrapper"
170
        self.test_module_existence()
171
172
    def test_py_functions(self):
173
        """Test the python functions."""
174
        self.module_name = "wrapper"
175
        self.test_module_functions()
176
177
178
class TestTopStructure(TestModuleStructure):
179
    """Test the top-level structure."""
180
181
    def setup_method(self):
182
        """Create a clean test environment."""
183
        self.module_name = None
184
        self.reference_list = list()
185
186
    def teardown_method(self):
187
        """Clean up the test environment."""
188
        del self.module_name, self.reference_list
189
190
    def test_top_existence(self):
191
        """Test the top level existence."""
192
        self.module_name = "aacgmv2"
193
        self.test_module_existence()
194
195
    def test_top_functions(self):
196
        """Test the deprecated functions."""
197
        self.module_name = "aacgmv2"
198
        self.reference_list = ["convert_bool_to_bit", "convert_str_to_bit",
199
                               "convert_mlt", "convert_latlon",
200
                               "convert_latlon_arr", "get_aacgm_coord",
201
                               "get_aacgm_coord_arr"]
202
        self.test_module_functions()
203
204
    def test_top_modules(self):
205
        """Test the deprecated functions."""
206
        self.module_name = "aacgmv2"
207
        self.reference_list = ["_aacgmv2", "wrapper", "utils",
208
                               "deprecated", "__main__"]
209
        self.test_modules()
210
211
212
class TestTopVariables(object):
213
    """Test the top-level variables."""
214
215
    def setup_method(self):
216
        """Create a clean test environment."""
217
        self.alt_limits = {"coeff": 2000.0, "trace": 6378.0}
218
        self.coeff_file = {"coeff": os.path.join("aacgmv2", "aacgmv2",
219
                                                 "aacgm_coeffs",
220
                                                 "aacgm_coeffs-13-"),
221
                           "igrf": os.path.join("aacgmv2", "aacgmv2",
222
                                                "magmodel_1590-2020.txt")}
223
224
    def teardown_method(self):
225
        """Clean up the test environment."""
226
        del self.alt_limits, self.coeff_file
227
228
    @pytest.mark.parametrize("env_var,fkey",
229
                             [(aacgmv2.AACGM_v2_DAT_PREFIX, "coeff"),
230
                              (aacgmv2.IGRF_COEFFS, "igrf")])
231
    def test_top_parameters(self, env_var, fkey):
232
        """Test module constants.
233
234
        Parameters
235
        ----------
236
        env_var : str
237
            Environment variable
238
        fkey : str
239
            Corresponding dict key
240
241
        """
242
243
        if env_var.find(self.coeff_file[fkey]) < 0:
244
            raise AssertionError("Bad env variable: {:} not {:}".format(
245
                self.coeff_file[fkey], env_var))
246
247
    @pytest.mark.parametrize("alt_var,alt_ref",
248
                             [(aacgmv2.high_alt_coeff, "coeff"),
249
                              (aacgmv2.high_alt_trace, "trace")])
250
    def test_high_alt_variables(self, alt_var, alt_ref):
251
        """Test that module altitude limits exist and are appropriate.
252
253
        Parameters
254
        ----------
255
        alt_var : float
256
            Altitude variable with float output
257
        alt_ref : str
258
            Output dict key
259
260
        """
261
262
        if not isinstance(alt_var, type(self.alt_limits[alt_ref])):
263
            raise TypeError("Altitude limit variable isn't a float")
264
265
        np.testing.assert_almost_equal(alt_var, self.alt_limits[alt_ref],
266
                                       decimal=4)
267
268
    def test_module_logger(self):
269
        """Test the module logger instance."""
270
271
        if not isinstance(aacgmv2.logger, logging.Logger):
272
            raise TypeError("Logger incorrect type")
273