Created
September 15, 2012 16:49
-
-
Save davybrion/3728791 to your computer and use it in GitHub Desktop.
code snippets for "First Steps With MongoDB, Mongoose and Jasmine-Node On Node.JS" post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mongoose = require('mongoose'), | |
Schema = mongoose.Schema, | |
ObjectId = Schema.ObjectId; | |
var customerSchema = new Schema({ | |
name: { type: String, required: true }, | |
address: { | |
street: { type: String, required: true }, | |
postalCode: { type: String, required: true }, | |
city: { type: String, required: true }, | |
country: String | |
}, | |
phoneNumber: String, | |
vatNumber: { type: String, required: true }, | |
contact: { | |
name: String, | |
email: String | |
}, | |
includeContactOnInvoice: { type: Boolean, required: true, default: false } | |
}); | |
mongoose.model('Customer', customerSchema); | |
var Customer = mongoose.model('Customer'); | |
var performedWorkSchema = new Schema({ | |
date: { type: Date, required: true }, | |
hours: { type: Number, min: 1, max: 8, required: true } | |
}); | |
mongoose.model('PerformedWork', performedWorkSchema); | |
var PerformedWork = mongoose.model('PerformedWork'); | |
var activitySchema = new Schema({ | |
customer: { type: ObjectId, required: true }, | |
description: { type: String, required: true }, | |
hourlyRate: { type: Number, required: true }, | |
performedWork: [performedWorkSchema], | |
billed: { type: Boolean, required: true, default: false } | |
}); | |
activitySchema.methods.addPerformedWork = function(date, hours) { | |
this.performedWork.push(new PerformedWork({ date: date, hours: hours })); | |
}; | |
mongoose.model('Activity', activitySchema); | |
var Activity = mongoose.model('Activity'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mongoose.model('Customer', customerSchema); | |
var Customer = mongoose.model('Customer'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe('when a customer is saved', function() { | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe('with none of its required fields filled in', function() { | |
it('should fail with validation errors for each required field', function() { | |
var customer = new Customer(); | |
customer.save(function(err) { | |
expect(err).not.toBeNull(); | |
expect(err).toHaveRequiredValidationErrorFor('name'); | |
expect(err).toHaveRequiredValidationErrorFor('vatNumber'); | |
expect(err).toHaveRequiredValidationErrorFor('address.street'); | |
expect(err).toHaveRequiredValidationErrorFor('address.postalCode'); | |
expect(err).toHaveRequiredValidationErrorFor('address.city'); | |
asyncSpecDone(); | |
}); | |
asyncSpecWait(); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
beforeEach(function() { | |
this.addMatchers((function() { | |
var toHaveValidationErrorFor = function(err, validatorName, propertyName) { | |
if (!err) { return false; } | |
if (err.name !== 'ValidationError') { return false; } | |
var value = err.errors[propertyName]; | |
if (!value) { return false; } | |
return (value === 'Validator "' + validatorName + '" failed for path ' + propertyName); | |
}; | |
return { | |
toHaveRequiredValidationErrorFor : function(propertyName) { | |
return toHaveValidationErrorFor(this.actual, 'required', propertyName); | |
}, | |
toHaveMaxValidationErrorFor: function(propertyName) { | |
return toHaveValidationErrorFor(this.actual, 'max', propertyName); | |
} | |
}; | |
}())); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe('with valid performed work added to it', function() { | |
it('should be inserted as well', function() { | |
var activity = new ActivityBuilder().build(); | |
var today = new Date(); | |
var yesterday = new Date(); | |
yesterday.setDate(yesterday.getDate() -1); | |
activity.addPerformedWork(yesterday, 8); | |
activity.addPerformedWork(today, 6); | |
activity.save(function(err) { | |
expect(err).toBeNull(); | |
Activity.findById(activity.id, function(err, result) { | |
expect(result.performedWork.length).toBe(2); | |
expect(result.performedWork[0].date).toEqual(yesterday); | |
expect(result.performedWork[0].hours).toEqual(8); | |
expect(result.performedWork[1].date).toEqual(today); | |
expect(result.performedWork[1].hours).toEqual(6); | |
asyncSpecDone(); | |
}); | |
}); | |
asyncSpecWait(); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"performedWork" : [ | |
{ | |
"_id" : ObjectId("4e25f7d2041ec8c006000006"), | |
"date" : ISODate("2011-07-18T21:32:02.652Z"), | |
"hours" : 8 | |
}, | |
{ | |
"_id" : ObjectId("4e25f7d2041ec8c006000008"), | |
"date" : ISODate("2011-07-19T21:32:02.652Z"), | |
"hours" : 6 | |
} | |
], | |
"billed" : false, | |
"_id" : ObjectId("4e25f7d2041ec8c006000005"), | |
"hourlyRate" : 75, | |
"description" : "some cool project", | |
"customer" : ObjectId("4e25937456436de850000006") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"address" : { | |
"country" : "some country", | |
"postalCode" : "1234", | |
"city" : "some city", | |
"street" : "some street" | |
}, | |
"contact" : { | |
"email" : "[email protected]", | |
"name" : "some name" | |
}, | |
"includeContactOnInvoice" : true, | |
"_id" : ObjectId("4e25f7d2041ec8c006000016"), | |
"vatNumber" : "0456.876.234", | |
"name" : "some customer", | |
"phoneNumber" : "123456789" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment