@@ -465,6 +465,155 @@ class Foo: pass
465
465
except TypeError :
466
466
pass
467
467
468
+
469
+ class BlobTests (unittest .TestCase ):
470
+ def setUp (self ):
471
+ self .cx = sqlite .connect (":memory:" )
472
+ self .cx .execute ("create table test(id integer primary key, blob_col blob)" )
473
+ self .blob_data = "a" * 100
474
+ self .cx .execute ("insert into test(blob_col) values (?)" , (self .blob_data , ))
475
+ self .blob = self .cx .blob ("test" , "blob_col" , 1 , 1 )
476
+ self .second_data = "b" * 100
477
+
478
+ def tearDown (self ):
479
+ self .blob .close ()
480
+ self .cx .close ()
481
+
482
+ def CheckLength (self ):
483
+ self .assertEqual (self .blob .length (), 100 )
484
+
485
+ def CheckTell (self ):
486
+ self .assertEqual (self .blob .tell (), 0 )
487
+
488
+ def CheckSeekFromBlobStart (self ):
489
+ self .blob .seek (10 )
490
+ self .assertEqual (self .blob .tell (), 10 )
491
+ self .blob .seek (10 , 0 )
492
+ self .assertEqual (self .blob .tell (), 10 )
493
+
494
+ def CheckSeekFromCurrentPosition (self ):
495
+ self .blob .seek (10 ,1 )
496
+ self .blob .seek (10 ,1 )
497
+ self .assertEqual (self .blob .tell (), 20 )
498
+
499
+ def CheckSeekFromBlobEnd (self ):
500
+ self .blob .seek (- 10 ,2 )
501
+ self .assertEqual (self .blob .tell (), 90 )
502
+
503
+ def CheckBlobSeekOverBlobSize (self ):
504
+ try :
505
+ self .blob .seek (1000 )
506
+ self .fail ("should have raised a ValueError" )
507
+ except ValueError :
508
+ pass
509
+ except Exception :
510
+ self .fail ("should have raised a ValueError" )
511
+
512
+ def CheckBlobSeekUnderBlobSize (self ):
513
+ try :
514
+ self .blob .seek (- 10 )
515
+ self .fail ("should have raised a ValueError" )
516
+ except ValueError :
517
+ pass
518
+ except Exception :
519
+ self .fail ("should have raised a ValueError" )
520
+
521
+ def CheckBlobRead (self ):
522
+ self .assertEqual (self .blob .read (), self .blob_data )
523
+
524
+ def CheckBlobReadSize (self ):
525
+ self .assertEqual (len (self .blob .read (10 )), 10 )
526
+
527
+ def CheckBlobReadAdvanceOffset (self ):
528
+ self .blob .read (10 )
529
+ self .assertEqual (self .blob .tell (), 10 )
530
+
531
+ def CheckBlobReadStartAtOffset (self ):
532
+ self .blob .seek (10 )
533
+ self .blob .write (self .second_data [:10 ])
534
+ self .blob .seek (10 )
535
+ self .assertEqual (self .blob .read (10 ), self .second_data [:10 ])
536
+
537
+ def CheckBlobWrite (self ):
538
+ self .blob .write (self .second_data )
539
+ self .assertEqual (str (self .cx .execute ("select blob_col from test" ).fetchone ()[0 ]), self .second_data )
540
+
541
+ def CheckBlobWriteAtOffset (self ):
542
+ self .blob .seek (50 )
543
+ self .blob .write (self .second_data [:50 ])
544
+ self .assertEqual (str (self .cx .execute ("select blob_col from test" ).fetchone ()[0 ]),
545
+ self .blob_data [:50 ] + self .second_data [:50 ])
546
+
547
+ def CheckBlobWriteAdvanceOffset (self ):
548
+ self .blob .write (self .second_data [:50 ])
549
+ self .assertEqual (self .blob .tell (), 50 )
550
+
551
+ def CheckBlobWriteMoreThenBlobSize (self ):
552
+ try :
553
+ self .blob .write ("a" * 1000 )
554
+ self .fail ("should have raised a sqlite.OperationalError" )
555
+ except sqlite .OperationalError :
556
+ pass
557
+ except Exception :
558
+ self .fail ("should have raised a sqlite.OperationalError" )
559
+
560
+ def CheckBlobReadAfterRowChange (self ):
561
+ self .cx .execute ("UPDATE test SET blob_col='aaaa' where id=1" )
562
+ try :
563
+ self .blob .read ()
564
+ self .fail ("should have raised a sqlite.OperationalError" )
565
+ except sqlite .OperationalError :
566
+ pass
567
+ except Exception :
568
+ self .fail ("should have raised a sqlite.OperationalError" )
569
+
570
+ def CheckBlobWriteAfterRowChange (self ):
571
+ self .cx .execute ("UPDATE test SET blob_col='aaaa' where id=1" )
572
+ try :
573
+ self .blob .write ("aaa" )
574
+ self .fail ("should have raised a sqlite.OperationalError" )
575
+ except sqlite .OperationalError :
576
+ pass
577
+ except Exception :
578
+ self .fail ("should have raised a sqlite.OperationalError" )
579
+
580
+ def CheckBlobOpenWithBadDb (self ):
581
+ try :
582
+ self .cx .blob ("test" , "blob_col" , 1 , 1 , dbname = "notexisintg" )
583
+ self .fail ("should have raised a sqlite.OperationalError" )
584
+ except sqlite .OperationalError :
585
+ pass
586
+ except Exception :
587
+ self .fail ("should have raised a sqlite.OperationalError" )
588
+
589
+ def CheckBlobOpenWithBadTable (self ):
590
+ try :
591
+ self .cx .blob ("notexisintg" , "blob_col" , 1 , 1 )
592
+ self .fail ("should have raised a sqlite.OperationalError" )
593
+ except sqlite .OperationalError :
594
+ pass
595
+ except Exception :
596
+ self .fail ("should have raised a sqlite.OperationalError" )
597
+
598
+ def CheckBlobOpenWithBadColumn (self ):
599
+ try :
600
+ self .cx .blob ("test" , "notexisting" , 1 , 1 )
601
+ self .fail ("should have raised a sqlite.OperationalError" )
602
+ except sqlite .OperationalError :
603
+ pass
604
+ except Exception :
605
+ self .fail ("should have raised a sqlite.OperationalError" )
606
+
607
+ def CheckBlobOpenWithBadRow (self ):
608
+ try :
609
+ self .cx .blob ("test" , "blob_col" , 2 , 1 )
610
+ self .fail ("should have raised a sqlite.OperationalError" )
611
+ except sqlite .OperationalError :
612
+ pass
613
+ except Exception :
614
+ self .fail ("should have raised a sqlite.OperationalError" )
615
+
616
+
468
617
class ThreadTests (unittest .TestCase ):
469
618
def setUp (self ):
470
619
self .con = sqlite .connect (":memory:" )
@@ -763,6 +912,20 @@ def CheckClosedCurExecute(self):
763
912
except :
764
913
self .fail ("Should have raised a ProgrammingError" )
765
914
915
+ def CheckClosedBlobRead (self ):
916
+ con = sqlite .connect (":memory:" )
917
+ con .execute ("create table test(id integer primary key, blob_col blob)" )
918
+ con .execute ("insert into test(blob_col) values (zeroblob(100))" )
919
+ blob = con .blob ("test" , "blob_col" , 1 )
920
+ con .close ()
921
+ try :
922
+ blob .read ()
923
+ self .fail ("Should have raised a ProgrammingError" )
924
+ except sqlite .ProgrammingError :
925
+ pass
926
+ except :
927
+ self .fail ("Should have raised a ProgrammingError" )
928
+
766
929
def CheckClosedCreateFunction (self ):
767
930
con = sqlite .connect (":memory:" )
768
931
con .close ()
@@ -859,16 +1022,115 @@ def CheckClosed(self):
859
1022
except :
860
1023
self .fail ("Should have raised a ProgrammingError: " + method_name )
861
1024
1025
+
1026
+ class ClosedBlobTests (unittest .TestCase ):
1027
+ def setUp (self ):
1028
+ self .cx = sqlite .connect (":memory:" )
1029
+ self .cx .execute ("create table test(id integer primary key, blob_col blob)" )
1030
+ self .cx .execute ("insert into test(blob_col) values (zeroblob(100))" )
1031
+
1032
+ def tearDown (self ):
1033
+ self .cx .close ()
1034
+
1035
+ def CheckClosedRead (self ):
1036
+ self .blob = self .cx .blob ("test" , "blob_col" , 1 )
1037
+ self .blob .close ()
1038
+ try :
1039
+ self .blob .read ()
1040
+ self .fail ("Should have raised a ProgrammingError" )
1041
+ except sqlite .ProgrammingError :
1042
+ pass
1043
+ except Exception :
1044
+ self .fail ("Should have raised a ProgrammingError" )
1045
+
1046
+ def CheckClosedWrite (self ):
1047
+ self .blob = self .cx .blob ("test" , "blob_col" , 1 )
1048
+ self .blob .close ()
1049
+ try :
1050
+ self .blob .write ("aaaaaaaaa" )
1051
+ self .fail ("Should have raised a ProgrammingError" )
1052
+ except sqlite .ProgrammingError :
1053
+ pass
1054
+ except Exception :
1055
+ self .fail ("Should have raised a ProgrammingError" )
1056
+
1057
+ def CheckClosedSeek (self ):
1058
+ self .blob = self .cx .blob ("test" , "blob_col" , 1 )
1059
+ self .blob .close ()
1060
+ try :
1061
+ self .blob .seek (10 )
1062
+ self .fail ("Should have raised a ProgrammingError" )
1063
+ except sqlite .ProgrammingError :
1064
+ pass
1065
+ except Exception :
1066
+ self .fail ("Should have raised a ProgrammingError" )
1067
+
1068
+ def CheckClosedTell (self ):
1069
+ self .blob = self .cx .blob ("test" , "blob_col" , 1 )
1070
+ self .blob .close ()
1071
+ try :
1072
+ self .blob .tell ()
1073
+ self .fail ("Should have raised a ProgrammingError" )
1074
+ except sqlite .ProgrammingError :
1075
+ pass
1076
+ except Exception :
1077
+ self .fail ("Should have raised a ProgrammingError" )
1078
+
1079
+ def CheckClosedClose (self ):
1080
+ self .blob = self .cx .blob ("test" , "blob_col" , 1 )
1081
+ self .blob .close ()
1082
+ try :
1083
+ self .blob .close ()
1084
+ self .fail ("Should have raised a ProgrammingError" )
1085
+ except sqlite .ProgrammingError :
1086
+ pass
1087
+ except Exception :
1088
+ self .fail ("Should have raised a ProgrammingError" )
1089
+
1090
+
1091
+ class BlobContextManagerTests (unittest .TestCase ):
1092
+ def setUp (self ):
1093
+ self .cx = sqlite .connect (":memory:" )
1094
+ self .cx .execute ("create table test(id integer primary key, blob_col blob)" )
1095
+ self .cx .execute ("insert into test(blob_col) values (zeroblob(100))" )
1096
+
1097
+ def tearDown (self ):
1098
+ self .cx .close ()
1099
+
1100
+ def CheckContextExecute (self ):
1101
+ data = "a" * 100
1102
+ with self .cx .blob ("test" , "blob_col" , 1 , 1 ) as blob :
1103
+ blob .write ("a" * 100 )
1104
+ self .assertEqual (str (self .cx .execute ("select blob_col from test" ).fetchone ()[0 ]), data )
1105
+
1106
+ def CheckContextCloseBlob (self ):
1107
+ with self .cx .blob ("test" , "blob_col" , 1 ) as blob :
1108
+ blob .seek (10 )
1109
+ try :
1110
+ blob .close ()
1111
+ self .fail ("Should have raised a ProgrammingError" )
1112
+ except sqlite .ProgrammingError :
1113
+ pass
1114
+ except Exception :
1115
+ self .fail ("Should have raised a ProgrammingError" )
1116
+
1117
+
1118
+
862
1119
def suite ():
863
1120
module_suite = unittest .makeSuite (ModuleTests , "Check" )
864
1121
connection_suite = unittest .makeSuite (ConnectionTests , "Check" )
865
1122
cursor_suite = unittest .makeSuite (CursorTests , "Check" )
1123
+ blob_suite = unittest .makeSuite (BlobTests , "Check" )
866
1124
thread_suite = unittest .makeSuite (ThreadTests , "Check" )
867
1125
constructor_suite = unittest .makeSuite (ConstructorTests , "Check" )
868
1126
ext_suite = unittest .makeSuite (ExtensionTests , "Check" )
869
1127
closed_con_suite = unittest .makeSuite (ClosedConTests , "Check" )
870
1128
closed_cur_suite = unittest .makeSuite (ClosedCurTests , "Check" )
871
- return unittest .TestSuite ((module_suite , connection_suite , cursor_suite , thread_suite , constructor_suite , ext_suite , closed_con_suite , closed_cur_suite ))
1129
+ closed_blob_suite = unittest .makeSuite (ClosedBlobTests , "Check" )
1130
+ blob_context_manager_suite = unittest .makeSuite (BlobContextManagerTests , "Check" )
1131
+ return unittest .TestSuite ((module_suite , connection_suite , cursor_suite , blob_suite , thread_suite ,
1132
+ constructor_suite , ext_suite , closed_con_suite , closed_cur_suite , closed_blob_suite ,
1133
+ blob_context_manager_suite , context_suite ))
872
1134
873
1135
def test ():
874
1136
runner = unittest .TextTestRunner ()
0 commit comments