- fix test: 'basics, MONGO | STRING'

This commit is contained in:
denihs
2023-02-13 13:50:31 -04:00
parent c5b07354d9
commit db7d1ac652

View File

@@ -242,40 +242,49 @@ _.each( ['STRING', 'MONGO'], function(idGeneration) {
]);
Tinytest.addAsync("mongo-livedata - basics, " + idGeneration, function (test, onComplete) {
Tinytest.addAsync('mongo-livedata - basics, ' + idGeneration, async function(
test,
onComplete
) {
var run = test.runId();
var coll, coll2;
if (Meteor.isClient) {
coll = new Mongo.Collection(null, collectionOptions) ; // local, unmanaged
coll = new Mongo.Collection(null, collectionOptions); // local, unmanaged
coll2 = new Mongo.Collection(null, collectionOptions); // local, unmanaged
} else {
coll = new Mongo.Collection("livedata_test_collection_"+run, collectionOptions);
coll2 = new Mongo.Collection("livedata_test_collection_2_"+run, collectionOptions);
coll = new Mongo.Collection(
'livedata_test_collection_' + run,
collectionOptions
);
coll2 = new Mongo.Collection(
'livedata_test_collection_2_' + run,
collectionOptions
);
}
var log = '';
var obs = coll.find({run: run}, {sort: ["x"]}).observe({
addedAt: function (doc, before_index, before) {
var obs = await coll.find({ run: run }, { sort: ['x'] }).observe({
addedAt: function(doc, before_index, before) {
log += 'a(' + doc.x + ',' + before_index + ',' + before + ')';
},
changedAt: function (new_doc, old_doc, at_index) {
changedAt: function(new_doc, old_doc, at_index) {
log += 'c(' + new_doc.x + ',' + at_index + ',' + old_doc.x + ')';
},
movedTo: function (doc, old_index, new_index) {
movedTo: function(doc, old_index, new_index) {
log += 'm(' + doc.x + ',' + old_index + ',' + new_index + ')';
},
removedAt: function (doc, at_index) {
removedAt: function(doc, at_index) {
log += 'r(' + doc.x + ',' + at_index + ')';
}
},
});
var captureObserve = function (f) {
const captureObserve = async function(f) {
if (Meteor.isClient) {
f();
await f();
} else {
var fence = new DDPServer._WriteFence;
DDPServer._CurrentWriteFence.withValue(fence, f);
fence.armAndWait();
const fence = new DDPServer._WriteFence();
await DDPServer._CurrentWriteFence.withValue(fence, f);
await fence.armAndWait();
}
var ret = log;
@@ -283,49 +292,59 @@ _.each( ['STRING', 'MONGO'], function(idGeneration) {
return ret;
};
var expectObserve = function (expected, f) {
if (!(expected instanceof Array))
expected = [expected];
const expectObserve = async function(expected, f) {
if (!(expected instanceof Array)) expected = [expected];
test.include(expected, captureObserve(f));
test.include(expected, await captureObserve(f));
};
test.equal(coll.find({run: run}).count(), 0);
test.equal(coll.findOne("abc"), undefined);
test.equal(coll.findOne({run: run}), undefined);
test.equal(await coll.find({ run: run }).countAsync(), 0);
test.equal(await coll.findOneAsync('abc'), undefined);
test.equal(await coll.findOneAsync({ run: run }), undefined);
expectObserve('a(1,0,null)', function () {
var id = coll.insert({run: run, x: 1});
test.equal(coll.find({run: run}).count(), 1);
test.equal(coll.findOne(id).x, 1);
test.equal(coll.findOne({run: run}).x, 1);
await expectObserve('a(1,0,null)', async function() {
const id = await coll.insertAsync({ run: run, x: 1 });
test.equal(await coll.find({ run: run }).countAsync(), 1);
test.equal((await coll.findOneAsync(id)).x, 1);
test.equal((await coll.findOneAsync({ run: run })).x, 1);
});
expectObserve('a(4,1,null)', function () {
var id2 = coll.insert({run: run, x: 4});
test.equal(coll.find({run: run}).count(), 2);
test.equal(coll.find({_id: id2}).count(), 1);
test.equal(coll.findOne(id2).x, 4);
await expectObserve('a(4,1,null)', async function() {
const id2 = await coll.insertAsync({ run: run, x: 4 });
test.equal(await coll.find({ run: run }).countAsync(), 2);
test.equal(await coll.find({ _id: id2 }).countAsync(), 1);
test.equal((await coll.findOneAsync(id2)).x, 4);
});
test.equal(coll.findOne({run: run}, {sort: ["x"], skip: 0}).x, 1);
test.equal(coll.findOne({run: run}, {sort: ["x"], skip: 1}).x, 4);
test.equal(coll.findOne({run: run}, {sort: {x: -1}, skip: 0}).x, 4);
test.equal(coll.findOne({run: run}, {sort: {x: -1}, skip: 1}).x, 1);
test.equal(
(await coll.findOneAsync({ run: run }, { sort: ['x'], skip: 0 })).x,
1
);
test.equal(
(await coll.findOneAsync({ run: run }, { sort: ['x'], skip: 1 })).x,
4
);
test.equal(
(await coll.findOneAsync({ run: run }, { sort: { x: -1 }, skip: 0 })).x,
4
);
test.equal(
(await coll.findOneAsync({ run: run }, { sort: { x: -1 }, skip: 1 })).x,
1
);
// - applySkipLimit is no longer an option
// Note that the current behavior is inconsistent on the client.
// (https://github.com/meteor/meteor/issues/1201)
if (Meteor.isServer) {
test.equal(coll.find({run: run}, {limit: 1}).count(), 1);
test.equal(await coll.find({ run: run }, { limit: 1 }).countAsync(), 1);
}
var cur = coll.find({run: run}, {sort: ["x"]});
var cur = coll.find({ run: run }, { sort: ['x'] });
var total = 0;
var index = 0;
var context = {};
cur.forEach(function (doc, i, cursor) {
await cur.forEachAsync(async function(doc, i, cursor) {
test.equal(i, index++);
test.isTrue(cursor === cur);
test.isTrue(context === this);
@@ -337,61 +356,85 @@ _.each( ['STRING', 'MONGO'], function(idGeneration) {
// callback's sleep sleep and the *= 10 will occur before the += 1, then
// total (at test.equal time) will be 5. If forEach does not wait for the
// callbacks to complete, then total (at test.equal time) will be 0.
Meteor._sleepForMs(5);
await Meteor._sleepForMs(5);
}
total += doc.x;
// verify the meteor environment is set up here
coll2.insert({total:total});
await coll2.insertAsync({ total: total });
}, context);
test.equal(total, 14);
index = 0;
test.equal(cur.map(function (doc, i, cursor) {
// XXX we could theoretically make map run its iterations in parallel or
// something which would make this fail
test.equal(i, index++);
test.isTrue(cursor === cur);
test.isTrue(context === this);
return doc.x * 2;
}, context), [2, 8]);
test.equal(
await cur.mapAsync(function(doc, i, cursor) {
// XXX we could theoretically make map run its iterations in parallel or
// something which would make this fail
test.equal(i, index++);
test.isTrue(cursor === cur);
test.isTrue(context === this);
return doc.x * 2;
}, context),
[2, 8]
);
test.equal(_.pluck(coll.find({run: run}, {sort: {x: -1}}).fetch(), "x"),
[4, 1]);
test.equal(
_.pluck(await coll.find({ run: run }, { sort: { x: -1 } }).fetchAsync(), 'x'),
[4, 1]
);
expectObserve('', function () {
var count = coll.update({run: run, x: -1}, {$inc: {x: 2}}, {multi: true});
await expectObserve('', async function() {
var count = await coll.updateAsync(
{ run: run, x: -1 },
{ $inc: { x: 2 } },
{ multi: true }
);
test.equal(count, 0);
});
expectObserve('c(3,0,1)c(6,1,4)', function () {
var count = coll.update({run: run}, {$inc: {x: 2}}, {multi: true});
await expectObserve('c(3,0,1)c(6,1,4)', async function() {
var count = await coll.updateAsync(
{ run: run },
{ $inc: { x: 2 } },
{ multi: true }
);
test.equal(count, 2);
test.equal(_.pluck(coll.find({run: run}, {sort: {x: -1}}).fetch(), "x"),
[6, 3]);
test.equal(
_.pluck(await coll.find({ run: run }, { sort: { x: -1 } }).fetchAsync(), 'x'),
[6, 3]
);
});
expectObserve(['c(13,0,3)m(13,0,1)', 'm(6,1,0)c(13,1,3)',
'c(13,0,3)m(6,1,0)', 'm(3,0,1)c(13,1,3)'], function () {
coll.update({run: run, x: 3}, {$inc: {x: 10}}, {multi: true});
test.equal(_.pluck(coll.find({run: run}, {sort: {x: -1}}).fetch(), "x"),
[13, 6]);
});
await expectObserve(
[
'c(13,0,3)m(13,0,1)',
'm(6,1,0)c(13,1,3)',
'c(13,0,3)m(6,1,0)',
'm(3,0,1)c(13,1,3)',
],
async function() {
await coll.updateAsync({ run: run, x: 3 }, { $inc: { x: 10 } }, { multi: true });
test.equal(
_.pluck(await coll.find({ run: run }, { sort: { x: -1 } }).fetchAsync(), 'x'),
[13, 6]
);
}
);
expectObserve('r(13,1)', function () {
var count = coll.remove({run: run, x: {$gt: 10}});
await expectObserve('r(13,1)', async function() {
var count = await coll.removeAsync({ run: run, x: { $gt: 10 } });
test.equal(count, 1);
test.equal(coll.find({run: run}).count(), 1);
test.equal(await coll.find({ run: run }).countAsync(), 1);
});
expectObserve('r(6,0)', function () {
coll.remove({run: run});
test.equal(coll.find({run: run}).count(), 0);
await expectObserve('r(6,0)', async function() {
await coll.removeAsync({ run: run });
test.equal(await coll.find({ run: run }).countAsync(), 0);
});
expectObserve('', function () {
var count = coll.remove({run: run});
await expectObserve('', async function() {
var count = await coll.removeAsync({ run: run });
test.equal(count, 0);
test.equal(coll.find({run: run}).count(), 0);
test.equal(await coll.find({ run: run }).countAsync(), 0);
});
obs.stop();