1 | from pymysql import cursors, OperationalError, Warning |
---|
2 | from pymysql.tests import base |
---|
3 | |
---|
4 | import os |
---|
5 | import warnings |
---|
6 | |
---|
7 | __all__ = ["TestLoadLocal"] |
---|
8 | |
---|
9 | |
---|
10 | class TestLoadLocal(base.PyMySQLTestCase): |
---|
11 | def test_no_file(self): |
---|
12 | """Test load local infile when the file does not exist""" |
---|
13 | conn = self.connections[0] |
---|
14 | c = conn.cursor() |
---|
15 | c.execute("CREATE TABLE test_load_local (a INTEGER, b INTEGER)") |
---|
16 | 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 | ) |
---|
23 | finally: |
---|
24 | c.execute("DROP TABLE test_load_local") |
---|
25 | c.close() |
---|
26 | |
---|
27 | def test_load_file(self): |
---|
28 | """Test load local infile with a valid file""" |
---|
29 | conn = self.connections[0] |
---|
30 | c = conn.cursor() |
---|
31 | c.execute("CREATE TABLE test_load_local (a INTEGER, b INTEGER)") |
---|
32 | filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), |
---|
33 | 'data', |
---|
34 | 'load_local_data.txt') |
---|
35 | try: |
---|
36 | c.execute( |
---|
37 | ("LOAD DATA LOCAL INFILE '{0}' INTO TABLE " + |
---|
38 | "test_load_local FIELDS TERMINATED BY ','").format(filename) |
---|
39 | ) |
---|
40 | c.execute("SELECT COUNT(*) FROM test_load_local") |
---|
41 | self.assertEqual(22749, c.fetchone()[0]) |
---|
42 | finally: |
---|
43 | c.execute("DROP TABLE test_load_local") |
---|
44 | |
---|
45 | def test_unbuffered_load_file(self): |
---|
46 | """Test unbuffered load local infile with a valid file""" |
---|
47 | conn = self.connections[0] |
---|
48 | c = conn.cursor(cursors.SSCursor) |
---|
49 | c.execute("CREATE TABLE test_load_local (a INTEGER, b INTEGER)") |
---|
50 | filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), |
---|
51 | 'data', |
---|
52 | 'load_local_data.txt') |
---|
53 | try: |
---|
54 | c.execute( |
---|
55 | ("LOAD DATA LOCAL INFILE '{0}' INTO TABLE " + |
---|
56 | "test_load_local FIELDS TERMINATED BY ','").format(filename) |
---|
57 | ) |
---|
58 | c.execute("SELECT COUNT(*) FROM test_load_local") |
---|
59 | self.assertEqual(22749, c.fetchone()[0]) |
---|
60 | finally: |
---|
61 | c.close() |
---|
62 | conn.close() |
---|
63 | conn.connect() |
---|
64 | c = conn.cursor() |
---|
65 | c.execute("DROP TABLE test_load_local") |
---|
66 | |
---|
67 | def test_load_warnings(self): |
---|
68 | """Test load local infile produces the appropriate warnings""" |
---|
69 | conn = self.connections[0] |
---|
70 | c = conn.cursor() |
---|
71 | c.execute("CREATE TABLE test_load_local (a INTEGER, b INTEGER)") |
---|
72 | filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), |
---|
73 | 'data', |
---|
74 | 'load_local_warn_data.txt') |
---|
75 | try: |
---|
76 | with warnings.catch_warnings(record=True) as w: |
---|
77 | warnings.simplefilter('always') |
---|
78 | c.execute( |
---|
79 | ("LOAD DATA LOCAL INFILE '{0}' INTO TABLE " + |
---|
80 | "test_load_local FIELDS TERMINATED BY ','").format(filename) |
---|
81 | ) |
---|
82 | self.assertEqual(w[0].category, Warning) |
---|
83 | expected_message = "Incorrect integer value" |
---|
84 | if expected_message not in str(w[-1].message): |
---|
85 | self.fail("%r not in %r" % (expected_message, w[-1].message)) |
---|
86 | finally: |
---|
87 | c.execute("DROP TABLE test_load_local") |
---|
88 | c.close() |
---|
89 | |
---|
90 | |
---|
91 | if __name__ == "__main__": |
---|
92 | import unittest |
---|
93 | unittest.main() |
---|