Advertisement
Guest User

Unusual Unittest behavior

a guest
Oct 28th, 2014
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.45 KB | None | 0 0
  1. #!/opt/local/bin/python
  2.  
  3. import copy
  4. import unittest
  5. import types
  6. import exceptions
  7.  
  8.  
  9. KIM1_RECORD_BEGIN = ";"
  10. RECORD_CHECKSUM_LENGTH = 4    # of characters
  11. RECORD_IDENTIFIER_LENGTH = 1  # of characters
  12. RECORD_BYTE_LENGTH = 2        # of characters
  13. RECORD_ADDRESS_LENGTH = 4     # of characters
  14. RECORD_DATA_LENGTH = 48       # of characters
  15.  
  16. ACTUAL_CHECKSUM_OFFSET = -4
  17.  
  18. EXPECTED_RECORD_LENGTH = RECORD_IDENTIFIER_LENGTH + RECORD_BYTE_LENGTH + RECORD_ADDRESS_LENGTH + RECORD_DATA_LENGTH + RECORD_CHECKSUM_LENGTH
  19.  
  20.  
  21. class RecordIntegrityError(exceptions.Exception):
  22.  
  23.     def __init__(self, args):
  24.         exceptions.Exception.__init__(self)
  25.         print type(args)
  26.         self.args=args
  27.  
  28.     def __str__(self):
  29.         return "".join(self.args)
  30.  
  31. class ChecksumValidationError(exceptions.Exception):
  32.  
  33.     def __init__(self, args):
  34.         exceptions.Exception.__init__(self)
  35.         self.args=args
  36.  
  37.     def __str__(self):
  38.         return "".join(self.args)
  39.  
  40. def checksum(record):
  41.  
  42.     if not record.startswith(KIM1_RECORD_BEGIN):
  43.         raise RecordIntegrityError("\nInvalid Record: does not begin with -> %s" % KIM1_RECORD_BEGIN)
  44.  
  45.     if type(record) is not types.StringType:
  46.         raise RecordIntegrityError("\nRecord data type must be StringType")
  47.  
  48.     actualRecordLength = len(record)
  49.     if actualRecordLength != EXPECTED_RECORD_LENGTH:
  50.         raise RecordIntegrityError("\nExpected record length: %lu bytes, Actual Record Length: %lu bytes," % (EXPECTED_RECORD_LENGTH, actualRecordLength))
  51.  
  52.     rawRecord = copy.deepcopy(record)
  53.     expectedChecksum = int(rawRecord[ACTUAL_CHECKSUM_OFFSET:],16) & 0xFFFF
  54.     workingRecord = iter(rawRecord[1:ACTUAL_CHECKSUM_OFFSET])
  55.  
  56.     calculatedChecksum = 0
  57.  
  58.     for byte in workingRecord:
  59.  
  60.         temp = []
  61.         temp.append(byte)
  62.         temp.append(workingRecord.next())
  63.  
  64.         calculatedChecksum += int("".join(temp),16) & 0xFFFF
  65.  
  66.     return expectedChecksum, calculatedChecksum
  67.  
  68.  
  69. class ChecksumValidaterPUT(unittest.TestCase):
  70.  
  71.     def setUp(self):
  72.  
  73.         self.record1 = ";180000FFEEDDCCBBAA0099887766554433221122334455667788990AFC"
  74.         self.record2 = ";180218778E4017203C02A20C8E4217A2318E4017203C02A20E8E420717"
  75.         self.record3 = ";180230EA65ED65EE85E9A204B5E995EACA10F938E999B0E8A5EB290FE8"
  76.         #record4 = ";1800000204A900A885FA8570A2028672A50085FBA601A57049FF850B2D"
  77.         #record5 = ";1800187191FAC8D0FBE6FBE4FBB0F5A672A50085FBA570CA1004A20FF6"
  78.         #record6 = ";1800300291FAC8D0F6E6FBA501C5FBB0ECA50085FBA672A571CA100F73"
  79.         #record7 = ";18004804A202A570D1FAD015C8D0F0E6FBA501C5FBB0E8C67210AD0F29"
  80.         #record8 = ";180060A57049FF30A184FA4C4F1C0000000000CE1ECE1ECE9E7E5E09FB"
  81.  
  82.         self.testRecords = (self.record1, self.record2, self.record3)
  83.  
  84.     def test_checksumvalidater(self):
  85.  
  86.         for recnum, record in enumerate(self.testRecords,start=1):
  87.  
  88.             print"\nRecord # %2lu, %s" % (recnum,record),
  89.             expectedChecksum, calculatedChecksum = checksum(record)
  90.             self.assertEqual(expectedChecksum,calculatedChecksum)
  91.             recnum +=1
  92.  
  93.  
  94. if __name__ == '__main__':
  95.  
  96.     unittest.main()
  97.  
  98.  
  99.  
  100.  
  101. Record #  1, ;180000FFEEDDCCBBAA0099887766554433221122334455667788990AFC
  102. Record #  2, ;180218778E4017203C02A20C8E4217A2318E4017203C02A20E8E420717
  103. .
  104. ----------------------------------------------------------------------
  105. Ran 1 test in 0.000s
  106.  
  107. OK
  108. Record #  3, ;180230EA65ED65EE85E9A204B5E995EACA10F938E999B0E8A5EB290FE8
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement