Passed
Pull Request — develop (#77)
by Angeline
01:21
created

TestPyStructure.test_py_functions()   A

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 4
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
# @pytest.mark.skip(reason="Not meant to be run alone")
11
class TestModuleStructure(object):
12
    """Class for testing the module structure."""
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
    def setup_method(self):
82
        """Create a clean test environment."""
83
        self.module_name = None
84
        self.reference_list = ["subsol", "igrf_dipole_axis", "gc2gd_lat"]
85
86
    def teardown_method(self):
87
        """Clean up the test environment."""
88
        del self.module_name, self.reference_list
89
90
    def test_dep_existence(self):
91
        """Test the deprecated functions."""
92
        self.module_name = "deprecated"
93
        self.test_module_existence()
94
95
    def test_dep_functions(self):
96
        """Test the deprecated functions."""
97
        self.module_name = "deprecated"
98
        self.test_module_functions()
99
100
101
class TestUtilsStructure(TestModuleStructure):
102
    """Test the utility structure."""
103
    def setup_method(self):
104
        """Create a clean test environment."""
105
        self.module_name = None
106
        self.reference_list = ["subsol", "igrf_dipole_axis", "gc2gd_lat"]
107
108
    def teardown_method(self):
109
        """Clean up the test environment."""
110
        del self.module_name, self.reference_list
111
112
    def test_dep_existence(self):
113
        """Test the utility functions."""
114
        self.module_name = "utils"
115
        self.test_module_existence()
116
117
    def test_dep_functions(self):
118
        """Test the utility functions."""
119
        self.module_name = "utils"
120
        self.test_module_functions()
121
122
123
class TestCStructure(TestModuleStructure):
124
    """Test the C structure."""
125
    def setup_method(self):
126
        """Create a clean test environment."""
127
        self.module_name = None
128
        self.reference_list = ["set_datetime", "convert", "inv_mlt_convert",
129
                               "inv_mlt_convert_yrsec", "mlt_convert",
130
                               "mlt_convert_yrsec", "inv_mlt_convert_arr",
131
                               "mlt_convert_arr", "convert_arr"]
132
133
    def teardown_method(self):
134
        """Clean up the test environment."""
135
        del self.module_name, self.reference_list
136
137
    def test_c_existence(self):
138
        """Test the C module existence."""
139
        self.module_name = "_aacgmv2"
140
        self.test_module_existence()
141
142
    def test_c_functions(self):
143
        """Test the C functions."""
144
        self.module_name = "_aacgmv2"
145
        self.test_module_functions()
146
147
148
class TestPyStructure(TestModuleStructure):
149
    """Test the Python structure."""
150
    def setup_method(self):
151
        """Create a clean test environment."""
152
        self.module_name = None
153
        self.reference_list = ["convert_bool_to_bit", "convert_str_to_bit",
154
                               "convert_mlt", "convert_latlon", "test_height",
155
                               "convert_latlon_arr", "get_aacgm_coord",
156
                               "get_aacgm_coord_arr", "set_coeff_path",
157
                               "test_time"]
158
159
    def teardown_method(self):
160
        """Clean up the test environment."""
161
        del self.module_name, self.reference_list
162
163
    def test_py_existence(self):
164
        """Test the python module existence."""
165
        self.module_name = "wrapper"
166
        self.test_module_existence()
167
168
    def test_py_functions(self):
169
        """Test the python functions."""
170
        self.module_name = "wrapper"
171
        self.test_module_functions()
172
173
174
class TestTopStructure(TestModuleStructure):
175
    """Test the top-level structure."""
176
    def setup_method(self):
177
        """Create a clean test environment."""
178
        self.module_name = None
179
        self.reference_list = list()
180
181
    def teardown_method(self):
182
        """Clean up the test environment."""
183
        del self.module_name, self.reference_list
184
185
    def test_top_existence(self):
186
        """Test the top level existence."""
187
        self.module_name = "aacgmv2"
188
        self.test_module_existence()
189
190
    def test_top_functions(self):
191
        """Test the deprecated functions."""
192
        self.module_name = "aacgmv2"
193
        self.reference_list = ["convert_bool_to_bit", "convert_str_to_bit",
194
                               "convert_mlt", "convert_latlon",
195
                               "convert_latlon_arr", "get_aacgm_coord",
196
                               "get_aacgm_coord_arr"]
197
        self.test_module_functions()
198
199
    def test_top_modules(self):
200
        """Test the deprecated functions."""
201
        self.module_name = "aacgmv2"
202
        self.reference_list = ["_aacgmv2", "wrapper", "utils",
203
                               "deprecated", "__main__"]
204
        self.test_modules()
205
206
207
class TestTopVariables(object):
208
    """Test the top-level variables."""
209
    def setup_method(self):
210
        """Create a clean test environment."""
211
        self.alt_limits = {"coeff": 2000.0, "trace": 6378.0}
212
        self.coeff_file = {"coeff": os.path.join("aacgmv2", "aacgmv2",
213
                                                 "aacgm_coeffs",
214
                                                 "aacgm_coeffs-13-"),
215
                           "igrf": os.path.join("aacgmv2", "aacgmv2",
216
                                                "magmodel_1590-2020.txt")}
217
218
    def teardown_method(self):
219
        """Clean up the test environment."""
220
        del self.alt_limits, self.coeff_file
221
222
    @pytest.mark.parametrize("env_var,fkey",
223
                             [(aacgmv2.AACGM_v2_DAT_PREFIX, "coeff"),
224
                              (aacgmv2.IGRF_COEFFS, "igrf")])
225
    def test_top_parameters(self, env_var, fkey):
226
        """Test module constants.
227
228
        Parameters
229
        ----------
230
        env_var : str
231
            Environment variable
232
        fkey : str
233
            Corresponding dict key
234
235
        """
236
237
        if env_var.find(self.coeff_file[fkey]) < 0:
238
            raise AssertionError("Bad env variable: {:} not {:}".format(
239
                self.coeff_file[fkey], env_var))
240
241
    @pytest.mark.parametrize("alt_var,alt_ref",
242
                             [(aacgmv2.high_alt_coeff, "coeff"),
243
                              (aacgmv2.high_alt_trace, "trace")])
244
    def test_high_alt_variables(self, alt_var, alt_ref):
245
        """Test that module altitude limits exist and are appropriate.
246
247
        Parameters
248
        ----------
249
        alt_var : float
250
            Altitude variable with float output
251
        alt_ref : str
252
            Output dict key
253
254
        """
255
256
        if not isinstance(alt_var, type(self.alt_limits[alt_ref])):
257
            raise TypeError("Altitude limit variable isn't a float")
258
259
        np.testing.assert_almost_equal(alt_var, self.alt_limits[alt_ref],
260
                                       decimal=4)
261
262
    def test_module_logger(self):
263
        """Test the module logger instance."""
264
265
        if not isinstance(aacgmv2.logger, logging.Logger):
266
            raise TypeError("Logger incorrect type")
267