Skip to content

Commit 462bf2a

Browse files
Axel Vialadarnuria
authored andcommitted
Add some Unit test for Gtid class alone.
- intervals - sub/add - encode/decode - ordering
1 parent 680d874 commit 462bf2a

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

pymysqlreplication/tests/test_basic.py

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from pymysqlreplication.tests import base
1313
from pymysqlreplication import BinLogStreamReader
14-
from pymysqlreplication.gtid import GtidSet
14+
from pymysqlreplication.gtid import GtidSet, Gtid
1515
from pymysqlreplication.event import *
1616
from pymysqlreplication.exceptions import TableMetadataUnavailableError
1717
from pymysqlreplication.constants.BINLOG import *
@@ -902,6 +902,63 @@ def test_gtidset_representation_payload(self):
902902

903903
self.assertEqual(str(myset), str(parsedset))
904904

905+
class GtidTests(unittest.TestCase):
906+
def test_ordering(self):
907+
gtid = Gtid("57b70f4e-20d3-11e5-a393-4a63946f7eac:1-56")
908+
other = Gtid("57b70f4e-20d3-11e5-a393-4a63946f7eac:5-10")
909+
assert gtid.__lt__(other)
910+
assert gtid.__le__(other)
911+
assert other.__gt__(gtid)
912+
assert other.__ge__(gtid)
913+
gtid = Gtid("57b70f4e-20d3-11e5-a393-4a63946f7eac:1-56")
914+
other = Gtid("deadbeef-20d3-11e5-a393-4a63946f7eac:5-10")
915+
assert gtid.__lt__(other)
916+
assert gtid.__le__(other)
917+
assert other.__gt__(gtid)
918+
assert other.__ge__(gtid)
919+
920+
def test_encode_decode(self):
921+
gtid = Gtid("57b70f4e-20d3-11e5-a393-4a63946f7eac:1-56")
922+
payload = gtid.encode()
923+
decoded = Gtid.decode(io.BytesIO(payload))
924+
assert str(gtid) == str(decoded)
925+
926+
def test_add_interval(self):
927+
gtid = Gtid("57b70f4e-20d3-11e5-a393-4a63946f7eac:5-56")
928+
end = Gtid("57b70f4e-20d3-11e5-a393-4a63946f7eac:57-58")
929+
assert (gtid + end).intervals == [(5, 59)]
930+
931+
start = Gtid("57b70f4e-20d3-11e5-a393-4a63946f7eac:1-2")
932+
assert (gtid + start).intervals == [(1, 3), (5, 57)]
933+
934+
sparse = Gtid("57b70f4e-20d3-11e5-a393-4a63946f7eac:1-4:7-10")
935+
within = Gtid("57b70f4e-20d3-11e5-a393-4a63946f7eac:5-6")
936+
assert (sparse + within).intervals == [(1, 11)]
937+
938+
def test_interval_non_merging(self):
939+
gtid = Gtid("57b70f4e-20d3-11e5-a393-4a63946f7eac:1-56")
940+
other = Gtid("57b70f4e-20d3-11e5-a393-4a63946f7eac:58-59")
941+
gtid = gtid + other
942+
self.assertEqual(str(gtid), "57b70f4e-20d3-11e5-a393-4a63946f7eac:1-56:58-59")
943+
944+
def test_merging(self):
945+
gtid = Gtid("57b70f4e-20d3-11e5-a393-4a63946f7eac:1-56")
946+
other = Gtid("57b70f4e-20d3-11e5-a393-4a63946f7eac:57-59")
947+
gtid = gtid + other
948+
self.assertEqual(str(gtid), "57b70f4e-20d3-11e5-a393-4a63946f7eac:1-59")
949+
950+
def test_sub_interval(self):
951+
gtid = Gtid("57b70f4e-20d3-11e5-a393-4a63946f7eac:1-56")
952+
start = Gtid("57b70f4e-20d3-11e5-a393-4a63946f7eac:1-5")
953+
assert (gtid - start).intervals == [(6, 57)]
954+
955+
end = Gtid("57b70f4e-20d3-11e5-a393-4a63946f7eac:55-56")
956+
assert (gtid - end).intervals == [(1, 55)]
957+
958+
within = Gtid("57b70f4e-20d3-11e5-a393-4a63946f7eac:25-26")
959+
assert (gtid - within).intervals == [(1, 25), (27, 57)]
960+
961+
905962
if __name__ == "__main__":
906963
import unittest
907964
unittest.main()

0 commit comments

Comments
 (0)