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 48badcd commit d1ffe7c
Show file tree
Hide file tree
Showing 12 changed files with 324 additions and 379 deletions.
38 changes: 18 additions & 20 deletions test/docs/date.test.js
Expand Up @@ -86,27 +86,25 @@ describe('Date Tutorial', function() {
let Episode;
let db;

before(function() {
return co(function*() {
const episodeSchema = new mongoose.Schema({
title: String,
airedAt: {
type: Date,
// The dates of the first and last episodes of
// Star Trek: The Next Generation
min: '1987-09-28',
max: '1994-05-23'
}
});
db = yield start().asPromise();
Episode = db.model('Episode', episodeSchema);

yield Episode.create([
{ title: 'Encounter at Farpoint', airedAt: '1987-09-28' },
{ title: 'The Last Outpost', airedAt: '1987-10-19' },
{ title: 'Where No One Has Gone Before', airedAt: '1987-10-26' }
]);
before(async function() {
const episodeSchema = new mongoose.Schema({
title: String,
airedAt: {
type: Date,
// The dates of the first and last episodes of
// Star Trek: The Next Generation
min: '1987-09-28',
max: '1994-05-23'
}
});
db = await start().asPromise();
Episode = db.model('Episode', episodeSchema);

await Episode.create([
{ title: 'Encounter at Farpoint', airedAt: '1987-09-28' },
{ title: 'The Last Outpost', airedAt: '1987-10-19' },
{ title: 'Where No One Has Gone Before', airedAt: '1987-10-26' }
]);
});

it('date queries', function() {
Expand Down
109 changes: 52 additions & 57 deletions test/model.field.selection.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 @@ -446,7 +445,7 @@ describe('model field selection', function() {
});
});

it('sets defaults correctly in child docs with projection (gh-7159)', function() {
it('sets defaults correctly in child docs with projection (gh-7159)', async function() {
const CalendarSchema = new Schema({
dateFormat: {
type: String,
Expand Down Expand Up @@ -476,85 +475,81 @@ describe('model field selection', function() {
db.deleteModel(/BlogPost/);
const BlogPost = db.model('BlogPost', BlogPostSchema);

return co(function*() {
yield BlogPost.create({
author: 'me',
settings: {
calendar: {
dateFormat: '1234'
}

await BlogPost.create({
author: 'me',
settings: {
calendar: {
dateFormat: '1234'
}
});
}
});

yield BlogPost.updateOne({}, { $unset: { 'settings.calendar.locale': 1 } });
await BlogPost.updateOne({}, { $unset: { 'settings.calendar.locale': 1 } });

let doc = yield BlogPost.findOne();
assert.strictEqual(doc.settings.calendar.locale, 'en-gb');
assert.strictEqual(doc.settings.calendar.dateFormat, '1234');
let doc = await BlogPost.findOne();
assert.strictEqual(doc.settings.calendar.locale, 'en-gb');
assert.strictEqual(doc.settings.calendar.dateFormat, '1234');

doc = yield BlogPost.findOne().select('settings author');
assert.strictEqual(doc.settings.calendar.locale, 'en-gb');
assert.strictEqual(doc.settings.calendar.dateFormat, '1234');
});
doc = await BlogPost.findOne().select('settings author');
assert.strictEqual(doc.settings.calendar.locale, 'en-gb');
assert.strictEqual(doc.settings.calendar.dateFormat, '1234');
});

it('when `select: true` in schema, works with $elemMatch in projection', function() {
return co(function*() {
it('when `select: true` in schema, works with $elemMatch in projection', async function() {

const productSchema = new Schema({
attributes: {
select: true,
type: [{ name: String, group: String }]
}
});

const Product = db.model('Product', productSchema);
const productSchema = new Schema({
attributes: {
select: true,
type: [{ name: String, group: String }]
}
});

const attributes = [
{ name: 'a', group: 'alpha' },
{ name: 'b', group: 'beta' }
];
const Product = db.model('Product', productSchema);

yield Product.create({ name: 'test', attributes });
const attributes = [
{ name: 'a', group: 'alpha' },
{ name: 'b', group: 'beta' }
];

const product = yield Product.findOne()
.select({ attributes: { $elemMatch: { group: 'beta' } } });
await Product.create({ name: 'test', attributes });

assert.equal(product.attributes[0].name, 'b');
assert.equal(product.attributes[0].group, 'beta');
assert.equal(product.attributes.length, 1);
});
const product = await Product.findOne()
.select({ attributes: { $elemMatch: { group: 'beta' } } });

assert.equal(product.attributes[0].name, 'b');
assert.equal(product.attributes[0].group, 'beta');
assert.equal(product.attributes.length, 1);
});

it('selection specified in query overwrites option in schema', function() {
return co(function*() {
const productSchema = new Schema({ name: { type: String, select: false } });
it('selection specified in query overwrites option in schema', async function() {

const Product = db.model('Product', productSchema);
const productSchema = new Schema({ name: { type: String, select: false } });

const Product = db.model('Product', productSchema);

yield Product.create({ name: 'Computer' });

const product = yield Product.findOne().select('name');
await Product.create({ name: 'Computer' });

assert.equal(product.name, 'Computer');
});
const product = await Product.findOne().select('name');

assert.equal(product.name, 'Computer');
});

it('selecting with `false` instead of `0` doesn\'t overwrite schema `select: false` (gh-8923)', function() {
return co(function*() {
const userSchema = new Schema({
name: { type: String, select: false },
age: { type: Number }
});
it('selecting with `false` instead of `0` doesn\'t overwrite schema `select: false` (gh-8923)', async function() {

const User = db.model('User', userSchema);
const userSchema = new Schema({
name: { type: String, select: false },
age: { type: Number }
});

yield User.create({ name: 'Hafez', age: 25 });
const User = db.model('User', userSchema);

const user = yield User.findOne().select({ age: false });
await User.create({ name: 'Hafez', age: 25 });

assert.ok(!user.name);
});
const user = await User.findOne().select({ age: false });

assert.ok(!user.name);
});
});
45 changes: 20 additions & 25 deletions test/model.findOneAndDelete.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,22 +77,20 @@ describe('model: findOneAndDelete:', 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.findOneAndDelete({ title: title });
const doc = await M.findOneAndDelete({ title: title });

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

const gone = yield M.findById(post.id);
assert.equal(gone, null);
});
const gone = await M.findById(post.id);
assert.equal(gone, null);
});

it('options/conditions/doc are merged when no callback is passed', function(done) {
Expand Down Expand Up @@ -326,25 +323,23 @@ describe('model: findOneAndDelete:', 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.findOneAndDelete({ foo: '123' }, { name: 'bar' });
await Model.findOneAndDelete({ foo: '123' }, { name: 'bar' });

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

describe('middleware', function() {
Expand Down
48 changes: 22 additions & 26 deletions test/model.findOneAndRemove.test.js
Expand Up @@ -7,14 +7,13 @@
const start = require('./common');

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

const mongoose = start.mongoose;
const Schema = mongoose.Schema;
const ObjectId = Schema.Types.ObjectId;
const DocumentObjectId = mongoose.Types.ObjectId;

describe('model: findOneAndRemove:', function() {
describe('model: findOneAndRemove:', async function() {
let Comments;
let BlogPost;
let db;
Expand Down Expand Up @@ -78,22 +77,20 @@ describe('model: findOneAndRemove:', 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.findOneAndRemove({ title: title });
const doc = await M.findOneAndRemove({ title: title });

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

const gone = yield M.findById(post.id);
assert.equal(gone, null);
});
const gone = await M.findById(post.id);
assert.equal(gone, null);
});

it('options/conditions/doc are merged when no callback is passed', function(done) {
Expand Down Expand Up @@ -326,25 +323,24 @@ describe('model: findOneAndRemove:', 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.findOneAndRemove({ foo: '123' }, { name: 'bar' });
await Model.findOneAndRemove({ foo: '123' }, { name: 'bar' });

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

it('with orFail() (gh-9381)', function() {
Expand Down

0 comments on commit d1ffe7c

Please sign in to comment.