Test Failed
Pull Request — master (#9)
by Angeline
03:32
created

test_dep_aacgmv2   F

Complexity

Total Complexity 68

Size/Duplication

Total Lines 207
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 68
eloc 150
dl 0
loc 207
rs 2.9411
c 0
b 0
f 0

20 Methods

Rating   Name   Duplication   Size   Complexity  
B TestDepAACGMV2.test_convert_location_failure() 0 7 5
A TestDepAACGMV2.test_gc2gd_lat_list() 0 6 1
A TestDepAACGMV2.teardown() 0 3 1
A TestDepAACGMV2.test_warning_below_ground_convert() 0 10 3
A TestDepAACGMV2.test_gc2gd_lat_arr() 0 6 1
A TestDepAACGMV2.test_convert_badidea_failure() 0 5 2
A TestDepAACGMV2.test_convert_lat_failure() 0 4 2
C TestDepAACGMV2.test_module_structure() 0 9 8
A TestDepAACGMV2.test_convert_time_failure() 0 4 2
A TestDepAACGMV2.test_subsol() 0 9 1
B TestDepAACGMV2.test_convert_list() 0 15 7
A TestDepAACGMV2.test_gc2gd_lat() 0 5 1
A TestDepAACGMV2.test_convert_maxalt_failure() 0 5 2
B TestDepAACGMV2.test_convert_arr() 0 17 7
A TestDepAACGMV2.test_convert_single_val() 0 8 4
A TestDepAACGMV2.test_set_coeff_path() 0 10 3
A TestDepAACGMV2.test_convert_datetime_date() 0 6 3
A TestDepAACGMV2.setup() 0 4 1
F TestDepAACGMV2.test_convert_unequal() 0 39 13
A TestDepAACGMV2.test_igrf_dipole_axis() 0 6 1

How to fix   Complexity   

Complexity

Complex classes like test_dep_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 numpy as np
6
import pytest
7
import aacgmv2
8
9
class TestDepAACGMV2:
10
    def setup(self):
11
        """Runs before every method to create a clean testing setup"""
12
        self.dtime = dt.datetime(2015, 1, 1, 0, 0, 0)
13
        self.ddate = dt.date(2015, 1, 1)
14
15
    def teardown(self):
16
        """Runs after every method to clean up previous testing"""
17
        del self.dtime, self.ddate
18
19
    def test_module_structure(self):
20
        """Test module structure for depricated routines"""
21
        assert aacgmv2
22
        assert aacgmv2.convert
23
        assert aacgmv2.set_coeff_path
24
        assert aacgmv2.subsol
25
        assert aacgmv2.depricated
26
        assert aacgmv2.depricated.gc2gd_lat
27
        assert aacgmv2.depricated.igrf_dipole_axis
28
29
    def test_set_coeff_path(self):
30
        """Test the depricated routine for appropriate warning"""
31
        import logbook
32
        lwarn = u"this routine is no longer needed"
33
34
        with logbook.TestHandler() as handler:
35
            aacgmv2.set_coeff_path()
36
            assert handler.has_warning(lwarn)
37
38
        handler.close()
39
40
    def test_convert_single_val(self):
41
        """Test conversion for a single value"""
42
        lat, lon = aacgmv2.convert(60, 0, 300, self.dtime)
43
        assert isinstance(lat, np.ndarray)
44
        assert isinstance(lon, np.ndarray)
45
        assert lat.shape == lon.shape and lat.shape == (1,)
46
        np.testing.assert_allclose(lat, [58.2258], rtol=1e-4)
47
        np.testing.assert_allclose(lon, [81.1685], rtol=1e-4)
48
49
    def test_convert_list(self):
50
        """Test conversion for list input"""
51
        lat, lon = aacgmv2.convert([60], [0], [300], self.dtime)
52
        assert isinstance(lat, np.ndarray)
53
        assert isinstance(lon, np.ndarray)
54
        assert lat.shape == lon.shape and lat.shape == (1,)
55
        np.testing.assert_allclose(lat, [58.2258], rtol=1e-4)
56
        np.testing.assert_allclose(lon, [81.1685], rtol=1e-4)
57
58
        lat, lon = aacgmv2.convert([60, 61], [0, 0], [300, 300], self.dtime)
59
        assert isinstance(lat, np.ndarray)
60
        assert isinstance(lon, np.ndarray)
61
        assert lat.shape == lon.shape and lat.shape == (2,)
62
        np.testing.assert_allclose(lat, [58.2258, 59.3186], rtol=1e-4)
63
        np.testing.assert_allclose(lon, [81.1685, 81.6140], rtol=1e-4)
64
65
    def test_convert_arr(self):
66
        """Test conversion for array input"""
67
        lat, lon = aacgmv2.convert(np.array([60]), np.array([0]),
68
                                   np.array([300]), self.dtime)
69
        assert isinstance(lat, np.ndarray)
70
        assert isinstance(lon, np.ndarray)
71
        assert lat.shape == lon.shape and lat.shape == (1,)
72
        np.testing.assert_allclose(lat, [58.2258], rtol=1e-4)
73
        np.testing.assert_allclose(lon, [81.1685], rtol=1e-4)
74
75
        lat, lon = aacgmv2.convert(np.array([60, 61]), np.array([0, 0]),
76
                                   np.array([300, 300]), self.dtime)
77
        assert isinstance(lat, np.ndarray)
78
        assert isinstance(lon, np.ndarray)
79
        assert lat.shape == lon.shape and lat.shape == (2,)
80
        np.testing.assert_allclose(lat, [58.2258, 59.3186], rtol=1e-4)
81
        np.testing.assert_allclose(lon, [81.1685, 81.6140], rtol=1e-4)
82
83
    def test_convert_unequal(self):
84
        """Test conversion for unequal sized input"""
85
        lat, lon = aacgmv2.convert([60, 61], 0, 300, self.dtime)
86
        assert isinstance(lat, np.ndarray)
87
        assert isinstance(lon, np.ndarray)
88
        assert lat.shape == lon.shape and lat.shape == (2,)
89
        np.testing.assert_allclose(lat, [58.2258, 59.3186], rtol=1e-4)
90
        np.testing.assert_allclose(lon, [81.1685, 81.6140], rtol=1e-4)
91
92
        lat, lon = aacgmv2.convert(np.array([60, 61]), 0, 300, self.dtime)
93
        assert isinstance(lat, np.ndarray)
94
        assert isinstance(lon, np.ndarray)
95
        assert lat.shape == lon.shape and lat.shape == (2,)
96
        np.testing.assert_allclose(lat, [58.2258, 59.3186], rtol=1e-4)
97
        np.testing.assert_allclose(lon, [81.1685, 81.6140], rtol=1e-4)
98
99
        lat, lon = aacgmv2.convert(np.array([[60, 61, 62], [63, 64, 65]]), 0,
100
                                   300, self.dtime)
101
        assert isinstance(lat, np.ndarray)
102
        assert isinstance(lon, np.ndarray)
103
        assert lat.shape == lon.shape and lat.shape == (2, 3)
104
        np.testing.assert_allclose(lat, [[58.2258, 59.3186, 60.4040],
105
                                         [61.4820, 62.5528, 63.6164]],
106
                                   rtol=1e-4)
107
        np.testing.assert_allclose(lon, [[81.1685, 81.6140, 82.0872],
108
                                         [82.5909, 83.1286, 83.7039]],
109
                                   rtol=1e-4)
110
111
        lat, lon = aacgmv2.convert(np.array([[60, 61, 62], [63, 64, 65]]), [0],
112
                                   [300], self.dtime)
113
        assert isinstance(lat, np.ndarray)
114
        assert isinstance(lon, np.ndarray)
115
        assert lat.shape == lon.shape and lat.shape == (2, 3)
116
        np.testing.assert_allclose(lat, [[58.2258, 59.3186, 60.4040],
117
                                         [61.4820, 62.5528, 63.6164]],
118
                                   rtol=1e-4)
119
        np.testing.assert_allclose(lon, [[81.1685, 81.6140, 82.0872],
120
                                         [82.5909, 83.1286, 83.7039]],
121
                                   rtol=1e-4)
122
123
    def test_convert_badidea_failure(self):
124
        """Test conversion failure for BADIDEA"""
125
        with pytest.raises(ValueError):
126
            lat, lon = aacgmv2.convert([60], [0], [3000], self.dtime,
127
                                       badidea=True)
128
129
    def test_convert_location_failure(self):
130
        """Test conversion with a bad location"""
131
        lat, lon = aacgmv2.convert([0], [0], [0], self.dtime)
132
        assert isinstance(lat, np.ndarray)
133
        assert isinstance(lon, np.ndarray)
134
        assert lat.shape == lon.shape and lat.shape == (1,)
135
        assert np.all([np.isnan(lat), np.isnan(lon)])
136
137
    def test_convert_time_failure(self):
138
        """Test conversion with a bad time"""
139
        with pytest.raises(AssertionError):
140
            lat, lon = aacgmv2.convert([60], [0], [300], None)
141
142
    def test_convert_datetime_date(self):
143
        """Test conversion with date and datetime input"""
144
        lat_1, lon_1 = aacgmv2.convert([60], [0], [300], self.ddate)
145
        lat_2, lon_2 = aacgmv2.convert([60], [0], [300], self.dtime)
146
        assert lat_1 == lat_2
147
        assert lon_1 == lon_2
148
149
    def test_warning_below_ground_convert(self):
150
        """ Test that a warning is issued if altitude is below zero"""
151
        import logbook
152
        lwarn = u"conversion not intended for altitudes < 0 km"
153
154
        with logbook.TestHandler() as handler:
155
            lat, lon = aacgmv2.convert([60], [0], [-1], self.dtime)
156
            assert handler.has_warning(lwarn)
157
158
        handler.close()
159
160
    def test_convert_maxalt_failure(self):
161
        """For an array, test failure for an altitude too high for
162
        coefficients"""
163
        lat, lon = aacgmv2.convert([60], [0], [2001], self.dtime)
164
        assert np.all([np.isnan(lat), np.isnan(lon)])
165
166
    def test_convert_lat_failure(self):
167
        """Test error return for co-latitudes above 90 for an array"""
168
        with pytest.raises(AssertionError):
169
            aacgmv2.convert([91, 60, -91], 0, 300, self.dtime)
170
171
    def test_subsol(self):
172
        """Test the subsolar calculation"""
173
        doy = int(self.dtime.strftime("%j"))
174
        ut = self.dtime.hour * 3600.0 + self.dtime.minute * 60.0 + \
175
             self.dtime.second
176
        slon, slat = aacgmv2.subsol(self.dtime.year, doy, ut)
177
178
        np.testing.assert_almost_equal(slon, -179.2004, decimal=4)
179
        np.testing.assert_almost_equal(slat, -23.0431, decimal=4)
180
181
    def test_gc2gd_lat(self):
182
        """Test the geocentric to geodetic conversion"""
183
        gd_lat = aacgmv2.depricated.gc2gd_lat(45.0)
184
185
        np.testing.assert_almost_equal(gd_lat, 45.1924, decimal=4)
186
187
    def test_gc2gd_lat_list(self):
188
        """Test the geocentric to geodetic conversion"""
189
        gc_lat = [45.0, -45.0]
190
        gd_lat = aacgmv2.depricated.gc2gd_lat(gc_lat)
191
192
        np.testing.assert_allclose(gd_lat, [45.1924, -45.1924], rtol=1.0e-4)
193
194
    def test_gc2gd_lat_arr(self):
195
        """Test the geocentric to geodetic conversion"""
196
        gc_lat = np.array([45.0, -45.0])
197
        gd_lat = aacgmv2.depricated.gc2gd_lat(gc_lat)
198
199
        np.testing.assert_allclose(gd_lat, [45.1924, -45.1924], rtol=1.0e-4)
200
201
    def test_igrf_dipole_axis(self):
202
        """Test the IGRF dipole axis calculation"""
203
        m = aacgmv2.depricated.igrf_dipole_axis(self.dtime)
204
205
        np.testing.assert_allclose(m, [0.050253, -0.160608, 0.985738],
206
                                   rtol=1.0e-4)
207