24
24
time ,
25
25
)
26
26
from io import StringIO
27
+ from pathlib import Path
27
28
import sqlite3
28
29
import warnings
29
30
72
73
SQLALCHEMY_INSTALLED = False
73
74
74
75
SQL_STRINGS = {
75
- "create_iris" : {
76
- "sqlite" : """CREATE TABLE iris (
77
- "SepalLength" REAL,
78
- "SepalWidth" REAL,
79
- "PetalLength" REAL,
80
- "PetalWidth" REAL,
81
- "Name" TEXT
82
- )""" ,
83
- "mysql" : """CREATE TABLE iris (
84
- `SepalLength` DOUBLE,
85
- `SepalWidth` DOUBLE,
86
- `PetalLength` DOUBLE,
87
- `PetalWidth` DOUBLE,
88
- `Name` VARCHAR(200)
89
- )""" ,
90
- "postgresql" : """CREATE TABLE iris (
91
- "SepalLength" DOUBLE PRECISION,
92
- "SepalWidth" DOUBLE PRECISION,
93
- "PetalLength" DOUBLE PRECISION,
94
- "PetalWidth" DOUBLE PRECISION,
95
- "Name" VARCHAR(200)
96
- )""" ,
97
- },
98
- "insert_iris" : {
99
- "sqlite" : """INSERT INTO iris VALUES(?, ?, ?, ?, ?)""" ,
100
- "mysql" : """INSERT INTO iris VALUES(%s, %s, %s, %s, "%s");""" ,
101
- "postgresql" : """INSERT INTO iris VALUES(%s, %s, %s, %s, %s);""" ,
102
- },
103
76
"create_test_types" : {
104
77
"sqlite" : """CREATE TABLE types_test_data (
105
78
"TextCol" TEXT,
192
165
},
193
166
"read_parameters" : {
194
167
"sqlite" : "SELECT * FROM iris WHERE Name=? AND SepalLength=?" ,
195
- "mysql" : ' SELECT * FROM iris WHERE `Name`="%s" AND `SepalLength`=%s' ,
168
+ "mysql" : " SELECT * FROM iris WHERE `Name`=%s AND `SepalLength`=%s" ,
196
169
"postgresql" : 'SELECT * FROM iris WHERE "Name"=%s AND "SepalLength"=%s' ,
197
170
},
198
171
"read_named_parameters" : {
201
174
""" ,
202
175
"mysql" : """
203
176
SELECT * FROM iris WHERE
204
- `Name`=" %(name)s" AND `SepalLength`=%(length)s
177
+ `Name`=%(name)s AND `SepalLength`=%(length)s
205
178
""" ,
206
179
"postgresql" : """
207
180
SELECT * FROM iris WHERE
222
195
}
223
196
224
197
198
+ def iris_table_metadata (dialect : str ):
199
+ from sqlalchemy import (
200
+ REAL ,
201
+ Column ,
202
+ Float ,
203
+ MetaData ,
204
+ String ,
205
+ Table ,
206
+ )
207
+
208
+ dtype = Float if dialect == "postgresql" else REAL
209
+ metadata = MetaData ()
210
+ iris = Table (
211
+ "iris" ,
212
+ metadata ,
213
+ Column ("SepalLength" , dtype ),
214
+ Column ("SepalWidth" , dtype ),
215
+ Column ("PetalLength" , dtype ),
216
+ Column ("PetalWidth" , dtype ),
217
+ Column ("Name" , String (200 )),
218
+ )
219
+ return iris
220
+
221
+
222
+ def create_and_load_iris_sqlite3 (conn : sqlite3 .Connection , iris_file : Path ):
223
+ cur = conn .cursor ()
224
+ stmt = """CREATE TABLE iris (
225
+ "SepalLength" REAL,
226
+ "SepalWidth" REAL,
227
+ "PetalLength" REAL,
228
+ "PetalWidth" REAL,
229
+ "Name" TEXT
230
+ )"""
231
+ cur .execute (stmt )
232
+ with iris_file .open (newline = None ) as csvfile :
233
+ reader = csv .reader (csvfile )
234
+ next (reader )
235
+ stmt = "INSERT INTO iris VALUES(?, ?, ?, ?, ?)"
236
+ cur .executemany (stmt , reader )
237
+
238
+
239
+ def create_and_load_iris (conn , iris_file : Path , dialect : str ):
240
+ from sqlalchemy import insert
241
+ from sqlalchemy .engine import Engine
242
+
243
+ iris = iris_table_metadata (dialect )
244
+ iris .drop (conn , checkfirst = True )
245
+ iris .create (bind = conn )
246
+
247
+ with iris_file .open (newline = None ) as csvfile :
248
+ reader = csv .reader (csvfile )
249
+ header = next (reader )
250
+ params = [{key : value for key , value in zip (header , row )} for row in reader ]
251
+ stmt = insert (iris ).values (params )
252
+ if isinstance (conn , Engine ):
253
+ with conn .connect () as conn :
254
+ conn .execute (stmt )
255
+ else :
256
+ conn .execute (stmt )
257
+
258
+
259
+ @pytest .fixture
260
+ def iris_path (datapath ):
261
+ iris_path = datapath ("io" , "data" , "csv" , "iris.csv" )
262
+ return Path (iris_path )
263
+
264
+
225
265
@pytest .fixture
226
266
def test_frame1 ():
227
267
columns = ["index" , "A" , "B" , "C" , "D" ]
@@ -341,24 +381,15 @@ def _get_exec(self):
341
381
else :
342
382
return self .conn .cursor ()
343
383
344
- @pytest .fixture (params = [("io" , "data" , "csv" , "iris.csv" )])
345
- def load_iris_data (self , datapath , request ):
346
-
347
- iris_csv_file = datapath (* request .param )
348
-
384
+ @pytest .fixture
385
+ def load_iris_data (self , iris_path ):
349
386
if not hasattr (self , "conn" ):
350
387
self .setup_connect ()
351
-
352
388
self .drop_table ("iris" )
353
- self ._get_exec ().execute (SQL_STRINGS ["create_iris" ][self .flavor ])
354
-
355
- with open (iris_csv_file , newline = None ) as iris_csv :
356
- r = csv .reader (iris_csv )
357
- next (r ) # skip header row
358
- ins = SQL_STRINGS ["insert_iris" ][self .flavor ]
359
-
360
- for row in r :
361
- self ._get_exec ().execute (ins , row )
389
+ if isinstance (self .conn , sqlite3 .Connection ):
390
+ create_and_load_iris_sqlite3 (self .conn , iris_path )
391
+ else :
392
+ create_and_load_iris (self .conn , iris_path , self .flavor )
362
393
363
394
def _load_iris_view (self ):
364
395
self .drop_table ("iris_view" )
@@ -1248,21 +1279,6 @@ def test_database_uri_string(self, test_frame1):
1248
1279
with pytest .raises (ImportError , match = "pg8000" ):
1249
1280
sql .read_sql ("select * from table" , db_uri )
1250
1281
1251
- def _make_iris_table_metadata (self ):
1252
- sa = sqlalchemy
1253
- metadata = sa .MetaData ()
1254
- iris = sa .Table (
1255
- "iris" ,
1256
- metadata ,
1257
- sa .Column ("SepalLength" , sa .REAL ),
1258
- sa .Column ("SepalWidth" , sa .REAL ),
1259
- sa .Column ("PetalLength" , sa .REAL ),
1260
- sa .Column ("PetalWidth" , sa .REAL ),
1261
- sa .Column ("Name" , sa .TEXT ),
1262
- )
1263
-
1264
- return iris
1265
-
1266
1282
def test_query_by_text_obj (self ):
1267
1283
# WIP : GH10846
1268
1284
name_text = sqlalchemy .text ("select * from iris where name=:name" )
@@ -1272,7 +1288,7 @@ def test_query_by_text_obj(self):
1272
1288
1273
1289
def test_query_by_select_obj (self ):
1274
1290
# WIP : GH10846
1275
- iris = self ._make_iris_table_metadata ( )
1291
+ iris = iris_table_metadata ( self .flavor )
1276
1292
1277
1293
name_select = sqlalchemy .select ([iris ]).where (
1278
1294
iris .c .Name == sqlalchemy .bindparam ("name" )
0 commit comments