Skip to content

Commit

Permalink
test: fix some more tests re: #8481
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Mar 15, 2020
1 parent 778f574 commit 9670bf4
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 137 deletions.
68 changes: 35 additions & 33 deletions test/aggregate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,23 @@ const Schema = mongoose.Schema;
* Test data
*/

const EmployeeSchema = new Schema({
name: String,
sal: Number,
dept: String,
customers: [String],
reportsTo: String
});

mongoose.model('Employee', EmployeeSchema);

function setupData(db, callback) {
const EmployeeSchema = new Schema({
name: String,
sal: Number,
dept: String,
customers: [String],
reportsTo: String
});

let saved = 0;
const emps = [
{ name: 'Alice', sal: 18000, dept: 'sales', customers: ['Eve', 'Fred'] },
{ name: 'Bob', sal: 15000, dept: 'sales', customers: ['Gary', 'Herbert', 'Isaac'], reportsTo: 'Alice' },
{ name: 'Carol', sal: 14000, dept: 'r&d', reportsTo: 'Bob' },
{ name: 'Dave', sal: 14500, dept: 'r&d', reportsTo: 'Carol' }
];
const Employee = db.model('Employee');
const Employee = db.model('Employee', EmployeeSchema);

Employee.deleteMany({}, function() {
emps.forEach(function(data) {
Expand Down Expand Up @@ -94,6 +92,10 @@ describe('aggregate: ', function() {
db.close(done);
});

beforeEach(() => db.deleteModel(/.*/));

afterEach(() => require('./util').clearTestData(db));

describe('append', function() {
it('(pipeline)', function(done) {
const aggregate = new Aggregate();
Expand Down Expand Up @@ -616,7 +618,7 @@ describe('aggregate: ', function() {
});

describe('exec', function() {
before(function(done) {
beforeEach(function(done) {
setupData(db, done);
});

Expand Down Expand Up @@ -774,7 +776,7 @@ describe('aggregate: ', function() {
aggregate.
model(db.model('Employee')).
graphLookup({
from: 'employees',
from: 'Employee',
startWith: '$reportsTo',
connectFromField: 'reportsTo',
connectToField: 'name',
Expand Down Expand Up @@ -1001,7 +1003,7 @@ describe('aggregate: ', function() {
next();
});

const M = db.model('gh5251', s);
const M = db.model('Test', s);

M.aggregate([{ $match: { name: 'test' } }], function(error, res) {
assert.ifError(error);
Expand All @@ -1019,7 +1021,7 @@ describe('aggregate: ', function() {
next();
});

const M = db.model('gh7606', s);
const M = db.model('Test', s);

return co(function*() {
yield M.create([{ name: 'alpha' }, { name: 'Zeta' }]);
Expand All @@ -1039,7 +1041,7 @@ describe('aggregate: ', function() {
next();
});

const M = db.model('gh8017', s);
const M = db.model('Test', s);

return co(function*() {
yield M.create([{ name: 'alpha' }, { name: 'Zeta' }]);
Expand All @@ -1060,7 +1062,7 @@ describe('aggregate: ', function() {
next();
});

const M = db.model('gh5251_post', s);
const M = db.model('Test', s);

M.aggregate([{ $match: { name: 'test' } }], function(error, res) {
assert.ifError(error);
Expand All @@ -1080,7 +1082,7 @@ describe('aggregate: ', function() {
next();
});

const M = db.model('gh5251_error_agg', s);
const M = db.model('Test', s);

M.aggregate([{ $fakeStage: { name: 'test' } }], function(error, res) {
assert.ok(error);
Expand All @@ -1105,7 +1107,7 @@ describe('aggregate: ', function() {
next();
});

const M = db.model('gh5251_error', s);
const M = db.model('Test', s);

M.aggregate([{ $match: { name: 'test' } }], function(error, res) {
assert.ok(error);
Expand All @@ -1131,7 +1133,7 @@ describe('aggregate: ', function() {
next();
});

const M = db.model('gh5251_cursor', s);
const M = db.model('Test', s);

let numDocs = 0;
M.
Expand Down Expand Up @@ -1163,7 +1165,7 @@ describe('aggregate: ', function() {
next();
});

const M = db.model('gh5887_cursor', s);
const M = db.model('Test', s);

return M.aggregate([{ $match: { name: 'test' } }]).explain().
then(() => {
Expand All @@ -1176,7 +1178,7 @@ describe('aggregate: ', function() {

it('readPref from schema (gh-5522)', function(done) {
const schema = new Schema({ name: String }, { read: 'secondary' });
const M = db.model('gh5522', schema);
const M = db.model('Test', schema);
const a = M.aggregate();
assert.equal(a.options.readPreference.mode, 'secondary');

Expand All @@ -1189,7 +1191,7 @@ describe('aggregate: ', function() {
});

it('cursor (gh-3160)', function() {
const MyModel = db.model('gh3160', { name: String });
const MyModel = db.model('Test', { name: String });

return co(function * () {
yield MyModel.create({ name: 'test' });
Expand All @@ -1205,7 +1207,7 @@ describe('aggregate: ', function() {
});

it('catch() (gh-7267)', function() {
const MyModel = db.model('gh7267', {});
const MyModel = db.model('Test', {});

return co(function * () {
const err = yield MyModel.aggregate([{ $group: { foo: 'bar' } }]).
Expand All @@ -1218,7 +1220,7 @@ describe('aggregate: ', function() {
it('cursor() without options (gh-3855)', function(done) {
const db = start();

const MyModel = db.model('gh3855', { name: String });
const MyModel = db.model('Test', { name: String });

db.on('open', function() {
const cursor = MyModel.
Expand All @@ -1231,7 +1233,7 @@ describe('aggregate: ', function() {
});

it('cursor() with useMongooseAggCursor (gh-5145)', function(done) {
const MyModel = db.model('gh5145', { name: String });
const MyModel = db.model('Test', { name: String });

const cursor = MyModel.
aggregate([{ $match: { name: 'test' } }]).
Expand All @@ -1243,7 +1245,7 @@ describe('aggregate: ', function() {
});

it('cursor() with useMongooseAggCursor works (gh-5145) (gh-5394)', function(done) {
const MyModel = db.model('gh5394', { name: String });
const MyModel = db.model('Test', { name: String });

MyModel.create({ name: 'test' }, function(error) {
assert.ifError(error);
Expand All @@ -1265,7 +1267,7 @@ describe('aggregate: ', function() {
});

it('cursor() eachAsync (gh-4300)', function(done) {
const MyModel = db.model('gh4300', { name: String });
const MyModel = db.model('Test', { name: String });

let cur = 0;
const expectedNames = ['Axl', 'Slash'];
Expand Down Expand Up @@ -1294,7 +1296,7 @@ describe('aggregate: ', function() {
});

it('cursor() eachAsync with options (parallel)', function(done) {
const MyModel = db.model('gh-6168', { name: String });
const MyModel = db.model('Test', { name: String });

const names = [];
const startedAt = [];
Expand Down Expand Up @@ -1327,7 +1329,7 @@ describe('aggregate: ', function() {
});

it('ability to add noCursorTimeout option (gh-4241)', function(done) {
const MyModel = db.model('gh4241', {
const MyModel = db.model('Test', {
name: String
});

Expand All @@ -1344,7 +1346,7 @@ describe('aggregate: ', function() {
});

it('query by document (gh-4866)', function(done) {
const MyModel = db.model('gh4866', {
const MyModel = db.model('Test', {
name: String
});

Expand All @@ -1359,7 +1361,7 @@ describe('aggregate: ', function() {
it('sort by text score (gh-5258)', function(done) {
const mySchema = new Schema({ test: String });
mySchema.index({ test: 'text' });
const M = db.model('gh5258', mySchema);
const M = db.model('Test', mySchema);

M.on('index', function(error) {
assert.ifError(error);
Expand Down Expand Up @@ -1388,7 +1390,7 @@ describe('aggregate: ', function() {
it('adds hint option', function(done) {
const mySchema = new Schema({ name: String, qty: Number });
mySchema.index({ qty: -1, name: -1 });
const M = db.model('gh6251', mySchema);
const M = db.model('Test', mySchema);
M.on('index', function(error) {
assert.ifError(error);
const docs = [
Expand Down

0 comments on commit 9670bf4

Please sign in to comment.