Skip to content
Browse files

Move to mocha, fix undefined and escaping issue

  • Loading branch information...
1 parent 28bce2d commit ceafa445b6c19614e88f46a491efa09146f0f3b1 @1602 1602 committed Mar 27, 2013
Showing with 46 additions and 17 deletions.
  1. +14 −0 Makefile
  2. BIN dump.rdb
  3. +18 −6 lib/sqlite3.js
  4. +3 −2 package.json
  5. +9 −0 test/init.js
  6. +0 −9 test/sqlite3.js
  7. +2 −0 test/sqlite3.test.js
View
14 Makefile
@@ -0,0 +1,14 @@
+## TESTS
+
+TESTER = ./node_modules/.bin/mocha
+OPTS = --require ./test/init.js
+TESTS = test/*.test.js
+
+test:
+ $(TESTER) $(OPTS) $(TESTS)
+test-verbose:
+ $(TESTER) $(OPTS) --reporter spec $(TESTS)
+testing:
+ $(TESTER) $(OPTS) --watch $(TESTS)
+
+.PHONY: test docs
View
BIN dump.rdb
Binary file not shown.
View
24 lib/sqlite3.js
@@ -63,7 +63,7 @@ SQLite3.prototype.save = function (model, data, callback) {
var sql = 'UPDATE ' + this.tableEscaped(model) + ' SET ' +
Object.keys(data).map(function (key) {
queryParams.push(data[key]);
- return key + ' = ?';
+ return '`' + key + '` = ?';
}).join(', ') + ' WHERE id = ' + data.id;
this.command(sql, queryParams, function (err) {
@@ -81,7 +81,7 @@ SQLite3.prototype.create = function (model, data, callback) {
questions.push('?');
return data[key];
});
- var sql = 'INSERT INTO ' + this.tableEscaped(model) + ' (' + Object.keys(data).join(',') + ') VALUES ('
+ var sql = 'INSERT INTO ' + this.tableEscaped(model) + ' (`' + Object.keys(data).join('`, `') + '`) VALUES ('
sql += questions.join(',');
sql += ')';
this.command(sql, values, function (err) {
@@ -96,7 +96,7 @@ SQLite3.prototype.updateOrCreate = function (model, data, callback) {
questions.push('?');
return data[key];
});
- var sql = 'INSERT OR REPLACE INTO ' + this.tableEscaped(model) + ' (' + Object.keys(data).join(',') + ') VALUES ('
+ var sql = 'INSERT OR REPLACE INTO ' + this.tableEscaped(model) + ' (`' + Object.keys(data).join('`, `') + '`) VALUES ('
sql += questions.join(',');
sql += ')';
this.command(sql, values, function (err) {
@@ -112,6 +112,7 @@ SQLite3.prototype.toFields = function (model, data) {
var props = this._models[model].properties;
Object.keys(data).forEach(function (key) {
if (props[key]) {
+ if (typeof data[key] === 'undefined') return;
fields.push('`' + key.replace(/\./g, '`.`') + '` = ' + this.toDatabase(props[key], data[key]));
}
}.bind(this));
@@ -232,7 +233,7 @@ SQLite3.prototype.exists = function (model, id, callback) {
var sql = 'SELECT 1 FROM ' + this.tableEscaped(model) + ' WHERE id = ' + id + ' LIMIT 1';
this.queryOne(sql, function (err, data) {
if (err) return callback(err);
- callback(null, data && data['1'] === 1);
+ callback(null, !!(data && data['1'] === 1));
});
};
@@ -342,7 +343,13 @@ SQLite3.prototype.all = function all(model, filter, callback) {
function buildOrderBy(order) {
if (typeof order === 'string') order = [order];
- return 'ORDER BY ' + order.join(', ');
+ return 'ORDER BY ' + order.map(function(o) {
+ var t = o.split(/\s+/);
+ if (t.length === 1) {
+ return '`' + o + '`';
+ }
+ return '`' + t[0] + '` ' + t[1];
+ }).join(', ');
}
function buildLimit(limit, offset) {
@@ -369,6 +376,7 @@ SQLite3.prototype.count = function count(model, callback, where) {
if (conds[key] === null) {
cs.push(keyEscaped + ' IS NULL');
} else {
+ if (conds[key] === undefined) return
cs.push(keyEscaped + ' = ?');
queryParams.push(self.toDatabase(props[key], conds[key]));
}
@@ -387,7 +395,11 @@ SQLite3.prototype.autoupdate = function (cb) {
Object.keys(this._models).forEach(function (model) {
wait += 1;
self.queryAll('PRAGMA table_info(' + self.tableEscaped(model)+')', function (err, fields) {
- self.alterTable(model, fields, done);
+ if (err) {
+ self.createTable(model);
+ } else {
+ self.alterTable(model, fields, done);
+ }
});
});
View
5 package.json
@@ -4,14 +4,15 @@
"description": "SQLite3 adapter for jugglingdb",
"main": "index.js",
"scripts": {
- "test": "nodeunit test/sqlite3.js"
+ "test": "make test"
},
"dependencies": {
"jugglingdb": "= 0.2.x",
"sqlite3": ">= 2.0.18"
},
"devDependencies": {
- "nodeunit": "latest"
+ "should": "~1.2.2",
+ "mocha": "~1.8.2"
},
"repository": {
"type": "git",
View
9 test/init.js
@@ -0,0 +1,9 @@
+var Schema = require('jugglingdb').Schema;
+
+global.getSchema = function() {
+ var db = new Schema(require('../'), {
+ database: ':memory:'
+ });
+ // db.log = function (a) { console.log(a); };
+ return db;
+};
View
9 test/sqlite3.js
@@ -1,9 +0,0 @@
-var jdb = require('jugglingdb'),
- Schema = jdb.Schema,
- test = jdb.test,
- schema = new Schema(__dirname + '/..', {
- database: ':memory:'
- });
-
-test(module.exports, schema);
-
View
2 test/sqlite3.test.js
@@ -0,0 +1,2 @@
+require('jugglingdb/test/common.batch.js');
+require('jugglingdb/test/include.test.js');

0 comments on commit ceafa44

Please sign in to comment.
Something went wrong with that request. Please try again.