-
-
Save soarez/a453fc2df08a8ca3569c to your computer and use it in GitHub Desktop.
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'); | |
mongoose.connect('mongodb://localhost/test'); | |
var Schema = mongoose.Schema; | |
var PageSchema = Schema({ | |
title: { type: String, default: '', trim: true }, | |
url: { type: String, default: '', trim: true }, | |
thumbnails: {type: Schema.ObjectId, ref: 'Thumbnail'}, | |
}); | |
var Page = mongoose.model('Page', PageSchema); | |
Page.schema.post('save', function(doc) { | |
if(doc.thumbnails) return; | |
Thumbnail.generate(doc.url, function(err, thumb){ | |
if (err) | |
throw err; // TODO: deal with error | |
doc.thumbnails = thumb; | |
doc.save(); | |
}); | |
}); | |
var ThumbnailSchema = Schema({ | |
domain: {type: String, trim: true}, | |
url: { | |
small: { type: String, trim: true }, | |
original: {type: String, trim: true } | |
} | |
}); | |
ThumbnailSchema.statics.generate = function(doc, cb) { | |
// generate site preview , save the thumbnail and call cb | |
cb(null, new Thumbnail()); | |
}; | |
var Thumbnail = mongoose.model('Thumbnail', ThumbnailSchema); | |
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
require('../'); | |
var assert = require('assert'); | |
var mongoose = require('mongoose'); | |
var Page = mongoose.model('Page'); | |
var Thumbnail = mongoose.model('Thumbnail'); | |
it('generates thumbnail if page does not have one', function(done) { | |
// Add another post save hook to page. | |
// Becase it doesn't have a thumbnail, so one should be created, added | |
// to the page and the page will be saved again, so we can expect this | |
// to fire twice. | |
var pageAttr = { title: 'Gist web page', url: 'http://www.gist.github.com' }; | |
var count = 0, expected = 2; | |
var page = new Page(pageAttr); | |
page.on('save', postPageSaveA); | |
page.save(); | |
function postPageSaveA(doc) { | |
++count; | |
assert(count <= expected, 'post saved fired too many times'); | |
// delay the next test to allow this test to fail for firing too many times | |
if (count === expected) | |
setTimeout(doUpdate, 400); | |
} | |
function doUpdate() { | |
// Now change the doc and save it again, now it should fire only once | |
page.url += '/changed'; | |
page.removeListener('save', postPageSaveA); | |
count = 0; expected = 1; | |
page.on('save', postPageSaveB); | |
page.save(); | |
} | |
function postPageSaveB(doc) { | |
++count; | |
assert(count <= expected, 'post saved fired too many times'); | |
// same thing, delay finish to allow failure | |
if (count === expected) | |
setTimeout(done, 400); | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment