TestUtilsStructure.setup_method()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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