Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
singlestore-labs
GitHub Repository: singlestore-labs/singlestoredb-python
Path: blob/main/singlestoredb/mysql/tests/test_load_local.py
469 views
1
# type: ignore
2
import os
3
4
from singlestoredb.mysql import cursors
5
from singlestoredb.mysql import OperationalError
6
from singlestoredb.mysql.tests import base
7
8
__all__ = ['TestLoadLocal']
9
10
11
class TestLoadLocal(base.PyMySQLTestCase):
12
13
def test_no_file(self):
14
"""Test load local infile when the file does not exist."""
15
conn = self.connect()
16
c = conn.cursor()
17
c.execute('CREATE TABLE test_load_local (a INTEGER, b INTEGER, c TEXT)')
18
try:
19
self.assertRaises(
20
OperationalError,
21
c.execute,
22
(
23
"LOAD DATA LOCAL INFILE 'no_data.txt' INTO TABLE "
24
"test_load_local fields terminated by ','"
25
),
26
)
27
finally:
28
c.execute('DROP TABLE test_load_local')
29
c.close()
30
31
def test_load_file(self):
32
"""Test load local infile with a valid file."""
33
conn = self.connect()
34
c = conn.cursor()
35
c.execute('CREATE TABLE test_load_local (a INTEGER, b INTEGER, c TEXT)')
36
filename = os.path.join(
37
os.path.dirname(os.path.realpath(__file__)), 'data', 'load_local_data.txt',
38
)
39
try:
40
c.execute(
41
f"LOAD DATA LOCAL INFILE '{filename}' INTO TABLE test_load_local "
42
"FIELDS TERMINATED BY ','",
43
)
44
c.execute('SELECT COUNT(*) FROM test_load_local')
45
self.assertEqual(22749, c.fetchone()[0])
46
finally:
47
c.execute('DROP TABLE test_load_local')
48
49
def test_unbuffered_load_file(self):
50
"""Test unbuffered load local infile with a valid file."""
51
conn = self.connect(cursorclass=cursors.SSCursor)
52
c = conn.cursor()
53
c.execute('CREATE TABLE test_load_local (a INTEGER, b INTEGER, c TEXT)')
54
filename = os.path.join(
55
os.path.dirname(os.path.realpath(__file__)), 'data', 'load_local_data.txt',
56
)
57
try:
58
c.execute(
59
f"LOAD DATA LOCAL INFILE '{filename}' INTO TABLE test_load_local "
60
"FIELDS TERMINATED BY ','",
61
)
62
c.execute('SELECT COUNT(*) FROM test_load_local')
63
self.assertEqual(22749, c.fetchone()[0])
64
finally:
65
c.close()
66
conn.close()
67
conn.connect()
68
c = conn.cursor()
69
c.execute('DROP TABLE test_load_local')
70
71
72
if __name__ == '__main__':
73
import unittest
74
75
unittest.main()
76
77