2017-11-01 19:46:15 +00:00
|
|
|
'use strict';
|
|
|
|
|
2017-10-11 05:07:13 +00:00
|
|
|
// Dependencies
|
2017-11-01 19:46:15 +00:00
|
|
|
var util = require('util'),
|
|
|
|
assert = require('assert'),
|
|
|
|
mongoose = require('mongoose'),
|
|
|
|
timestamp = require('../../libs/timestamp.server.plugin'),
|
|
|
|
Schema = mongoose.Schema,
|
|
|
|
ObjectId = Schema.ObjectId;
|
2017-10-11 05:07:13 +00:00
|
|
|
|
|
|
|
// Run tests
|
|
|
|
describe('Timestamp', function () {
|
|
|
|
describe('#default()', function () {
|
2017-11-01 19:46:15 +00:00
|
|
|
var FooSchema = new Schema();
|
|
|
|
FooSchema.plugin(timestamp);
|
|
|
|
var FooModel = mongoose.model('timeFoo', FooSchema),
|
|
|
|
bar = new FooModel();
|
2017-10-11 05:07:13 +00:00
|
|
|
|
|
|
|
before(function () {
|
|
|
|
FooModel.remove(function (err) {
|
2017-11-01 19:46:15 +00:00
|
|
|
assert.strictEqual(err, null);
|
|
|
|
});
|
|
|
|
});
|
2017-10-11 05:07:13 +00:00
|
|
|
|
|
|
|
it('should have custom properties', function (done) {
|
2017-11-01 19:46:15 +00:00
|
|
|
assert.strictEqual(typeof FooSchema.virtuals.created, 'object');
|
|
|
|
assert.strictEqual(typeof FooSchema.paths.modified, 'object');
|
|
|
|
done();
|
|
|
|
});
|
2017-10-11 05:07:13 +00:00
|
|
|
|
|
|
|
it('should create the default attributes', function (done) {
|
|
|
|
bar.save(function (err, doc) {
|
2017-11-01 19:46:15 +00:00
|
|
|
assert.strictEqual(err, null);
|
|
|
|
assert.strictEqual(util.isDate(doc.created), true);
|
|
|
|
assert.strictEqual(util.isDate(doc.modified), true);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-10-11 05:07:13 +00:00
|
|
|
|
|
|
|
describe('#custom()', function () {
|
2017-11-01 19:46:15 +00:00
|
|
|
var FooSchema = new Schema();
|
2017-10-11 05:07:13 +00:00
|
|
|
FooSchema.plugin(timestamp, {
|
2017-11-01 19:46:15 +00:00
|
|
|
createdPath: 'oh',
|
|
|
|
modifiedPath: 'hai',
|
|
|
|
useVirtual: false
|
|
|
|
});
|
|
|
|
var BarModel = mongoose.model('timeBar', FooSchema),
|
|
|
|
bar = new BarModel();
|
2017-10-11 05:07:13 +00:00
|
|
|
|
|
|
|
before(function () {
|
|
|
|
BarModel.remove(function (err) {
|
2017-11-01 19:46:15 +00:00
|
|
|
assert.strictEqual(err, null);
|
|
|
|
});
|
|
|
|
});
|
2017-10-11 05:07:13 +00:00
|
|
|
|
|
|
|
it('should have custom properties', function (done) {
|
2017-11-01 19:46:15 +00:00
|
|
|
assert.strictEqual(typeof FooSchema.paths.oh, 'object');
|
|
|
|
assert.strictEqual(typeof FooSchema.paths.hai, 'object');
|
|
|
|
done();
|
|
|
|
});
|
2017-10-11 05:07:13 +00:00
|
|
|
|
|
|
|
it('should create custom attributes', function (done) {
|
|
|
|
bar.save(function (err, doc) {
|
2017-11-01 19:46:15 +00:00
|
|
|
assert.strictEqual(err, null);
|
|
|
|
assert.strictEqual(util.isDate(doc.oh), true);
|
|
|
|
assert.strictEqual(util.isDate(doc.hai), true);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|