Skip to content

Commit 8d003d2

Browse files
heehehemikanizstarcat37mjs1995
authored
Modify test structure to support multiple database (#502)
Co-authored-by: mikaniz <[email protected]> Co-authored-by: starcat37 <[email protected]> Co-authored-by: mjs <[email protected]>
1 parent c0cd6f7 commit 8d003d2

File tree

6 files changed

+113
-99
lines changed

6 files changed

+113
-99
lines changed

.github/workflows/pytest.yml

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
name: PyTest
22
on: [push, pull_request]
3-
3+
env:
4+
PYTEST_SKIP_OPTION: "not test_no_trailing_rotate_event and not test_end_log_pos and not test_query_event_latin1"
45
jobs:
56
test:
67
strategy:
@@ -29,22 +30,34 @@ jobs:
2930
docker compose create
3031
docker compose start
3132
echo "wait mysql server"
32-
33+
3334
while :
3435
do
35-
if mysql -h 127.0.0.1 --user=root --execute "SELECT version();" 2>&1 >/dev/null && mysql -h 127.0.0.1 --port=3307 --user=root --execute "SELECT version();" 2>&1 >/dev/null; then
36+
if mysql -h 127.0.0.1 --user=root --execute "SELECT version();" 2>&1 >/dev/null && mysql -h 127.0.0.1 --port=3307 --user=root --execute "SELECT version();" 2>&1 >/dev/null; then
3637
break
3738
fi
3839
sleep 1
3940
done
40-
41+
4142
echo "run pytest"
4243
4344
- name: Install dependencies
4445
run: |
4546
pip install .
4647
pip install pytest
4748
48-
- name: Run test suite
49-
run: |
50-
pytest -k "not test_no_trailing_rotate_event and not test_end_log_pos"
49+
- name: Run tests for mysql-5
50+
working-directory: pymysqlreplication/tests
51+
run: pytest -k "$PYTEST_SKIP_OPTION" --db=mysql-5
52+
53+
- name: Run tests for mysql-5-ctl
54+
working-directory: pymysqlreplication/tests
55+
run: pytest -k "$PYTEST_SKIP_OPTION" --db=mysql-5-ctl
56+
57+
- name: Run tests for mysql-8
58+
working-directory: pymysqlreplication/tests
59+
run: pytest -k "$PYTEST_SKIP_OPTION" --db=mysql-8
60+
61+
- name: Run tests for mariadb-10
62+
working-directory: pymysqlreplication/tests
63+
run: pytest -k "$PYTEST_SKIP_OPTION" -m mariadb --db=mariadb-10

pymysqlreplication/tests/base.py

Lines changed: 23 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,35 @@
22
import copy
33
from pymysqlreplication import BinLogStreamReader
44
import os
5+
import json
6+
import pytest
7+
58
import unittest
69

10+
11+
def get_databases():
12+
databases = {}
13+
with open(
14+
os.path.join(os.path.dirname(os.path.abspath(__file__)), "config.json")
15+
) as f:
16+
databases = json.load(f)
17+
return databases
18+
19+
720
base = unittest.TestCase
821

922

1023
class PyMySQLReplicationTestCase(base):
1124
def ignoredEvents(self):
1225
return []
1326

14-
def setUp(self, charset="utf8"):
15-
# default
27+
@pytest.fixture(autouse=True)
28+
def setUpDatabase(self, get_db):
29+
databases = get_databases()
30+
# For local testing, set the get_dbms parameter to one of the following values: 'mysql-5', 'mysql-8', mariadb-10'.
31+
# This value should correspond to the desired database configuration specified in the 'config.json' file.
32+
self.database = databases[get_db]
33+
"""
1634
self.database = {
1735
"host": os.environ.get("MYSQL_5_7") or "localhost",
1836
"user": "root",
@@ -22,7 +40,10 @@ def setUp(self, charset="utf8"):
2240
"charset": charset,
2341
"db": "pymysqlreplication_test",
2442
}
43+
"""
2544

45+
def setUp(self, charset="utf8"):
46+
# default
2647
self.conn_control = None
2748
db = copy.copy(self.database)
2849
db["db"] = None
@@ -122,62 +143,3 @@ def bin_log_basename(self):
122143
bin_log_basename = cursor.fetchone()[0]
123144
bin_log_basename = bin_log_basename.split("/")[-1]
124145
return bin_log_basename
125-
126-
127-
class PyMySQLReplicationMariaDbTestCase(PyMySQLReplicationTestCase):
128-
def setUp(self):
129-
# default
130-
self.database = {
131-
"host": os.environ.get("MARIADB_10_6") or "localhost",
132-
"user": "root",
133-
"passwd": "",
134-
"port": int(os.environ.get("MARIADB_10_6_PORT") or 3308),
135-
"use_unicode": True,
136-
"charset": "utf8",
137-
"db": "pymysqlreplication_test",
138-
}
139-
140-
self.conn_control = None
141-
db = copy.copy(self.database)
142-
db["db"] = None
143-
self.connect_conn_control(db)
144-
self.execute("DROP DATABASE IF EXISTS pymysqlreplication_test")
145-
self.execute("CREATE DATABASE pymysqlreplication_test")
146-
db = copy.copy(self.database)
147-
self.connect_conn_control(db)
148-
self.stream = None
149-
self.resetBinLog()
150-
151-
def bin_log_basename(self):
152-
cursor = self.execute("SELECT @@log_bin_basename")
153-
bin_log_basename = cursor.fetchone()[0]
154-
bin_log_basename = bin_log_basename.split("/")[-1]
155-
return bin_log_basename
156-
157-
158-
class PyMySQLReplicationVersion8TestCase(PyMySQLReplicationTestCase):
159-
def setUp(self):
160-
super().setUp()
161-
# default
162-
self.database = {
163-
"host": os.environ.get("MYSQL_8_0") or "localhost",
164-
"user": "root",
165-
"passwd": "",
166-
"port": int(os.environ.get("MYSQL_8_0_PORT") or 3309),
167-
"use_unicode": True,
168-
"charset": "utf8",
169-
"db": "pymysqlreplication_test",
170-
}
171-
172-
self.conn_control = None
173-
db = copy.copy(self.database)
174-
db["db"] = None
175-
self.connect_conn_control(db)
176-
self.execute("DROP DATABASE IF EXISTS pymysqlreplication_test")
177-
self.execute("CREATE DATABASE pymysqlreplication_test")
178-
db = copy.copy(self.database)
179-
self.connect_conn_control(db)
180-
self.stream = None
181-
self.resetBinLog()
182-
self.isMySQL80AndMore()
183-
self.__is_mariaDB = None
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"mysql-5": {
3+
"host": "localhost",
4+
"user": "root",
5+
"passwd": "",
6+
"port": 3306,
7+
"use_unicode": true,
8+
"charset": "utf8",
9+
"db": "pymysqlreplication_test"
10+
},
11+
"mysql-5-ctl": {
12+
"host": "localhost",
13+
"user": "root",
14+
"passwd": "",
15+
"port": 3307,
16+
"use_unicode": true,
17+
"charset": "utf8",
18+
"db": "pymysqlreplication_test"
19+
},
20+
"mariadb-10": {
21+
"host": "localhost",
22+
"user": "root",
23+
"passwd": "",
24+
"port": 3308,
25+
"use_unicode": true,
26+
"charset": "utf8",
27+
"db": "pymysqlreplication_test"
28+
},
29+
"mysql-8": {
30+
"host": "localhost",
31+
"user": "root",
32+
"passwd": "",
33+
"port": 3309,
34+
"use_unicode": true,
35+
"charset": "utf8",
36+
"db": "pymysqlreplication_test"
37+
}
38+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import pytest
2+
3+
4+
def pytest_addoption(parser):
5+
parser.addoption("--db", action="store", default="mysql-5")
6+
7+
8+
@pytest.fixture
9+
def get_db(request):
10+
return request.config.getoption("--db")

pymysqlreplication/tests/test_basic.py

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
import copy
21
import io
3-
import os
42
import time
5-
import pymysql
63
import unittest
74

85
from pymysqlreplication.tests import base
@@ -13,6 +10,7 @@
1310
from pymysqlreplication.row_event import *
1411
from pymysqlreplication.packet import BinLogPacketWrapper
1512
from pymysql.protocol import MysqlPacket
13+
import pytest
1614

1715
__all__ = [
1816
"TestBasicBinLogStreamReader",
@@ -826,42 +824,22 @@ def test_alter_column(self):
826824

827825

828826
class TestCTLConnectionSettings(base.PyMySQLReplicationTestCase):
829-
def setUp(self):
827+
def setUp(self, charset="utf8"):
830828
super().setUp()
831-
self.stream.close()
832-
ctl_db = copy.copy(self.database)
833-
ctl_db["db"] = None
834-
ctl_db["port"] = int(os.environ.get("MYSQL_5_7_CTL_PORT") or 3307)
835-
ctl_db["host"] = os.environ.get("MYSQL_5_7_CTL") or "localhost"
836-
self.ctl_conn_control = pymysql.connect(**ctl_db)
837-
self.ctl_conn_control.cursor().execute(
838-
"DROP DATABASE IF EXISTS pymysqlreplication_test"
839-
)
840-
self.ctl_conn_control.cursor().execute(
841-
"CREATE DATABASE pymysqlreplication_test"
842-
)
843-
self.ctl_conn_control.close()
844-
ctl_db["db"] = "pymysqlreplication_test"
845-
self.ctl_conn_control = pymysql.connect(**ctl_db)
846829
self.stream = BinLogStreamReader(
847830
self.database,
848-
ctl_connection_settings=ctl_db,
849831
server_id=1024,
850832
only_events=(WriteRowsEvent,),
851833
)
852834

853-
def tearDown(self):
854-
super().tearDown()
855-
self.ctl_conn_control.close()
856-
857835
def test_separate_ctl_settings_no_error(self):
858836
self.execute("CREATE TABLE test (id INTEGER(11))")
859837
self.execute("INSERT INTO test VALUES (1)")
860838
self.execute("DROP TABLE test")
861839
self.execute("COMMIT")
862-
self.ctl_conn_control.cursor().execute("CREATE TABLE test (id INTEGER(11))")
863-
self.ctl_conn_control.cursor().execute("INSERT INTO test VALUES (1)")
864-
self.ctl_conn_control.cursor().execute("COMMIT")
840+
self.conn_control.cursor().execute("CREATE TABLE test (id INTEGER(11))")
841+
self.conn_control.cursor().execute("INSERT INTO test VALUES (1)")
842+
self.conn_control.cursor().execute("COMMIT")
865843
try:
866844
self.stream.fetchone()
867845
except Exception as e:
@@ -1322,7 +1300,13 @@ def tearDown(self):
13221300
super(TestStatementConnectionSetting, self).tearDown()
13231301

13241302

1325-
class TestMariadbBinlogStreamReader(base.PyMySQLReplicationMariaDbTestCase):
1303+
@pytest.mark.mariadb
1304+
class TestMariadbBinlogStreamReader(base.PyMySQLReplicationTestCase):
1305+
def setUp(self):
1306+
super().setUp()
1307+
if not self.isMariaDB():
1308+
self.skipTest("Skipping the entire class for MariaDB")
1309+
13261310
def test_binlog_checkpoint_event(self):
13271311
self.stream.close()
13281312
self.stream = BinLogStreamReader(
@@ -1353,7 +1337,13 @@ def test_binlog_checkpoint_event(self):
13531337
self.assertEqual(event.filename, self.bin_log_basename() + ".000001")
13541338

13551339

1356-
class TestMariadbBinlogStreamReader2(base.PyMySQLReplicationMariaDbTestCase):
1340+
@pytest.mark.mariadb
1341+
class TestMariadbBinlogStreamReader2(base.PyMySQLReplicationTestCase):
1342+
def setUp(self):
1343+
super().setUp()
1344+
if not self.isMariaDB():
1345+
self.skipTest("Skipping the entire class for MariaDB")
1346+
13571347
def test_annotate_rows_event(self):
13581348
query = "CREATE TABLE test (id INT NOT NULL AUTO_INCREMENT, data VARCHAR (50) NOT NULL, PRIMARY KEY (id))"
13591349
self.execute(query)
@@ -1498,7 +1488,8 @@ def test_query_event_latin1(self):
14981488
assert event.query == r"CREATE TABLE test_latin1_\xd6\xc6\xdb (a INT)"
14991489

15001490

1501-
class TestOptionalMetaData(base.PyMySQLReplicationVersion8TestCase):
1491+
@pytest.mark.mariadb
1492+
class TestOptionalMetaData(base.PyMySQLReplicationTestCase):
15021493
def setUp(self):
15031494
super(TestOptionalMetaData, self).setUp()
15041495
self.stream.close()

pymysqlreplication/tests/test_data_type.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,7 @@ def test_varbinary(self):
942942
self.assertEqual(event.rows[0]["values"]["b"], b"\xff\x01\x00\x00")
943943

944944

945-
class TestDataTypeVersion8(base.PyMySQLReplicationVersion8TestCase):
945+
class TestDataTypeVersion8(base.PyMySQLReplicationTestCase):
946946
def ignoredEvents(self):
947947
return [GtidEvent, PreviousGtidsEvent]
948948

0 commit comments

Comments
 (0)