Completed
Branch master (4f6d0d)
by Jeffrey
05:41 queued 01:29
created

APRSUtilTestCase.test_latitude_south()   D

Complexity

Conditions 1

Size

Total Lines 30

Duplication

Lines 1
Ratio 3.33 %

Importance

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