aprs-python/tests/test_parse_header.py

114 lines
3.0 KiB
Python

import unittest
import string
from random import randint, randrange, sample
from aprs.parse import _parse_header
from aprs.parse import _validate_callsign
from aprs.exceptions import ParseError
class ValidateCallsign(unittest.TestCase):
def test_valid_input(self):
chars = string.letters.upper() + string.digits
def random_valid_callsigns():
for x in xrange(0, 500):
call = "".join(sample(chars, randrange(1, 6)))
if bool(randint(0, 1)):
call += "-%d" % randint(0, 15)
yield call
for call in random_valid_callsigns():
try:
_validate_callsign(call)
except ParseError:
self.fail(
"%s('%s') raised ParseError" %
(_validate_callsign.__name__, call)
)
def test_invalid_input(self):
testData = [
"",
"-",
"-1",
"---1",
"1234567",
"CALL-",
"CALL-16",
"CALLCALL",
]
for call in testData:
self.assertRaises(ParseError, _validate_callsign, call)
class ParseHeader(unittest.TestCase):
def test_valid_input_and_format(self):
# empty path header
expected = {
"from": "A",
"to": "B",
"via": "",
"path": []
}
result = _parse_header("A>B")
self.assertEqual(expected, result)
# with path
expected2 = {
"from": "A",
"to": "B",
"via": "",
"path": list('CDE')
}
result2 = _parse_header("A>B,C,D,E")
self.assertEqual(expected2, result2)
# test all currently valid q-cosntructs
expected3 = {
"from": "A",
"to": "B",
"via": "E",
"path": ['C', 'D', 'qAR', 'E']
}
result3 = _parse_header("A>B,C,D,qAR,E")
self.assertEqual(expected3, result3)
for qCon in map(lambda x: "qA"+x, list("CXUoOSrRRZI")):
expected4 = {
"from": "A",
"to": "B",
"via": "C",
"path": [qCon, 'C']
}
result4 = _parse_header("A>B,%s,C" % qCon)
self.assertEqual(expected4, result4)
def test_invalid_format(self):
testData = [
"", # empty header
">", # empty fromcall
"A>", # empty tocall
"A>b", # invalid tocall
"aaaaaaaaaaa", # invalid fromcall
"A>aaaaaaaaaaa", # invalid tocall
"A>B,1234567890,C", # invalid call in path
"A>B,C,1234567890,D", # invalid call in path
"A>B,C,1234567890", # invalid call in path
]
for head in testData:
self.assertRaises(ParseError, _parse_header, head)
if __name__ == '__main__':
unittest.main()