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