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
|
import io
import os
import sys
import unittest
from textwrap import dedent
from unittest.mock import patch
from sqlalchemy.exc import IntegrityError, OperationalError
from csvkit.utilities.csvsql import CSVSQL, launch_new_instance
from csvkit.utilities.sql2csv import SQL2CSV
from tests.utils import CSVKitTestCase, EmptyFileTests, stdin_as_string
class TestCSVSQL(CSVKitTestCase, EmptyFileTests):
Utility = CSVSQL
def test_launch_new_instance(self):
with patch.object(sys, 'argv', [self.Utility.__name__.lower(), 'examples/dummy.csv']):
launch_new_instance()
def test_options(self):
for args, message in (
(
['--db', 'sqlite:///:memory:', '--dialect', 'sqlite'],
'The --dialect option is only valid when neither --db nor --query are specified.',
),
(
['--insert'],
'The --insert option is only valid when either --db or --query is specified.',
),
(
['--db', 'sqlite:///:memory:', '--insert', '--no-create', '--overwrite'],
'The --overwrite option is only valid if --no-create is not specified.',
),
(
['--db', 'sqlite:///:memory:', '--insert', '--no-create', '--create-if-not-exists'],
'The --no-create and --create-if-not-exists options are mutually exclusive.',
),
):
with self.subTest(args=args):
self.assertError(launch_new_instance, args, message)
def test_insert_options(self):
for args in (
['--no-create'],
['--create-if-not-exists'],
['--overwrite'],
['--before-insert'],
['--after-insert'],
['--chunk-size', '1'],
):
with self.subTest(args=args):
self.assertError(
launch_new_instance,
args,
f'The {args[0]} option is only valid if --insert is also specified.'
)
def setUp(self):
self.db_file = 'foo.db'
def tearDown(self):
if os.path.exists(self.db_file):
os.remove(self.db_file)
@unittest.skipIf(os.name != 'nt', 'Windows only')
def test_glob(self):
sql = self.get_output(['examples/dummy?.csv'])
self.assertEqual(sql.replace('\t', ' '), dedent('''\
CREATE TABLE dummy2 (
a BOOLEAN NOT NULL,
b DECIMAL NOT NULL,
c DECIMAL NOT NULL
);
CREATE TABLE dummy3 (
a BOOLEAN NOT NULL,
b DECIMAL NOT NULL,
c DECIMAL NOT NULL
);
''')) # noqa: W291
def test_create_table(self):
sql = self.get_output(['--tables', 'foo', 'examples/testfixed_converted.csv'])
self.assertEqual(sql.replace('\t', ' '), dedent('''\
CREATE TABLE foo (
text VARCHAR NOT NULL,
date DATE,
integer DECIMAL,
boolean BOOLEAN,
float DECIMAL,
time DATETIME,
datetime TIMESTAMP,
empty_column BOOLEAN
);
''')) # noqa: W291
def test_no_blanks(self):
sql = self.get_output(['--tables', 'foo', 'examples/blanks.csv'])
self.assertEqual(sql.replace('\t', ' '), dedent('''\
CREATE TABLE foo (
a BOOLEAN,
b BOOLEAN,
c BOOLEAN,
d BOOLEAN,
e BOOLEAN,
f BOOLEAN
);
''')) # noqa: W291
def test_blanks(self):
sql = self.get_output(['--tables', 'foo', '--blanks', 'examples/blanks.csv'])
self.assertEqual(sql.replace('\t', ' '), dedent('''\
CREATE TABLE foo (
a VARCHAR NOT NULL,
b VARCHAR NOT NULL,
c VARCHAR NOT NULL,
d VARCHAR NOT NULL,
e VARCHAR NOT NULL,
f VARCHAR NOT NULL
);
''')) # noqa: W291
def test_no_inference(self):
sql = self.get_output(['--tables', 'foo', '--no-inference', 'examples/testfixed_converted.csv'])
self.assertEqual(sql.replace('\t', ' '), dedent('''\
CREATE TABLE foo (
text VARCHAR NOT NULL,
date VARCHAR,
integer VARCHAR,
boolean VARCHAR,
float VARCHAR,
time VARCHAR,
datetime VARCHAR,
empty_column VARCHAR
);
''')) # noqa: W291
def test_no_header_row(self):
sql = self.get_output(['--tables', 'foo', '--no-header-row', 'examples/no_header_row.csv'])
self.assertEqual(sql.replace('\t', ' '), dedent('''\
CREATE TABLE foo (
a BOOLEAN NOT NULL,
b DECIMAL NOT NULL,
c DECIMAL NOT NULL
);
''')) # noqa: W291
def test_linenumbers(self):
sql = self.get_output(['--tables', 'foo', '--linenumbers', 'examples/dummy.csv'])
self.assertEqual(sql.replace('\t', ' '), dedent('''\
CREATE TABLE foo (
a BOOLEAN NOT NULL,
b DECIMAL NOT NULL,
c DECIMAL NOT NULL
);
''')) # noqa: W291
def test_stdin(self):
input_file = io.BytesIO(b'a,b,c\n4,2,3\n')
with stdin_as_string(input_file):
sql = self.get_output(['--tables', 'foo'])
self.assertEqual(sql.replace('\t', ' '), dedent('''\
CREATE TABLE foo (
a DECIMAL NOT NULL,
b DECIMAL NOT NULL,
c DECIMAL NOT NULL
);
''')) # noqa: W291
input_file.close()
def test_stdin_and_filename(self):
input_file = io.BytesIO(b'a,b,c\n1,2,3\n')
with stdin_as_string(input_file):
sql = self.get_output(['-', 'examples/dummy.csv'])
self.assertTrue('CREATE TABLE stdin' in sql)
self.assertTrue('CREATE TABLE dummy' in sql)
input_file.close()
def test_query(self):
input_file = io.BytesIO(b'a,b,c\n1,2,3\n')
with stdin_as_string(input_file):
sql = self.get_output(['--query', 'SELECT m.usda_id, avg(i.sepal_length) AS mean_sepal_length FROM iris '
'AS i JOIN irismeta AS m ON (i.species = m.species) GROUP BY m.species',
'examples/iris.csv', 'examples/irismeta.csv'])
self.assertTrue('usda_id,mean_sepal_length' in sql)
self.assertTrue('IRSE,5.00' in sql)
self.assertTrue('IRVE2,5.936' in sql)
self.assertTrue('IRVI,6.58' in sql)
input_file.close()
def test_query_empty(self):
input_file = io.BytesIO()
with stdin_as_string(input_file):
output = self.get_output(['--query', 'SELECT 1'])
self.assertEqual(output, '1\n1\n')
input_file.close()
def test_query_text(self):
sql = self.get_output(['--query', 'SELECT text FROM testfixed_converted WHERE text LIKE "Chicago%"',
'examples/testfixed_converted.csv'])
self.assertEqual(sql,
"text\n"
"Chicago Reader\n"
"Chicago Sun-Times\n"
"Chicago Tribune\n")
def test_query_file(self):
sql = self.get_output(['--query', 'examples/test_query.sql', 'examples/testfixed_converted.csv'])
self.assertEqual(sql,
"question,text\n"
"36,©\n")
def test_query_update(self):
sql = self.get_output(['--query', 'UPDATE dummy SET a=10 WHERE a=1', '--no-inference', 'examples/dummy.csv'])
self.assertEqual(sql, '')
def test_before_after_insert(self):
self.get_output(['--db', 'sqlite:///' + self.db_file, '--insert', 'examples/dummy.csv', '--before-insert',
'SELECT 1; CREATE TABLE foobar (date DATE)', '--after-insert',
'INSERT INTO dummy VALUES (0, 5, 6)'])
output_file = io.StringIO()
utility = SQL2CSV(['--db', 'sqlite:///' + self.db_file, '--query', 'SELECT * FROM foobar'], output_file)
utility.run()
output = output_file.getvalue()
output_file.close()
self.assertEqual(output, 'date\n')
output_file = io.StringIO()
utility = SQL2CSV(['--db', 'sqlite:///' + self.db_file, '--query', 'SELECT * FROM dummy'], output_file)
utility.run()
output = output_file.getvalue()
output_file.close()
self.assertEqual(output, 'a,b,c\n1,2.0,3.0\n0,5.0,6.0\n')
def test_no_prefix_unique_constraint(self):
self.get_output(['--db', 'sqlite:///' + self.db_file, '--insert',
'examples/dummy.csv', '--unique-constraint', 'a'])
with self.assertRaises(IntegrityError):
self.get_output(['--db', 'sqlite:///' + self.db_file, '--insert', 'examples/dummy.csv', '--no-create'])
def test_prefix_unique_constraint(self):
self.get_output(['--db', 'sqlite:///' + self.db_file, '--insert',
'examples/dummy.csv', '--unique-constraint', 'a'])
self.get_output(['--db', 'sqlite:///' + self.db_file, '--insert',
'examples/dummy.csv', '--no-create', '--prefix', 'OR IGNORE'])
def test_no_create_if_not_exists(self):
self.get_output(['--db', 'sqlite:///' + self.db_file, '--insert', '--tables', 'foo', 'examples/foo1.csv'])
with self.assertRaises(OperationalError):
self.get_output(['--db', 'sqlite:///' + self.db_file, '--insert', '--tables', 'foo', 'examples/foo2.csv'])
def test_create_if_not_exists(self):
self.get_output(['--db', 'sqlite:///' + self.db_file, '--insert', '--tables', 'foo', 'examples/foo1.csv'])
self.get_output(['--db', 'sqlite:///' + self.db_file, '--insert', '--tables',
'foo', 'examples/foo2.csv', '--create-if-not-exists'])
|