@benjamin-vanryseghem/mongoose-dummy
Version:
Mongoose dummy data generator
129 lines (120 loc) • 4.7 kB
JavaScript
'use strict';
process.env.NODE_ENV = 'test';
const chai = require("chai");
const mongoose = require('mongoose');
const should = chai.should();
const expect = chai.expect;
const dummy = require('..');
const isObjectId = mongoose.Types.ObjectId.isValid;
function validateEmail(email) {
let re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
}
describe('mongoose-dummy', () => {
describe('generateRandomModel', () => {
it('should generate random model', (done) => {
const ignoredFields = ['_id', 'created_at', '__v', /detail.*_info/];
let genderValues = ['Male', 'Female'];
let schemaDefinition = new mongoose.Schema({
name: {
type: String,
required: true,
lowercase: true,
trim: true
},
email: {
type: String,
},
birth_date: {
type: Date
},
gender: {
type: String,
enum: genderValues
},
data: {
type: Object,
default: null
},
results: [
{
score: Number,
course: Number
}
],
subSchema: {
type: new mongoose.Schema({ score: Number, name: String })
},
subSchemaArray: {
type: [new mongoose.Schema({ score: Number, name: String })]
},
is_student: {
type: Boolean
},
parent: {
type: mongoose.Schema.Types.ObjectId
},
school: {
type: mongoose.Schema.Types.ObjectId,
ref: "School"
},
detail: {
main_info: String,
some_info: String,
none_match: String
},
created_at: {
type: Date,
default: Date.now
}
}, { toObject: { virtuals: true }});
schemaDefinition.virtual("books", {
ref: "Book",
localField: "_id",
foreignField: "owner",
justOne: false
});
schemaDefinition.virtual("briefcase", {
ref: "BriefCase",
localField: "_id",
foreignField: "owner",
justOne: true
});
let model = mongoose.model('Student', schemaDefinition);
let randomObject = dummy(model, {
ignore: ignoredFields,
returnDate: true,
maxDepth: 3,
resolveRef: ref => `${__dirname}/${ref.toLowerCase()}.js`,
force: {
parent: '5af8a4f33f56930349d8f45b'
}
});
expect(randomObject).to.not.be.null;
randomObject.name.should.be.a('string');
randomObject.email.should.be.a('string');
randomObject.detail.none_match.should.be.a('string');
validateEmail(randomObject.email).should.be.true;
randomObject.birth_date.should.be.a('date');
genderValues.indexOf(randomObject.gender).should.not.eql(-1);
randomObject.data.should.be.an('object');
randomObject.results.should.be.an('array');
randomObject.results[0].should.have.property('score');
randomObject.is_student.should.be.a('boolean');
randomObject.parent.should.equal('5af8a4f33f56930349d8f45b');
randomObject.school.name.should.be.a('string');
randomObject.school.description.should.be.a('string');
randomObject.books.should.be.an('array');
randomObject.books[0].description.should.be.a('string');
randomObject.briefcase.description.should.be.a('string');
isObjectId(randomObject.parent).should.be.true;
// Check ignore fields
expect(randomObject.created_at).to.be.undefined;
expect(randomObject._id).to.be.undefined;
expect(randomObject.__v).to.be.undefined;
expect(randomObject.detail.main_info).to.be.undefined;
expect(randomObject.detail.some_info).to.be.undefined;
done();
});
});
});