Skip to content

Commit 2efc6dd

Browse files
committed
add other test cases of util bytes
1 parent 7124eaa commit 2efc6dd

File tree

1 file changed

+73
-8
lines changed

1 file changed

+73
-8
lines changed

pymysqlreplication/tests/test_util_bytes.py

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,51 @@
11
import unittest
2+
from datetime import time
3+
24
from pymysqlreplication.util import *
35

46

7+
class TestIsDataShort(unittest.TestCase):
8+
def test_data_is_shorter(self):
9+
# Test with data shorter than expected.
10+
data = bytearray([0x01]) # 1-byte data
11+
expected_length = 2
12+
self.assertTrue(is_data_short(data, expected_length))
13+
14+
def test_data_is_equal_length(self):
15+
# Test with data equal to expected length.
16+
data = bytearray([0x01, 0x00]) # 2-byte data
17+
expected_length = 2
18+
self.assertFalse(is_data_short(data, expected_length))
19+
20+
def test_data_is_longer(self):
21+
# Test with data longer than expected.
22+
data = bytearray([0x01, 0x00, 0x02]) # 3-byte data
23+
expected_length = 2
24+
self.assertFalse(is_data_short(data, expected_length))
25+
26+
def test_data_is_empty(self):
27+
# Test with empty data.
28+
data = bytearray([])
29+
expected_length = 1
30+
self.assertTrue(is_data_short(data, expected_length))
31+
32+
33+
class TestDecodeCount(unittest.TestCase):
34+
def test_small_format(self):
35+
# Test with 2-byte input and small format.
36+
data = bytearray([0x01, 0x00]) # Represents the unsigned integer 1
37+
is_small = True
38+
result = decode_count(data, is_small)
39+
self.assertEqual(result, 1)
40+
41+
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
44+
is_small = False
45+
result = decode_count(data, is_small)
46+
self.assertEqual(result, 1)
47+
48+
549
class TestDecodeUint(unittest.TestCase):
650
def test_valid_input(self):
751
# Test with a known 2-byte input.
@@ -52,15 +96,36 @@ def test_three_bytes(self):
5296

5397

5498
class TestDecodeTime(unittest.TestCase):
99+
def test_midnight(self):
100+
# Test decoding of midnight
101+
data = bytearray([0x00] * 8) # Represents 00:00:00
102+
result = decode_time(data)
103+
self.assertEqual(result, datetime.time(0, 0, 0))
104+
55105
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)
106+
data = bytearray(
107+
[0x00, 0x00, 0x00, 0xC0, 0x18, 0x01, 0x00, 0x00]
108+
) # Represents 17:35:00
109+
result = decode_time(data)
110+
self.assertEqual(result, datetime.time(17, 35, 0))
111+
112+
113+
class TestDecodeDatetime(unittest.TestCase):
114+
def test_zero_datetime(self):
115+
# Test decoding of zero datetime
116+
data = bytearray([0x00] * 8) # Represents 0000-00-00 00:00:00
117+
result = decode_datetime(data)
118+
self.assertEqual(result, "0000-00-00 00:00:00")
119+
120+
def test_valid_datetime(self):
121+
data = bytearray(
122+
[0x00, 0x00, 0x00, 0xC0, 0x18, 0x9B, 0xB1, 0x19]
123+
) # Represents 2023-11-13 17:35:00
124+
expected_datetime = datetime.datetime(
125+
year=2023, month=11, day=13, hour=17, minute=35, second=0
126+
)
127+
result = decode_datetime(data)
128+
self.assertEqual(result, expected_datetime)
64129

65130

66131
# Running the tests

0 commit comments

Comments
 (0)