@@ -5,71 +5,44 @@ var sqlite3 = require('sqlite3'),
55 Buffer = require ( 'buffer' ) . Buffer ;
66
77// lots of elmo
8- var elmo = fs . readFileSync ( __dirname + '/support/elmo.png' , 'binary' ) ;
9- var elmo_str = elmo . toString ( 'binary' ) ;
8+ var elmo = fs . readFileSync ( __dirname + '/support/elmo.png' ) ;
109
11- exports [ 'Blob overflow test' ] = function ( beforeExit ) {
12- var db = new sqlite3 . Database ( '' ) ;
10+ exports [ 'blob test' ] = function ( beforeExit ) {
11+ var db = new sqlite3 . Database ( ':memory: ' ) ;
1312 var total = 10 ;
1413 var inserted = 0 ;
1514 var retrieved = 0 ;
1615
17- Step (
18- function ( ) {
19- var next = this ;
20- db . prepare ( 'CREATE TABLE elmos (image BLOB);' ) . run ( next ) ;
21- } ,
22- function ( ) {
23- var group = this . group ( ) ;
24- for ( var i = 0 ; i < total ; i ++ ) {
25- var next = group ( ) ;
26- db . prepare ( 'INSERT INTO elmos (image) VALUES (?)' , function ( err , statement ) {
27- assert . isUndefined ( err ) ;
28- statement . bind ( 1 , elmo , function ( ) {
29- statement . step ( function ( err ) {
30- assert . isUndefined ( err ) ;
31- inserted ++ ;
32- next ( ) ;
33- } ) ;
34- } ) ;
35- } ) ;
36- }
37- } ,
38- function ( ) {
39- var next = this ;
40- db . execute ( 'SELECT COUNT(*) as amount FROM elmos' , function ( err , rows ) {
41- assert . isUndefined ( err ) ;
42- assert . eql ( rows [ 0 ] . amount , total ) ;
43- next ( ) ;
44- } ) ;
45- } ,
46- function ( ) {
47- var next = this ;
48- db . prepare ( 'SELECT image FROM elmos;' , function ( err , statement ) {
49- assert . isUndefined ( err ) ;
50- fetch ( ) ;
16+ db . serialize ( function ( ) {
17+ db . run ( 'CREATE TABLE elmos (id INT, image BLOB)' ) ;
5118
52- function fetch ( ) {
53- statement . step ( function ( err , row ) {
54- assert . isUndefined ( err ) ;
55- if ( row ) {
56- // Not using assert.equal here because it's image data
57- // and we don't want that in the command line.
58- assert . ok ( elmo_str === row . image ) ;
59- retrieved ++ ;
60- fetch ( ) ;
61- }
62- else {
63- next ( ) ;
64- }
65- } ) ;
66- }
19+ for ( var i = 0 ; i < total ; i ++ ) {
20+ db . run ( 'INSERT INTO elmos (id, image) VALUES (?, ?)' , i , elmo , function ( err ) {
21+ if ( err ) throw err ;
22+ inserted ++ ;
6723 } ) ;
6824 }
69- ) ;
25+
26+ db . all ( 'SELECT id, image FROM elmos ORDER BY id' , function ( err , rows ) {
27+ if ( err ) throw err ;
28+ for ( var i = 0 ; i < rows . length ; i ++ ) {
29+ assert . ok ( Buffer . isBuffer ( rows [ i ] [ 1 ] ) ) ;
30+ assert . ok ( elmo . length , rows [ i ] [ 1 ] ) ;
31+
32+ for ( var j = 0 ; j < elmo . length ; j ++ ) {
33+ if ( elmo [ j ] !== rows [ i ] [ 1 ] [ j ] ) {
34+ assert . ok ( false , "Wrong byte" ) ;
35+ }
36+ }
37+
38+ retrieved ++ ;
39+ }
40+ } ) ;
41+
42+ } ) ;
7043
7144 beforeExit ( function ( ) {
72- assert . eql ( inserted , total ) ;
73- assert . eql ( retrieved , total ) ;
45+ assert . equal ( inserted , total ) ;
46+ assert . equal ( retrieved , total ) ;
7447 } )
7548}
0 commit comments