Passed
Pull Request — master (#40)
by Angeline
01:18
created

test_struct_aacgmv2   A

Complexity

Total Complexity 42

Size/Duplication

Total Lines 208
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 42
eloc 134
dl 0
loc 208
rs 9.0399
c 0
b 0
f 0

25 Methods

Rating   Name   Duplication   Size   Complexity  
A TestDepStructure.test_dep_functions() 0 4 1
A TestModuleStructure.setup() 0 7 3
A TestDepStructure.teardown() 0 2 1
A TestCStructure.test_c_existence() 0 4 1
A TestModuleStructure.test_modules() 0 14 3
A TestCStructure.test_c_functions() 0 4 1
A TestDepStructure.setup() 0 4 1
A TestCStructure.setup() 0 5 1
A TestModuleStructure.test_module_functions() 0 23 5
A TestModuleStructure.teardown() 0 2 1
A TestCStructure.teardown() 0 2 1
A TestModuleStructure.test_module_existence() 0 13 3
A TestDepStructure.test_dep_existence() 0 4 1
A TestPyStructure.test_py_functions() 0 4 1
A TestPyStructure.test_py_existence() 0 4 1
A TestTopStructure.test_top_functions() 0 8 1
A TestTopStructure.test_top_parameters() 0 14 3
A TestTopStructure.test_module_logger() 0 6 2
A TestPyStructure.teardown() 0 2 1
A TestPyStructure.setup() 0 7 1
A TestTopStructure.setup() 0 3 1
A TestTopStructure.teardown() 0 2 1
A TestTopStructure.test_high_alt_variables() 0 15 5
A TestTopStructure.test_top_existence() 0 4 1
A TestTopStructure.test_top_modules() 0 6 1

How to fix   Complexity   

Complexity

Complex classes like test_struct_aacgmv2 often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
# -*- coding: utf-8 -*-
2
from __future__ import division, absolute_import, unicode_literals
3
4
import datetime as dt
5
import logging
6
import numpy as np
7
import os
8
import pkgutil
9
import pytest
10
11
import aacgmv2
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
class TestDepStructure(TestModuleStructure):
80
    def setup(self):
81
        self.module_name = None
82
        self.reference_list = ["convert", "subsol", "gc2gd_lat",
83
                               "igrf_dipole_axis"]
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
class TestCStructure(TestModuleStructure):
99
    def setup(self):
100
        self.module_name = None
101
        self.reference_list = ["set_datetime", "convert", "inv_mlt_convert",
102
                               "inv_mlt_convert_yrsec", "mlt_convert",
103
                               "mlt_convert_yrsec"]
104
105
    def teardown(self):
106
        del self.module_name, self.reference_list
107
108
    def test_c_existence(self):
109
        """ Test the C module existence"""
110
        self.module_name = "_aacgmv2"
111
        self.test_module_existence()
112
113
    def test_c_functions(self):
114
        """ Test the C functions"""
115
        self.module_name = "_aacgmv2"
116
        self.test_module_functions()
117
118
119
class TestPyStructure(TestModuleStructure):
120
    def setup(self):
121
        self.module_name = None
122
        self.reference_list = ["convert_bool_to_bit", "convert_str_to_bit",
123
                               "convert_mlt", "convert_latlon", "test_height",
124
                               "convert_latlon_arr", "get_aacgm_coord",
125
                               "get_aacgm_coord_arr", "set_coeff_path",
126
                               "test_time"]
127
128
    def teardown(self):
129
        del self.module_name, self.reference_list
130
131
    def test_py_existence(self):
132
        """ Test the python module existence"""
133
        self.module_name = "wrapper"
134
        self.test_module_existence()
135
136
    def test_py_functions(self):
137
        """ Test the python functions"""
138
        self.module_name = "wrapper"
139
        self.test_module_functions()
140
141
142
class TestTopStructure(TestModuleStructure):
143
    def setup(self):
144
        self.module_name = None
145
        self.reference_list = list()
146
147
    def teardown(self):
148
        del self.module_name, self.reference_list
149
150
    def test_top_existence(self):
151
        """ Test the top level existence"""
152
        self.module_name = "aacgmv2"
153
        self.test_module_existence()
154
155
    def test_top_functions(self):
156
        """ Test the deprecated functions"""
157
        self.module_name = "aacgmv2"
158
        self.reference_list = ["convert_bool_to_bit", "convert_str_to_bit",
159
                               "convert_mlt", "convert_latlon",
160
                               "convert_latlon_arr", "get_aacgm_coord",
161
                               "get_aacgm_coord_arr", "convert"]
162
        self.test_module_functions()
163
164
    def test_top_modules(self):
165
        """ Test the deprecated functions"""
166
        self.module_name = "aacgmv2"
167
        self.reference_list = ["_aacgmv2", "wrapper",
168
                               "deprecated", "__main__"]
169
        self.test_modules()
170
171
    @classmethod
172
    def test_top_parameters(self):
173
        """Test module constants"""
174
175
        path1 = os.path.join("aacgmv2", "aacgmv2", "aacgm_coeffs",
176
                          "aacgm_coeffs-12-")
177
        if aacgmv2.AACGM_v2_DAT_PREFIX.find(path1) < 0:
178
            raise AssertionError()
179
180
        path2 = os.path.join("aacgmv2", "aacgmv2", "magmodel_1590-2015.txt")
181
        if aacgmv2.IGRF_COEFFS.find(path2) < 0:
182
            raise AssertionError()
183
184
        del path1, path2
185
186
    @classmethod
187
    def test_high_alt_variables(self):
188
        """ Test that module altitude limits exist and are appropriate"""
189
190
        if not isinstance(aacgmv2.high_alt_coeff, float):
191
            raise TypeError("Coefficient upper limit not float")
192
193
        if not isinstance(aacgmv2.high_alt_trace, float):
194
            raise TypeError("Trace upper limit not float")
195
196
        if aacgmv2.high_alt_coeff != 2000.0:
197
            raise ValueError("unexpected coefficient upper limit")
198
199
        if aacgmv2.high_alt_trace <= aacgmv2.high_alt_trace:
200
            raise ValueError("Trace limit lower than coefficient limit")
201
202
    @classmethod
203
    def test_module_logger(self):
204
        """ Test the module logger instance"""
205
        
206
        if not isinstance(aacgmv2.logger, logging.Logger):
207
            raise TypeError("Logger incorrect type")
208