Merge pull request #142 from sdrdis/master

Fixes on common_test
This commit is contained in:
Anatoliy Chakkaev 2012-11-03 08:25:55 -07:00
commit f26096aab4
1 changed files with 96 additions and 39 deletions

View File

@ -59,6 +59,7 @@ function performTestFor(schemaName) {
}
function testOrm(schema) {
var requestsAreCounted = schema.name !== 'mongodb';
var Post, User, Passport;
var start = Date.now();
@ -449,9 +450,50 @@ function testOrm(schema) {
});
it('hasMany should be cached', function (test) {
if (
!schema.name.match(/redis/) &&
schema.name !== 'memory' &&
schema.name !== 'neo4j' &&
schema.name !== 'cradle' &&
schema.name !== 'mongodb'
)
it('hasMany should support additional conditions', function (test) {
User.find(1, function(err, user) {
// Finding one post with an existing author associated
Post.all(function (err, posts) {
// We try to get the first post with a userId != NULL
for (var i = 0; i < posts.length; i++) {
var post = posts[i];
if (post.userId !== null) {
// We could get the user with belongs to relationship but it is better if there is no interactions.
User.find(post.userId, function(err, user) {
user.posts({where: {id: post.id}}, function(err, posts) {
test.equal(posts.length, 1, 'There should be only 1 post.');
test.done();
});
});
break;
}
}
});
});
if (
!schema.name.match(/redis/) &&
schema.name !== 'memory' &&
schema.name !== 'neo4j' &&
schema.name !== 'cradle'
)
it('hasMany should be cached', function (test) {
// Finding one post with an existing author associated
Post.all(function (err, posts) {
// We try to get the first post with a userId != NULL
for (var i = 0; i < posts.length; i++) {
var post = posts[i];
if (post.userId !== null) {
// We could get the user with belongs to relationship but it is better if there is no interactions.
User.find(post.userId, function(err, user) {
User.create(function(err, voidUser) {
Post.create({userId: user.id}, function() {
@ -462,15 +504,18 @@ function testOrm(schema) {
user.posts(function(err, data2) {
test.equal(data.length, 2, 'There should be 2 posts.');
test.equal(data.length, data2.length, 'Posts should be the same, since we are loading on the same object.');
test.equal(nbInitialRequests, nbSchemaRequests, 'There should not be any request because value is cached.');
requestsAreCounted && test.equal(nbInitialRequests, nbSchemaRequests, 'There should not be any request because value is cached.');
user.posts({where: {id: 12}}, function(err, data) {
if (schema.name === 'mongodb') { // for the moment mongodb doesn\'t support additional conditions on hasMany relations (see above)
test.done();
} else {
user.posts({where: {id: data[0].id}}, function(err, data) {
test.equal(data.length, 1, 'There should be only one post.');
test.equal(nbInitialRequests + 1, nbSchemaRequests, 'There should be one additional request since we added conditions.');
requestsAreCounted && test.equal(nbInitialRequests + 1, nbSchemaRequests, 'There should be one additional request since we added conditions.');
user.posts(function(err, data) {
test.equal(data.length, 2, 'Previous get shouldn\'t have changed cached value though, since there was additional conditions.');
test.equal(nbInitialRequests + 1, nbSchemaRequests, 'There should not be any request because value is cached.');
requestsAreCounted && test.equal(nbInitialRequests + 1, nbSchemaRequests, 'There should not be any request because value is cached.');
// We are now testing cases when user doesn't have any post
voidUser.posts(function(err, data) {
@ -478,11 +523,11 @@ function testOrm(schema) {
voidUser.posts(function(err, data2) {
test.equal(data.length, 0, 'There shouldn\'t be any posts (1/2).');
test.equal(data2.length, 0, 'There shouldn\'t be any posts (2/2).');
test.equal(nbInitialRequests, nbSchemaRequests, 'There should not be any request because value is cached.');
requestsAreCounted && test.equal(nbInitialRequests, nbSchemaRequests, 'There should not be any request because value is cached.');
voidUser.posts(true, function(err, data3) {
test.equal(data3.length, 0, 'There shouldn\'t be any posts.');
test.equal(nbInitialRequests + 1, nbSchemaRequests, 'There should be one additional request since we forced refresh.');
requestsAreCounted && test.equal(nbInitialRequests + 1, nbSchemaRequests, 'There should be one additional request since we forced refresh.');
test.done();
});
@ -491,12 +536,18 @@ function testOrm(schema) {
});
});
}
});
});
});
});
});
break;
}
}
});
});
@ -936,6 +987,12 @@ function testOrm(schema) {
});
});
if (
!schema.name.match(/redis/) &&
schema.name !== 'memory' &&
schema.name !== 'neo4j' &&
schema.name !== 'cradle'
)
it('belongsTo should be cached', function (test) {
User.findOne(function(err, user) {
@ -948,7 +1005,7 @@ function testOrm(schema) {
var nbInitialRequests = nbSchemaRequests;
passport.owner(function(err, data2) {
test.equal(data.id, data2.id, 'The value should remain the same');
test.equal(nbInitialRequests, nbSchemaRequests, 'There should not be any request because value is cached.');
requestsAreCounted && test.equal(nbInitialRequests, nbSchemaRequests, 'There should not be any request because value is cached.');
// We are now testing cases when passport has not an owner
passport2.owner(function(err, data) {
@ -956,16 +1013,16 @@ function testOrm(schema) {
passport2.owner(function(err, data2) {
test.equal(data, null, 'The value should be null since there is no owner');
test.equal(data, data2, 'The value should remain the same (null)');
test.equal(nbInitialRequests2, nbSchemaRequests, 'There should not be any request because value is cached.');
requestsAreCounted && test.equal(nbInitialRequests2, nbSchemaRequests, 'There should not be any request because value is cached.');
passport2.owner(user.id);
passport2.owner(function(err, data3) {
test.equal(data3.id, user.id, 'Owner should now be the user.');
test.equal(nbInitialRequests2 + 1, nbSchemaRequests, 'If we changed owner id, there should be one more request.');
requestsAreCounted && test.equal(nbInitialRequests2 + 1, nbSchemaRequests, 'If we changed owner id, there should be one more request.');
passport2.owner(true, function(err, data4) {
test.equal(data3.id, data3.id, 'The value should remain the same');
test.equal(nbInitialRequests2 + 2, nbSchemaRequests, 'If we forced refreshing, there should be one more request.');
requestsAreCounted && test.equal(nbInitialRequests2 + 2, nbSchemaRequests, 'If we forced refreshing, there should be one more request.');
test.done();
});
});