Browse files

add additional tests

  • Loading branch information...
1 parent 976aedb commit 6aa0636a5b1e20152387d66e9b0b4d2faa4b079b steve committed Nov 22, 2012
Showing with 64 additions and 23 deletions.
  1. +29 −9 crud.js
  2. +1 −1 drivers/test.js
  3. +3 −2 table.js
  4. +1 −1 test/find.js
  5. +27 −7 test/integration/common/blog.js
  6. +3 −3 test/join.js
View
38 crud.js
@@ -62,8 +62,7 @@ CRUD.prototype.findOne = function(options, callback) {
query += this.applyJoins(options);
query += this.applyCriteria(options, values);
- if (typeof(callback) !== 'function')
- callback = CRUD.returnOne(callback);
+ callback = CRUD.returnOne(callback);
this.driver.query(query, values, this.tableName, callback);
};
@@ -194,7 +193,7 @@ CRUD.prototype.applyJoins = function(options) {
// build a join clause
CRUD.prototype.join = function(criteria) {
- var query = ' JOIN ' + criteria.table + ' ON ',
+ var query = ' LEFT OUTER JOIN ' + criteria.table + ' ON ',
childTable = TableRegistry.getTableDefinition(criteria.table),
onClauses = [],
self = this;
@@ -249,6 +248,10 @@ CRUD.prototype.applyCriteria = function(options, values) {
case 'GTE': operator = '>='; break;
case 'ne':
case 'NE': operator = '<>'; break;
+ case 'like':
+ case 'LIKE': operator = 'LIKE'; break;
+ case 'ilike':
+ case 'ILIKE': operator = 'ILIKE'; break;
}
if (operator) {
if ((operator === '<>') && (value[keys[0]] === null)) {
@@ -336,11 +339,18 @@ CRUD.prototype.createJoinCallback = function (options, callback) {
var columns = join.columns;
if (!currentRow[join.name])
currentRow[join.name] = [];
- currentRow[join.name].push(data);
+
+ var haveData = false;
for (var c = 0; c < columns.length; c++) {
- data[columns[c]] = row[join.name + '.' + columns[c]];
- delete row[join.name + '.' + columns[c]];
+ var name = join.name + '.' + columns[c];
+ if (row[name]) {
+ data[columns[c]] = row[name];
+ haveData = true;
+ }
+ delete row[name];
}
+ if (haveData)
+ currentRow[join.name].push(data);
}
}
self.logger('\n\tconverted to ' + JSON.stringify(resultHash.items));
@@ -379,10 +389,20 @@ CRUD.returnMany = function(response) {
CRUD.returnOne = function(response) {
return function(err, result) {
if (err) {
- response.send('[ database error: ' + err + ']', 500);
+ if (response.send)
+ response.send('[ database error: ' + err + ']', 500);
+ else if (typeof response === "function")
+ response(err, result);
} else {
- CRUD.clean(result.rows[0]);
- response.send(result.rows[0]);
+ if (response.send) {
+ CRUD.clean(result.rows[0]);
+ response.send(result.rows[0]);
+ } else if (typeof response === "function") {
+ if (result.rows)
+ response(null, result.rows[0]);
+ else
+ response(null, result);
+ }
}
}
};
View
2 drivers/test.js
@@ -4,7 +4,7 @@ function createTestDriver(context, options) {
query: function testDriver(query, parameters, id, cb) {
options.data.query = query;
options.data.params = parameters;
- cb();
+ cb(null, { count: 0, rows: [] });
},
getInsertQueryText:function (table) {
View
5 table.js
@@ -50,8 +50,9 @@ Table.prototype.selectColumnList = function (dbColumns, joinsOrPrefix) {
var dbName = self.columnMap[col];
if (dbName === col)
return prefix + col;
- else
+ else if (dbName)
return prefix + dbName + ' AS "' + col + '"';
+ return col;
});
if (joins.length > 0)
@@ -89,7 +90,7 @@ Table.prototype.needsJoinPostProcessing = function (names) {
for (var i = 0; i < names.length; i++) {
var join = this.joins[names[i]];
- if (join.type === Join.ONE_TO_MANY)
+ if (join && (join.type === Join.ONE_TO_MANY))
return true;
}
View
2 test/find.js
@@ -56,7 +56,7 @@ exports['a simple findById query'] = function (t) {
exports['a query with sorted results'] = function (t) {
setup();
- blogs.findMany({ sort:{ createdAt: "DESC" }}, callback);
+ blogs.findMany({ sort: { createdAt: "DESC" }}, callback);
t.equal(data.params.length, 0);
t.ok(/SELECT\s+id.*user_id.*text.*created_at.*FROM\s+blogs\s+ORDER\s+BY\s+created_at\s+DESC/.test(data.query), 'generates a select');
t.ok(data.callbackCalled, "callback was called");
View
34 test/integration/common/blog.js
@@ -4,7 +4,9 @@ var ORM = require('../../../main'),
userClient,
blogClient,
commentClient,
+ counter = 0;
userIds = {},
+ userNames = {},
blogIds = {},
commentIds = {},
prefix = 'thinorm_',
@@ -25,7 +27,8 @@ exports['table definition'] = function (t) {
userClient = ORM.createClient(driver, usersTableName);
ORM.table(blogsTableName)
.columns(blogColumns)
- .join('login').oneToOne(usersTableName).on({ userId: 'id' }).columnMap({ name: 'login' });
+ .join('login').oneToOne(usersTableName).on({ userId: 'id' }).columnMap({ name: 'login' })
+ .join('comments').oneToMany(commentsTableName).on({ id: 'blogId' });
blogClient = ORM.createClient(driver, blogsTableName);
ORM.table(commentsTableName)
.columns(commentColumns);
@@ -38,21 +41,24 @@ exports['load test data'] = function(t) {
var userIterator = function(info, cb) {
userClient.create(info, function(err, result) {
- if (!err) userIds[info.data.login] = result.id;
+ if (!err) {
+ userIds[info.data.login] = result.id;
+ userNames[result.id] = info.data.login;
+ }
cb(err);
});
};
var blogIterator = function (info, cb) {
- var counter = 1;
blogClient.create(info, function (err, result) {
- if (!err) blogIds["" + info.data.userId + counter++] = result.id;
+ counter += 1;
+ if (!err) blogIds["" + userNames[info.data.userId] + counter] = result.id;
cb(err);
});
};
var commentIterator = function (info, cb) {
- var counter = 1;
commentClient.create(info, function (err, result) {
- if (!err) commentIds["" + info.data.userId + counter++] = result.id;
+ counter += 1;
+ if (!err) commentIds["" + info.data.userId + counter] = result.id;
cb(err);
});
};
@@ -69,6 +75,7 @@ exports['load test data'] = function(t) {
});
},
function (callback) {
+ counter = 0;
async.forEachSeries([
{ data:{userId: userIds['sam'], text:'blog text 1'}},
{ data:{userId: userIds['sam'], text:'blog text 2'}},
@@ -82,6 +89,7 @@ exports['load test data'] = function(t) {
},
function (callback) {
+ counter = 0;
async.forEachSeries([
{ data:{userId: userIds['sam'], blogId:blogIds['martha4'], text:'so true'}},
{ data:{userId: userIds['martha'], blogId:blogIds['sam1'], text:'you must be kidding'}},
@@ -107,7 +115,7 @@ exports['find user joe by id'] = function (t) {
function(err, result) {
if (err)
t.fail("query failed" + err);
- t.equal(result.rows[0].first, 'Joe');
+ t.equal(result.first, 'Joe');
t.done();
}
);
@@ -184,6 +192,18 @@ exports['a query of the blog postings should now show the new login'] = function
);
};
+exports['view all of sam\'s blogs with comments'] = function (t) {
+ blogClient.findMany({ criteria:{ userId:userIds['sam'] }, joins:[ 'login', 'comments' ] },
+ function (err, result) {
+ if (err)
+ t.fail("query failed" + err);
+ t.equal(result.rows.length, 3);
+ t.equal(result.rows[0].name, 'samuel');
+ t.done();
+ }
+ );
+};
+
exports['delete all of sam\'s comments'] = function(t) {
commentClient.remove({ criteria: { userId: userIds['sam'] }} ,
function (err, result) {
View
6 test/join.js
@@ -45,7 +45,7 @@ exports['caller can specify a one to one join'] = function (t) {
setup();
blogClient.findMany({ criteria:{ userId: 1 }, joins:[ 'login' ] }, callback);
t.equal(data.params.length, 1);
- t.ok(/SELECT\s+blogs.id.*blogs.user_id.*blogs.text.*blogs.created_at.*users.login\s+AS\s+"name"\s+FROM\s+blogs\s+JOIN\s+users\s+ON\s+blogs.user_id\s+=\s+users.id\s+WHERE\s+blogs.user_id\s+=\s+\$1/.test(data.query), 'generates a select');
+ t.ok(/SELECT\s+blogs.id.*blogs.user_id.*blogs.text.*blogs.created_at.*users.login\s+AS\s+"name"\s+FROM\s+blogs\s+LEFT\s+OUTER\s+JOIN\s+users\s+ON\s+blogs.user_id\s+=\s+users.id\s+WHERE\s+blogs.user_id\s+=\s+\$1/.test(data.query), 'generates a select');
t.ok(data.callbackCalled, "callback was called");
t.done();
};
@@ -54,7 +54,7 @@ exports['caller can specify a one to many join'] = function (t) {
setup();
blogClient.findMany({ criteria:{ userId:1 }, joins:[ 'comments' ] }, callback);
t.equal(data.params.length, 1);
- t.ok(/SELECT\s+blogs.id.*blogs.user_id.*blogs.text.*blogs.created_at.*.*comments.id.*comments.user_id.*comments.blog_id.*comments.text.*comments.created_at.*FROM\s+blogs\s+JOIN\s+comments\s+ON\s+blogs.id\s+=\s+comments.blog_id\s+WHERE\s+blogs.user_id\s+=\s+\$1/.test(data.query), 'generates a select');
+ t.ok(/SELECT\s+blogs.id.*blogs.user_id.*blogs.text.*blogs.created_at.*.*comments.id.*comments.user_id.*comments.blog_id.*comments.text.*comments.created_at.*FROM\s+blogs\s+LEFT\s+OUTER\s+JOIN\s+comments\s+ON\s+blogs.id\s+=\s+comments.blog_id\s+WHERE\s+blogs.user_id\s+=\s+\$1/.test(data.query), 'generates a select');
t.ok(data.callbackCalled, "callback was called");
t.done();
};
@@ -63,7 +63,7 @@ exports['a default join should always be incorporated into the query'] = functio
setup();
commentClient.findMany({ criteria:{ userId:1 }}, callback);
t.equal(data.params.length, 1);
- t.ok(/SELECT\s+comments.id.*comments.user_id.*comments.blog_id.*comments.text.*comments.created_at.*users.login.*FROM\s+comments\s+JOIN\s+users\s+ON\s+comments.user_id\s+=\s+users.id\s+WHERE\s+comments.user_id\s+=\s+\$1/.test(data.query), 'generates a select');
+ t.ok(/SELECT\s+comments.id.*comments.user_id.*comments.blog_id.*comments.text.*comments.created_at.*users.login.*FROM\s+comments\s+LEFT\s+OUTER\s+JOIN\s+users\s+ON\s+comments.user_id\s+=\s+users.id\s+WHERE\s+comments.user_id\s+=\s+\$1/.test(data.query), 'generates a select');
t.ok(data.callbackCalled, "callback was called");
t.done();
};

0 comments on commit 6aa0636

Please sign in to comment.