/
test_pp_core.py
296 lines (238 loc) · 8.36 KB
/
test_pp_core.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2010, 2012.
# SMHI,
# Folkborgsvägen 1,
# Norrköping,
# Sweden
# Author(s):
# Martin Raspaud <martin.raspaud@smhi.se>
# This file is part of mpop.
# mpop is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
# mpop is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along with
# mpop. If not, see <http://www.gnu.org/licenses/>.
"""Integration testing of
- :mod:`mpop.scene`
- :mod:`mpop.channel`
- :mod:`mpop.projector`
"""
import ConfigParser
import random
import unittest
import numpy as np
from pyresample import geometry, utils, kd_tree, image
import mpop.scene
class FakeAreaDefinition:
"""Fake AreaDefinition.
"""
def __init__(self, *args, **kwargs):
self.args = args
self.shape = None
self.area_id = args[0]
self.x_size = random.random()
self.y_size = random.random()
self.proj_id = random_string(20)
self.proj_dict = random_string(20)
self.area_extent = random_string(20)
class FakeSwathDefinition:
"""Fake SwathDefinition.
"""
def __init__(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs
self.shape = None
self.area_id = random_string(20)
class FakeImageContainer:
"""Fake ImageContainer
"""
def __init__(self, data, *args, **kwargs):
del args, kwargs
self.data = data
def get_array_from_linesample(self, *args):
"""Fake method.
"""
del args
return self.data + 1
def patch_geometry():
"""Patching the geometry module
"""
geometry.OldAreaDefinition = geometry.AreaDefinition
geometry.AreaDefinition = FakeAreaDefinition
geometry.OldSwathDefinition = geometry.SwathDefinition
geometry.SwathDefinition = FakeSwathDefinition
def unpatch_geometry():
"""Unpatching the geometry module.
"""
geometry.AreaDefinition = geometry.OldAreaDefinition
delattr(geometry, "OldAreaDefinition")
geometry.SwathDefinition = geometry.OldSwathDefinition
delattr(geometry, "OldSwathDefinition")
def patch_kd_tree():
"""Patching the kd_tree module.
"""
def fake_get_neighbour_info(*args, **kwargs):
"""Fake function.
"""
del args, kwargs
return (np.random.standard_normal((3, 1)),
np.random.standard_normal((3, 1)),
np.random.standard_normal((3, 1)),
np.random.standard_normal((3, 1)))
def fake_gsfni(typ, area, data, *args, **kwargs):
"""Fake function.
"""
del typ, area, args, kwargs
return data - 1
kd_tree.old_get_neighbour_info = kd_tree.get_neighbour_info
kd_tree.get_neighbour_info = fake_get_neighbour_info
kd_tree.old_gsfni = kd_tree.get_sample_from_neighbour_info
kd_tree.get_sample_from_neighbour_info = fake_gsfni
def unpatch_kd_tree():
"""Unpatching the kd_tree module.
"""
kd_tree.get_neighbour_info = kd_tree.old_get_neighbour_info
delattr(kd_tree, "old_get_neighbour_info")
kd_tree.get_sample_from_neighbour_info = kd_tree.old_gsfni
delattr(kd_tree, "old_gsfni")
def patch_utils():
"""Patching the utils module.
"""
def fake_parse_area_file(filename, area):
"""Fake function.
"""
del filename
if area == "raise" or not isinstance(area, str):
raise utils.AreaNotFound("This area is not to be found")
else:
return [geometry.AreaDefinition(area)]
def fake_gqla(*args):
"""Fake function.
"""
del args
return (np.random.standard_normal((3, 1)),
np.random.standard_normal((3, 1)))
utils.old_parse_area_file = utils.parse_area_file
utils.parse_area_file = fake_parse_area_file
utils.old_generate_quick_linesample_arrays = \
utils.generate_quick_linesample_arrays
utils.generate_quick_linesample_arrays = \
fake_gqla
def unpatch_utils():
"""Unpatching the utils module.
"""
utils.parse_area_file = utils.old_parse_area_file
delattr(utils, "old_parse_area_file")
utils.generate_quick_linesample_arrays = \
utils.old_generate_quick_linesample_arrays
delattr(utils, "old_generate_quick_linesample_arrays")
def patch_image():
"""Patching the pyresample.image module.
"""
image.OldImageContainer = image.ImageContainer
image.ImageContainer = FakeImageContainer
def unpatch_image():
"""Unpatching the pyresample.image module.
"""
image.ImageContainer = image.OldImageContainer
delattr(image, "OldImageContainer")
def patch_configparser():
"""Patch to fake ConfigParser.
"""
class FakeConfigParser:
"""Dummy ConfigParser class.
"""
def __init__(self, *args, **kwargs):
pass
def read(self, *args, **kwargs):
"""Dummy read method
"""
del args, kwargs
self = self
def get(self, *args, **kwargs):
"""Dummy get method
"""
del args, kwargs
self = self
return "test_plugin"
def sections(self):
"""Dummy sections method
"""
raise ConfigParser.NoSectionError("Dummy sections.")
ConfigParser.OldConfigParser = ConfigParser.ConfigParser
ConfigParser.ConfigParser = FakeConfigParser
def unpatch_configparser():
"""Unpatch fake ConfigParser.
"""
ConfigParser.ConfigParser = ConfigParser.OldConfigParser
delattr(ConfigParser, "OldConfigParser")
def random_string(length,
choices="abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"):
"""Generates a random string with elements from *set* of the specified
*length*.
"""
return "".join([random.choice(choices)
for dummy_itr in range(length)])
class TestPPCore(unittest.TestCase):
"""Class for testing the core of mpop.
"""
def setUp(self):
"""Apply patches.
"""
self.scene = mpop.scene.SatelliteInstrumentScene()
patch_geometry()
patch_utils()
patch_kd_tree()
patch_image()
patch_configparser()
def test_channel_list_syntax(self):
"""Test syntax for channel list
"""
channels = [["00_7", (0.5, 0.7, 0.9), 2500],
["06_4", (5.7, 6.4, 7.1), 5000],
["11_5", (10.5, 11.5, 12.5), 5000]]
class Satscene(mpop.scene.SatelliteInstrumentScene):
"""Adding a channel list.
"""
instrument_name = random_string(8)
channel_list = channels
self.scene = Satscene()
for i, chn in enumerate(self.scene.channels):
self.assertTrue(isinstance(chn, mpop.channel.Channel))
self.assertEquals(chn.name, channels[i][0])
self.assertEquals(chn.wavelength_range, list(channels[i][1]))
self.assertEquals(chn.resolution, channels[i][2])
def test_project(self):
"""Test project
"""
channels = [["00_7", (0.5, 0.7, 0.9), 2500],
["06_4", (5.7, 6.4, 7.1), 5000],
["11_5", (10.5, 11.5, 12.5), 5000]]
class Satscene(mpop.scene.SatelliteInstrumentScene):
"""Adding a channel list.
"""
instrument_name = random_string(8)
channel_list = channels
area = random_string(8)
self.scene = Satscene(area=area)
area2 = random_string(8)
new_scene = self.scene.project(area2)
self.assertEquals(new_scene.area.area_id, area2)
for chn in new_scene.channels:
self.assertEquals(chn.area, area2)
def tearDown(self):
"""Remove patches.
"""
unpatch_geometry()
unpatch_utils()
unpatch_kd_tree()
unpatch_image()
unpatch_configparser()
if __name__ == '__main__':
unittest.main()