|
| 1 | +import unittest |
| 2 | +from pymysqlreplication.util import * |
| 3 | + |
| 4 | + |
| 5 | +class TestDecodeUint(unittest.TestCase): |
| 6 | + def test_valid_input(self): |
| 7 | + # Test with a known 2-byte input. |
| 8 | + data = bytearray([0x01, 0x00]) # Represents the unsigned integer 1 |
| 9 | + result = decode_uint(data) |
| 10 | + self.assertEqual(result, 1) |
| 11 | + |
| 12 | + def test_short_data(self): |
| 13 | + # Test with a 1-byte input, which is less than expected. |
| 14 | + data = bytearray([0x01]) |
| 15 | + result = decode_uint(data) |
| 16 | + self.assertEqual(result, 0) |
| 17 | + |
| 18 | + def test_empty_data(self): |
| 19 | + # Test with an empty input. |
| 20 | + data = bytearray([]) |
| 21 | + result = decode_uint(data) |
| 22 | + self.assertEqual(result, 0) |
| 23 | + |
| 24 | + |
| 25 | +class TestParseUint16(unittest.TestCase): |
| 26 | + def test_valid_input(self): |
| 27 | + data = bytearray([0x01, 0x00]) # Represents the unsigned integer 1 |
| 28 | + result = parse_uint16(data) |
| 29 | + self.assertEqual(result, 1) |
| 30 | + |
| 31 | + def test_different_input(self): |
| 32 | + data = bytearray([0xFF, 0x00]) # Represents the unsigned integer 255 |
| 33 | + result = parse_uint16(data) |
| 34 | + self.assertEqual(result, 255) |
| 35 | + |
| 36 | + |
| 37 | +class TestLengthEncodedInt(unittest.TestCase): |
| 38 | + def test_single_byte(self): |
| 39 | + data = bytearray([0x05]) |
| 40 | + result, _, _ = length_encoded_int(data) |
| 41 | + self.assertEqual(result, 5) |
| 42 | + |
| 43 | + def test_two_bytes(self): |
| 44 | + data = bytearray([0xFC, 0x12, 0x34]) |
| 45 | + result, _, _ = length_encoded_int(data) |
| 46 | + self.assertEqual(result, 0x3412) |
| 47 | + |
| 48 | + def test_three_bytes(self): |
| 49 | + data = bytearray([0xFD, 0x01, 0x02, 0x03]) |
| 50 | + result, _, _ = length_encoded_int(data) |
| 51 | + self.assertEqual(result, 0x030201) |
| 52 | + |
| 53 | + |
| 54 | +class TestDecodeTime(unittest.TestCase): |
| 55 | + def test_valid_time(self): |
| 56 | + # Assuming parse_int64 returns an integer representing time |
| 57 | + # Here we use a mocked function for parse_int64 |
| 58 | + data = bytearray(8) # Mocked data, replace with actual test data |
| 59 | + with unittest.mock.patch( |
| 60 | + "pymysqlreplication.util.parse_int64", return_value=12345678 |
| 61 | + ): |
| 62 | + result = decode_time(data) |
| 63 | + self.assertIsInstance(result, datetime.time) |
| 64 | + |
| 65 | + |
| 66 | +# Running the tests |
| 67 | +if __name__ == "__main__": |
| 68 | + unittest.main() |
0 commit comments