add test cases for caching in hasMany and fixed test cases for caching in belongsTo

This commit is contained in:
Sébastien Drouyer 2012-11-01 01:17:09 +01:00
parent 46b7747c87
commit 89b8329764
1 changed files with 81 additions and 24 deletions

View File

@ -448,6 +448,58 @@ function testOrm(schema) {
}); });
}); });
it('hasMany should be cached', function (test) {
User.find(1, function(err, user) {
User.create(function(err, voidUser) {
Post.create({userId: user.id}, function() {
// There can't be any concurrency because we are counting requests
// We are first testing cases when user has posts
user.posts(function(err, data) {
var nbInitialRequests = nbSchemaRequests;
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.');
user.posts({where: {id: 12}}, 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.');
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.');
// We are now testing cases when user doesn't have any post
voidUser.posts(function(err, data) {
var nbInitialRequests = nbSchemaRequests;
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.');
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.');
test.done();
});
});
});
});
});
});
});
});
});
});
});
// it('should handle hasOne relationship', function (test) { // it('should handle hasOne relationship', function (test) {
// User.create(function (err, u) { // User.create(function (err, u) {
// if (err) return console.log(err); // if (err) return console.log(err);
@ -885,40 +937,45 @@ function testOrm(schema) {
}); });
it('belongsTo should be cached', function (test) { it('belongsTo should be cached', function (test) {
var passport = new Passport({ownerId: 8}); User.findOne(function(err, user) {
var passport2 = new Passport({ownerId: null});
// There can't be any concurrency because we are counting requests var passport = new Passport({ownerId: user.id});
passport.owner(function(err, data) { var passport2 = new Passport({ownerId: null});
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.');
passport2.owner(function(err, data) { // There can't be any concurrency because we are counting requests
var nbInitialRequests2 = nbSchemaRequests; // We are first testing cases when passport has an owner
passport2.owner(function(err, data2) { passport.owner(function(err, data) {
test.equal(data, null, 'The value should be null since there is no owner'); var nbInitialRequests = nbSchemaRequests;
test.equal(data, data2, 'The value should remain the same (null)'); passport.owner(function(err, data2) {
test.equal(nbInitialRequests2, nbSchemaRequests, 'There should not be any request because value is cached.'); 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.');
passport2.owner(8); // We are now testing cases when passport has not an owner
passport2.owner(function(err, data3) { passport2.owner(function(err, data) {
test.equal(data3.id, 8, 'There should be an object'); var nbInitialRequests2 = nbSchemaRequests;
test.equal(nbInitialRequests2 + 1, nbSchemaRequests, 'There should not be any request because value is cached.'); 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.');
passport2.owner(true, function(err, data4) { passport2.owner(user.id);
test.equal(data3.id, data3.id, 'The value should remain the same'); passport2.owner(function(err, data3) {
test.equal(nbInitialRequests2 + 2, nbSchemaRequests, 'If we forced refreshing, there should be one more request.'); test.equal(data3.id, user.id, 'Owner should now be the user.');
test.done(); 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.');
test.done();
});
}); });
}); });
}); });
}); });
}); });
}); });
}); });
if (schema.name !== 'mongoose' && schema.name !== 'neo4j') if (schema.name !== 'mongoose' && schema.name !== 'neo4j')