File: test_load_local.py

package info (click to toggle)
python-pymysql 1.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 836 kB
  • sloc: python: 6,473; makefile: 134; sh: 44; sql: 10
file content (64 lines) | stat: -rw-r--r-- 1,890 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
from pymysql import cursors, OperationalError
from pymysql.constants import ER
from pymysql.tests import base

import os

__all__ = ["TestLoadLocal"]


class TestLoadLocal(base.PyMySQLTestCase):
    def test_no_file(self):
        """Test load local infile when the file does not exist"""
        conn = self.connect()
        c = conn.cursor()
        c.execute("CREATE TABLE test_load_local (a INTEGER, b INTEGER)")
        try:
            self.assertRaises(
                OperationalError,
                c.execute,
                (
                    "LOAD DATA LOCAL INFILE 'no_data.txt' INTO TABLE "
                    "test_load_local fields terminated by ','"
                ),
            )
        finally:
            c.execute("DROP TABLE test_load_local")
            c.close()

    def test_load_warnings(self):
        """Test load local infile produces the appropriate warnings"""
        conn = self.connect()
        c = conn.cursor()
        c.execute("CREATE TABLE test_load_local (a INTEGER, b INTEGER)")
        filename = os.path.join(
            os.path.dirname(os.path.realpath(__file__)),
            "data",
            "load_local_warn_data.txt",
        )
        try:
            c.execute(
                (
                    "LOAD DATA LOCAL INFILE '{0}' INTO TABLE "
                    + "test_load_local FIELDS TERMINATED BY ','"
                ).format(filename)
            )
            self.assertEqual(1, c.warning_count)

            c.execute("SHOW WARNINGS")
            w = c.fetchone()

            self.assertEqual(ER.TRUNCATED_WRONG_VALUE_FOR_FIELD, w[1])
            self.assertIn(
                "incorrect integer value",
                w[2].lower(),
            )
        finally:
            c.execute("DROP TABLE test_load_local")
            c.close()


if __name__ == "__main__":
    import unittest

    unittest.main()