From 49c8bce039fe066c0b7b46b65a654d1a263feed1 Mon Sep 17 00:00:00 2001 From: Jeffrey Phillips Freeman <jeffrey.freeman@syncleus.com> Date: Sun, 25 Sep 2016 04:11:17 -0400 Subject: [PATCH] Added back many of the previously commented out tests, some of them have been changed significantly however. --- src/apex/aprs/aprs.py | 2 +- src/apex/kiss/kiss_serial.py | 2 +- tests/test_aprs.py | 180 +++++++++++++++++++++++++-------- tests/test_kiss_util.py | 2 +- tests/test_util.py | 189 ++++------------------------------- 5 files changed, 162 insertions(+), 213 deletions(-) diff --git a/src/apex/aprs/aprs.py b/src/apex/aprs/aprs.py index 1c02f03..976f267 100644 --- a/src/apex/aprs/aprs.py +++ b/src/apex/aprs/aprs.py @@ -125,7 +125,7 @@ class Aprs(object): for p in frame['path']: enc_frame += Aprs.__encode_callsign(Aprs.__parse_identity_string(p)) - return enc_frame[:-1] + [enc_frame[-1] | 0x01] + [apex.kiss.constants.SLOT_TIME] + [0xf0] + frame['text'] + return enc_frame[:-1] + [enc_frame[-1] | 0x01] + [apex.kiss.constants.SLOT_TIME] + [0xf0] + [ord(c) for c in frame['text']] @staticmethod def __encode_callsign(callsign): diff --git a/src/apex/kiss/kiss_serial.py b/src/apex/kiss/kiss_serial.py index 2f5b5b4..de4894a 100644 --- a/src/apex/kiss/kiss_serial.py +++ b/src/apex/kiss/kiss_serial.py @@ -70,7 +70,7 @@ class KissSerial(Kiss): waiting_data = self.serial.inWaiting() if waiting_data: read_data += self.serial.read(waiting_data) - return map(ord, read_data) + return [ord(c) for c in read_data] def _write_interface(self, data): self.serial.write(data) diff --git a/tests/test_aprs.py b/tests/test_aprs.py index aa8cf7f..9bb99d1 100644 --- a/tests/test_aprs.py +++ b/tests/test_aprs.py @@ -16,6 +16,7 @@ import unittest import apex.aprs.aprs_internet_service import apex.aprs.constants +from .constants import * if sys.version_info < (3, 0): import httpretty @@ -32,54 +33,153 @@ NUMBERS = '0123456789' POSITIVE_NUMBERS = NUMBERS[1:] ALPHANUM = ''.join([ALPHABET, NUMBERS]) +DECODED_CALLSIGN = callsign = {'callsign': 'W2GMD', 'ssid': 1} +ENCODED_CALLSIGN = [174, 100, 142, 154, 136, 64, 98] + +DECODED_CALLSIGN_DIGIPEATED = {'callsign': 'W2GMD*', 'ssid': 1} +ENCODED_CALLSIGN_DIGIPEATED = [174, 100, 142, 154, 136, 64, 226] + +DECODED_FRAME = { + 'source': 'W2GMD-1', + 'destination': 'OMG', + 'path': ['WIDE1-1'], + 'text': 'test_encode_frame' + } +ENCODED_FRAME = [158, 154, 142, 64, 64, 64, 96, 174, 100, 142, 154, 136, 64, 98, 174, 146, 136, 138, 98, 64, 99, 3, 240, + 116, 101, 115, 116, 95, 101, 110, 99, 111, 100, 101, 95, 102, 114, 97, 109, 101] + +DECODED_FRAME_RECORDED = { + 'source': 'W2GMD-6', + 'destination': 'APRX24', + 'path': ['WIDE1-1'], + 'text': ('!3745.75NI12228.05W#W2GMD-6 Inner Sunset, ' + 'SF iGate/Digipeater http://w2gmd.org') + } +ENCODED_FRAME_RECORDED = [130, 160, 164, 176, 100, 104, 96, 174, 100, 142, 154, 136, 64, 108, 174, 146, 136, 138, 98, + 64, 99, 3, 240, 33, 51, 55, 52, 53, 46, 55, 53, 78, 73, 49, 50, 50, 50, 56, 46, 48, 53, 87, + 35, 87, 50, 71, 77, 68, 45, 54, 32, 73, 110, 110, 101, 114, 32, 83, 117, 110, 115, 101, 116, + 44, 32, 83, 70, 32, 105, 71, 97, 116, 101, 47, 68, 105, 103, 105, 112, 101, 97, 116, 101, 114, + 32, 104, 116, 116, 112, 58, 47, 47, 119, 50, 103, 109, 100, 46, 111, 114, 103] + +DECODED_FRAME_MULTIPATH = { + 'source': 'W2GMD-1', + 'destination': 'OMG', + 'path': ['WIDE1-1', 'WIDE2-2'], + 'text': 'test_encode_frame' + } +ENCODED_FRAME_MULTIPATH = [158, 154, 142, 64, 64, 64, 96, 174, 100, 142, 154, 136, 64, 98, 174, 146, 136, 138, 98, 64, + 98, 174, 146, 136, 138, 100, 64, 101, 3, 240, 116, 101, 115, 116, 95, 101, 110, 99, 111, 100, + 101, 95, 102, 114, 97, 109, 101] + class AprsTest(unittest.TestCase): # pylint: disable=R0904 """Tests for Python APRS-IS Bindings.""" - logger = logging.getLogger(__name__) - logger.setLevel(apex.aprs.constants.LOG_LEVEL) - console_handler = logging.StreamHandler() - console_handler.setLevel(apex.aprs.constants.LOG_LEVEL) - formatter = logging.Formatter(apex.aprs.constants.LOG_FORMAT) - console_handler.setFormatter(formatter) - logger.addHandler(console_handler) - logger.propagate = False + def setUp(self): # pylint: disable=C0103 + self.fake_server = 'http://localhost:5567/' + self.fake_callsign = 'KWN4YGH-5' - @classmethod - def random(cls, length=8, alphabet=ALPHANUM): + def test_encode_callsign(self): """ - Generates a random string for test cases. + Tests encoding a callsign. + """ + encoded_callsign = apex.aprs.Aprs._Aprs__encode_callsign(DECODED_CALLSIGN) + self.assertEqual(ENCODED_CALLSIGN, encoded_callsign) - :param length: Length of string to generate. - :param alphabet: Alphabet to use to create string. - :type length: int - :type alphabet: str + def test_encode_callsign_digipeated(self): + """ + Tests encoding a digipeated callsign with + `aprs.util.encode_callsign()`. """ - return ''.join(random.choice(alphabet) for _ in xrange(length)) + encoded_callsign = apex.aprs.Aprs._Aprs__encode_callsign(DECODED_CALLSIGN_DIGIPEATED) + self.assertEqual(ENCODED_CALLSIGN_DIGIPEATED, encoded_callsign) - def setUp(self): # pylint: disable=C0103 - self.fake_server = ''.join([ - 'http://localhost:', - self.random(4, POSITIVE_NUMBERS), - '/' - ]) - - self.fake_callsign = ''.join([ - self.random(1, 'KWN'), - self.random(1, NUMBERS), - self.random(3, ALPHABET), - '-', - self.random(1, POSITIVE_NUMBERS) - ]) - - self.real_server = 'http://localhost:14580' - self.real_callsign = '-'.join(['W2GMD', self.random(1, '123456789')]) - - self.logger.debug( - 'fake_server=%s fake_callsign=%s', - self.fake_server, - self.fake_callsign - ) + def test_decode_callsign(self): + """ + Tests extracting the callsign from a KISS-encoded APRS frame. + """ + decoded_callsign = apex.aprs.Aprs._Aprs__extract_callsign(ENCODED_CALLSIGN) + self.assertEqual(DECODED_CALLSIGN, decoded_callsign) + + def test_decode_callsign_digipeated(self): + """ + Tests extracting the callsign from a KISS-encoded APRS frame. + """ + decoded_callsign = apex.aprs.Aprs._Aprs__extract_callsign(ENCODED_CALLSIGN_DIGIPEATED) + self.assertEqual(DECODED_CALLSIGN, decoded_callsign) + + def test_encode_frame(self): + """ + Tests KISS-encoding an APRS. + """ + encoded_frame = apex.aprs.Aprs._Aprs__encode_frame(DECODED_FRAME) + self.assertEqual(ENCODED_FRAME, encoded_frame) + + def test_encode_frame_recorded(self): + """ + Tests encoding a KISS-encoded APRS. + """ + encoded_frame = apex.aprs.Aprs._Aprs__encode_frame(DECODED_FRAME_RECORDED) + self.assertEqual(ENCODED_FRAME_RECORDED, encoded_frame) + + def test_encode_frame_multipath(self): + """ + Tests encoding a KISS-encoded APRS. + """ + encoded_frame = apex.aprs.Aprs._Aprs__encode_frame(DECODED_FRAME_MULTIPATH) + self.assertEqual(ENCODED_FRAME_MULTIPATH, encoded_frame) + + def test_decode_frame(self): + """ + Tests KISS-encoding an APRS + """ + decoded_frame = apex.aprs.Aprs._Aprs__decode_frame(ENCODED_FRAME) + self.assertEqual(DECODED_FRAME, decoded_frame) + + def test_decode_frame_recorded(self): + """ + Tests decoding a KISS-encoded APRS frame + """ + decoded_frame = apex.aprs.Aprs._Aprs__decode_frame(ENCODED_FRAME_RECORDED) + self.assertEqual(DECODED_FRAME_RECORDED, decoded_frame) + + def test_decode_frame_multipath(self): + """ + Tests decoding a KISS-encoded APRS frame + """ + decoded_frame = apex.aprs.Aprs._Aprs__decode_frame(ENCODED_FRAME_MULTIPATH) + self.assertEqual(DECODED_FRAME_MULTIPATH, decoded_frame) + + def test_extract_path(self): + """ + Tests extracting the APRS path from a KISS-encoded. + """ + extracted_path = apex.aprs.Aprs._Aprs__extract_path(3, ENCODED_FRAME) + self.assertEqual(DECODED_FRAME['path'][0], extracted_path[0]) + + def test_idwentity_as_string_with_ssid(self): + """ + Tests creating a full callsign string from a callsign+ssid dict using + `aprs.util.full_callsign()`. + """ + callsign = { + 'callsign': 'W2GMD', + 'ssid': 1 + } + full_callsign = apex.aprs.Aprs._Aprs__identity_as_string(callsign) + self.assertEqual(full_callsign, 'W2GMD-1') + + def test_identity_as_string_sans_ssid(self): + """ + Tests creating a full callsign string from a callsign dict using + `aprs.util.full_callsign()`. + """ + callsign = { + 'callsign': 'W2GMD', + 'ssid': 0 + } + full_callsign = apex.aprs.Aprs._Aprs__identity_as_string(callsign) + self.assertEqual(full_callsign, 'W2GMD') if sys.version_info < (3, 0): @httpretty.httprettified @@ -105,7 +205,6 @@ class AprsTest(unittest.TestCase): # pylint: disable=R0904 'path': ['TCPIP*'], 'text': '=3745.00N/12227.00W-Simulated Location' } - self.logger.debug(locals()) result = aprs_conn.send(msg) @@ -134,7 +233,6 @@ class AprsTest(unittest.TestCase): # pylint: disable=R0904 'path': ['TCPIP*'], 'text': '=3745.00N/12227.00W-Simulated Location' } - self.logger.debug(locals()) result = aprs_conn.send(msg, protocol='HTTP') diff --git a/tests/test_kiss_util.py b/tests/test_kiss_util.py index 3d584a4..0cc6de1 100644 --- a/tests/test_kiss_util.py +++ b/tests/test_kiss_util.py @@ -9,6 +9,7 @@ from __future__ import division from __future__ import print_function import unittest +# import apex.kiss.util from . import constants @@ -35,7 +36,6 @@ class KissUtilTestCase(unittest.TestCase): self.test_frames.close() # # All other tests only work on python2 - # # if sys.version_info < (3, 0): # def test_extract_ui(self): # """ # Tests `kiss.util.extract_ui` util function. diff --git a/tests/test_util.py b/tests/test_util.py index 06b7905..1a3bad9 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -13,7 +13,7 @@ import logging.handlers import unittest import apex.aprs.util -from apex.aprs import constants as aprsConstants +from apex.aprs import constants as aprs_constants __author__ = 'Jeffrey Phillips Freeman (WI2ARD)' __maintainer__ = 'Jeffrey Phillips Freeman (WI2ARD)' @@ -36,10 +36,10 @@ class AprsUtilTestCase(unittest.TestCase): # pylint: disable=R0904 """Tests for Python APRS Utils.""" logger = logging.getLogger(__name__) - logger.setLevel(aprsConstants.LOG_LEVEL) + logger.setLevel(aprs_constants.LOG_LEVEL) console_handler = logging.StreamHandler() - console_handler.setLevel(aprsConstants.LOG_LEVEL) - formatter = logging.Formatter(aprsConstants.LOG_FORMAT) + console_handler.setLevel(aprs_constants.LOG_LEVEL) + formatter = logging.Formatter(aprs_constants.LOG_FORMAT) console_handler.setFormatter(formatter) logger.addHandler(console_handler) logger.propagate = False @@ -207,171 +207,22 @@ class AprsUtilTestCase(unittest.TestCase): # pylint: disable=R0904 frame ) - # All other tests only work on python2 - # if sys.version_info < (3, 0): - # def setUp(self): # pylint: disable=C0103 - # """Setup.""" - # self.test_frames = open(constants.TEST_FRAMES, 'r') - # self.test_frame = self.test_frames.readlines()[0].strip() - # - # def tearDown(self): # pylint: disable=C0103 - # """Teardown.""" - # self.test_frames.close() - # - # def test_format_aprs_frame(self): - # """ - # Tests formatting an APRS frame-as-string from an APRS frame-as-dict - # using `aprs.util.format_aprs_frame()`. - # """ - # frame = { - # 'source': 'W2GMD-1', - # 'destination': 'OMG', - # 'path': 'WIDE1-1', - # 'text': 'test_format_aprs_frame' - # } - # formatted_frame = apex.aprs.util.format_aprs_frame(frame) - # self.assertEqual( - # formatted_frame, - # 'W2GMD-1>OMG,WIDE1-1:test_format_aprs_frame' - # ) - - # def test_extract_callsign_source(self): - # """ - # Tests extracting the source callsign from a KISS-encoded APRS frame - # using `aprs.util.extract_callsign()`. - # """ - # callsign = {'callsign': 'W2GMD', 'ssid': 6} - # extracted_callsign = apex.aprs.util.extract_callsign(self.test_frame[7:]) - # self.assertEqual(callsign, extracted_callsign) - # - # def test_extract_callsign_dest(self): - # """ - # Tests extracting the destination callsign from a KISS-encoded APRS - # frame using `aprs.util.extract_callsign()`. - # """ - # extracted_callsign = apex.aprs.util.extract_callsign(self.test_frame) - # self.assertEqual(extracted_callsign['callsign'], 'APRX24') - # - # def test_full_callsign_with_ssid(self): - # """ - # Tests creating a full callsign string from a callsign+ssid dict using - # `aprs.util.full_callsign()`. - # """ - # callsign = { - # 'callsign': 'W2GMD', - # 'ssid': 1 - # } - # full_callsign = apex.aprs.util.full_callsign(callsign) - # self.assertEqual(full_callsign, 'W2GMD-1') - # - # def test_full_callsign_sans_ssid(self): - # """ - # Tests creating a full callsign string from a callsign dict using - # `aprs.util.full_callsign()`. - # """ - # callsign = { - # 'callsign': 'W2GMD', - # 'ssid': 0 - # } - # full_callsign = apex.aprs.util.full_callsign(callsign) - # self.assertEqual(full_callsign, 'W2GMD') - # - # def test_extract_path(self): - # """ - # Tests extracting the APRS path from a KISS-encoded frame - # using `aprs.util.extract_path()`. - # """ - # extracted_path = apex.aprs.util.extract_path(3, self.test_frame) - # self.assertEqual('WIDE1-1', extracted_path[0]) - # - # def test_format_path(self): - # """ - # Tests formatting an APRS path from a KISS-encoded frame - # using `aprs.util.format_path()`. - # """ - # extracted_path = apex.aprs.util.format_path(3, self.test_frame) - # self.assertEqual('WIDE1-1', extracted_path) - # - # def test_encode_frame(self): - # """ - # Tests KISS-encoding an APRS frame using - # `aprs.util.encode_frame()`. - # """ - # frame = { - # 'source': 'W2GMD-1', - # 'destination': 'OMG', - # 'path': 'WIDE1-1', - # 'text': 'test_encode_frame' - # } - # encoded_frame = apex.aprs.util.encode_frame(frame) - # legit = ('\x9e\x9a\x8e@@@`\xaed\x8e\x9a\x88@b' - # '\xae\x92\x88\x8ab@c\x03\xf0test_encode_frame') - # self.assertEqual(legit, encoded_frame) - # - # def test_decode_frame_recorded(self): - # """ - # Tests decoding a KISS-encoded APRS frame using - # `aprs.util.decode_frame()`. - # """ - # frame = { - # 'path': 'WIDE1-1', - # 'destination': 'APRX24', - # 'source': 'W2GMD-6', - # 'text': ('!3745.75NI12228.05W#W2GMD-6 Inner Sunset, ' - # 'SF iGate/Digipeater http://w2gmd.org') - # } - # self.assertEqual(frame, apex.aprs.util.decode_frame(self.test_frame)) - # - # def test_decode_frame(self): - # """ - # Tests decoding a KISS-encoded APRS frame using - # `aprs.util.decode_frame()`. - # """ - # frame = { - # 'source': 'W2GMD-1', - # 'destination': 'OMG', - # 'path': 'WIDE1-1,WIDE2-2', - # 'text': 'test_encode_frame' - # } - # encoded_frame = apex.aprs.util.encode_frame(frame) - # decoded_frame = apex.aprs.util.decode_frame(encoded_frame) - # self.assertEqual(frame, decoded_frame) - # - # def test_create_callsign(self): - # """ - # Tests creating a callsign string from a callsign dict using - # `aprs.util.create_callsign()`. - # """ - # full_callsign = 'W2GMD-1' - # callsign = apex.aprs.util.create_callsign(full_callsign) - # self.assertEqual({'callsign': 'W2GMD', 'ssid': 1}, callsign) - # - # def test_full_callsign(self): - # """ - # Tests converting a callsign dict to a callsing string - # (callsign-ssid) using `aprs.util.full_callsign()`. - # """ - # callsign = {'callsign': 'W2GMD', 'ssid': 1} - # full_callsign = apex.aprs.util.full_callsign(callsign) - # self.assertEqual('W2GMD-1', full_callsign) - # - # def test_encode_callsign_digipeated(self): - # """ - # Tests encoding a digipeated callsign with - # `aprs.util.encode_callsign()`. - # """ - # callsign = {'callsign': 'W2GMD*', 'ssid': 1} - # encoded_callsign = apex.aprs.util.encode_callsign(callsign) - # self.assertEqual('\xaed\x8e\x9a\x88@\xe2', encoded_callsign) - # - # def test_encode_callsign(self): - # """ - # Tests encoding a non-digipeated callsign with - # `aprs.util.encode_callsign()`. - # """ - # callsign = {'callsign': 'W2GMD', 'ssid': 1} - # encoded_callsign = apex.aprs.util.encode_callsign(callsign) - # self.assertEqual('\xaed\x8e\x9a\x88@b', encoded_callsign) + def test_format_aprs_frame(self): + """ + Tests formatting an APRS frame-as-string from an APRS frame-as-dict + using `aprs.util.format_aprs_frame()`. + """ + frame = { + 'source': 'W2GMD-1', + 'destination': 'OMG', + 'path': ['WIDE1-1'], + 'text': 'test_format_aprs_frame' + } + formatted_frame = apex.aprs.util.format_aprs_frame(frame) + self.assertEqual( + formatted_frame, + 'W2GMD-1>OMG,WIDE1-1:test_format_aprs_frame' + ) if __name__ == '__main__': -- GitLab