|
| 1 | +'use strict'; |
| 2 | +const Database = require('../.'); |
| 3 | + |
| 4 | +describe('Database#unsafeMode()', function () { |
| 5 | + beforeEach(function () { |
| 6 | + this.db = new Database(util.next()); |
| 7 | + this.db.exec('create table foo (x)'); |
| 8 | + this.read = this.db.prepare('select 5'); |
| 9 | + this.write = this.db.prepare('insert into foo values (0)'); |
| 10 | + }); |
| 11 | + afterEach(function () { |
| 12 | + this.db.close(); |
| 13 | + }); |
| 14 | + |
| 15 | + it('should not allow unsafe operations by default', function () { |
| 16 | + let hadRow = false; |
| 17 | + for (const row of this.read.iterate()) { |
| 18 | + expect(() => this.write.run()).to.throw(TypeError); |
| 19 | + expect(() => this.db.exec('select 5')).to.throw(TypeError); |
| 20 | + expect(() => this.db.pragma('cache_size')).to.throw(TypeError); |
| 21 | + hadRow = true; |
| 22 | + } |
| 23 | + expect(hadRow).to.be.true; |
| 24 | + |
| 25 | + this.db.pragma('journal_mode = OFF'); |
| 26 | + this.db.pragma('writable_schema = ON'); |
| 27 | + expect(this.db.pragma('journal_mode', { simple: true })).to.equal('delete'); |
| 28 | + expect(() => this.db.exec("update sqlite_master set name = 'bar' where name = 'foo'")).to.throw(Database.SqliteError); |
| 29 | + }); |
| 30 | + it('should allow unsafe operations when toggled on', function () { |
| 31 | + this.db.unsafeMode(); |
| 32 | + |
| 33 | + let hadRow = false; |
| 34 | + for (const row of this.read.iterate()) { |
| 35 | + this.write.run(); |
| 36 | + this.db.exec('select 5'); |
| 37 | + this.db.pragma('cache_size'); |
| 38 | + hadRow = true; |
| 39 | + } |
| 40 | + expect(hadRow).to.be.true; |
| 41 | + |
| 42 | + this.db.pragma('journal_mode = OFF'); |
| 43 | + this.db.pragma('writable_schema = ON'); |
| 44 | + expect(this.db.pragma('journal_mode', { simple: true })).to.equal('off'); |
| 45 | + this.db.exec("update sqlite_master set name = 'bar' where name = 'foo'"); |
| 46 | + |
| 47 | + this.db.unsafeMode(false); |
| 48 | + expect(() => this.db.exec("update sqlite_master set name = 'foo' where name = 'bar'")).to.throw(Database.SqliteError); |
| 49 | + this.db.unsafeMode(true); |
| 50 | + this.db.exec("update sqlite_master set name = 'foo' where name = 'bar'"); |
| 51 | + }); |
| 52 | +}); |
0 commit comments