File: test_satellites.py

package info (click to toggle)
python-mpop 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 20,516 kB
  • ctags: 1,877
  • sloc: python: 15,374; xml: 820; makefile: 90; sh: 8
file content (236 lines) | stat: -rw-r--r-- 7,590 bytes parent folder | download
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2010, 2011.

# 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/>.

"""Unit tests for the module :mod:`pp.satellites`.
"""
import ConfigParser
import random
import unittest

import mpop.instruments.visir
import mpop.satellites

INSTRUMENTS = ()

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)])


def patch_configparser():
    """Patch to fake ConfigParser.
    """
    class FakeConfigParser:
        """Dummy ConfigParser class.
        """
        def __init__(self, *args, **kwargs):
            pass
        
        def sections(self, *args, **kwargs):
            """Dummy sections method.
            """
            self = self
            del args, kwargs
            sections = []
            for i in INSTRUMENTS:
                for j in range(int(random.random() * 10 + 1)):
                    sections += [i + str(j)]
            return sections
        
        def read(self, *args, **kwargs):
            """Dummy read method
            """
            del args, kwargs
            self = self

        def get(self, *args, **kwargs):
            """Dummy get method
            """
            del kwargs
            self = self
            if args[1] == "instruments":
                return str(INSTRUMENTS)
            if args[1] == "name":
                return "'" + random_string(3) + "'"
            if args[1] == "resolution":
                return str(int(random.random() * 50000 + 1))
            if args[1] == "frequency":
                return str(random.random())
            if args[1] == "module":
                return random_string(8)
            
    ConfigParser.OldConfigParser = ConfigParser.ConfigParser
    ConfigParser.ConfigParser = FakeConfigParser

def unpatch_configparser():
    """Unpatch fake ConfigParser.
    """
    ConfigParser.ConfigParser = ConfigParser.OldConfigParser
    delattr(ConfigParser, "OldConfigParser")

def patch_scene():
    """Patch the :mod:`mpop.instruments.visir` module to avoid using it in these
    tests.
    """
    class FakeChannel(object):
        """FakeChannel class.
        """
        def __init__(self, val):
            self.data = val

        def check_range(self, *args):
            """Dummy check_range function.
            """
            del args
            return self.data

    
    class FakeSatscene(object):
        """Fake SatelliteInstrumentScene.
        """
        __version__ = "fake"
        def __init__(self):
            self.channels = None
            self.area = None
            self.time_slot = None
            self.error = []
        
        def check_channels(self, *args):
            """Dummy check_channels function.
            """
            self.channels = args

        def __getitem__(self, key):
            if key in self.error:
                raise KeyError()
            return FakeChannel(key)
    mpop.instruments.visir.OldVisirCompositer = mpop.instruments.visir.VisirCompositer
    mpop.instruments.visir.VisirCompositer = FakeSatscene
    reload(mpop.satellites)
    

def unpatch_scene():
    """Unpatch the :mod:`mpop.scene` module.
    """
    mpop.instruments.visir.VisirCompositer = mpop.instruments.visir.OldVisirCompositer
    delattr(mpop.instruments.visir, "OldVisirCompositer")
    reload(mpop)
    reload(mpop.instruments)
    reload(mpop.instruments.visir)
    reload(mpop.satellites)


class TestSatellites(unittest.TestCase):
    """Test the satellites base functions.
    """

    def setUp(self):
        """Patch stuff.
        """
        patch_configparser()
        patch_scene()
        
    def test_buildinstrument(self):
        """Test the :func:`mpop.satellites.build_instrument` function.
        """
        name = random_string(10)
        #ch_list = [random_string(10), random_string(12)]
        inst = mpop.satellites.build_instrument_compositer(name)

        # Test that the patches are applied
        self.assertEquals(inst.__version__, "fake")

        
        #self.assertEquals(inst.channel_list, ch_list)
        self.assertEquals(inst.instrument_name, name)
        self.assertEquals(inst.mro()[1], mpop.instruments.visir.VisirCompositer)

    def test_build_satellite_class(self):
        """Test the :func:`mpop.satellites.build_satellite_class` function.
        """
        global INSTRUMENTS
        inst = random_string(10)
        INSTRUMENTS = (inst, )
        satname = random_string(10)
        satnumber = random_string(10)
        satvar = random_string(10)
        myclass = mpop.satellites.build_sat_instr_compositer((satname,
                                                              satnumber,
                                                              satvar),
                                                             inst)
        #self.assertEquals(myclass.satname, satname)
        #self.assertEquals(myclass.number, satnumber)
        #self.assertEquals(myclass.variant, satvar)
        self.assertEquals(myclass.mro()[1].__name__,
                          inst.capitalize() +
                          "Compositer")
            
    def test_get_satellite_class(self):
        """Test the :func:`mpop.satellites.get_satellite_class` function.
        """
        global INSTRUMENTS

        inst = random_string(10)
        INSTRUMENTS = ("avhrr", inst)
        satname = random_string(11)
        satnumber = random_string(10)
        satvar = random_string(10)
        klass = mpop.satellites.get_sat_instr_compositer((satname,
                                                          satnumber,
                                                          satvar),
                                                         inst)
        self.assertTrue(klass.mro()[0].__name__.startswith(
            satvar.capitalize() + satname.capitalize() +
            satnumber.capitalize()))


        INSTRUMENTS = (inst,)
        satname = random_string(11)
        satnumber = random_string(10)
        satvar = random_string(10)
        klass = mpop.satellites.get_sat_instr_compositer((satname,
                                                          satnumber,
                                                          satvar),
                                                         inst)
        pklass = klass.mro()[0]
        self.assertTrue(pklass.__name__.startswith(
            satvar.capitalize() + satname.capitalize() +
            satnumber.capitalize()))

    def tearDown(self):
        """Unpatch stuff.
        """
        unpatch_configparser()
        unpatch_scene()

if __name__ == '__main__':
    unittest.main()