@@ -364,7 +364,7 @@ def test_to_sql_append(self):
364
364
def test_to_sql_series (self ):
365
365
s = Series (np .arange (5 , dtype = 'int64' ), name = 'series' )
366
366
sql .to_sql (s , "test_series" , self .conn , flavor = 'sqlite' , index = False )
367
- s2 = sql .read_sql ("SELECT * FROM test_series" , self .conn ,
367
+ s2 = sql .read_sql ("SELECT * FROM test_series" , self .conn ,
368
368
flavor = 'sqlite' )
369
369
tm .assert_frame_equal (s .to_frame (), s2 )
370
370
@@ -473,7 +473,7 @@ def connect(self):
473
473
474
474
def test_to_sql_index_label (self ):
475
475
temp_frame = DataFrame ({'col1' : range (4 )})
476
-
476
+
477
477
# no index name, defaults to 'pandas_index'
478
478
sql .to_sql (temp_frame , 'test_index_label' , self .conn )
479
479
frame = sql .read_table ('test_index_label' , self .conn )
@@ -507,8 +507,52 @@ class TestSQLLegacyApi(_TestSQLApi):
507
507
"""
508
508
flavor = 'sqlite'
509
509
510
- def connect (self ):
511
- return sqlite3 .connect (':memory:' )
510
+ def connect (self , database = ":memory:" ):
511
+ return sqlite3 .connect (database )
512
+
513
+ def _load_test2_data (self ):
514
+ columns = ['index' , 'A' , 'B' ]
515
+ data = [(
516
+ '2000-01-03 00:00:00' , 2 ** 31 - 1 , - 1.987670 ),
517
+ ('2000-01-04 00:00:00' , - 29 , - 0.0412318367011 ),
518
+ ('2000-01-05 00:00:00' , 20000 , 0.731167677815 ),
519
+ ('2000-01-06 00:00:00' , - 290867 , 1.56762092543 )]
520
+
521
+ self .test_frame2 = DataFrame (data , columns = columns )
522
+
523
+ def test_sql_open_close (self ):
524
+ """
525
+ Test if the IO in the database still work if the connection
526
+ is closed between the writing and reading (as in many real
527
+ situations).
528
+ """
529
+
530
+ self ._load_test2_data ()
531
+
532
+ with tm .ensure_clean () as name :
533
+
534
+ conn = self .connect (name )
535
+
536
+ sql .to_sql (
537
+ self .test_frame2 ,
538
+ "test_frame2_legacy" ,
539
+ conn ,
540
+ flavor = "sqlite" ,
541
+ index = False ,
542
+ )
543
+
544
+ conn .close ()
545
+ conn = self .connect (name )
546
+
547
+ result = sql .read_sql (
548
+ "SELECT * FROM test_frame2_legacy;" ,
549
+ conn ,
550
+ flavor = "sqlite" ,
551
+ )
552
+
553
+ conn .close ()
554
+
555
+ tm .assert_frame_equal (self .test_frame2 , result )
512
556
513
557
514
558
class _TestSQLAlchemy (PandasSQLTest ):
@@ -601,7 +645,7 @@ def test_default_type_conversion(self):
601
645
self .assertTrue (issubclass (df .IntColWithNull .dtype .type , np .floating ),
602
646
"IntColWithNull loaded with incorrect type" )
603
647
# Bool column with NA values becomes object
604
- self .assertTrue (issubclass (df .BoolColWithNull .dtype .type , np .object ),
648
+ self .assertTrue (issubclass (df .BoolColWithNull .dtype .type , np .object ),
605
649
"BoolColWithNull loaded with incorrect type" )
606
650
607
651
def test_default_date_load (self ):
@@ -696,14 +740,14 @@ def test_default_type_conversion(self):
696
740
self .assertTrue (issubclass (df .IntColWithNull .dtype .type , np .floating ),
697
741
"IntColWithNull loaded with incorrect type" )
698
742
# Non-native Bool column with NA values stays as float
699
- self .assertTrue (issubclass (df .BoolColWithNull .dtype .type , np .floating ),
743
+ self .assertTrue (issubclass (df .BoolColWithNull .dtype .type , np .floating ),
700
744
"BoolColWithNull loaded with incorrect type" )
701
745
702
746
def test_default_date_load (self ):
703
747
df = sql .read_table ("types_test_data" , self .conn )
704
748
705
749
# IMPORTANT - sqlite has no native date type, so shouldn't parse, but
706
- self .assertFalse (issubclass (df .DateCol .dtype .type , np .datetime64 ),
750
+ self .assertFalse (issubclass (df .DateCol .dtype .type , np .datetime64 ),
707
751
"DateCol loaded with incorrect type" )
708
752
709
753
@@ -865,21 +909,21 @@ def test_default_type_conversion(self):
865
909
"FloatCol loaded with incorrect type" )
866
910
self .assertTrue (issubclass (df .IntCol .dtype .type , np .integer ),
867
911
"IntCol loaded with incorrect type" )
868
- # MySQL has no real BOOL type (it's an alias for TINYINT)
912
+ # MySQL has no real BOOL type (it's an alias for TINYINT)
869
913
self .assertTrue (issubclass (df .BoolCol .dtype .type , np .integer ),
870
914
"BoolCol loaded with incorrect type" )
871
915
872
916
# Int column with NA values stays as float
873
917
self .assertTrue (issubclass (df .IntColWithNull .dtype .type , np .floating ),
874
918
"IntColWithNull loaded with incorrect type" )
875
919
# Bool column with NA = int column with NA values => becomes float
876
- self .assertTrue (issubclass (df .BoolColWithNull .dtype .type , np .floating ),
920
+ self .assertTrue (issubclass (df .BoolColWithNull .dtype .type , np .floating ),
877
921
"BoolColWithNull loaded with incorrect type" )
878
922
879
923
880
924
class TestPostgreSQLAlchemy (_TestSQLAlchemy ):
881
925
flavor = 'postgresql'
882
-
926
+
883
927
def connect (self ):
884
928
return sqlalchemy .create_engine (
885
929
'postgresql+{driver}://postgres@localhost/pandas_nosetest' .format (driver = self .driver ))
0 commit comments