Skip to content

Commit cdead05

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

File tree

1 file changed

+69
-1
lines changed

1 file changed

+69
-1
lines changed

pymysqlreplication/tests/test_basic.py

Lines changed: 69 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,74 @@ 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+
def test_parsing(self):
962+
with self.assertRaises(ValueError) as exc:
963+
gtid = Gtid("57b70f4e-20d3-11e5-a393-4a63946f7eac:1-5 57b70f4e-20d3-11e5-a393-4a63946f7eac:1-56")
964+
gtid = Gtid("NNNNNNNN-20d3-11e5-a393-4a63946f7eac:1-5")
965+
gtid = Gtid("-20d3-11e5-a393-4a63946f7eac:1-5")
966+
gtid = Gtid("-20d3-11e5-a393-4a63946f7eac:1-")
967+
gtid = Gtid("57b70f4e-20d3-11e5-a393-4a63946f7eac:A-1")
968+
gtid = Gtid("57b70f4e-20d3-11e5-a393-4a63946f7eac:-1")
969+
gtid = Gtid("57b70f4e-20d3-11e5-a393-4a63946f7eac:1-:1")
970+
gtid = Gtid("57b70f4e-20d3-11e5-a393-4a63946f7eac::1")
971+
972+
905973
if __name__ == "__main__":
906974
import unittest
907975
unittest.main()

0 commit comments

Comments
 (0)