Passed
Pull Request — develop (#57)
by Angeline
01:07
created

test_struct_aacgmv2.TestTopVariables.setup()   A

Complexity

Conditions 1

Size

Total Lines 7
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nop 1
1
# -*- coding: utf-8 -*-
2
from __future__ import division, absolute_import, unicode_literals
3
4
import logging
5
import numpy as np
6
import os
7
import pkgutil
8
import pytest
9
10
import aacgmv2
11
12
13
# @pytest.mark.skip(reason="Not meant to be run alone")
14
class TestModuleStructure:
15
    def setup(self):
16
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(self):
24
        del self.reference_list, self.module_name
25
26
    def test_module_existence(self):
27
        """Test the module existence"""
28
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
43
        # Get the dictionary of functions for the specified module
44
        retrieved_dict = aacgmv2.__dict__
45
46
        if self.module_name is None:
47
            assert True
48
        else:
49
            if self.module_name != "aacgmv2":
50
                assert self.module_name in retrieved_dict.keys()
51
                retrieved_dict = retrieved_dict[self.module_name].__dict__
52
53
            # Get the functions attached to this module and make sure they
54
            # are supposed to be there
55
            retrieved_list = list()
56
            for name in retrieved_dict.keys():
57
                if callable(retrieved_dict[name]):
58
                    assert name in self.reference_list
59
                    retrieved_list.append(name)
60
61
            # Test to see if all of the modules match
62
            assert len(retrieved_list) == len(self.reference_list)
63
64
    def test_modules(self):
65
        """Test module submodule structure"""
66
67
        if self.module_name is None:
68
            assert True
69
        else:
70
            # Get the submodules and make sure they are supposed to be there
71
            retrieved_list = list()
72
            for imp, name, ispkg in pkgutil.iter_modules(path=aacgmv2.__path__):
73
                assert name in self.reference_list
74
                retrieved_list.append(name)
75
76
            # Test to see if all of the modules match
77
            assert len(retrieved_list) == len(self.reference_list)
78
79
80
class TestDepStructure(TestModuleStructure):
81
    def setup(self):
82
        self.module_name = None
83
        self.reference_list = ["subsol", "igrf_dipole_axis", "gc2gd_lat"]
84
85
    def teardown(self):
86
        del self.module_name, self.reference_list
87
88
    def test_dep_existence(self):
89
        """ Test the deprecated functions"""
90
        self.module_name = "deprecated"
91
        self.test_module_existence()
92
93
    def test_dep_functions(self):
94
        """ Test the deprecated functions"""
95
        self.module_name = "deprecated"
96
        self.test_module_functions()
97
98
99
class TestUtilsStructure(TestModuleStructure):
100
    def setup(self):
101
        self.module_name = None
102
        self.reference_list = ["subsol", "igrf_dipole_axis", "gc2gd_lat"]
103
104
    def teardown(self):
105
        del self.module_name, self.reference_list
106
107
    def test_dep_existence(self):
108
        """ Test the utility functions"""
109
        self.module_name = "utils"
110
        self.test_module_existence()
111
112
    def test_dep_functions(self):
113
        """ Test the utility functions"""
114
        self.module_name = "utils"
115
        self.test_module_functions()
116
117
118
class TestCStructure(TestModuleStructure):
119
    def setup(self):
120
        self.module_name = None
121
        self.reference_list = ["set_datetime", "convert", "inv_mlt_convert",
122
                               "inv_mlt_convert_yrsec", "mlt_convert",
123
                               "mlt_convert_yrsec", "inv_mlt_convert_arr",
124
                               "mlt_convert_arr", "convert_arr"]
125
126
    def teardown(self):
127
        del self.module_name, self.reference_list
128
129
    def test_c_existence(self):
130
        """ Test the C module existence"""
131
        self.module_name = "_aacgmv2"
132
        self.test_module_existence()
133
134
    def test_c_functions(self):
135
        """ Test the C functions"""
136
        self.module_name = "_aacgmv2"
137
        self.test_module_functions()
138
139
140
class TestPyStructure(TestModuleStructure):
141
    def setup(self):
142
        self.module_name = None
143
        self.reference_list = ["convert_bool_to_bit", "convert_str_to_bit",
144
                               "convert_mlt", "convert_latlon", "test_height",
145
                               "convert_latlon_arr", "get_aacgm_coord",
146
                               "get_aacgm_coord_arr", "set_coeff_path",
147
                               "test_time"]
148
149
    def teardown(self):
150
        del self.module_name, self.reference_list
151
152
    def test_py_existence(self):
153
        """ Test the python module existence"""
154
        self.module_name = "wrapper"
155
        self.test_module_existence()
156
157
    def test_py_functions(self):
158
        """ Test the python functions"""
159
        self.module_name = "wrapper"
160
        self.test_module_functions()
161
162
163
class TestTopStructure(TestModuleStructure):
164
    def setup(self):
165
        self.module_name = None
166
        self.reference_list = list()
167
168
    def teardown(self):
169
        del self.module_name, self.reference_list
170
171
    def test_top_existence(self):
172
        """ Test the top level existence"""
173
        self.module_name = "aacgmv2"
174
        self.test_module_existence()
175
176
    def test_top_functions(self):
177
        """ Test the deprecated functions"""
178
        self.module_name = "aacgmv2"
179
        self.reference_list = ["convert_bool_to_bit", "convert_str_to_bit",
180
                               "convert_mlt", "convert_latlon",
181
                               "convert_latlon_arr", "get_aacgm_coord",
182
                               "get_aacgm_coord_arr"]
183
        self.test_module_functions()
184
185
    def test_top_modules(self):
186
        """ Test the deprecated functions"""
187
        self.module_name = "aacgmv2"
188
        self.reference_list = ["_aacgmv2", "wrapper", "utils",
189
                               "deprecated", "__main__"]
190
        self.test_modules()
191
192
193
class TestTopVariables:
194
    def setup(self):
195
        self.alt_limits = {"coeff": 2000.0, "trace": 6378.0}
196
        self.coeff_file = {"coeff": os.path.join("aacgmv2", "aacgmv2",
197
                                                 "aacgm_coeffs",
198
                                                 "aacgm_coeffs-13-"),
199
                           "igrf": os.path.join("aacgmv2", "aacgmv2",
200
                                                "magmodel_1590-2020.txt")}
201
202
    def teardown(self):
203
        del self.alt_limits, self.coeff_file
204
205
    @pytest.mark.parametrize("env_var,fkey",
206
                             [(aacgmv2.AACGM_v2_DAT_PREFIX, "coeff"),
207
                              (aacgmv2.IGRF_COEFFS, "igrf")])
208
    def test_top_parameters(self, env_var, fkey):
209
        """Test module constants"""
210
211
        if env_var.find(self.coeff_file[fkey]) < 0:
212
            raise AssertionError("Bad env variable: {:} not {:}".format(
213
                self.coeff_file[fkey], env_var))
214
215
    @pytest.mark.parametrize("alt_var,alt_ref",
216
                             [(aacgmv2.high_alt_coeff, "coeff"),
217
                              (aacgmv2.high_alt_trace, "trace")])
218
    def test_high_alt_variables(self, alt_var, alt_ref):
219
        """ Test that module altitude limits exist and are appropriate"""
220
221
        if not isinstance(alt_var, type(self.alt_limits[alt_ref])):
222
            raise TypeError("Altitude limit variable isn't a float")
223
224
        np.testing.assert_almost_equal(alt_var, self.alt_limits[alt_ref],
225
                                       decimal=4)
226
227
    def test_module_logger(self):
228
        """ Test the module logger instance"""
229
230
        if not isinstance(aacgmv2.logger, logging.Logger):
231
            raise TypeError("Logger incorrect type")
232