Skip to content

Commit

Permalink
refactor more tests to async/await
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdelrahmanHafez committed Aug 29, 2021
1 parent 3089342 commit 48badcd
Show file tree
Hide file tree
Showing 5 changed files with 270 additions and 298 deletions.
38 changes: 17 additions & 21 deletions test/docs/transactions.test.js
Expand Up @@ -323,45 +323,41 @@ describe('transactions', function() {
then(() => session.endSession());
});

it('remove, update, updateOne (gh-7455)', function() {
it('remove, update, updateOne (gh-7455)', async function() {
const Character = db.model('gh7455_Character', new Schema({ name: String, title: String }, { versionKey: false }));

return co(function*() {
yield Character.create({ name: 'Tyrion Lannister' });
const session = yield db.startSession();
await Character.create({ name: 'Tyrion Lannister' });
const session = await db.startSession();

session.startTransaction();

const tyrion = yield Character.findOne().session(session);
const tyrion = await Character.findOne().session(session);

yield tyrion.updateOne({ title: 'Hand of the King' });
await tyrion.updateOne({ title: 'Hand of the King' });

// Session isn't committed
assert.equal(yield Character.countDocuments({ title: /hand/i }), 0);
assert.equal(await Character.countDocuments({ title: /hand/i }), 0);

yield tyrion.remove();
await tyrion.remove();

// Undo both update and delete since doc should pull from `$session()`
yield session.abortTransaction();
await session.abortTransaction();
session.endSession();

const fromDb = yield Character.findOne().then(doc => doc.toObject());
const fromDb = await Character.findOne().then(doc => doc.toObject());
delete fromDb._id;
assert.deepEqual(fromDb, { name: 'Tyrion Lannister' });
});
});

it('save() with no changes (gh-8571)', function() {
return co(function*() {
it('save() with no changes (gh-8571)', async function() {
const Test = db.model('Test', Schema({ name: String }));

yield Test.createCollection();
const session = yield db.startSession();
yield session.withTransaction(() => co(function*() {
const test = yield Test.create([{}], { session }).then(res => res[0]);
yield test.save(); // throws DocumentNotFoundError
}));
yield session.endSession();
});
await Test.createCollection();
const session = await db.startSession();
await session.withTransaction(async () => {
const test = await Test.create([{}], { session }).then(res => res[0]);
await test.save(); // throws DocumentNotFoundError
});
await session.endSession();
});
});
112 changes: 52 additions & 60 deletions test/model.findOneAndReplace.test.js
Expand Up @@ -7,7 +7,6 @@
const start = require('./common');

const assert = require('assert');
const co = require('co');

const mongoose = start.mongoose;
const Schema = mongoose.Schema;
Expand Down Expand Up @@ -78,19 +77,17 @@ describe('model: findOneAndReplace:', function() {
BlogPost = db.model('BlogPost', BlogPost);
});

it('returns the original document', function() {
it('returns the original document', async function() {
const M = BlogPost;
const title = 'remove muah';

const post = new M({ title: title });

return co(function*() {
yield post.save();
await post.save();

const doc = yield M.findOneAndReplace({ title: title });
const doc = await M.findOneAndReplace({ title: title });

assert.equal(post.id, doc.id);
});
assert.equal(post.id, doc.id);
});

it('options/conditions/doc are merged when no callback is passed', function(done) {
Expand Down Expand Up @@ -292,25 +289,24 @@ describe('model: findOneAndReplace:', function() {
});
});

it('only calls setters once (gh-6203)', function() {
return co(function*() {
const calls = [];
const userSchema = new mongoose.Schema({
name: String,
foo: {
type: String,
set: function(val) {
calls.push(val);
return val + val;
}
it('only calls setters once (gh-6203)', async function() {

const calls = [];
const userSchema = new mongoose.Schema({
name: String,
foo: {
type: String,
set: function(val) {
calls.push(val);
return val + val;
}
});
const Model = db.model('Test', userSchema);
}
});
const Model = db.model('Test', userSchema);

yield Model.findOneAndReplace({ foo: '123' }, { name: 'bar' });
await Model.findOneAndReplace({ foo: '123' }, { name: 'bar' });

assert.deepEqual(calls, ['123']);
});
assert.deepEqual(calls, ['123']);
});

describe('middleware', function() {
Expand Down Expand Up @@ -388,69 +384,65 @@ describe('model: findOneAndReplace:', function() {
});
});

it('works (gh-7654)', function() {
it('works (gh-7654)', async function() {
const schema = new Schema({ name: String, age: Number });
const Model = db.model('Test', schema);

return co(function*() {
yield Model.findOneAndReplace({}, { name: 'Jean-Luc Picard', age: 59 }, { upsert: true });

const doc = yield Model.findOne();
assert.equal(doc.name, 'Jean-Luc Picard');
await Model.findOneAndReplace({}, { name: 'Jean-Luc Picard', age: 59 }, { upsert: true });

const err = yield Model.findOneAndReplace({}, { age: 'not a number' }, {}).
then(() => null, err => err);
assert.ok(err);
assert.ok(err.errors['age'].message.indexOf('not a number') !== -1,
err.errors['age'].message);
});
const doc = await Model.findOne();
assert.equal(doc.name, 'Jean-Luc Picard');

const err = await Model.findOneAndReplace({}, { age: 'not a number' }, {}).
then(() => null, err => err);
assert.ok(err);
assert.ok(err.errors['age'].message.indexOf('not a number') !== -1,
err.errors['age'].message);
});

it('schema-level projection (gh-7654)', function() {
it('schema-level projection (gh-7654)', async function() {
const schema = new Schema({ name: String, age: { type: Number, select: false } });
const Model = db.model('Test', schema);

return co(function*() {
const doc = yield Model.findOneAndReplace({}, { name: 'Jean-Luc Picard', age: 59 }, {
upsert: true,
returnOriginal: false
});

assert.ok(!doc.age);
const doc = await Model.findOneAndReplace({}, { name: 'Jean-Luc Picard', age: 59 }, {
upsert: true,
returnOriginal: false
});

assert.ok(!doc.age);
});

it('supports `new` in addition to `returnOriginal` (gh-7846)', function() {
it('supports `new` in addition to `returnOriginal` (gh-7846)', async function() {
const schema = new Schema({ name: String, age: Number });
const Model = db.model('Test', schema);

return co(function*() {
const doc = yield Model.findOneAndReplace({}, { name: 'Jean-Luc Picard', age: 59 }, {
upsert: true,
new: true
});

assert.equal(doc.age, 59);
const doc = await Model.findOneAndReplace({}, { name: 'Jean-Luc Picard', age: 59 }, {
upsert: true,
new: true
});

assert.equal(doc.age, 59);
});

it('orFail() (gh-8030)', function() {
it('orFail() (gh-8030)', async function() {
const schema = Schema({ name: String, age: Number });
const Model = db.model('Test', schema);

return co(function*() {
let err = yield Model.findOneAndReplace({}, { name: 'test' }).orFail().
then(() => assert.ok(false), err => err);

assert.ok(err);
assert.equal(err.name, 'DocumentNotFoundError');
let err = await Model.findOneAndReplace({}, { name: 'test' }).orFail().
then(() => assert.ok(false), err => err);

yield Model.create({ name: 'test' });
err = yield Model.findOneAndReplace({ name: 'test' }, { name: 'test2' }).
orFail().
then(() => null, err => err);
assert.ok(err);
assert.equal(err.name, 'DocumentNotFoundError');

assert.ifError(err);
});
await Model.create({ name: 'test' });
err = await Model.findOneAndReplace({ name: 'test' }, { name: 'test2' }).
orFail().
then(() => null, err => err);

assert.ifError(err);
});
});

0 comments on commit 48badcd

Please sign in to comment.