From 285a358c91bab15affa96d56fc55032903171373 Mon Sep 17 00:00:00 2001 From: Florian Bienefelt Date: Mon, 17 Jun 2019 17:12:12 +0200 Subject: [PATCH 1/2] Add tests for queries with denormalized links --- lib/query/lib/prepareForDelivery.js | 7 ++- lib/query/testing/bootstrap/authors/links.js | 6 ++- lib/query/testing/bootstrap/comments/links.js | 6 ++- lib/query/testing/server.test.js | 48 +++++++++++++++++++ package.js | 1 + 5 files changed, 64 insertions(+), 4 deletions(-) diff --git a/lib/query/lib/prepareForDelivery.js b/lib/query/lib/prepareForDelivery.js index 9422748b..d8c8c550 100755 --- a/lib/query/lib/prepareForDelivery.js +++ b/lib/query/lib/prepareForDelivery.js @@ -270,7 +270,7 @@ function snapBackCaches(node) { node.collectionNodes.forEach(collectionNode => { snapBackCaches(collectionNode); }); - + if (!_.isEmpty(node.snapCaches)) { // process stuff _.each(node.snapCaches, (linkName, cacheField) => { @@ -295,7 +295,10 @@ function snapBackCaches(node) { result[linkName] = result[cacheField]; } - delete result[cacheField]; + // Don't remove the cacheField if it was requested explicitly by the query + if (!node.body[cacheField]) { + delete result[cacheField]; + } } }) }) diff --git a/lib/query/testing/bootstrap/authors/links.js b/lib/query/testing/bootstrap/authors/links.js index c8fa1d8e..07f4b0d0 100755 --- a/lib/query/testing/bootstrap/authors/links.js +++ b/lib/query/testing/bootstrap/authors/links.js @@ -16,7 +16,11 @@ Authors.addLinks({ type: 'many', metadata: true, collection: Groups, - field: 'groupIds' + field: 'groupIds', + denormalize: { + field: 'groupsCache', + body: { _id: 1, name: 1 }, + } } }); diff --git a/lib/query/testing/bootstrap/comments/links.js b/lib/query/testing/bootstrap/comments/links.js index 7399eda1..1a7874c1 100755 --- a/lib/query/testing/bootstrap/comments/links.js +++ b/lib/query/testing/bootstrap/comments/links.js @@ -7,7 +7,11 @@ Comments.addLinks({ type: 'one', collection: Authors, field: 'authorId', - index: true + index: true, + denormalize: { + field: 'authorCache', + body: { _id: 1, name: 1, groupsCache: 1 }, + } }, post: { diff --git a/lib/query/testing/server.test.js b/lib/query/testing/server.test.js index feccb31a..0ab6853f 100755 --- a/lib/query/testing/server.test.js +++ b/lib/query/testing/server.test.js @@ -1168,3 +1168,51 @@ describe('intersectDeep', () => { expect(res).to.be.eql({}); }); }); + + + +describe.only('denormalized queries', () => { + it('Gets both the grapher cache and the real cache', () => { + const data = createQuery({ + comments: { + author: { + name: 1, + }, + authorCache: 1, + }, + }).fetchOne(); + console.log('data:', data); + + expect(Object.keys(data)).to.be.eql(['_id', 'author', 'authorCache']); + // expect(Object.keys(data.author)).to.be.eql(['_id', 'name']); + // expect(Object.keys(data.authorCache)).to.be.eql(['_id', 'name', 'groupsCache']); + }); + + it('Gets the right data when mixing caches and reducers 1', () => { + const data = createQuery({ + comments: { + author: { + groupsCache: 1, + }, + authorCache: 1, + authorLinkReducer: 1, + }, + }).fetchOne(); + + expect(Object.keys(data)).to.be.eql(['_id', 'author', 'authorCache', 'authorLinkReducer']); + }); + + it('Gets the right data when mixing caches and reducers 2', () => { + const data = createQuery({ + comments: { + author: { + fullName: 1, + }, + authorCache: 1, + }, + }).fetchOne(); + console.log('data:', data); + + expect(Object.keys(data)).to.be.eql(['_id', 'authorCache', 'author']); + }); +}) \ No newline at end of file diff --git a/package.js b/package.js index 8ea7bf4d..c5558fc2 100755 --- a/package.js +++ b/package.js @@ -15,6 +15,7 @@ Npm.depends({ 'dot-object': '1.5.4', 'lodash.clonedeep': '4.5.0', 'deep-extend': '0.5.0', + chai: '4.2.0', }); Package.onUse(function(api) { From f9739ea97ae3080fe67a48b3244e6070f3519739 Mon Sep 17 00:00:00 2001 From: Florian Bienefelt Date: Mon, 17 Jun 2019 17:28:52 +0200 Subject: [PATCH 2/2] Improve tests --- .npm/package/npm-shrinkwrap.json | 53 ++++++++++++++++++++++++++------ lib/query/testing/server.test.js | 7 +++-- 2 files changed, 48 insertions(+), 12 deletions(-) diff --git a/.npm/package/npm-shrinkwrap.json b/.npm/package/npm-shrinkwrap.json index b2eb3bff..6e1ab2f1 100644 --- a/.npm/package/npm-shrinkwrap.json +++ b/.npm/package/npm-shrinkwrap.json @@ -1,26 +1,46 @@ { "lockfileVersion": 1, "dependencies": { + "assertion-error": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-1.1.0.tgz", + "integrity": "sha512-jgsaNduz+ndvGyFt3uSuWqvy4lCnIJiovtouQN5JZHOKCS2QuhEdbcQHFhVksz2N2U9hXJo8odG7ETyWlEeuDw==" + }, "balanced-match": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=" }, "brace-expansion": { - "version": "1.1.8", - "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.8.tgz", - "integrity": "sha1-wHshHHyVLsH479Uad+8NHTmQopI=" + "version": "1.1.11", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", + "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==" + }, + "chai": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/chai/-/chai-4.2.0.tgz", + "integrity": "sha512-XQU3bhBukrOsQCuwZndwGcCVQHyZi53fQ6Ys1Fym7E4olpIqqZZhhoFJoaKVvV17lWQoXYwgWN2nF5crA8J2jw==" + }, + "check-error": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/check-error/-/check-error-1.0.2.tgz", + "integrity": "sha1-V00xLt2Iu13YkS6Sht1sCu1KrII=" }, "commander": { - "version": "2.12.1", - "resolved": "https://registry.npmjs.org/commander/-/commander-2.12.1.tgz", - "integrity": "sha512-PCNLExLlI5HiPdaJs4pMXwOTHkSCpNQ1QJH9ykZLKtKEyKu3p9HgmH5l97vM8c0IUz6d54l+xEu2GG9yuYrFzA==" + "version": "2.20.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-2.20.0.tgz", + "integrity": "sha512-7j2y+40w61zy6YC2iRNpUe/NwhNyoXrYpHMrSunaMG64nRnaf96zO/KMQR4OyN/UnE5KLyEBnKHd4aG3rskjpQ==" }, "concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=" }, + "deep-eql": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/deep-eql/-/deep-eql-3.0.1.tgz", + "integrity": "sha512-+QeIQyN5ZuO+3Uk5DYh6/1eKO0m0YmJFGNmFHGACpf1ClL1nmlV/p4gNgbl2pJGxgXb4faqo6UE+M5ACEMyVcw==" + }, "deep-extend": { "version": "0.5.0", "resolved": "https://registry.npmjs.org/deep-extend/-/deep-extend-0.5.0.tgz", @@ -36,10 +56,15 @@ "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=" }, + "get-func-name": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/get-func-name/-/get-func-name-2.0.0.tgz", + "integrity": "sha1-6td0q+5y4gQJQzoGY2YCPdaIekE=" + }, "glob": { - "version": "7.1.2", - "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.2.tgz", - "integrity": "sha512-MJTUg1kjuLeQCJ+ccE4Vpa6kKVXkPYJ2mOCQyUuKLcLQsdrMCpBPUi8qVE6+YuaJkozeA9NusTAw3hLr8Xe5EQ==" + "version": "7.1.4", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.4.tgz", + "integrity": "sha512-hkLPepehmnKk41pUGm3sYxoFs/umurYfYJCerbXEyFIWcAzvpipAgVkBqqT9RBKMGjnq6kMuyYwha6csxbiM1A==" }, "inflight": { "version": "1.0.6", @@ -71,11 +96,21 @@ "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=" }, + "pathval": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/pathval/-/pathval-1.1.0.tgz", + "integrity": "sha1-uULm1L3mUwBe9rcTYd74cn0GReA=" + }, "sift": { "version": "3.2.6", "resolved": "https://registry.npmjs.org/sift/-/sift-3.2.6.tgz", "integrity": "sha1-rht0k2FN9ftfZVdzLU9PAm7zQMg=" }, + "type-detect": { + "version": "4.0.8", + "resolved": "https://registry.npmjs.org/type-detect/-/type-detect-4.0.8.tgz", + "integrity": "sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g==" + }, "wrappy": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", diff --git a/lib/query/testing/server.test.js b/lib/query/testing/server.test.js index 0ab6853f..e08d5c11 100755 --- a/lib/query/testing/server.test.js +++ b/lib/query/testing/server.test.js @@ -1171,8 +1171,8 @@ describe('intersectDeep', () => { -describe.only('denormalized queries', () => { - it('Gets both the grapher cache and the real cache', () => { +describe('denormalized queries', () => { + it('Gets both the grapher cache and the real cache 1', () => { const data = createQuery({ comments: { author: { @@ -1191,13 +1191,14 @@ describe.only('denormalized queries', () => { it('Gets the right data when mixing caches and reducers 1', () => { const data = createQuery({ comments: { + authorLinkReducer: 1, author: { groupsCache: 1, }, authorCache: 1, - authorLinkReducer: 1, }, }).fetchOne(); + console.log('data:', data); expect(Object.keys(data)).to.be.eql(['_id', 'author', 'authorCache', 'authorLinkReducer']); });