From 18b47177cc93cc725f8db754232ed6455bbcfd83 Mon Sep 17 00:00:00 2001 From: denihs Date: Thu, 16 Feb 2023 14:31:47 -0400 Subject: [PATCH] - fix test: ''collection - fetch, STRING | MONGO' --- .../ddp-client/common/livedata_connection.js | 22 ++-- packages/minimongo/local_collection.js | 4 + packages/mongo/allow_tests.js | 117 +++++++++++++----- 3 files changed, 99 insertions(+), 44 deletions(-) diff --git a/packages/ddp-client/common/livedata_connection.js b/packages/ddp-client/common/livedata_connection.js index a652d66e94..51b644d023 100644 --- a/packages/ddp-client/common/livedata_connection.js +++ b/packages/ddp-client/common/livedata_connection.js @@ -731,19 +731,18 @@ export class Connection { "delivering result of invoking '" + name + "'" ); } - - // Keep our args safe from mutation (eg if we don't send the message for a - // while because of a wait method). - args = EJSON.clone(args); - const { hasStub, exception, stubReturnValue, alreadyInSimulation, randomSeed, + stubArgs, } = stubCallValue; + // Keep our args safe from mutation (eg if we don't send the message for a + // while because of a wait method). + args = EJSON.clone(stubArgs); // If we're in a simulation, stop and return the result we have, // rather than going on to do an RPC. If there was no stub, // we'll end up returning undefined. @@ -812,7 +811,7 @@ export class Connection { // only thing we can do is to return undefined and discard the // result of the RPC. If an error occurred then print the error // to the console. - callback = err => { + callback = (err) => { err && Meteor._debug("Error invoking Method '" + name + "'", err); }; } else { @@ -881,7 +880,7 @@ export class Connection { // block waiting for the result. if (future) { return options.returnStubValue - ? future + ? future.then(() => stubReturnValue) : { stubValuePromise: future, }; @@ -910,7 +909,10 @@ export class Connection { const randomSeed = { value: null}; const defaultReturn = { - alreadyInSimulation, randomSeed, isFromCallAsync + alreadyInSimulation, + randomSeed, + isFromCallAsync, + stubArgs: args, }; if (!stub) { return { ...defaultReturn, hasStub: false }; @@ -956,10 +958,10 @@ export class Connection { // don't allow stubs to yield. return Meteor._noYieldsAllowed(() => { // re-clone, so that the stub can't affect our caller's values - return stub.apply(invocation, EJSON.clone(args)); + return stub.apply(invocation, args); }); } else { - return stub.apply(invocation, EJSON.clone(args)); + return stub.apply(invocation, args); } }; return { ...defaultReturn, hasStub: true, stubInvocation, invocation }; diff --git a/packages/minimongo/local_collection.js b/packages/minimongo/local_collection.js index a06bf58aa1..a5c078693c 100644 --- a/packages/minimongo/local_collection.js +++ b/packages/minimongo/local_collection.js @@ -128,7 +128,9 @@ export default class LocalCollection { // XXX possibly enforce that 'undefined' does not appear (we assume // this in our handling of null and $exists) insert(doc, callback) { + const docId = doc._id; doc = EJSON.clone(doc); + doc._id = docId; const id = this.prepareInsert(doc); const queriesToRecompute = []; @@ -171,7 +173,9 @@ export default class LocalCollection { return id; } async insertAsync(doc, callback) { + const docId = doc._id; doc = EJSON.clone(doc); + doc._id = docId; const id = this.prepareInsert(doc); const queriesToRecompute = []; diff --git a/packages/mongo/allow_tests.js b/packages/mongo/allow_tests.js index 415aeeec32..2095adef3b 100644 --- a/packages/mongo/allow_tests.js +++ b/packages/mongo/allow_tests.js @@ -175,13 +175,13 @@ if (Meteor.isServer) { // verify that we only fetch the fields specified - we should // be fetching just field1, field2, and field3. restrictedCollectionForFetchTest.allow({ - insert: function() { return true; }, - update: function(userId, doc) { + insertAsync: function() { return true; }, + updateAsync: function(userId, doc) { // throw fields in doc so that we can inspect them in test throw new Meteor.Error( 999, "Test: Fields in doc: " + _.keys(doc).sort().join(',')); }, - remove: function(userId, doc) { + removeAsync: function(userId, doc) { // throw fields in doc so that we can inspect them in test throw new Meteor.Error( 999, "Test: Fields in doc: " + _.keys(doc).sort().join(',')); @@ -198,13 +198,13 @@ if (Meteor.isServer) { // verify that not passing fetch to one of the calls to allow // causes all fields to be fetched restrictedCollectionForFetchAllTest.allow({ - insert: function() { return true; }, - update: function(userId, doc) { + insertAsync: function() { return true; }, + updateAsync: function(userId, doc) { // throw fields in doc so that we can inspect them in test throw new Meteor.Error( 999, "Test: Fields in doc: " + _.keys(doc).sort().join(',')); }, - remove: function(userId, doc) { + removeAsync: function(userId, doc) { // throw fields in doc so that we can inspect them in test throw new Meteor.Error( 999, "Test: Fields in doc: " + _.keys(doc).sort().join(',')); @@ -212,7 +212,7 @@ if (Meteor.isServer) { fetch: ['field1'] }); restrictedCollectionForFetchAllTest.allow({ - update: function() { return true; } + updateAsync: function() { return true; } }); } @@ -316,34 +316,83 @@ if (Meteor.isClient) { // test that we only fetch the fields specified - testAsyncMulti("collection - fetch, " + idGeneration, [ - function (test, expect) { - var fetchId = restrictedCollectionForFetchTest.insert( - {field1: 1, field2: 1, field3: 1, field4: 1}); - var fetchAllId = restrictedCollectionForFetchAllTest.insert( - {field1: 1, field2: 1, field3: 1, field4: 1}); - restrictedCollectionForFetchTest.update( - fetchId, {$set: {updated: true}}, expect(function (err, res) { - test.equal(err.reason, - "Test: Fields in doc: _id,field1,field2,field3"); - })); - restrictedCollectionForFetchTest.remove( - fetchId, expect(function (err, res) { - test.equal(err.reason, - "Test: Fields in doc: _id,field1,field2,field3"); - })); + testAsyncMulti('collection - fetch, ' + idGeneration, [ + async function(test, expect) { + var fetchId = await restrictedCollectionForFetchTest.insertAsync({ + field1: 1, + field2: 1, + field3: 1, + field4: 1, + },{ + returnServerResultPromise: true, + }); + var fetchAllId = await restrictedCollectionForFetchAllTest.insertAsync({ + field1: 1, + field2: 1, + field3: 1, + field4: 1, + },{ + returnServerResultPromise: true, + }); + await restrictedCollectionForFetchTest + .updateAsync( + fetchId, + { $set: { updated: true } }, + { + returnServerResultPromise: true, + } + ) + .catch( + expect(function(err) { + test.equal( + err.reason, + 'Test: Fields in doc: _id,field1,field2,field3' + ); + }) + ); - restrictedCollectionForFetchAllTest.update( - fetchAllId, {$set: {updated: true}}, expect(function (err, res) { - test.equal(err.reason, - "Test: Fields in doc: _id,field1,field2,field3,field4"); - })); - restrictedCollectionForFetchAllTest.remove( - fetchAllId, expect(function (err, res) { - test.equal(err.reason, - "Test: Fields in doc: _id,field1,field2,field3,field4"); - })); - } + await restrictedCollectionForFetchTest + .removeAsync(fetchId, { + returnServerResultPromise: true, + }) + .catch( + expect(function(err) { + test.equal( + err.reason, + 'Test: Fields in doc: _id,field1,field2,field3' + ); + }) + ); + + await restrictedCollectionForFetchAllTest + .updateAsync( + fetchAllId, + { $set: { updated: true } }, + { + returnServerResultPromise: true, + } + ) + .catch( + expect(function(err) { + test.equal( + err.reason, + 'Test: Fields in doc: _id,field1,field2,field3,field4' + ); + }) + ); + await restrictedCollectionForFetchAllTest + .removeAsync(fetchAllId, { + returnServerResultPromise: true, + }) + .catch( + expect(function(err) { + test.equal( + err.reason, + 'Test: Fields in doc: _id,field1,field2,field3,field4' + ); + }) + ); + }, ]); (function(){