11import unittest
2- from datetime import time
3-
42from pymysqlreplication .util import *
53
64
75class TestIsDataShort (unittest .TestCase ):
86 def test_data_is_shorter (self ):
9- # Test with data shorter than expected.
10- data = bytearray ([0x01 ]) # 1-byte data
7+ data = bytearray ([0x01 ])
118 expected_length = 2
129 self .assertTrue (is_data_short (data , expected_length ))
1310
1411 def test_data_is_equal_length (self ):
15- # Test with data equal to expected length.
16- data = bytearray ([0x01 , 0x00 ]) # 2-byte data
12+ data = bytearray ([0x01 , 0x00 ])
1713 expected_length = 2
1814 self .assertFalse (is_data_short (data , expected_length ))
1915
2016 def test_data_is_longer (self ):
21- # Test with data longer than expected.
22- data = bytearray ([0x01 , 0x00 , 0x02 ]) # 3-byte data
17+ data = bytearray ([0x01 , 0x00 , 0x02 ])
2318 expected_length = 2
2419 self .assertFalse (is_data_short (data , expected_length ))
2520
2621 def test_data_is_empty (self ):
27- # Test with empty data.
2822 data = bytearray ([])
2923 expected_length = 1
3024 self .assertTrue (is_data_short (data , expected_length ))
3125
3226
3327class TestDecodeCount (unittest .TestCase ):
3428 def test_small_format (self ):
35- # Test with 2-byte input and small format.
36- data = bytearray ([0x01 , 0x00 ]) # Represents the unsigned integer 1
29+ data = bytearray ([0x01 , 0x00 ])
3730 is_small = True
3831 result = decode_count (data , is_small )
3932 self .assertEqual (result , 1 )
4033
4134 def test_large_format (self ):
42- # Test with 4-byte input and large format.
43- data = bytearray ([0x01 , 0x00 , 0x00 , 0x00 ]) # Represents the unsigned integer 1
35+ data = bytearray ([0x01 , 0x00 , 0x00 , 0x00 ])
4436 is_small = False
4537 result = decode_count (data , is_small )
4638 self .assertEqual (result , 1 )
4739
4840
4941class TestDecodeUint (unittest .TestCase ):
5042 def test_valid_input (self ):
51- # Test with a known 2-byte input.
52- data = bytearray ([0x01 , 0x00 ]) # Represents the unsigned integer 1
43+ data = bytearray ([0x01 , 0x00 ])
5344 result = decode_uint (data )
5445 self .assertEqual (result , 1 )
5546
5647 def test_short_data (self ):
57- # Test with a 1-byte input, which is less than expected.
5848 data = bytearray ([0x01 ])
5949 result = decode_uint (data )
6050 self .assertEqual (result , 0 )
6151
6252 def test_empty_data (self ):
63- # Test with an empty input.
6453 data = bytearray ([])
6554 result = decode_uint (data )
6655 self .assertEqual (result , 0 )
6756
6857
6958class TestDecodeVariableLength (unittest .TestCase ):
7059 def test_single_byte (self ):
71- # Test with a single byte where the high bit is not set (indicating the end)
72- data = bytearray ([0x05 ]) # 5 with the high bit not set
60+ data = bytearray ([0x05 ])
7361 length , pos = decode_variable_length (data )
7462 self .assertEqual (length , 5 )
7563 self .assertEqual (pos , 1 )
7664
7765 def test_multiple_bytes (self ):
78- # Test with multiple bytes
79- # 0x81 -> 1 with the high bit set, indicating more bytes
80- # 0x01 -> 1 with the high bit not set, indicating the end
81- # Combined value is 1 + (1 << 7) = 129
8266 data = bytearray ([0x81 , 0x01 ])
8367 length , pos = decode_variable_length (data )
8468 self .assertEqual (length , 129 )
8569 self .assertEqual (pos , 2 )
8670
8771 def test_max_length (self ):
88- # Test with the maximum length (5 bytes)
89- # This will test the boundary condition of the loop in the function
90- data = bytearray (
91- [0x80 , 0x80 , 0x80 , 0x80 , 0x01 ]
92- ) # Each 0x80 has the high bit set, 0x01 does not
93- # The value is 1 << (7 * 4) = 2**28
72+ data = bytearray ([0x80 , 0x80 , 0x80 , 0x80 , 0x01 ])
9473 length , pos = decode_variable_length (data )
9574 self .assertEqual (length , 2 ** 28 )
9675 self .assertEqual (pos , 5 )
9776
98- # You can add more test cases to cover different scenarios and edge cases.
99-
10077
10178class TestParseUint16 (unittest .TestCase ):
10279 def test_valid_input (self ):
103- data = bytearray ([0x01 , 0x00 ]) # Represents the unsigned integer 1
80+ data = bytearray ([0x01 , 0x00 ])
10481 result = parse_uint16 (data )
10582 self .assertEqual (result , 1 )
10683
10784 def test_different_input (self ):
108- data = bytearray ([0xFF , 0x00 ]) # Represents the unsigned integer 255
85+ data = bytearray ([0xFF , 0x00 ])
10986 result = parse_uint16 (data )
11087 self .assertEqual (result , 255 )
11188
@@ -129,37 +106,30 @@ def test_three_bytes(self):
129106
130107class TestDecodeTime (unittest .TestCase ):
131108 def test_midnight (self ):
132- # Test decoding of midnight
133- data = bytearray ([0x00 ] * 8 ) # Represents 00:00:00
109+ data = bytearray ([0x00 ] * 8 )
134110 result = decode_time (data )
135111 self .assertEqual (result , datetime .time (0 , 0 , 0 ))
136112
137113 def test_valid_time (self ):
138- data = bytearray (
139- [0x00 , 0x00 , 0x00 , 0xC0 , 0x18 , 0x01 , 0x00 , 0x00 ]
140- ) # Represents 17:35:00
114+ data = bytearray ([0x00 , 0x00 , 0x00 , 0xC0 , 0x18 , 0x01 , 0x00 , 0x00 ])
141115 result = decode_time (data )
142116 self .assertEqual (result , datetime .time (17 , 35 , 0 ))
143117
144118
145119class TestDecodeDatetime (unittest .TestCase ):
146120 def test_zero_datetime (self ):
147- # Test decoding of zero datetime
148- data = bytearray ([0x00 ] * 8 ) # Represents 0000-00-00 00:00:00
121+ data = bytearray ([0x00 ] * 8 )
149122 result = decode_datetime (data )
150123 self .assertEqual (result , "0000-00-00 00:00:00" )
151124
152125 def test_valid_datetime (self ):
153- data = bytearray (
154- [0x00 , 0x00 , 0x00 , 0xC0 , 0x18 , 0x9B , 0xB1 , 0x19 ]
155- ) # Represents 2023-11-13 17:35:00
126+ data = bytearray ([0x00 , 0x00 , 0x00 , 0xC0 , 0x18 , 0x9B , 0xB1 , 0x19 ])
156127 expected_datetime = datetime .datetime (
157128 year = 2023 , month = 11 , day = 13 , hour = 17 , minute = 35 , second = 0
158129 )
159130 result = decode_datetime (data )
160131 self .assertEqual (result , expected_datetime )
161132
162133
163- # Running the tests
164134if __name__ == "__main__" :
165135 unittest .main ()
0 commit comments