|
1 |
| -from pymysql import OperationalError, Warning |
2 |
| -from pymysql.tests import base |
| 1 | +from tornado.testing import gen_test |
| 2 | +from tornado_mysql.err import OperationalError, Warning |
| 3 | +from tornado_mysql.tests import base |
3 | 4 |
|
4 | 5 | import os
|
5 | 6 | import warnings
|
|
8 | 9 |
|
9 | 10 |
|
10 | 11 | class TestLoadLocal(base.PyMySQLTestCase):
|
| 12 | + @gen_test |
11 | 13 | def test_no_file(self):
|
12 | 14 | """Test load local infile when the file does not exist"""
|
13 | 15 | conn = self.connections[0]
|
14 | 16 | c = conn.cursor()
|
15 |
| - c.execute("CREATE TABLE test_load_local (a INTEGER, b INTEGER)") |
| 17 | + with warnings.catch_warnings(): |
| 18 | + warnings.simplefilter("ignore") |
| 19 | + yield c.execute("DROP TABLE IF EXISTS test_load_local") |
| 20 | + yield c.execute("CREATE TABLE test_load_local (a INTEGER, b INTEGER)") |
16 | 21 | try:
|
17 |
| - self.assertRaises( |
18 |
| - OperationalError, |
19 |
| - c.execute, |
20 |
| - ("LOAD DATA LOCAL INFILE 'no_data.txt' INTO TABLE " |
21 |
| - "test_load_local fields terminated by ','") |
22 |
| - ) |
| 22 | + with self.assertRaises(OperationalError) as cm: |
| 23 | + yield c.execute( |
| 24 | + "LOAD DATA LOCAL INFILE 'no_data.txt' INTO TABLE " |
| 25 | + "test_load_local fields terminated by ','") |
23 | 26 | finally:
|
24 |
| - c.execute("DROP TABLE test_load_local") |
| 27 | + yield c.execute("DROP TABLE IF EXISTS test_load_local") |
25 | 28 | c.close()
|
26 |
| - |
| 29 | + @gen_test |
27 | 30 | def test_load_file(self):
|
28 | 31 | """Test load local infile with a valid file"""
|
29 | 32 | conn = self.connections[0]
|
30 | 33 | c = conn.cursor()
|
31 |
| - c.execute("CREATE TABLE test_load_local (a INTEGER, b INTEGER)") |
| 34 | + with warnings.catch_warnings(): |
| 35 | + warnings.simplefilter("ignore") |
| 36 | + yield c.execute("DROP TABLE IF EXISTS test_load_local") |
| 37 | + yield c.execute("CREATE TABLE test_load_local (a INTEGER, b INTEGER)") |
32 | 38 | filename = os.path.join(os.path.dirname(os.path.realpath(__file__)),
|
33 | 39 | 'data',
|
34 | 40 | 'load_local_data.txt')
|
35 | 41 | try:
|
36 |
| - c.execute( |
| 42 | + yield c.execute( |
37 | 43 | ("LOAD DATA LOCAL INFILE '{0}' INTO TABLE " +
|
38 | 44 | "test_load_local FIELDS TERMINATED BY ','").format(filename)
|
39 | 45 | )
|
40 |
| - c.execute("SELECT COUNT(*) FROM test_load_local") |
| 46 | + yield c.execute("SELECT COUNT(*) FROM test_load_local") |
41 | 47 | self.assertEqual(22749, c.fetchone()[0])
|
42 | 48 | finally:
|
43 |
| - c.execute("DROP TABLE test_load_local") |
| 49 | + yield c.execute("DROP TABLE IF EXISTS test_load_local") |
44 | 50 |
|
| 51 | + @gen_test |
45 | 52 | def test_load_warnings(self):
|
46 | 53 | """Test load local infile produces the appropriate warnings"""
|
47 | 54 | conn = self.connections[0]
|
48 | 55 | c = conn.cursor()
|
49 |
| - c.execute("CREATE TABLE test_load_local (a INTEGER, b INTEGER)") |
| 56 | + with warnings.catch_warnings(): |
| 57 | + warnings.simplefilter("ignore") |
| 58 | + yield c.execute("DROP TABLE IF EXISTS test_load_local") |
| 59 | + yield c.execute("CREATE TABLE test_load_local (a INTEGER, b INTEGER)") |
50 | 60 | filename = os.path.join(os.path.dirname(os.path.realpath(__file__)),
|
51 | 61 | 'data',
|
52 | 62 | 'load_local_warn_data.txt')
|
53 | 63 | try:
|
54 | 64 | with warnings.catch_warnings(record=True) as w:
|
55 | 65 | warnings.simplefilter('always')
|
56 |
| - c.execute( |
| 66 | + yield c.execute( |
57 | 67 | ("LOAD DATA LOCAL INFILE '{0}' INTO TABLE " +
|
58 | 68 | "test_load_local FIELDS TERMINATED BY ','").format(filename)
|
59 | 69 | )
|
60 | 70 | self.assertEqual(w[0].category, Warning)
|
61 | 71 | self.assertTrue("Incorrect integer value" in str(w[-1].message))
|
62 | 72 | finally:
|
63 |
| - c.execute("DROP TABLE test_load_local") |
| 73 | + yield c.execute("DROP TABLE IF EXISTS test_load_local") |
64 | 74 |
|
65 | 75 |
|
66 | 76 | if __name__ == "__main__":
|
|
0 commit comments