Skip to content

Commit 8496e0f

Browse files
committed
add test fot to_json, to_dict
1 parent 0a65ab8 commit 8496e0f

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import pytest
2+
3+
4+
from pymysqlreplication.tests.base import PyMySQLReplicationTestCase
5+
from pymysqlreplication.event import QueryEvent
6+
from pymysqlreplication import BinLogStreamReader
7+
import json
8+
class BinLogEventTestCase(PyMySQLReplicationTestCase):
9+
10+
def setUp(self):
11+
super(BinLogEventTestCase, self).setUp()
12+
self.execute("SET SESSION binlog_rows_query_log_events=1")
13+
14+
def tearDown(self):
15+
self.execute("SET SESSION binlog_rows_query_log_events=0")
16+
super(BinLogEventTestCase, self).tearDown()
17+
18+
target_fields = [
19+
"timestamp",
20+
"log_pos",
21+
"event_size",
22+
"read_bytes"
23+
]
24+
25+
def test_to_dict(self):
26+
self.stream = BinLogStreamReader(self.database, server_id=1024)
27+
query = "CREATE TABLE test (id INT NOT NULL AUTO_INCREMENT, data VARCHAR (50) NOT NULL, PRIMARY KEY (id))"
28+
self.execute(query)
29+
self.execute("COMMIT")
30+
31+
event = self.stream.fetchone()
32+
33+
event_dict = event.to_dict()
34+
35+
self.assertEqual(set(event_dict.keys()), set(self.target_fields))
36+
self.assertEqual(event_dict["timestamp"], event.formatted_timestamp)
37+
self.assertEqual(event_dict["log_pos"], event.packet.log_pos)
38+
self.assertEqual(event_dict["read_bytes"], event.packet.read_bytes)
39+
self.assertEqual(event_dict["event_size"], event.event_size)
40+
41+
def test_to_json(self):
42+
self.stream = BinLogStreamReader(self.database, server_id=1024)
43+
query = "CREATE TABLE test (id INT NOT NULL AUTO_INCREMENT, data VARCHAR (50) NOT NULL, PRIMARY KEY (id))"
44+
self.execute(query)
45+
self.execute("COMMIT")
46+
47+
event = self.stream.fetchone()
48+
49+
assert event.to_json() == json.dumps(event.to_dict())
50+

0 commit comments

Comments
 (0)