1 | #!/usr/bin/env python |
||
2 | # -*- coding: utf-8 -*- |
||
3 | |||
4 | """Tests for Python APRS util methods.""" |
||
5 | |||
6 | # These imports are for python3 compatibility inside python2 |
||
7 | from __future__ import absolute_import |
||
8 | from __future__ import division |
||
9 | from __future__ import print_function |
||
10 | |||
11 | import logging |
||
12 | import logging.handlers |
||
13 | import unittest |
||
14 | |||
15 | import apex.aprs.util |
||
16 | from apex.aprs import constants as aprs_constants |
||
17 | |||
18 | __author__ = 'Jeffrey Phillips Freeman (WI2ARD)' |
||
19 | __maintainer__ = 'Jeffrey Phillips Freeman (WI2ARD)' |
||
20 | __email__ = '[email protected]' |
||
21 | __license__ = 'Apache License, Version 2.0' |
||
22 | __copyright__ = 'Copyright 2016, Syncleus, Inc. and contributors' |
||
23 | __credits__ = [] |
||
24 | |||
25 | ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' |
||
26 | NUMBERS = '0123456789' |
||
27 | POSITIVE_NUMBERS = NUMBERS[1:] |
||
28 | ALPHANUM = ''.join([ALPHABET, NUMBERS]) |
||
29 | |||
30 | VALID_CALLSIGNS = ['W2GMD', 'W2GMD-1', 'KF4MKT', 'KF4MKT-1', 'KF4LZA-15'] |
||
31 | INVALID_CALLSIGNS = ['xW2GMDx', 'W2GMD-16', 'W2GMD-A', 'W', 'W2GMD-1-0', |
||
32 | 'W*GMD', 'W2GMD-123'] |
||
33 | |||
34 | |||
35 | class AprsUtilTestCase(unittest.TestCase): # pylint: disable=R0904 |
||
36 | """Tests for Python APRS Utils.""" |
||
37 | |||
38 | logger = logging.getLogger(__name__) |
||
39 | logger.setLevel(aprs_constants.LOG_LEVEL) |
||
40 | console_handler = logging.StreamHandler() |
||
41 | console_handler.setLevel(aprs_constants.LOG_LEVEL) |
||
42 | View Code Duplication | formatter = logging.Formatter(aprs_constants.LOG_FORMAT) |
|
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
43 | console_handler.setFormatter(formatter) |
||
44 | logger.addHandler(console_handler) |
||
45 | logger.propagate = False |
||
46 | |||
47 | def test_latitude_north(self): |
||
48 | """Test Decimal to APRS Latitude conversion. |
||
49 | |||
50 | Spec per ftp://ftp.tapr.org/aprssig/aprsspec/spec/aprs101/APRS101.pdf |
||
51 | -- |
||
52 | Latitude is expressed as a fixed 8-character field, in degrees and |
||
53 | decimal minutes (to two decimal places), followed by the letter N for |
||
54 | north or S for south. Latitude degrees are in the range 00 to 90. |
||
55 | Latitude minutes are expressed as whole minutes and hundredths of a |
||
56 | minute, separated by a decimal point. |
||
57 | |||
58 | For example: |
||
59 | |||
60 | 4903.50N is 49 degrees 3 minutes 30 seconds north. |
||
61 | |||
62 | In generic format examples, the latitude is shown as the 8-character |
||
63 | string ddmm.hhN (i.e. degrees, minutes and hundredths of a minute |
||
64 | north). |
||
65 | """ |
||
66 | test_lat = 37.7418096 |
||
67 | aprs_lat = apex.aprs.util.dec2dm_lat(test_lat) |
||
68 | self.logger.debug('aprs_lat=%s', aprs_lat) |
||
69 | |||
70 | lat_deg = int(aprs_lat.split('.')[0][:1]) |
||
71 | # lat_hsec = aprs_lat.split('.')[1] |
||
72 | |||
73 | self.assertTrue(len(aprs_lat) == 8) |
||
74 | self.assertTrue(lat_deg >= 00) |
||
75 | self.assertTrue(lat_deg <= 90) |
||
76 | self.assertTrue(aprs_lat.endswith('N')) |
||
77 | |||
78 | def test_latitude_south(self): |
||
79 | """Test Decimal to APRS Latitude conversion. |
||
80 | |||
81 | Spec per ftp://ftp.tapr.org/aprssig/aprsspec/spec/aprs101/APRS101.pdf |
||
82 | -- |
||
83 | Latitude is expressed as a fixed 8-character field, in degrees and |
||
84 | decimal minutes (to two decimal places), followed by the letter N for |
||
85 | north or S for south. Latitude degrees are in the range 00 to 90. |
||
86 | Latitude minutes are expressed as whole minutes and hundredths of a |
||
87 | minute, separated by a decimal point. |
||
88 | |||
89 | For example: |
||
90 | |||
91 | 4903.50N is 49 degrees 3 minutes 30 seconds north. |
||
92 | |||
93 | In generic format examples, the latitude is shown as the 8-character |
||
94 | string ddmm.hhN (i.e. degrees, minutes and hundredths of a minute |
||
95 | north). |
||
96 | """ |
||
97 | test_lat = -37.7418096 |
||
98 | aprs_lat = apex.aprs.util.dec2dm_lat(test_lat) |
||
99 | self.logger.debug('aprs_lat=%s', aprs_lat) |
||
100 | |||
101 | lat_deg = int(aprs_lat.split('.')[0][:1]) |
||
102 | # lat_hsec = aprs_lat.split('.')[1] |
||
103 | |||
104 | View Code Duplication | self.assertTrue(len(aprs_lat) == 8) |
|
0 ignored issues
–
show
|
|||
105 | self.assertTrue(lat_deg >= 00) |
||
106 | self.assertTrue(lat_deg <= 90) |
||
107 | self.assertTrue(aprs_lat.endswith('S')) |
||
108 | |||
109 | def test_longitude_west(self): |
||
110 | """Test Decimal to APRS Longitude conversion. |
||
111 | |||
112 | Spec per ftp://ftp.tapr.org/aprssig/aprsspec/spec/aprs101/APRS101.pdf |
||
113 | -- |
||
114 | Longitude is expressed as a fixed 9-character field, in degrees and |
||
115 | decimal minutes (to two decimal places), followed by the letter E for |
||
116 | east or W for west. |
||
117 | |||
118 | Longitude degrees are in the range 000 to 180. Longitude minutes are |
||
119 | expressed as whole minutes and hundredths of a minute, separated by a |
||
120 | decimal point. |
||
121 | |||
122 | For example: |
||
123 | |||
124 | 07201.75W is 72 degrees 1 minute 45 seconds west. |
||
125 | |||
126 | In generic format examples, the longitude is shown as the 9-character |
||
127 | string dddmm.hhW (i.e. degrees, minutes and hundredths of a minute |
||
128 | west). |
||
129 | """ |
||
130 | test_lng = -122.38833 |
||
131 | aprs_lng = apex.aprs.util.dec2dm_lng(test_lng) |
||
132 | self.logger.debug('aprs_lng=%s', aprs_lng) |
||
133 | |||
134 | lng_deg = int(aprs_lng.split('.')[0][:2]) |
||
135 | # lng_hsec = aprs_lng.split('.')[1] |
||
136 | |||
137 | self.assertTrue(len(aprs_lng) == 9) |
||
138 | self.assertTrue(lng_deg >= 000) |
||
139 | self.assertTrue(lng_deg <= 180) |
||
140 | self.assertTrue(aprs_lng.endswith('W')) |
||
141 | |||
142 | def test_longitude_east(self): |
||
143 | """Test Decimal to APRS Longitude conversion. |
||
144 | |||
145 | Spec per ftp://ftp.tapr.org/aprssig/aprsspec/spec/aprs101/APRS101.pdf |
||
146 | -- |
||
147 | Longitude is expressed as a fixed 9-character field, in degrees and |
||
148 | decimal minutes (to two decimal places), followed by the letter E for |
||
149 | east or W for west. |
||
150 | |||
151 | Longitude degrees are in the range 000 to 180. Longitude minutes are |
||
152 | expressed as whole minutes and hundredths of a minute, separated by a |
||
153 | decimal point. |
||
154 | |||
155 | For example: |
||
156 | |||
157 | 07201.75W is 72 degrees 1 minute 45 seconds west. |
||
158 | |||
159 | In generic format examples, the longitude is shown as the 9-character |
||
160 | string dddmm.hhW (i.e. degrees, minutes and hundredths of a minute |
||
161 | west). |
||
162 | """ |
||
163 | test_lng = 122.38833 |
||
164 | aprs_lng = apex.aprs.util.dec2dm_lng(test_lng) |
||
165 | self.logger.debug('aprs_lng=%s', aprs_lng) |
||
166 | |||
167 | lng_deg = int(aprs_lng.split('.')[0][:2]) |
||
168 | # lng_hsec = aprs_lng.split('.')[1] |
||
169 | |||
170 | self.assertTrue(len(aprs_lng) == 9) |
||
171 | self.assertTrue(lng_deg >= 000) |
||
172 | self.assertTrue(lng_deg <= 180) |
||
173 | self.assertTrue(aprs_lng.endswith('E')) |
||
174 | |||
175 | def test_valid_callsign_valid(self): |
||
176 | """ |
||
177 | Tests valid callsigns using `aprs.util.valid_callsign()`. |
||
178 | """ |
||
179 | for i in VALID_CALLSIGNS: |
||
180 | self.assertTrue(apex.aprs.util.valid_callsign(i), '%s is a valid call' % i) |
||
181 | |||
182 | def test_valid_callsign_invalid(self): |
||
183 | """ |
||
184 | Tests invalid callsigns using `aprs.util.valid_callsign()`. |
||
185 | """ |
||
186 | for i in INVALID_CALLSIGNS: |
||
187 | self.assertFalse( |
||
188 | apex.aprs.util.valid_callsign(i), '%s is an invalid call' % i) |
||
189 | |||
190 | def test_decode_aprs_ascii_frame(self): |
||
191 | """ |
||
192 | Tests creating an APRS frame-as-dict from an APRS frame-as-string |
||
193 | using `aprs.util.decode_aprs_ascii_frame()`. |
||
194 | """ |
||
195 | ascii_frame = ( |
||
196 | 'W2GMD-9>APOTC1,WIDE1-1,WIDE2-1:!3745.94N/12228.05W>118/010/' |
||
197 | 'A=000269 38C=Temp http://w2gmd.org/ Twitter: @ampledata') |
||
198 | frame = apex.aprs.util.decode_frame(ascii_frame) |
||
199 | self.assertEqual( |
||
200 | { |
||
201 | 'source': 'W2GMD-9', |
||
202 | 'destination': 'APOTC1', |
||
203 | 'path': 'APOTC1,WIDE1-1,WIDE2-1', |
||
204 | 'text': ('!3745.94N/12228.05W>118/010/A=000269 38C=Temp ' |
||
205 | 'http://w2gmd.org/ Twitter: @ampledata'), |
||
206 | }, |
||
207 | frame |
||
208 | ) |
||
209 | |||
210 | def test_format_aprs_frame(self): |
||
211 | """ |
||
212 | Tests formatting an APRS frame-as-string from an APRS frame-as-dict |
||
213 | using `aprs.util.format_aprs_frame()`. |
||
214 | """ |
||
215 | frame = { |
||
216 | 'source': 'W2GMD-1', |
||
217 | 'destination': 'OMG', |
||
218 | 'path': ['WIDE1-1'], |
||
219 | 'text': 'test_format_aprs_frame' |
||
220 | } |
||
221 | formatted_frame = apex.aprs.util.encode_frame(frame) |
||
222 | self.assertEqual( |
||
223 | formatted_frame, |
||
224 | 'W2GMD-1>OMG,WIDE1-1:test_format_aprs_frame' |
||
225 | ) |
||
226 | |||
227 | |||
228 | if __name__ == '__main__': |
||
229 | unittest.main() |
||
230 |