File: 0001-Include-upstream-test-suite-missing-in-source-tarbal.patch

package info (click to toggle)
python-testing.mysqld 1.4.0-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 176 kB
  • sloc: python: 388; makefile: 9
file content (294 lines) | stat: -rw-r--r-- 12,179 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
From 5b00df5e53b49298d887e99ee644f346bf9c5297 Mon Sep 17 00:00:00 2001
From: Dominik George <nik@naturalnet.de>
Date: Thu, 13 Oct 2016 16:59:55 +0200
Subject: Include upstream test suite missing in source tarball

Bug: https://github.com/tk0miya/testing.mysqld/issues/4
---
 tests/test_mysql.py | 279 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 279 insertions(+)
 create mode 100644 tests/test_mysql.py

--- /dev/null
+++ b/tests/test_mysql.py
@@ -0,0 +1,280 @@
+# -*- coding: utf-8 -*-
+
+import os
+import sys
+import signal
+import tempfile
+import testing.mysqld
+from mock import patch
+from time import sleep
+from shutil import rmtree
+from contextlib import closing
+import pymysql
+import sqlalchemy
+
+if sys.version_info < (2, 7):
+    import unittest2 as unittest
+else:
+    import unittest
+
+
+class TestMysqld(unittest.TestCase):
+    def test_basic(self):
+        try:
+            # start mysql server
+            mysqld = testing.mysqld.Mysqld(my_cnf={'skip-networking': None})
+            self.assertIsNotNone(mysqld)
+            self.assertEqual(mysqld.dsn(),
+                             dict(unix_socket=mysqld.base_dir + '/tmp/mysql.sock',
+                                  user='root',
+                                  db='test'))
+
+            # connect to mysql (w/ pymysql)
+            conn = pymysql.connect(**mysqld.dsn())
+            self.assertIsNotNone(conn)
+            self.assertRegexpMatches(mysqld.read_bootlog(), 'ready for connections')
+
+            # connect to mysql (w/ sqlalchemy)
+            engine = sqlalchemy.create_engine(mysqld.url())
+            self.assertIsNotNone(engine)
+
+            # shutting down
+            pid = mysqld.server_pid
+            self.assertTrue(os.path.exists(mysqld.base_dir + '/tmp/mysql.sock'))
+            self.assertTrue(mysqld.is_alive())
+        finally:
+            mysqld.stop()
+            sleep(1)
+
+            self.assertFalse(os.path.exists(mysqld.base_dir + '/tmp/mysql.sock'))
+            self.assertFalse(mysqld.is_alive())
+            with self.assertRaises(OSError):
+                os.kill(pid, 0)  # process is down
+
+    def test_stop(self):
+        # start mysql server
+        mysqld = testing.mysqld.Mysqld(my_cnf={'skip-networking': None})
+        self.assertTrue(os.path.exists(mysqld.base_dir))
+        self.assertTrue(mysqld.is_alive())
+
+        # call stop()
+        mysqld.stop()
+        self.assertFalse(os.path.exists(mysqld.base_dir))
+        self.assertFalse(mysqld.is_alive())
+
+        # call stop() again
+        mysqld.stop()
+        self.assertFalse(os.path.exists(mysqld.base_dir))
+        self.assertFalse(mysqld.is_alive())
+
+        # delete mysqld object after stop()
+        del mysqld
+
+    def test_dsn_and_url(self):
+        mysqld = testing.mysqld.Mysqld(auto_start=0)
+        self.assertEqual({'db': 'test', 'unix_socket': mysqld.my_cnf['socket'], 'user': 'root'},
+                         mysqld.dsn())
+        self.assertEqual("mysql+pymysql://root@localhost/test?unix_socket=%s" % mysqld.my_cnf['socket'],
+                         mysqld.url())
+        self.assertEqual("mysql+pymysql://root@localhost/test?unix_socket=%s&charset=utf8" % mysqld.my_cnf['socket'],
+                         mysqld.url(charset='utf8'))
+        self.assertEqual("mysql+mysqldb://root@localhost/test?unix_socket=%s" % mysqld.my_cnf['socket'],
+                         mysqld.url(driver='mysqldb'))
+
+        mysqld = testing.mysqld.Mysqld(my_cnf={'port': 12345}, auto_start=0)
+        self.assertEqual({'db': 'test', 'host': '127.0.0.1', 'port': 12345, 'user': 'root'},
+                         mysqld.dsn())
+        self.assertEqual("mysql+pymysql://root@127.0.0.1:12345/test", mysqld.url())
+        self.assertEqual("mysql+pymysql://root@127.0.0.1:12345/test?charset=utf8", mysqld.url(charset='utf8'))
+        self.assertEqual("mysql+mysqldb://root@127.0.0.1:12345/test", mysqld.url(driver='mysqldb'))
+
+    def test_with_mysql(self):
+        with testing.mysqld.Mysqld(my_cnf={'skip-networking': None}) as mysqld:
+            self.assertIsNotNone(mysqld)
+
+            # connect to mysql
+            conn = pymysql.connect(**mysqld.dsn())
+            self.assertIsNotNone(conn)
+            self.assertTrue(mysqld.is_alive())
+
+        self.assertFalse(mysqld.is_alive())
+
+    def test_multiple_mysql(self):
+        mysqld1 = testing.mysqld.Mysqld(my_cnf={'skip-networking': None})
+        mysqld2 = testing.mysqld.Mysqld(my_cnf={'skip-networking': None})
+        self.assertNotEqual(mysqld1.server_pid, mysqld2.server_pid)
+
+        self.assertTrue(mysqld1.is_alive())
+        self.assertTrue(mysqld2.is_alive())
+
+    @patch("testing.mysqld.find_program")
+    def test_mysqld_is_not_found(self, find_program):
+        find_program.side_effect = RuntimeError
+
+        with self.assertRaises(RuntimeError):
+            testing.mysqld.Mysqld(my_cnf={'skip-networking': None})
+
+    def test_fork(self):
+        mysqld = testing.mysqld.Mysqld(my_cnf={'skip-networking': None})
+        if os.fork() == 0:
+            del mysqld
+            mysqld = None
+            os.kill(os.getpid(), signal.SIGTERM)  # exit tests FORCELY
+        else:
+            os.wait()
+            sleep(1)
+            self.assertTrue(mysqld.is_alive())  # process is alive (delete mysqld obj in child does not effect)
+
+    def test_stop_on_child_process(self):
+        mysqld = testing.mysqld.Mysqld(my_cnf={'skip-networking': None})
+        if os.fork() == 0:
+            mysqld.stop()
+            os.kill(mysqld.server_pid, 0)  # process is alive (calling stop() is ignored)
+            os.kill(os.getpid(), signal.SIGTERM)  # exit tests FORCELY
+        else:
+            os.wait()
+            sleep(1)
+            self.assertTrue(mysqld.is_alive())  # process is alive (calling stop() in child is ignored)
+
+    def test_copy_data_from(self):
+        try:
+            tmpdir = tempfile.mkdtemp()
+
+            # create new database
+            with testing.mysqld.Mysqld(my_cnf={'skip-networking': None}, base_dir=tmpdir) as mysqld:
+                conn = pymysql.connect(**mysqld.dsn())
+                cursor = conn.cursor()
+                cursor.execute("CREATE TABLE hello(id int, value varchar(256))")
+                cursor.execute("INSERT INTO hello values(1, 'hello'), (2, 'ciao')")
+                conn.commit()
+
+            # create another database from first one
+            data_dir = os.path.join(tmpdir, 'var')
+            with testing.mysqld.Mysqld(my_cnf={'skip-networking': None}, copy_data_from=data_dir) as mysqld:
+                conn = pymysql.connect(**mysqld.dsn())
+                cursor = conn.cursor()
+                cursor.execute('SELECT * FROM test.hello ORDER BY id')
+
+                self.assertEqual(cursor.fetchall(), ((1, 'hello'), (2, 'ciao')))
+        finally:
+            rmtree(tmpdir)
+
+    def test_copy_data_from_with_passwd(self):
+        try:
+            tmpdir = tempfile.mkdtemp()
+
+            # create new database
+            with testing.mysqld.Mysqld(my_cnf={'skip-networking': None}, base_dir=tmpdir) as mysqld:
+                conn = pymysql.connect(**mysqld.dsn())
+                cursor = conn.cursor()
+                cursor.execute("CREATE TABLE hello(id int, value varchar(256))")
+                cursor.execute("INSERT INTO hello values(1, 'hello'), (2, 'ciao')")
+                cursor.execute("SET PASSWORD FOR 'root'@'localhost' = PASSWORD('secret')")
+                cursor.execute("FLUSH PRIVILEGES")
+                conn.commit()
+
+            # create another database from first one
+            data_dir = os.path.join(tmpdir, 'var')
+            with testing.mysqld.Mysqld(my_cnf={'skip-networking': None},
+                                       copy_data_from=data_dir, passwd="secret") as mysqld:
+                conn = pymysql.connect(**mysqld.dsn())
+                cursor = conn.cursor()
+                cursor.execute('SELECT * FROM test.hello ORDER BY id')
+
+                self.assertEqual(cursor.fetchall(), ((1, 'hello'), (2, 'ciao')))
+        finally:
+            rmtree(tmpdir)
+
+    def test_skipIfNotInstalled_found(self):
+        @testing.mysqld.skipIfNotInstalled
+        def testcase():
+            pass
+
+        self.assertEqual(False, hasattr(testcase, '__unittest_skip__'))
+        self.assertEqual(False, hasattr(testcase, '__unittest_skip_why__'))
+
+    @patch("testing.mysqld.find_program")
+    def test_skipIfNotInstalled_notfound(self, find_program):
+        find_program.side_effect = RuntimeError
+
+        @testing.mysqld.skipIfNotInstalled
+        def testcase():
+            pass
+
+        self.assertEqual(True, hasattr(testcase, '__unittest_skip__'))
+        self.assertEqual(True, hasattr(testcase, '__unittest_skip_why__'))
+        self.assertEqual(True, testcase.__unittest_skip__)
+        self.assertEqual("mysqld not found", testcase.__unittest_skip_why__)
+
+    def test_skipIfNotInstalled_with_args_found(self):
+        path = testing.mysqld.find_program('mysqld', ['sbin'])
+
+        @testing.mysqld.skipIfNotInstalled(path)
+        def testcase():
+            pass
+
+        self.assertEqual(False, hasattr(testcase, '__unittest_skip__'))
+        self.assertEqual(False, hasattr(testcase, '__unittest_skip_why__'))
+
+    def test_skipIfNotInstalled_with_args_notfound(self):
+        @testing.mysqld.skipIfNotInstalled("/path/to/anywhere")
+        def testcase():
+            pass
+
+        self.assertEqual(True, hasattr(testcase, '__unittest_skip__'))
+        self.assertEqual(True, hasattr(testcase, '__unittest_skip_why__'))
+        self.assertEqual(True, testcase.__unittest_skip__)
+        self.assertEqual("mysqld not found", testcase.__unittest_skip_why__)
+
+    def test_skipIfNotFound_found(self):
+        @testing.mysqld.skipIfNotFound
+        def testcase():
+            pass
+
+        self.assertEqual(False, hasattr(testcase, '__unittest_skip__'))
+        self.assertEqual(False, hasattr(testcase, '__unittest_skip_why__'))
+
+    @patch("testing.mysqld.find_program")
+    def test_skipIfNotFound_notfound(self, find_program):
+        find_program.side_effect = RuntimeError
+
+        @testing.mysqld.skipIfNotFound
+        def testcase():
+            pass
+
+        self.assertEqual(True, hasattr(testcase, '__unittest_skip__'))
+        self.assertEqual(True, hasattr(testcase, '__unittest_skip_why__'))
+        self.assertEqual(True, testcase.__unittest_skip__)
+        self.assertEqual("mysqld not found", testcase.__unittest_skip_why__)
+
+    def test_MysqldFactory(self):
+        Mysqld = testing.mysqld.MysqldFactory(cache_initialized_db=True)
+        with Mysqld() as mysqld1:
+            self.assertTrue(mysqld1.settings['copy_data_from'])
+            copy_data_from1 = mysqld1.settings['copy_data_from']
+            self.assertTrue(os.path.exists(copy_data_from1))
+        with Mysqld() as mysqld2:
+            self.assertEqual(copy_data_from1, mysqld2.settings['copy_data_from'])
+        Mysqld.clear_cache()
+        self.assertFalse(os.path.exists(copy_data_from1))
+
+    def test_MysqldFactory_with_initialized_handler(self):
+        def handler(mysqld):
+            conn = pymysql.connect(**mysqld.dsn())
+            with closing(conn.cursor()) as cursor:
+                cursor.execute("CREATE TABLE hello(id int, value varchar(256))")
+                cursor.execute("INSERT INTO hello values(1, 'hello'), (2, 'ciao')")
+            conn.commit()
+            conn.close()
+
+        Mysqld = testing.mysqld.MysqldFactory(cache_initialized_db=True,
+                                              on_initialized=handler)
+        try:
+            with Mysqld() as mysqld:
+                conn = pymysql.connect(**mysqld.dsn())
+                with closing(conn.cursor()) as cursor:
+                    cursor.execute('SELECT * FROM hello ORDER BY id')
+                    self.assertEqual(cursor.fetchall(), ((1, 'hello'), (2, 'ciao')))
+                conn.close()
+        finally:
+            Mysqld.clear_cache()