V8 Coverage Report
Files covered Lines
. / test/open_close.test.js
93.61 %
176 / 188
    1.      1var sqlite3 = require('..');
    2.      1var assert = require('assert');
    3.      1var fs = require('fs');
    4.      1var helper = require('./support/helper');
    5.      1
    6.      1describe('open/close', function() {
    7.      1    before(function() {
    8.      1        helper.ensureExists('test/tmp');
    9.      1    });
   10.      1
   11.      1    describe('open and close non-existant database', function() {
   12.      1        before(function() {
   13.      1            helper.deleteFile('test/tmp/test_create.db');
   14.      1        });
   15.      1
   16.      1        var db;
   17.      1        it('should open the database', function(done) {
   18.      1            db = new sqlite3.Database('test/tmp/test_create.db', done);
   19.      1        });
   20.      1
   21.      1        it('should close the database', function(done) {
   22.      1            db.close(done);
   23.      1        });
   24.      1
   25.      1        it('should have created the file', function() {
   26.      1            assert.fileExists('test/tmp/test_create.db');
   27.      1        });
   28.      1
   29.      1        after(function() {
   30.      1            helper.deleteFile('test/tmp/test_create.db');
   31.      1        });
   32.      1    });
   33.      1    
   34.      1    describe('open and close non-existant shared database', function() {
   35.      1        before(function() {
   36.      1            helper.deleteFile('test/tmp/test_create_shared.db');
   37.      1        });
   38.      1
   39.      1        var db;
   40.      1        it('should open the database', function(done) {
   41.      1            db = new sqlite3.Database('file:./test/tmp/test_create_shared.db', sqlite3.OPEN_URI | sqlite3.OPEN_SHAREDCACHE | sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE, done);
   42.      1        });
   43.      1
   44.      1        it('should close the database', function(done) {
   45.      1            db.close(done);
   46.      1        });
   47.      1
   48.      1        it('should have created the file', function() {
   49.      1            assert.fileExists('test/tmp/test_create_shared.db');
   50.      1        });
   51.      1
   52.      1        after(function() {
   53.      1            helper.deleteFile('test/tmp/test_create_shared.db');
   54.      1        });
   55.      1    });
   56.      1
   57.      1
   58.      0    (sqlite3.VERSION_NUMBER < 3008000 ? describe.skip : describe)('open and close shared memory database', function() {
   59.      1
   60.      1        var db1;
   61.      1        var db2;
   62.      1
   63.      1        it('should open the first database', function(done) {
   64.      1            db1 = new sqlite3.Database('file:./test/tmp/test_memory.db?mode=memory', sqlite3.OPEN_URI | sqlite3.OPEN_SHAREDCACHE | sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE, done);
   65.      1        });
   66.      1
   67.      1        it('should open the second database', function(done) {
   68.      1            db2 = new sqlite3.Database('file:./test/tmp/test_memory.db?mode=memory', sqlite3.OPEN_URI | sqlite3.OPEN_SHAREDCACHE | sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE, done);
   69.      1        });
   70.      1
   71.      1        it('first database should set the user_version', function(done) {
   72.      1            db1.exec('PRAGMA user_version=42', done);
   73.      1        });
   74.      1
   75.      1        it('second database should get the user_version', function(done) {
   76.      1            db2.get('PRAGMA user_version', function(err, row) {
   77.      0                if (err) throw err;
   78.      1                assert.equal(row.user_version, 42);
   79.      1                done();
   80.      1            });
   81.      1        });
   82.      1
   83.      1        it('should close the first database', function(done) {
   84.      1            db1.close(done);
   85.      1        });
   86.      1
   87.      1        it('should close the second database', function(done) {
   88.      1            db2.close(done);
   89.      1        });
   90.      1    });
   91.      1
   92.      1    it('should not be unable to open an inaccessible database', function(done) {
   93.      1        // NOTE: test assumes that the user is not allowed to create new files
   94.      1        // in /usr/bin.
   95.      1        var db = new sqlite3.Database('/test/tmp/directory-does-not-exist/test.db', function(err) {
   96.      1            if (err && err.errno === sqlite3.CANTOPEN) {
   97.      1                done();
   98.      0            } else if (err) {
   99.      0                done(err);
  100.      0            } else {
  101.      0                done('Opened database that should be inaccessible');
  102.      0            }
  103.      1        });
  104.      1    });
  105.      1
  106.      1
  107.      1    describe('creating database without create flag', function() {
  108.      1        before(function() {
  109.      1            helper.deleteFile('test/tmp/test_readonly.db');
  110.      1        });
  111.      1
  112.      1        it('should fail to open the database', function(done) {
  113.      1            new sqlite3.Database('tmp/test_readonly.db', sqlite3.OPEN_READONLY, function(err) {
  114.      1                if (err && err.errno === sqlite3.CANTOPEN) {
  115.      1                    done();
  116.      0                } else if (err) {
  117.      0                    done(err);
  118.      0                } else {
  119.      0                    done('Created database without create flag');
  120.      0                }
  121.      1            });
  122.      1        });
  123.      1
  124.      1        it('should not have created the file', function() {
  125.      1            assert.fileDoesNotExist('test/tmp/test_readonly.db');
  126.      1        });
  127.      1
  128.      1        after(function() {
  129.      1            helper.deleteFile('test/tmp/test_readonly.db');
  130.      1        });
  131.      1    });
  132.      1
  133.      1    describe('open and close memory database queuing', function() {
  134.      1        var db;
  135.      1        it('should open the database', function(done) {
  136.      1            db = new sqlite3.Database(':memory:', done);
  137.      1        });
  138.      1
  139.      1        it('should close the database', function(done) {
  140.      1            db.close(done);
  141.      1        });
  142.      1
  143.      1        it('shouldn\'t close the database again', function(done) {
  144.      1            db.close(function(err) {
  145.      1                assert.ok(err, 'No error object received on second close');
  146.      1                assert.ok(err.errno === sqlite3.MISUSE);
  147.      1                done();
  148.      1            });
  149.      1        });
  150.      1    });
  151.      1
  152.      1    describe('closing with unfinalized statements', function(done) {
  153.      1        var completed = false;
  154.      1        var completedSecond = false;
  155.      1        var closed = false;
  156.      1
  157.      1        var db;
  158.      1        before(function() {
  159.      1            db = new sqlite3.Database(':memory:', done);
  160.      1        });
  161.      1
  162.      1        it('should create a table', function(done) {
  163.      1            db.run("CREATE TABLE foo (id INT, num INT)", done);
  164.      1        });
  165.      1
  166.      1        var stmt;
  167.      1        it('should prepare/run a statement', function(done) {
  168.      1            stmt = db.prepare('INSERT INTO foo VALUES (?, ?)');
  169.      1            stmt.run(1, 2, done);
  170.      1        });
  171.      1
  172.      1        it('should fail to close the database', function(done) {
  173.      1            db.close(function(err) {
  174.      1                assert.ok(err.message,
  175.      1                    "SQLITE_BUSY: unable to close due to unfinalised statements");
  176.      1                done();
  177.      1            });
  178.      1        });
  179.      1
  180.      1        it('should succeed to close the database after finalizing', function(done) {
  181.      1            stmt.run(3, 4, function() {
  182.      1                stmt.finalize();
  183.      1                db.close(done);
  184.      1            });
  185.      1        });
  186.      1    });
  187.      1});
  188.      1