From 6d8454c5ac2595fc843f2cc09e83266d23849283 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Drouyer?= Date: Thu, 1 Nov 2012 00:15:23 +0100 Subject: [PATCH] added test case for the cache of belongsTo --- test/common_test.js | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/test/common_test.js b/test/common_test.js index 09efafc7..ce04139d 100644 --- a/test/common_test.js +++ b/test/common_test.js @@ -26,6 +26,7 @@ var schemas = { var specificTest = getSpecificTests(); var testPerformed = false; +var nbSchemaRequests = 0; Object.keys(schemas).forEach(function (schemaName) { if (process.env.ONLY && process.env.ONLY !== schemaName) return; @@ -48,7 +49,8 @@ function performTestFor(schemaName) { }); schema.log = function (a) { - console.log(a); + console.log(a); + nbSchemaRequests++; }; testOrm(schema); @@ -882,6 +884,43 @@ function testOrm(schema) { }); }); + it('belongsTo should be cached', function (test) { + var passport = new Passport({ownerId: 8}); + var passport2 = new Passport({ownerId: null}); + + // There can't be any concurrency because we are counting requests + passport.owner(function(err, data) { + 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) { + var nbInitialRequests2 = nbSchemaRequests; + 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(8); + passport2.owner(function(err, data3) { + test.equal(data3.id, 8, 'There should be an object'); + test.equal(nbInitialRequests2 + 1, nbSchemaRequests, 'There should not be any request because value is cached.'); + + 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') it('should update or create record', function (test) { var newData = {