Completed
Push — master ( 6f1544...a8928e )
by Jeffrey
05:27
created

APRSUtilTestCase.test_longitude_east()   B

Complexity

Conditions 1

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
c 2
b 0
f 0
dl 0
loc 32
rs 8.8571
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
4
"""Tests for Python APRS util methods."""
5
6
# These imports are for python3 compatability inside python2
7
from __future__ import absolute_import
8
from __future__ import division
9
from __future__ import print_function
10
from __future__ import unicode_literals
11
12
import logging
13
import logging.handlers
14
import unittest
15
16
import apex.aprs.util
17
from apex.aprs import constants as aprsConstants
18
19
__author__ = 'Jeffrey Phillips Freeman (WI2ARD)'
20
__maintainer__ = "Jeffrey Phillips Freeman (WI2ARD)"
21
__email__ = "[email protected]"
22
__license__ = 'Apache License, Version 2.0'
23
__copyright__ = 'Copyright 2016, Syncleus, Inc. and contributors'
24
__credits__ = []
25
26
ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
27
NUMBERS = '0123456789'
28
POSITIVE_NUMBERS = NUMBERS[1:]
29
ALPHANUM = ''.join([ALPHABET, NUMBERS])
30
31
VALID_CALLSIGNS = ['W2GMD', 'W2GMD-1', 'KF4MKT', 'KF4MKT-1', 'KF4LZA-15']
32
INVALID_CALLSIGNS = ['xW2GMDx', 'W2GMD-16', 'W2GMD-A', 'W', 'W2GMD-1-0',
33
                     'W*GMD', 'W2GMD-123']
34
35
36
class APRSUtilTestCase(unittest.TestCase):  # pylint: disable=R0904
37
    """Tests for Python APRS Utils."""
38
39
    logger = logging.getLogger(__name__)
40
    logger.setLevel(aprsConstants.LOG_LEVEL)
41
    console_handler = logging.StreamHandler()
42 View Code Duplication
    console_handler.setLevel(aprsConstants.LOG_LEVEL)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
43
    formatter = logging.Formatter(aprsConstants.LOG_FORMAT)
44
    console_handler.setFormatter(formatter)
45
    logger.addHandler(console_handler)
46
    logger.propagate = False
47
48
    def test_latitude_north(self):
49
        """Test Decimal to APRS Latitude conversion.
50
51
        Spec per ftp://ftp.tapr.org/aprssig/aprsspec/spec/aprs101/APRS101.pdf
52
        --
53
        Latitude is expressed as a fixed 8-character field, in degrees and
54
        decimal minutes (to two decimal places), followed by the letter N for
55
        north or S for south. Latitude degrees are in the range 00 to 90.
56
        Latitude minutes are expressed as whole minutes and hundredths of a
57
        minute, separated by a decimal point.
58
59
        For example:
60
61
            4903.50N is 49 degrees 3 minutes 30 seconds north.
62
63
        In generic format examples, the latitude is shown as the 8-character
64
        string ddmm.hhN (i.e. degrees, minutes and hundredths of a minute
65
        north).
66
        """
67
        test_lat = 37.7418096
68
        aprs_lat = apex.aprs.util.dec2dm_lat(test_lat)
69
        self.logger.debug('aprs_lat=%s', aprs_lat)
70
71
        lat_deg = int(aprs_lat.split('.')[0][:1])
72
        # lat_hsec = aprs_lat.split('.')[1]
73
74
        self.assertTrue(len(aprs_lat) == 8)
75
        self.assertTrue(lat_deg >= 00)
76
        self.assertTrue(lat_deg <= 90)
77
        self.assertTrue(aprs_lat.endswith('N'))
78
79
    def test_latitude_south(self):
80
        """Test Decimal to APRS Latitude conversion.
81
82
        Spec per ftp://ftp.tapr.org/aprssig/aprsspec/spec/aprs101/APRS101.pdf
83
        --
84
        Latitude is expressed as a fixed 8-character field, in degrees and
85
        decimal minutes (to two decimal places), followed by the letter N for
86
        north or S for south. Latitude degrees are in the range 00 to 90.
87
        Latitude minutes are expressed as whole minutes and hundredths of a
88
        minute, separated by a decimal point.
89
90
        For example:
91
92
            4903.50N is 49 degrees 3 minutes 30 seconds north.
93
94
        In generic format examples, the latitude is shown as the 8-character
95
        string ddmm.hhN (i.e. degrees, minutes and hundredths of a minute
96
        north).
97
        """
98
        test_lat = -37.7418096
99
        aprs_lat = apex.aprs.util.dec2dm_lat(test_lat)
100
        self.logger.debug('aprs_lat=%s', aprs_lat)
101
102
        lat_deg = int(aprs_lat.split('.')[0][:1])
103
        # lat_hsec = aprs_lat.split('.')[1]
104 View Code Duplication
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
105
        self.assertTrue(len(aprs_lat) == 8)
106
        self.assertTrue(lat_deg >= 00)
107
        self.assertTrue(lat_deg <= 90)
108
        self.assertTrue(aprs_lat.endswith('S'))
109
110
    def test_longitude_west(self):
111
        """Test Decimal to APRS Longitude conversion.
112
113
        Spec per ftp://ftp.tapr.org/aprssig/aprsspec/spec/aprs101/APRS101.pdf
114
        --
115
        Longitude is expressed as a fixed 9-character field, in degrees and
116
        decimal minutes (to two decimal places), followed by the letter E for
117
        east or W for west.
118
119
        Longitude degrees are in the range 000 to 180. Longitude minutes are
120
        expressed as whole minutes and hundredths of a minute, separated by a
121
        decimal point.
122
123
        For example:
124
125
            07201.75W is 72 degrees 1 minute 45 seconds west.
126
127
        In generic format examples, the longitude is shown as the 9-character
128
        string dddmm.hhW (i.e. degrees, minutes and hundredths of a minute
129
        west).
130
        """
131
        test_lng = -122.38833
132
        aprs_lng = apex.aprs.util.dec2dm_lng(test_lng)
133
        self.logger.debug('aprs_lng=%s', aprs_lng)
134
135
        lng_deg = int(aprs_lng.split('.')[0][:2])
136
        # lng_hsec = aprs_lng.split('.')[1]
137
138
        self.assertTrue(len(aprs_lng) == 9)
139
        self.assertTrue(lng_deg >= 000)
140
        self.assertTrue(lng_deg <= 180)
141
        self.assertTrue(aprs_lng.endswith('W'))
142
143
    def test_longitude_east(self):
144
        """Test Decimal to APRS Longitude conversion.
145
146
        Spec per ftp://ftp.tapr.org/aprssig/aprsspec/spec/aprs101/APRS101.pdf
147
        --
148
        Longitude is expressed as a fixed 9-character field, in degrees and
149
        decimal minutes (to two decimal places), followed by the letter E for
150
        east or W for west.
151
152
        Longitude degrees are in the range 000 to 180. Longitude minutes are
153
        expressed as whole minutes and hundredths of a minute, separated by a
154
        decimal point.
155
156
        For example:
157
158
            07201.75W is 72 degrees 1 minute 45 seconds west.
159
160
        In generic format examples, the longitude is shown as the 9-character
161
        string dddmm.hhW (i.e. degrees, minutes and hundredths of a minute
162
        west).
163
        """
164
        test_lng = 122.38833
165
        aprs_lng = apex.aprs.util.dec2dm_lng(test_lng)
166
        self.logger.debug('aprs_lng=%s', aprs_lng)
167
168
        lng_deg = int(aprs_lng.split('.')[0][:2])
169
        # lng_hsec = aprs_lng.split('.')[1]
170
171
        self.assertTrue(len(aprs_lng) == 9)
172
        self.assertTrue(lng_deg >= 000)
173
        self.assertTrue(lng_deg <= 180)
174
        self.assertTrue(aprs_lng.endswith('E'))
175
176
    def test_valid_callsign_valid(self):
177
        """
178
        Tests valid callsigns using `aprs.util.valid_callsign()`.
179
        """
180
        for i in VALID_CALLSIGNS:
181
            self.assertTrue(
182
                apex.aprs.util.valid_callsign(i), "%s is a valid call" % i)
183
184
    def test_valid_callsign_invalid(self):
185
        """
186
        Tests invalid callsigns using `aprs.util.valid_callsign()`.
187
        """
188
        for i in INVALID_CALLSIGNS:
189
            self.assertFalse(
190
                apex.aprs.util.valid_callsign(i), "%s is an invalid call" % i)
191
192
    def test_decode_aprs_ascii_frame(self):
193
        """
194
        Tests creating an APRS frame-as-dict from an APRS frame-as-string
195
        using `aprs.util.decode_aprs_ascii_frame()`.
196
        """
197
        ascii_frame = (
198
            'W2GMD-9>APOTC1,WIDE1-1,WIDE2-1:!3745.94N/12228.05W>118/010/'
199
            'A=000269 38C=Temp http://w2gmd.org/ Twitter: @ampledata')
200
        frame = apex.aprs.util.decode_aprs_ascii_frame(ascii_frame)
201
        self.assertEqual(
202
            {
203
                'source': 'W2GMD-9',
204
                'destination': 'APOTC1',
205
                'path': 'APOTC1,WIDE1-1,WIDE2-1',
206
                'text': ('!3745.94N/12228.05W>118/010/A=000269 38C=Temp '
207
                         'http://w2gmd.org/ Twitter: @ampledata'),
208
            },
209
            frame
210
        )
211
212
    # All other tests only work on python2
213
    # if sys.version_info < (3, 0):
214
    #     def setUp(self):  # pylint: disable=C0103
215
    #         """Setup."""
216
    #         self.test_frames = open(constants.TEST_FRAMES, 'r')
217
    #         self.test_frame = self.test_frames.readlines()[0].strip()
218
    #
219
    #     def tearDown(self):  # pylint: disable=C0103
220
    #         """Teardown."""
221
    #         self.test_frames.close()
222
    #
223
    #     def test_format_aprs_frame(self):
224
    #         """
225
    #         Tests formatting an APRS frame-as-string from an APRS frame-as-dict
226
    #         using `aprs.util.format_aprs_frame()`.
227
    #         """
228
    #         frame = {
229
    #             'source': 'W2GMD-1',
230
    #             'destination': 'OMG',
231
    #             'path': 'WIDE1-1',
232
    #             'text': 'test_format_aprs_frame'
233
    #         }
234
    #         formatted_frame = apex.aprs.util.format_aprs_frame(frame)
235
    #         self.assertEqual(
236
    #             formatted_frame,
237
    #             'W2GMD-1>OMG,WIDE1-1:test_format_aprs_frame'
238
    #         )
239
240
        # def test_extract_callsign_source(self):
241
        #     """
242
        #     Tests extracting the source callsign from a KISS-encoded APRS frame
243
        #     using `aprs.util.extract_callsign()`.
244
        #     """
245
        #     callsign = {'callsign': 'W2GMD', 'ssid': 6}
246
        #     extracted_callsign = apex.aprs.util.extract_callsign(self.test_frame[7:])
247
        #     self.assertEqual(callsign, extracted_callsign)
248
        #
249
        # def test_extract_callsign_dest(self):
250
        #     """
251
        #     Tests extracting the destination callsign from a KISS-encoded APRS
252
        #     frame using `aprs.util.extract_callsign()`.
253
        #     """
254
        #     extracted_callsign = apex.aprs.util.extract_callsign(self.test_frame)
255
        #     self.assertEqual(extracted_callsign['callsign'], 'APRX24')
256
        #
257
        # def test_full_callsign_with_ssid(self):
258
        #     """
259
        #     Tests creating a full callsign string from a callsign+ssid dict using
260
        #     `aprs.util.full_callsign()`.
261
        #     """
262
        #     callsign = {
263
        #         'callsign': 'W2GMD',
264
        #         'ssid': 1
265
        #     }
266
        #     full_callsign = apex.aprs.util.full_callsign(callsign)
267
        #     self.assertEqual(full_callsign, 'W2GMD-1')
268
        #
269
        # def test_full_callsign_sans_ssid(self):
270
        #     """
271
        #     Tests creating a full callsign string from a callsign dict using
272
        #     `aprs.util.full_callsign()`.
273
        #     """
274
        #     callsign = {
275
        #         'callsign': 'W2GMD',
276
        #         'ssid': 0
277
        #     }
278
        #     full_callsign = apex.aprs.util.full_callsign(callsign)
279
        #     self.assertEqual(full_callsign, 'W2GMD')
280
        #
281
        # def test_extract_path(self):
282
        #     """
283
        #     Tests extracting the APRS path from a KISS-encoded frame
284
        #     using `aprs.util.extract_path()`.
285
        #     """
286
        #     extracted_path = apex.aprs.util.extract_path(3, self.test_frame)
287
        #     self.assertEqual('WIDE1-1', extracted_path[0])
288
        #
289
        # def test_format_path(self):
290
        #     """
291
        #     Tests formatting an APRS path from a KISS-encoded frame
292
        #     using `aprs.util.format_path()`.
293
        #     """
294
        #     extracted_path = apex.aprs.util.format_path(3, self.test_frame)
295
        #     self.assertEqual('WIDE1-1', extracted_path)
296
        #
297
        # def test_encode_frame(self):
298
        #     """
299
        #     Tests KISS-encoding an APRS frame using
300
        #     `aprs.util.encode_frame()`.
301
        #     """
302
        #     frame = {
303
        #         'source': 'W2GMD-1',
304
        #         'destination': 'OMG',
305
        #         'path': 'WIDE1-1',
306
        #         'text': 'test_encode_frame'
307
        #     }
308
        #     encoded_frame = apex.aprs.util.encode_frame(frame)
309
        #     legit = ('\x9e\x9a\x8e@@@`\xaed\x8e\x9a\x88@b'
310
        #              '\xae\x92\x88\x8ab@c\x03\xf0test_encode_frame')
311
        #     self.assertEqual(legit, encoded_frame)
312
        #
313
        # def test_decode_frame_recorded(self):
314
        #     """
315
        #     Tests decoding a KISS-encoded APRS frame using
316
        #     `aprs.util.decode_frame()`.
317
        #     """
318
        #     frame = {
319
        #         'path': 'WIDE1-1',
320
        #         'destination': 'APRX24',
321
        #         'source': 'W2GMD-6',
322
        #         'text': ('!3745.75NI12228.05W#W2GMD-6 Inner Sunset, '
323
        #                  'SF iGate/Digipeater http://w2gmd.org')
324
        #     }
325
        #     self.assertEqual(frame, apex.aprs.util.decode_frame(self.test_frame))
326
        #
327
        # def test_decode_frame(self):
328
        #     """
329
        #     Tests decoding a KISS-encoded APRS frame using
330
        #     `aprs.util.decode_frame()`.
331
        #     """
332
        #     frame = {
333
        #         'source': 'W2GMD-1',
334
        #         'destination': 'OMG',
335
        #         'path': 'WIDE1-1,WIDE2-2',
336
        #         'text': 'test_encode_frame'
337
        #     }
338
        #     encoded_frame = apex.aprs.util.encode_frame(frame)
339
        #     decoded_frame = apex.aprs.util.decode_frame(encoded_frame)
340
        #     self.assertEqual(frame, decoded_frame)
341
        #
342
        # def test_create_callsign(self):
343
        #     """
344
        #     Tests creating a callsign string from a callsign dict using
345
        #     `aprs.util.create_callsign()`.
346
        #     """
347
        #     full_callsign = 'W2GMD-1'
348
        #     callsign = apex.aprs.util.create_callsign(full_callsign)
349
        #     self.assertEqual({'callsign': 'W2GMD', 'ssid': 1}, callsign)
350
        #
351
        # def test_full_callsign(self):
352
        #     """
353
        #     Tests converting a callsign dict to a callsing string
354
        #     (callsign-ssid) using `aprs.util.full_callsign()`.
355
        #     """
356
        #     callsign = {'callsign': 'W2GMD', 'ssid': 1}
357
        #     full_callsign = apex.aprs.util.full_callsign(callsign)
358
        #     self.assertEqual('W2GMD-1', full_callsign)
359
        #
360
        # def test_encode_callsign_digipeated(self):
361
        #     """
362
        #     Tests encoding a digipeated callsign with
363
        #     `aprs.util.encode_callsign()`.
364
        #     """
365
        #     callsign = {'callsign': 'W2GMD*', 'ssid': 1}
366
        #     encoded_callsign = apex.aprs.util.encode_callsign(callsign)
367
        #     self.assertEqual('\xaed\x8e\x9a\x88@\xe2', encoded_callsign)
368
        #
369
        # def test_encode_callsign(self):
370
        #     """
371
        #     Tests encoding a non-digipeated callsign with
372
        #     `aprs.util.encode_callsign()`.
373
        #     """
374
        #     callsign = {'callsign': 'W2GMD', 'ssid': 1}
375
        #     encoded_callsign = apex.aprs.util.encode_callsign(callsign)
376
        #     self.assertEqual('\xaed\x8e\x9a\x88@b', encoded_callsign)
377
378
379
if __name__ == '__main__':
380
    unittest.main()
381