From 48574d87224af2e6cd459d925166f67e89954de0 Mon Sep 17 00:00:00 2001 From: steven-supersolid Date: Fri, 21 Oct 2016 17:10:18 +0100 Subject: [PATCH 1/4] Try to get schema from main schema if not found in single schema --- spec/SchemaCache.spec.js | 35 ++++++++++++++++++++++++++++++++++ src/Controllers/SchemaCache.js | 16 +++++++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 spec/SchemaCache.spec.js diff --git a/spec/SchemaCache.spec.js b/spec/SchemaCache.spec.js new file mode 100644 index 0000000000..e1f05cc946 --- /dev/null +++ b/spec/SchemaCache.spec.js @@ -0,0 +1,35 @@ +var CacheController = require('../src/Controllers/CacheController.js').default; +var InMemoryCacheAdapter = require('../src/Adapters/Cache/InMemoryCacheAdapter').default; +var SchemaCache = require('../src/Controllers/SchemaCache').default; + +describe('SchemaCache', () => { + var schemaCache; + + beforeEach(() => { + var cacheAdapter = new InMemoryCacheAdapter({}); + var cacheController = new CacheController(cacheAdapter, 'appId'); + schemaCache = new SchemaCache(cacheController); + }); + + it('can retrieve a single schema after all schemas stored', () => { + var allSchemas = [{ + className: 'Class1' + }, { + className: 'Class2' + }]; + schemaCache.setAllClasses(allSchemas); + schemaCache.getOneSchema('Class2').then((schema) => { + expect(schema).not.toBeNull(); + }); + }); + + it('does not return all schemas after a single schema is stored', () => { + var schema = { + className: 'Class1' + }; + schemaCache.setOneSchema('Class1', schema); + schemaCache.getAllClasses().then((allSchemas) => { + expect(allSchemas).toBeNull(); + }); + }); +}); \ No newline at end of file diff --git a/src/Controllers/SchemaCache.js b/src/Controllers/SchemaCache.js index a72fc0210d..ebfe6ac0f6 100644 --- a/src/Controllers/SchemaCache.js +++ b/src/Controllers/SchemaCache.js @@ -50,7 +50,21 @@ export default class SchemaCache { if (!this.ttl) { return Promise.resolve(null); } - return this.cache.get(this.prefix+className); + return this.cache.get(this.prefix+className).then((schema) => { + if (schema) { + return Promise.resolve(schema); + } + this.cache.get(this.prefix+MAIN_SCHEMA).then((cachedSchemas) => { + cachedSchemas = cachedSchemas || []; + schema = cachedSchemas.find((cachedSchema) => { + return cachedSchema.className === className; + }); + if (schema) { + return Promise.resolve(schema); + } + return Promise.resolve(null); + }); + }); } clear() { From 82474a63d92da830b1067995f1ab53494d6c5eee Mon Sep 17 00:00:00 2001 From: steven-supersolid Date: Fri, 21 Oct 2016 17:12:40 +0100 Subject: [PATCH 2/4] Add newline --- spec/SchemaCache.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/SchemaCache.spec.js b/spec/SchemaCache.spec.js index e1f05cc946..7403357746 100644 --- a/spec/SchemaCache.spec.js +++ b/spec/SchemaCache.spec.js @@ -32,4 +32,4 @@ describe('SchemaCache', () => { expect(allSchemas).toBeNull(); }); }); -}); \ No newline at end of file +}); From adcbbb831997948b52deec8674dbaf94849174a6 Mon Sep 17 00:00:00 2001 From: steven-supersolid Date: Fri, 21 Oct 2016 17:21:43 +0100 Subject: [PATCH 3/4] Add missing return --- src/Controllers/SchemaCache.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Controllers/SchemaCache.js b/src/Controllers/SchemaCache.js index ebfe6ac0f6..63dc481d97 100644 --- a/src/Controllers/SchemaCache.js +++ b/src/Controllers/SchemaCache.js @@ -54,7 +54,7 @@ export default class SchemaCache { if (schema) { return Promise.resolve(schema); } - this.cache.get(this.prefix+MAIN_SCHEMA).then((cachedSchemas) => { + return this.cache.get(this.prefix+MAIN_SCHEMA).then((cachedSchemas) => { cachedSchemas = cachedSchemas || []; schema = cachedSchemas.find((cachedSchema) => { return cachedSchema.className === className; From 7ee41e994f0dce6f0866e0154be056f8fb07ca72 Mon Sep 17 00:00:00 2001 From: steven-supersolid Date: Sun, 23 Oct 2016 09:43:19 +0100 Subject: [PATCH 4/4] Add missing done to tests --- spec/SchemaCache.spec.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/spec/SchemaCache.spec.js b/spec/SchemaCache.spec.js index 7403357746..12e9fb387f 100644 --- a/spec/SchemaCache.spec.js +++ b/spec/SchemaCache.spec.js @@ -11,7 +11,7 @@ describe('SchemaCache', () => { schemaCache = new SchemaCache(cacheController); }); - it('can retrieve a single schema after all schemas stored', () => { + it('can retrieve a single schema after all schemas stored', (done) => { var allSchemas = [{ className: 'Class1' }, { @@ -20,16 +20,18 @@ describe('SchemaCache', () => { schemaCache.setAllClasses(allSchemas); schemaCache.getOneSchema('Class2').then((schema) => { expect(schema).not.toBeNull(); + done(); }); }); - it('does not return all schemas after a single schema is stored', () => { + it('does not return all schemas after a single schema is stored', (done) => { var schema = { className: 'Class1' }; schemaCache.setOneSchema('Class1', schema); schemaCache.getAllClasses().then((allSchemas) => { expect(allSchemas).toBeNull(); + done(); }); }); });