Skip to content

Commit

Permalink
Add tests for sqlite Dialect.clear. Closes #497
Browse files Browse the repository at this point in the history
  • Loading branch information
dxg committed May 14, 2014
1 parent 051089e commit acf603d
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 22 deletions.
25 changes: 6 additions & 19 deletions lib/Drivers/DML/sqlite.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,26 +215,13 @@ Driver.prototype.remove = function (table, conditions, cb) {
};

Driver.prototype.clear = function (table, cb) {
var query = "DELETE FROM " + this.query.escapeId(table),
debug = this.opts.debug;
var debug = this.opts.debug;

if (debug) {
require("../../Debug").sql('sqlite', query);
}
this.db.all(query, function (err) {
if (err) {
return cb(err);
}
var query = "DELETE FROM " +
this.query.escapeId("sqlite_sequence") +
" WHERE name=" +
this.query.escapeVal(table);

if (debug) {
require("../../Debug").sql('sqlite', query);
}
this.db.all(query, cb);
}.bind(this));
this.execQuery("DELETE FROM ??", [table], function (err) {
if (err) return cb(err);

this.execQuery("DELETE FROM ?? WHERE NAME = ?", ['sqlite_sequence', table], cb);
}.bind(this));
};

Driver.prototype.valueToProperty = function (value, property) {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"sqlite",
"mongodb"
],
"version" : "2.1.9",
"version" : "2.1.10",
"license" : "MIT",
"homepage" : "http://dresende.github.io/node-orm2",
"repository" : "http://github.com/dresende/node-orm2.git",
Expand All @@ -38,7 +38,7 @@
"dependencies": {
"enforce" : "0.1.2",
"sql-query" : "git://github.com/dresende/node-sql-query.git#v0.1.17",
"sql-ddl-sync" : "git://github.com/dresende/node-sql-ddl-sync.git#v0.3.3",
"sql-ddl-sync" : "git://github.com/dresende/node-sql-ddl-sync.git#v0.3.5",
"hat" : "0.0.3",
"lodash" : "2.4.1"
},
Expand Down
2 changes: 1 addition & 1 deletion test/integration/drivers/postgres_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var Driver = require('../../../lib/Drivers/DML/postgres').Driver;
var helper = require('../../support/spec_helper');
var common = require('../../common');

if (common.protocol() == "mongodb") return;
if (common.protocol() != "postgres") return;

describe("Postgres driver", function() {
describe("#propertyToValue", function () {
Expand Down
68 changes: 68 additions & 0 deletions test/integration/drivers/sqlite_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
var _ = require('lodash');
var should = require('should');
var Driver = require('../../../lib/Drivers/DML/sqlite').Driver;
var helper = require('../../support/spec_helper');
var common = require('../../common');

if (common.protocol() != "sqlite") return;

describe("Sqlite driver", function() {
var db = null;
var Person = null;

before(function (done) {
helper.connect(function (connection) {
db = connection;

Person = db.define("person", {
name: String
});

return helper.dropSync([ Person ], done);
});
});

after(function () {
return db.close();
});

describe("#clear", function () {
beforeEach(function (done) {
Person.create([{ name: 'John' }, { name: 'Jane' }], function (err) {
Person.count(function (err, count) {
should.not.exist(err);
should.equal(count, 2);
done();
});
});
});

it("should drop all items", function (done) {
Person.clear(function (err) {
should.not.exist(err);

Person.count(function (err, count) {
should.not.exist(err);
should.equal(count, 0);
done();
});
});
});

it("should reset id sequence", function (done) {
Person.clear(function (err) {
should.not.exist(err);
db.driver.execQuery("SELECT * FROM ?? WHERE ?? = ?", ['sqlite_sequence', 'name', Person.table], function (err, data) {
should.not.exist(err);

Person.create({ name: 'Bob' }, function (err, person) {
should.not.exist(err);
should.equal(person.id, 1);

done();
});
});
});
});
});
});

0 comments on commit acf603d

Please sign in to comment.