Passed
Pull Request — master (#46)
by Angeline
03:47
created

TestDepAACGMV2.test_convert_unequal_arra()   A

Complexity

Conditions 2

Size

Total Lines 18
Code Lines 16

Duplication

Lines 18
Ratio 100 %

Importance

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