Skip to content

Commit

Permalink
refactor: move more schemas into before hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Mar 23, 2017
1 parent b72eaca commit a160e4e
Show file tree
Hide file tree
Showing 10 changed files with 232 additions and 209 deletions.
24 changes: 12 additions & 12 deletions test/model.stream.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ var start = require('./common'),

var names = ('Aaden Aaron Adrian Aditya Agustin Jim Bob Jonah Frank Sally Lucy').split(' ');

/**
* Setup.
*/

var Person = new Schema({
name: String
});

mongoose.model('PersonForStream', Person);
var collection = 'personforstream_' + random();

describe('query stream:', function() {
var db = start();
var P = db.model('PersonForStream', collection);
var Person;
var collection = 'personforstream_' + random();
var P;

before(function() {
Person = new Schema({
name: String
});

mongoose.model('PersonForStream', Person);
P = db.model('PersonForStream', collection);
});

before(function(done) {
var people = names.map(function(name) {
Expand Down
101 changes: 51 additions & 50 deletions test/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,62 +14,65 @@ var start = require('./common'),
EmbeddedDocument = mongoose.Types.Embedded,
MongooseError = mongoose.Error;

/**
* Setup.
*/

var Comments = new Schema;
describe('Model', function() {
var db;
var Test;
var Comments;
var BlogPost;
var bpSchema;
var collection;

Comments.add({
title: String,
date: Date,
body: String,
comments: [Comments]
});
before(function() {
Comments = new Schema;

var BlogPost = new Schema({
title: String,
author: String,
slug: String,
date: Date,
meta: {
date: Date,
visitors: Number
},
published: Boolean,
mixed: {},
numbers: [Number],
owners: [ObjectId],
comments: [Comments],
nested: {array: [Number]}
});
Comments.add({
title: String,
date: Date,
body: String,
comments: [Comments]
});

BlogPost
.virtual('titleWithAuthor')
.get(function() {
return this.get('title') + ' by ' + this.get('author');
})
.set(function(val) {
var split = val.split(' by ');
this.set('title', split[0]);
this.set('author', split[1]);
});
BlogPost = new Schema({
title: String,
author: String,
slug: String,
date: Date,
meta: {
date: Date,
visitors: Number
},
published: Boolean,
mixed: {},
numbers: [Number],
owners: [ObjectId],
comments: [Comments],
nested: {array: [Number]}
});

BlogPost.method('cool', function() {
return this;
});
BlogPost
.virtual('titleWithAuthor')
.get(function() {
return this.get('title') + ' by ' + this.get('author');
})
.set(function(val) {
var split = val.split(' by ');
this.set('title', split[0]);
this.set('author', split[1]);
});

BlogPost.static('woot', function() {
return this;
});
BlogPost.method('cool', function() {
return this;
});

mongoose.model('BlogPost', BlogPost);
var bpSchema = BlogPost;
BlogPost.static('woot', function() {
return this;
});

var collection = 'blogposts_' + random();
mongoose.model('BlogPost', BlogPost);
bpSchema = BlogPost;

describe('Model', function() {
var db, Test;
collection = 'blogposts_' + random();
});

before(function() {
db = start();
Expand Down Expand Up @@ -122,9 +125,7 @@ describe('Model', function() {
});
});
});
});

describe('Model', function() {
describe('constructor', function() {
it('works without "new" keyword', function(done) {
var B = mongoose.model('BlogPost');
Expand Down
117 changes: 59 additions & 58 deletions test/model.update.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,71 +10,72 @@ var start = require('./common'),
ObjectId = Schema.Types.ObjectId,
DocumentObjectId = mongoose.Types.ObjectId;

/**
* Setup.
*/

var Comments = new Schema({});

Comments.add({
title: String,
date: Date,
body: String,
comments: [Comments]
});

var BlogPost = new Schema({
title: String,
author: String,
slug: String,
date: Date,
meta: {
date: Date,
visitors: Number
},
published: Boolean,
mixed: {},
numbers: [Number],
owners: [ObjectId],
comments: [Comments]
}, {strict: false});

BlogPost.virtual('titleWithAuthor')
.get(function() {
return this.get('title') + ' by ' + this.get('author');
})
.set(function(val) {
var split = val.split(' by ');
this.set('title', split[0]);
this.set('author', split[1]);
});
describe('model: update:', function() {
var post;
var title = 'Tobi ' + random();
var author = 'Brian ' + random();
var newTitle = 'Woot ' + random();
var id0;
var id1;
var Comments;
var BlogPost;
var collection;
var strictSchema;

before(function() {
Comments = new Schema({});

Comments.add({
title: String,
date: Date,
body: String,
comments: [Comments]
});

BlogPost.method('cool', function() {
return this;
});
BlogPost = new Schema({
title: String,
author: String,
slug: String,
date: Date,
meta: {
date: Date,
visitors: Number
},
published: Boolean,
mixed: {},
numbers: [Number],
owners: [ObjectId],
comments: [Comments]
}, {strict: false});

BlogPost.static('woot', function() {
return this;
});
BlogPost.virtual('titleWithAuthor')
.get(function() {
return this.get('title') + ' by ' + this.get('author');
})
.set(function(val) {
var split = val.split(' by ');
this.set('title', split[0]);
this.set('author', split[1]);
});

mongoose.model('BlogPostForUpdates', BlogPost);
BlogPost.method('cool', function() {
return this;
});

var collection = 'blogposts_' + random();
BlogPost.static('woot', function() {
return this;
});

var strictSchema = new Schema({name: String, x: {nested: String}});
strictSchema.virtual('foo').get(function() {
return 'i am a virtual FOO!';
});
mongoose.model('UpdateStrictSchema', strictSchema);
mongoose.model('BlogPostForUpdates', BlogPost);

collection = 'blogposts_' + random();

describe('model: update:', function() {
var post,
title = 'Tobi ' + random(),
author = 'Brian ' + random(),
newTitle = 'Woot ' + random(),
id0,
id1;
strictSchema = new Schema({name: String, x: {nested: String}});
strictSchema.virtual('foo').get(function() {
return 'i am a virtual FOO!';
});
mongoose.model('UpdateStrictSchema', strictSchema);
});

before(function(done) {
var db = start(),
Expand Down
23 changes: 13 additions & 10 deletions test/object.create.null.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,24 @@ var start = require('./common'),
mongoose = start.mongoose,
Schema = mongoose.Schema;

var schema = new Schema({
a: String,
b: {
c: Number,
d: [{e: String}]
},
f: {g: Date},
h: {}
});
var schema;

describe('is compatible with object created using Object.create(null) (gh-1484)', function() {
var db;
var M;

before(function() {
schema = new Schema({
a: String,
b: {
c: Number,
d: [{e: String}]
},
f: {g: Date},
h: {}
});
});

before(function() {
db = start();
M = db.model('1484', schema);
Expand Down Expand Up @@ -133,4 +137,3 @@ describe('is compatible with object created using Object.create(null) (gh-1484)'
done();
});
});

45 changes: 25 additions & 20 deletions test/promise_provider.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,33 @@ var PromiseProvider = require('../lib/promise_provider');
var Schema = require('../lib/schema');

var db;
var testSchema = new Schema({test: {type: String, required: true}});
testSchema.pre('save', function(next) {
if (this.$__saveSucceeds === false) {
return next(new Error('fail'));
}
next();
});
testSchema.pre('validate', function(next) {
if (this.$__validateSucceeds === false) {
return next(new Error('validation failed'));
}
next();
});
testSchema.pre('findOne', function(next) {
if (this.$__findOneSucceeds === false) {
return next(new Error('findOne failed'));
}
next();
});
var MyModel;

describe('ES6 promises: ', function() {
var testSchema;
var MyModel;

before(function() {
testSchema = new Schema({test: {type: String, required: true}});
testSchema.pre('save', function(next) {
if (this.$__saveSucceeds === false) {
return next(new Error('fail'));
}
next();
});
testSchema.pre('validate', function(next) {
if (this.$__validateSucceeds === false) {
return next(new Error('validation failed'));
}
next();
});
testSchema.pre('findOne', function(next) {
if (this.$__findOneSucceeds === false) {
return next(new Error('findOne failed'));
}
next();
});
});

describe('native: ', function() {
if (!global.Promise) {
return;
Expand Down
Loading

0 comments on commit a160e4e

Please sign in to comment.