Total Complexity | 56 |
Total Lines | 333 |
Duplicated Lines | 22.22 % |
Changes | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 | import warnings |
||
9 | import logging |
||
10 | |||
11 | class TestFutureDepWarning: |
||
12 | def setup(self): |
||
13 | # Initialize the routine to be tested |
||
14 | self.test_routine = None |
||
15 | self.test_args = [] |
||
16 | self.test_kwargs = {} |
||
17 | |||
18 | def teardown(self): |
||
19 | del self.test_routine, self.test_args, self.test_kwargs |
||
20 | |||
21 | def test_future_dep_warning(self): |
||
22 | """Test the implementation of FutureWarning for deprecated routines""" |
||
23 | if self.test_routine is None: |
||
24 | assert True |
||
25 | else: |
||
26 | with warnings.catch_warnings(record=True) as w: |
||
27 | # Cause all warnings to always be triggered. |
||
28 | warnings.simplefilter("always") |
||
29 | |||
30 | # Trigger a warning. |
||
31 | self.test_routine(*self.test_args, **self.test_kwargs) |
||
32 | |||
33 | # Verify some things |
||
34 | assert len(w) == 1 |
||
35 | assert issubclass(w[-1].category, FutureWarning) |
||
36 | assert "Deprecated routine" in str(w[-1].message) |
||
37 | |||
38 | |||
39 | class TestDepAACGMV2Warning(TestFutureDepWarning): |
||
40 | def setup(self): |
||
41 | self.dtime = dt.datetime(2015, 1, 1, 0, 0, 0) |
||
42 | self.test_routine = None |
||
43 | self.test_args = [] |
||
44 | self.test_kwargs = {} |
||
45 | |||
46 | def teardown(self): |
||
47 | del self.dtime, self.test_routine, self.test_args, self.test_kwargs |
||
48 | |||
49 | def test_convert_warning(self): |
||
50 | """Test future deprecation warning for convert""" |
||
51 | |||
52 | self.test_routine = aacgmv2.deprecated.convert |
||
53 | self.test_args = [60, 0, 300, self.dtime] |
||
54 | self.test_future_dep_warning() |
||
55 | |||
56 | def test_subsol_warning(self): |
||
57 | """Test future deprecation warning for subsol""" |
||
58 | |||
59 | self.test_routine = aacgmv2.deprecated.subsol |
||
60 | self.test_args = [self.dtime.year, int(self.dtime.strftime("%j")), |
||
61 | self.dtime.second + self.dtime.minute * 60 + |
||
62 | self.dtime.hour * 3600] |
||
63 | self.test_future_dep_warning() |
||
64 | |||
65 | def test_gc2gd_lat_warning(self): |
||
66 | """Test future deprecation warning for gc2gd_lat""" |
||
67 | |||
68 | self.test_routine = aacgmv2.deprecated.gc2gd_lat |
||
69 | self.test_args = [60.0] |
||
70 | self.test_future_dep_warning() |
||
71 | |||
72 | def test_igrf_dipole_axis_warning(self): |
||
73 | """Test future deprecation warning for igrf_dipole_axis""" |
||
74 | |||
75 | self.test_routine = aacgmv2.deprecated.igrf_dipole_axis |
||
76 | self.test_args = [self.dtime] |
||
77 | self.test_future_dep_warning() |
||
78 | |||
79 | |||
80 | class TestDepLogging: |
||
81 | def setup(self): |
||
82 | """Runs before every method to create a clean testing setup""" |
||
83 | from io import StringIO |
||
84 | |||
85 | self.log_capture = StringIO() |
||
86 | aacgmv2.logger.addHandler(logging.StreamHandler(self.log_capture)) |
||
87 | |||
88 | self.in_convert = ([60], [0], [-1], dt.datetime(2015, 1, 1, 0, 0, 0)) |
||
89 | self.lwarn = u"conversion not intended for altitudes < 0 km" |
||
90 | self.lout = '' |
||
91 | |||
92 | def teardown(self): |
||
93 | """Runs after every method to clean up previous testing""" |
||
94 | self.log_capture.close() |
||
95 | del self.in_convert, self.lwarn, self.lout |
||
96 | |||
97 | def test_warning_below_ground_convert(self): |
||
98 | """ Test that a warning is issued if altitude is below zero""" |
||
99 | |||
100 | aacgmv2.convert(*self.in_convert) |
||
101 | self.lout = self.log_capture.getvalue() |
||
102 | assert self.lout.find(self.lwarn) >= 0 |
||
103 | |||
104 | |||
105 | class TestDepAACGMV2: |
||
106 | def setup(self): |
||
107 | """Runs before every method to create a clean testing setup""" |
||
108 | self.dtime = dt.datetime(2015, 1, 1, 0, 0, 0) |
||
109 | self.ddate = dt.date(2015, 1, 1) |
||
110 | self.lat = None |
||
111 | self.lon = None |
||
112 | |||
113 | def teardown(self): |
||
114 | """Runs after every method to clean up previous testing""" |
||
115 | del self.dtime, self.ddate, self.lat, self.lon |
||
116 | |||
117 | def test_convert_single_val(self): |
||
118 | """Test conversion for a single value""" |
||
119 | with warnings.catch_warnings(): |
||
120 | warnings.simplefilter("ignore") |
||
121 | self.lat, self.lon = aacgmv2.convert(60, 0, 300, self.dtime) |
||
122 | |||
123 | assert isinstance(self.lat, np.ndarray) |
||
124 | assert isinstance(self.lon, np.ndarray) |
||
125 | assert self.lat.shape == self.lon.shape and self.lat.shape == (1,) |
||
126 | np.testing.assert_allclose(self.lat, [58.2258], rtol=1e-4) |
||
127 | np.testing.assert_allclose(self.lon, [81.1685], rtol=1e-4) |
||
128 | |||
129 | def test_convert_list(self): |
||
130 | """Test conversion for list input""" |
||
131 | with warnings.catch_warnings(): |
||
132 | warnings.simplefilter("ignore") |
||
133 | self.lat, self.lon = aacgmv2.convert([60], [0], [300], self.dtime) |
||
134 | |||
135 | assert isinstance(self.lat, np.ndarray) |
||
136 | assert isinstance(self.lon, np.ndarray) |
||
137 | assert self.lat.shape == self.lon.shape and self.lat.shape == (1,) |
||
138 | np.testing.assert_allclose(self.lat, [58.2258], rtol=1e-4) |
||
139 | np.testing.assert_allclose(self.lon, [81.1685], rtol=1e-4) |
||
140 | |||
141 | with warnings.catch_warnings(): |
||
142 | warnings.simplefilter("ignore") |
||
143 | self.lat, self.lon = aacgmv2.convert([60, 61], [0, 0], [300, 300], |
||
144 | self.dtime) |
||
145 | |||
146 | assert isinstance(self.lat, np.ndarray) |
||
147 | assert isinstance(self.lon, np.ndarray) |
||
148 | assert self.lat.shape == self.lon.shape and self.lat.shape == (2,) |
||
149 | np.testing.assert_allclose(self.lat, [58.2258, 59.3186], rtol=1e-4) |
||
150 | np.testing.assert_allclose(self.lon, [81.1685, 81.6140], rtol=1e-4) |
||
151 | |||
152 | View Code Duplication | def test_convert_arr_single(self): |
|
|
|||
153 | """Test conversion for array input with one element""" |
||
154 | with warnings.catch_warnings(): |
||
155 | warnings.simplefilter("ignore") |
||
156 | self.lat, self.lon = aacgmv2.convert(np.array([60]), np.array([0]), |
||
157 | np.array([300]), self.dtime) |
||
158 | |||
159 | assert isinstance(self.lat, np.ndarray) |
||
160 | assert isinstance(self.lon, np.ndarray) |
||
161 | assert self.lat.shape == self.lon.shape and self.lat.shape == (1,) |
||
162 | np.testing.assert_allclose(self.lat, [58.2258], rtol=1e-4) |
||
163 | np.testing.assert_allclose(self.lon, [81.1685], rtol=1e-4) |
||
164 | |||
165 | View Code Duplication | def test_convert_arr(self): |
|
166 | """Test conversion for array input""" |
||
167 | |||
168 | with warnings.catch_warnings(): |
||
169 | warnings.simplefilter("ignore") |
||
170 | self.lat, self.lon = aacgmv2.convert(np.array([60, 61]), |
||
171 | np.array([0, 0]), |
||
172 | np.array([300, 300]), |
||
173 | self.dtime) |
||
174 | |||
175 | assert isinstance(self.lat, np.ndarray) |
||
176 | assert isinstance(self.lon, np.ndarray) |
||
177 | assert self.lat.shape == self.lon.shape and self.lat.shape == (2,) |
||
178 | np.testing.assert_allclose(self.lat, [58.2258, 59.3186], rtol=1e-4) |
||
179 | np.testing.assert_allclose(self.lon, [81.1685, 81.6140], rtol=1e-4) |
||
180 | |||
181 | def test_convert_list_mix(self): |
||
182 | """Test conversion for a list and floats""" |
||
183 | |||
184 | with warnings.catch_warnings(): |
||
185 | warnings.simplefilter("ignore") |
||
186 | self.lat, self.lon = aacgmv2.convert([60, 61], 0, 300, self.dtime) |
||
187 | |||
188 | assert isinstance(self.lat, np.ndarray) |
||
189 | assert isinstance(self.lon, np.ndarray) |
||
190 | assert self.lat.shape == self.lon.shape and self.lat.shape == (2,) |
||
191 | np.testing.assert_allclose(self.lat, [58.2258, 59.3186], rtol=1e-4) |
||
192 | np.testing.assert_allclose(self.lon, [81.1685, 81.6140], rtol=1e-4) |
||
193 | |||
194 | View Code Duplication | def test_convert_arr_mix(self): |
|
195 | """Test conversion for an array and floats""" |
||
196 | with warnings.catch_warnings(): |
||
197 | warnings.simplefilter("ignore") |
||
198 | self.lat, self.lon = aacgmv2.convert(np.array([60, 61]), 0, 300, |
||
199 | self.dtime) |
||
200 | |||
201 | assert isinstance(self.lat, np.ndarray) |
||
202 | assert isinstance(self.lon, np.ndarray) |
||
203 | assert self.lat.shape == self.lon.shape and self.lat.shape == (2,) |
||
204 | np.testing.assert_allclose(self.lat, [58.2258, 59.3186], rtol=1e-4) |
||
205 | np.testing.assert_allclose(self.lon, [81.1685, 81.6140], rtol=1e-4) |
||
206 | |||
207 | View Code Duplication | def test_convert_mult_array_mix(self): |
|
208 | """Test conversion for a multi-dim array and floats""" |
||
209 | with warnings.catch_warnings(): |
||
210 | warnings.simplefilter("ignore") |
||
211 | self.lat, self.lon = aacgmv2.convert(np.array([[60, 61, 62], |
||
212 | [63, 64, 65]]), 0, |
||
213 | 300, self.dtime) |
||
214 | |||
215 | assert isinstance(self.lat, np.ndarray) |
||
216 | assert isinstance(self.lon, np.ndarray) |
||
217 | assert self.lat.shape == self.lon.shape and self.lat.shape == (2, 3) |
||
218 | np.testing.assert_allclose(self.lat, [[58.2258, 59.3186, 60.4040], |
||
219 | [61.4820, 62.5528, 63.6164]], |
||
220 | rtol=1e-4) |
||
221 | np.testing.assert_allclose(self.lon, [[81.1685, 81.6140, 82.0872], |
||
222 | [82.5909, 83.1286, 83.7039]], |
||
223 | rtol=1e-4) |
||
224 | |||
225 | View Code Duplication | def test_convert_unequal_arra(self): |
|
226 | """Test conversion for unequal sized arrays""" |
||
227 | with warnings.catch_warnings(): |
||
228 | warnings.simplefilter("ignore") |
||
229 | self.lat, self.lon = aacgmv2.convert(np.array([[60, 61, 62], |
||
230 | [63, 64, 65]]), |
||
231 | np.array([0]), np.array([300]), |
||
232 | self.dtime) |
||
233 | |||
234 | assert isinstance(self.lat, np.ndarray) |
||
235 | assert isinstance(self.lon, np.ndarray) |
||
236 | assert self.lat.shape == self.lon.shape and self.lat.shape == (2, 3) |
||
237 | np.testing.assert_allclose(self.lat, [[58.2258, 59.3186, 60.4040], |
||
238 | [61.4820, 62.5528, 63.6164]], |
||
239 | rtol=1e-4) |
||
240 | np.testing.assert_allclose(self.lon, [[81.1685, 81.6140, 82.0872], |
||
241 | [82.5909, 83.1286, 83.7039]], |
||
242 | rtol=1e-4) |
||
243 | |||
244 | def test_convert_location_failure(self): |
||
245 | """Test conversion with a bad location""" |
||
246 | with warnings.catch_warnings(): |
||
247 | warnings.simplefilter("ignore") |
||
248 | self.lat, self.lon = aacgmv2.convert([0], [0], [0], self.dtime) |
||
249 | |||
250 | assert isinstance(self.lat, np.ndarray) |
||
251 | assert isinstance(self.lon, np.ndarray) |
||
252 | assert self.lat.shape == self.lon.shape and self.lat.shape == (1,) |
||
253 | assert np.all([np.isnan(self.lat), np.isnan(self.lon)]) |
||
254 | |||
255 | def test_convert_time_failure(self): |
||
256 | """Test conversion with a bad time""" |
||
257 | with pytest.raises(ValueError): |
||
258 | with warnings.catch_warnings(): |
||
259 | warnings.simplefilter("ignore") |
||
260 | self.lat, self.lon = aacgmv2.convert([60], [0], [300], None) |
||
261 | |||
262 | def test_convert_datetime_date(self): |
||
263 | """Test conversion with date and datetime input""" |
||
264 | with warnings.catch_warnings(): |
||
265 | warnings.simplefilter("ignore") |
||
266 | self.lat, self.lon = aacgmv2.convert([60], [0], [300], self.ddate) |
||
267 | |||
268 | np.testing.assert_allclose(self.lat, [58.2258], rtol=1e-4) |
||
269 | np.testing.assert_allclose(self.lon, [81.1685], rtol=1e-4) |
||
270 | |||
271 | def test_convert_maxalt_failure(self): |
||
272 | """For an array, test failure for an altitude too high for |
||
273 | coefficients""" |
||
274 | with pytest.raises(ValueError): |
||
275 | with warnings.catch_warnings(): |
||
276 | warnings.simplefilter("ignore") |
||
277 | aacgmv2.convert([60], [0], [2001], self.dtime) |
||
278 | |||
279 | def test_convert_lat_failure(self): |
||
280 | """Test error return for co-latitudes above 90 for an array""" |
||
281 | with pytest.raises(ValueError): |
||
282 | with warnings.catch_warnings(): |
||
283 | warnings.simplefilter("ignore") |
||
284 | aacgmv2.convert([91, 60, -91], 0, 300, self.dtime) |
||
285 | |||
286 | def test_subsol(self): |
||
287 | """Test the subsolar calculation""" |
||
288 | doy = int(self.dtime.strftime("%j")) |
||
289 | ut = self.dtime.hour * 3600.0 + self.dtime.minute * 60.0 + \ |
||
290 | self.dtime.second |
||
291 | with warnings.catch_warnings(): |
||
292 | warnings.simplefilter("ignore") |
||
293 | self.lon, self.lat = aacgmv2.deprecated.subsol(self.dtime.year, |
||
294 | doy, ut) |
||
295 | |||
296 | np.testing.assert_almost_equal(self.lon, -179.2004, decimal=4) |
||
297 | np.testing.assert_almost_equal(self.lat, -23.0431, decimal=4) |
||
298 | |||
299 | def test_gc2gd_lat(self): |
||
300 | """Test the geocentric to geodetic conversion""" |
||
301 | with warnings.catch_warnings(): |
||
302 | warnings.simplefilter("ignore") |
||
303 | self.lat = aacgmv2.deprecated.gc2gd_lat(45.0) |
||
304 | |||
305 | np.testing.assert_almost_equal(self.lat, 45.1924, decimal=4) |
||
306 | |||
307 | def test_gc2gd_lat_list(self): |
||
308 | """Test the geocentric to geodetic conversion""" |
||
309 | self.lat = [45.0, -45.0] |
||
310 | with warnings.catch_warnings(): |
||
311 | warnings.simplefilter("ignore") |
||
312 | self.lat = aacgmv2.deprecated.gc2gd_lat(self.lat) |
||
313 | |||
314 | np.testing.assert_allclose(self.lat, [45.1924, -45.1924], rtol=1.0e-4) |
||
315 | |||
316 | def test_gc2gd_lat_arr(self): |
||
317 | """Test the geocentric to geodetic conversion""" |
||
318 | self.lat = np.array([45.0, -45.0]) |
||
319 | with warnings.catch_warnings(): |
||
320 | warnings.simplefilter("ignore") |
||
321 | self.lat = aacgmv2.deprecated.gc2gd_lat(self.lat) |
||
322 | |||
323 | np.testing.assert_allclose(self.lat, [45.1924, -45.1924], rtol=1.0e-4) |
||
324 | |||
325 | def test_igrf_dipole_axis(self): |
||
326 | """Test the IGRF dipole axis calculation""" |
||
327 | with warnings.catch_warnings(): |
||
328 | warnings.simplefilter("ignore") |
||
329 | m = aacgmv2.deprecated.igrf_dipole_axis(self.dtime) |
||
330 | |||
331 | np.testing.assert_allclose(m, [0.050253, -0.160608, 0.985738], |
||
332 | rtol=1.0e-4) |
||
333 |