forked from TryGhost/node-sqlite3
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathblob.test.js
54 lines (45 loc) · 1.48 KB
/
blob.test.js
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
var sqlite3 = require('..'),
fs = require('fs'),
assert = require('assert'),
Buffer = require('buffer').Buffer;
// lots of elmo
var elmo = fs.readFileSync(__dirname + '/support/elmo.png');
describe('blob', function() {
var db;
before(function(done) {
db = new sqlite3.Database(':memory:');
db.run("CREATE TABLE elmos (id INT, image BLOB)", done);
});
var total = 10;
var inserted = 0;
var retrieved = 0;
it('should insert blobs', function(done) {
for (var i = 0; i < total; i++) {
db.run('INSERT INTO elmos (id, image) VALUES (?, ?)', i, elmo, function(err) {
if (err) throw err;
inserted++;
});
}
db.wait(function() {
assert.equal(inserted, total);
done();
});
});
it('should retrieve the blobs', function(done) {
db.all('SELECT id, image FROM elmos ORDER BY id', function(err, rows) {
if (err) throw err;
for (var i = 0; i < rows.length; i++) {
assert.ok(Buffer.isBuffer(rows[i].image));
assert.ok(elmo.length, rows[i].image);
for (var j = 0; j < elmo.length; j++) {
if (elmo[j] !== rows[i].image[j]) {
assert.ok(false, "Wrong byte");
}
}
retrieved++;
}
assert.equal(retrieved, total);
done();
});
});
});