Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates to mongoose 3.0 and replaces expresso with mocha #26

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
**.swp
node_modules
8 changes: 2 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
EXPRESSO = support/expresso/bin/expresso -I lib

MOCHA = ./node_modules/mocha/bin/mocha
TESTS = tests/*.test.js

test:
@$(EXPRESSO) $(TESTS) $(TEST_FLAGS)

test-cov:
@$(MAKE) TEST_FLAGS=--cov test
@$(MOCHA) $(TESTS) $(TEST_FLAGS)

.PHONY: test test-cov
2 changes: 1 addition & 1 deletion lib/plugins/useTimestamps.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var mongoose = require('mongoose')
, ObjectID = mongoose.ObjectID
, BinaryParser = mongoose.mongo.BinaryParser;
, BinaryParser = require('mongoose/node_modules/mongodb/node_modules/bson').BinaryParser

exports.useTimestamps = function (schema, options) {
if (schema.path('_id')) {
Expand Down
2 changes: 1 addition & 1 deletion lib/types/email.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var mongoose = require('mongoose')

module.exports.loadType = function (mongoose) {
var SchemaTypes = mongoose.SchemaTypes;
var SchemaTypes = mongoose.Schema.Types;

function Email (path, options) {
SchemaTypes.String.call(this, path, options);
Expand Down
2 changes: 1 addition & 1 deletion lib/types/url.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var Url = require("url");

module.exports.loadType = function (mongoose) {
var SchemaTypes = mongoose.SchemaTypes;
var SchemaTypes = mongoose.Schema.Types;

function Url (path, options) {
SchemaTypes.String.call(this, path, options);
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
, "description": "More types for mongoose"
, "version": "1.0.3"
, "author": "Brian Noguchi"
, "dependencies": { "mongoose": ">= 1.0.16"}
, "dependencies": { "mongoose": ">= 3.0.0"}
, "devDependencies": { "mocha": "~1.3.0" }
, "keywords": [ "mongoose", "mongo", "mongodb", "types" ]
, "scripts": { "test": "make test" }
, "engines": { "node": ">= 0.1.101" }
, "engines": { "node": ">= 0.6.x" }
, "main": "./index"
, "licenses": [ { "type": "The MIT License", "url": "http://www.opensource.org/licenses/mit-license.php" } ]
}
1 change: 0 additions & 1 deletion support/expresso
Submodule expresso deleted from 7f10ab
54 changes: 31 additions & 23 deletions tests/email.test.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,54 @@
require('should');
var mongoose = require('mongoose')
var assert = require('assert')
, mongoose = require('mongoose')
, Schema = mongoose.Schema
, db = mongoose.createConnection('mongodb://localhost/mongoose_types_tests');

require("../").loadTypes(mongoose, 'email');

var UserSchema = new Schema({
email: mongoose.SchemaTypes.Email
email: mongoose.Schema.Types.Email
});

mongoose.model('User', UserSchema);
var User;

module.exports = {
before: function(){
User = db.model('User', UserSchema);
User.remove({}, function (err) {});
},
'test invalid email validation': function () {
describe('email', function () {

it('should test invalid email validation', function (done) {
var user = new User({email: 'hello'});
user.save(function (err) {
err.message.should.equal('Validator "email is invalid" failed for path email');
user.isNew.should.be.true;
assert.equal(err.message, 'Validation failed')
assert(user.isNew);
done();
});
},
'test valid email validation': function () {
});

it('should test valid email validation', function (done) {
var user = new User({ email: '[email protected]' });
user.save(function (err) {
err.should.eql(null);
user.isNew.should.be.false;
assert.equal(err, null);
assert.equal(user.isNew, false);
done();
});
},
'email should be converted to lowercase': function () {
});

it('should convert email to lowercase', function (done) {
var user = new User({ email: '[email protected]'});
user.save(function (err) {
user.email.should.equal('[email protected]');
assert.equal(user.email, '[email protected]');
User.findById(user._id, function (err, refreshed) {
refreshed.email.should.equal('[email protected]');
assert.equal(refreshed.email, '[email protected]')
done();
});
});
},
teardown: function(){
});

before(function () {
User = db.model('User', UserSchema);
User.remove({}, function (err) {});
});

after(function () {
db.close();
}
};
});
});
62 changes: 30 additions & 32 deletions tests/url.test.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,53 @@
require('should');
var mongoose = require('mongoose')
var assert = require('assert')
, mongoose = require('mongoose')
, Schema = mongoose.Schema
, db = mongoose.createConnection('mongodb://localhost/mongoose_types_tests');

require("../").loadTypes(mongoose, 'url');

var WebpageSchema = new Schema({
url: mongoose.SchemaTypes.Url
url: mongoose.Schema.Types.Url
});

mongoose.model('Webpage', WebpageSchema);
var Webpage;

module.exports = {
before: function(){
Webpage = db.model('Webpage', WebpageSchema);
Webpage.remove({}, function (err) {});
},
'test invalid url validation': function () {
describe('url', function () {
it('should test invalid url validation', function (done) {
var webpage = new Webpage({url: 'file:///home/'});
webpage.save(function (err) {
err.message.should.equal('Validator "url is invalid" failed for path url');
webpage.isNew.should.be.true;
assert.equal(err.message, 'Validation failed');
assert(webpage.isNew);
done();
});
},
'test valid url validation': function () {
});

it('should test valid url validation', function (done) {
var webpage = new Webpage({ url: 'http://www.google.com/' });
webpage.save(function (err) {
err.should.eql(null);
webpage.isNew.should.be.false;
assert.equal(err, null);
assert.equal(webpage.isNew, false);
done();
});
},
'url normalization should remove www.': function () {
});

it('should remove www in url normalization', function (done) {
var webpage = new Webpage({ url: 'http://www.google.com/'});
webpage.save(function (err) {
webpage.url.should.equal('http://google.com/');
assert.equal(webpage.url, 'http://google.com/?');
Webpage.findById(webpage._id, function (err, refreshed) {
refreshed.url.should.equal('http://google.com/');
assert.equal(refreshed.url, 'http://google.com/?');
done();
});
});
},
'url normalization should add a trailing slash': function () {
var webpage = new Webpage({ url: 'http://google.com'});
webpage.save(function (err) {
webpage.url.should.equal('http://google.com/');
Webpage.findById(webpage._id, function (err, refreshed) {
refreshed.url.should.equal('http://google.com/');
});
});
},
teardown: function(){
});

before(function () {
Webpage = db.model('Webpage', WebpageSchema);
Webpage.remove({}, function (err) {});
});

after(function () {
db.close();
}
};
});
});
41 changes: 21 additions & 20 deletions tests/useTimestamps.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require('should');
var mongoose = require('mongoose')
var assert = require('assert')
, mongoose = require('mongoose')
, Schema = mongoose.Schema
, db = mongoose.createConnection('mongodb://localhost/mongoose_types_tests')
, useTimestamps = require("../").useTimestamps;
Expand All @@ -13,34 +13,35 @@ var TimeCopSchema = new Schema({
mongoose.model('TimeCop', TimeCopSchema);
var TimeCop;

module.exports = {
before: function(done){
TimeCop = db.model('TimeCop', TimeCopSchema);
TimeCop.remove({}, function () {
done();
});
},
'createdAt and updatedAt should be set to the same value on creation': function (done) {
describe('timestamps', function () {
it('should set createdAt and updateAt to the same value on creation', function (done) {
var cop = new TimeCop({ email: '[email protected]' });
cop.save( function (err) {
cop.createdAt.should.be.an.instanceof(Date);
cop.updatedAt.should.be.an.instanceof(Date);
assert.equal(cop.createdAt, cop.updatedAt)
done();
});
},
'updatedAt should be later than createdAt upon updating': function (done) {
});

it('should verify updateAt is later than createdAt upon updating', function (done) {
TimeCop.findOne({email: '[email protected]'}, function (err, found) {
found.email = '[email protected]';
setTimeout( function () {
found.save( function (err, updated) {
updated.updatedAt.should.be.greater.than(updated.createdAt);
assert.ok(updated.updatedAt > updated.createdAt);
assert(updated.updatedAt > updated.createdAt)
done();
});
}, 1000);
});
},
teardown: function(){
});

before(function (done) {
TimeCop = db.model('TimeCop', TimeCopSchema);
TimeCop.remove({}, function () {
done();
});
});

after(function() {
db.close();
}
};
});
});