Passed
Pull Request — develop (#36)
by Angeline
01:06
created

test_dep_aacgmv2.TestDepLogging.teardown()   A

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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