Completed
Pull Request — master (#2)
by Jeffrey
04:10
created

APRSUtilTestCase.test_format_path()   A

Complexity

Conditions 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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