Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
singlestore-labs
GitHub Repository: singlestore-labs/singlestoredb-python
Path: blob/main/singlestoredb/mysql/tests/base.py
469 views
1
# type: ignore
2
import json
3
import os
4
import platform
5
import re
6
import unittest
7
import warnings
8
9
import singlestoredb.mysql as sv
10
from singlestoredb.connection import build_params
11
12
DBNAME_BASE = 'singlestoredb__test_%s_%s_%s_%s_' % \
13
(
14
*platform.python_version_tuple()[:2],
15
platform.system(), platform.machine(),
16
)
17
18
19
class PyMySQLTestCase(unittest.TestCase):
20
# You can specify your test environment creating a file named
21
# "databases.json" or editing the `databases` variable below.
22
fname = os.path.join(os.path.dirname(__file__), 'databases.json')
23
if os.path.exists(fname):
24
with open(fname) as f:
25
databases = json.load(f)
26
else:
27
params = build_params()
28
databases = [
29
{
30
'host': params['host'],
31
'port': params['port'],
32
'user': params['user'],
33
'passwd': params['password'],
34
'database': DBNAME_BASE + '1',
35
'use_unicode': True,
36
'local_infile': True,
37
'buffered': params['buffered'],
38
},
39
{
40
'host': params['host'], 'user': params['user'],
41
'port': params['port'], 'passwd': params['password'],
42
'database': DBNAME_BASE + '2',
43
'buffered': params['buffered'],
44
},
45
]
46
47
def mysql_server_is(self, conn, version_tuple):
48
"""
49
Return True if the given connection is on the version given or greater.
50
51
This only checks the server version string provided when the
52
connection is established, therefore any check for a version tuple
53
greater than (5, 5, 5) will always fail on MariaDB, as it always
54
starts with 5.5.5, e.g. 5.5.5-10.7.1-MariaDB-1:10.7.1+maria~focal.
55
56
Examples
57
--------
58
59
if self.mysql_server_is(conn, (5, 6, 4)):
60
# do something for MySQL 5.6.4 and above
61
62
"""
63
server_version = conn.get_server_info()
64
server_version_tuple = tuple(
65
(int(dig) if dig is not None else 0)
66
for dig in re.match(r'(\d+)\.(\d+)\.(\d+)', server_version).group(1, 2, 3)
67
)
68
return server_version_tuple >= version_tuple
69
70
_connections = None
71
72
@property
73
def connections(self):
74
if self._connections is None:
75
self._connections = []
76
for params in self.databases:
77
conn = sv.connect(**params)
78
self._connections.append(conn)
79
self.addCleanup(self._teardown_connections)
80
return self._connections
81
82
def connect(self, **params):
83
p = self.databases[0].copy()
84
p.update(params)
85
conn = sv.connect(**p)
86
87
@self.addCleanup
88
def teardown():
89
if conn.open:
90
conn.close()
91
92
return conn
93
94
def _teardown_connections(self):
95
if self._connections:
96
for connection in self._connections:
97
if connection.open:
98
connection.close()
99
self._connections = None
100
101
def safe_create_table(self, connection, tablename, ddl, cleanup=True):
102
"""
103
Create a table.
104
105
Ensures any existing version of that table is first dropped.
106
107
Also adds a cleanup rule to drop the table after the test
108
completes.
109
110
"""
111
cursor = connection.cursor()
112
113
with warnings.catch_warnings():
114
warnings.simplefilter('ignore')
115
cursor.execute('drop table if exists `%s`' % (tablename,))
116
cursor.execute(ddl)
117
cursor.close()
118
if cleanup:
119
self.addCleanup(self.drop_table, connection, tablename)
120
121
def drop_table(self, connection, tablename):
122
cursor = connection.cursor()
123
with warnings.catch_warnings():
124
warnings.simplefilter('ignore')
125
cursor.execute('drop table if exists `%s`' % (tablename,))
126
cursor.close()
127
128