-
-
Notifications
You must be signed in to change notification settings - Fork 847
Expand file tree
/
Copy pathblob.test.js
More file actions
68 lines (58 loc) · 2.03 KB
/
blob.test.js
File metadata and controls
68 lines (58 loc) · 2.03 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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();
});
});
it('should be able to select empty blobs', function(done) {
const empty = new sqlite3.Database(':memory:');
empty.serialize(function() {
empty.run("CREATE TABLE files (id INTEGER PRIMARY KEY, data BLOB)");
empty.run("INSERT INTO files (data) VALUES (X'')");
});
empty.get("SELECT data FROM files LIMIT 1", (err, row) => {
if (err) throw err;
assert.ok(Buffer.isBuffer(row.data));
assert.equal(row.data.length, 0);
empty.close(done);
});
})
});