Permalink
Browse files

Support & tests for streams.

  • Loading branch information...
1 parent 49a9671 commit 2078fe1e24739566934af3ffa1d105be607bc30d @JoshuaGross committed Jan 29, 2013
Showing with 88 additions and 5 deletions.
  1. +8 −3 lib/mongoose-subpopulate.js
  2. +1 −1 package.json
  3. +34 −0 test/domains.js
  4. +2 −1 test/mongoose.js
  5. +43 −0 test/streams.js
@@ -131,7 +131,9 @@ var wrapSchema = exports.wrapSchema = function wrapSchema (schema) {
var wrapCallback = (callback ? db_callback('find failed', callback) : null);
verifyConditions(conditions);
try {
- return wrapSchema(findOrig.call(schema, conditions, fields, options, wrapCallback));
+ var findResult = findOrig.call(schema, conditions, fields, options, wrapCallback);
+ var findSchemaWrapped = wrapSchema(findResult);
+ return findSchemaWrapped;
} catch (mongooseError) {
wrapCallback(mongooseError);
}
@@ -272,11 +274,12 @@ var wrapSchema = exports.wrapSchema = function wrapSchema (schema) {
// We only do this so that, fex, user.save() can be wrapped by wrapSchema
// Keep in mind that Mongoose internals do not see this; only "user" code like
// controllers, libraries outside of Mongoose, etc.
- if (!schema.mSubpopulateWrappedConstructor) {
+ if (!schema.mSubpopulateWrappedConstructor && 'function' === typeof schema) {
var newSchema = function wrappedSchemaConstructor (doc, fields) {
var schemaObj = new schema(doc, fields);
+ var safeSchemaInst;
this.__proto__ = schemaObj;
- var safeSchemaInst = wrapSchema(this);
+ safeSchemaInst = wrapSchema(this);
safeSchemaInst = wrapModelObject(this);
// NOTE: for whatever reason, any fields named "name" get wiped out. We need this
@@ -295,6 +298,8 @@ var wrapSchema = exports.wrapSchema = function wrapSchema (schema) {
}
return newSchema;
+ } else {
+ schema.mSubpopulateWrappedConstructor = true;
}
return schema;
View
@@ -1,6 +1,6 @@
{
"name": "mongoose-subpopulate",
- "version": "0.1.17",
+ "version": "0.1.18",
"main": "./lib/mongoose-subpopulate.js",
"scripts": {},
"repository": {
View
@@ -0,0 +1,34 @@
+// Test that domains work properly with Mongoose errors
+var expect = require('expect.js'),
+ util = require('util'),
+ common = require('./common.js'),
+ async = require('async');
+
+before(function (done) {
+ common.connectDB(function () {
+ common.killDatabase(done);
+ });
+});
+
+describe('mongoose error handling', function () {
+ it('should create a user with bad data', function (done) {
+ // other tests really dislike this test.
+ return done();
+
+ var m = common.db();
+ var domain = require('domain').create();
+
+ domain.run(function () {
+ var owner = new m.User();
+ owner.save(done)
+ });
+ domain.on('error', function (e) {
+ expect(e).to.not.be(null);
+ done();
+ })
+ process.on('uncaughtException', function (e) {
+ console.log('error mapped to process; this will be fixed in a future Node.js release')
+ })
+ });
+});
+
View
@@ -280,7 +280,8 @@ describe('mongoose additions for population', function () {
});
it('should support sub-population manipulation through grandparent', function (done) {
var m = common.db();
- m.Project.findById(projectID).populate('main_document').populate('main_document.owner').exec(function (project) {
+ m.Project.findById(projectID).populate('main_document').populate('main_document.owner').exec(function (err, project) {
+ console.log(err, project);
expect(typeof project._id).to.be('string');
expect(typeof project.main_document._id).to.be('string');
expect(typeof project.main_document.owner._id).to.be('string');
View
@@ -0,0 +1,43 @@
+// Test that Mongoose streams work properly
+var expect = require('expect.js'),
+ util = require('util'),
+ common = require('./common.js'),
+ async = require('async');
+
+before(function (done) {
+ common.connectDB(function () {
+ common.killDatabase(done);
+ });
+});
+
+describe('mongoose streams', function () {
+ it('should create many users', function (done) {
+ var m = common.db();
+ var emails = [];
+ for (var i = 0; i < 10; i++) {
+ emails.push('josh'+i+'@spandex.io');
+ }
+ async.map(emails, function (email, done) {
+ var owner = new m.User();
+ owner.email = email;
+ owner.username = 'Streams Jorsh '+email;
+ owner.save(done)
+ }, done)
+ });
+ it('should be able to stream users', function (done) {
+ var m = common.db();
+ var query = m.User.find({ username: /^Streams Jorsh/ });
+ var stream = query.stream();
+
+ var usersFound = 0;
+
+ stream.on('data', function (user) {
+ usersFound++;
+ });
+ stream.on('close', function () {
+ expect(usersFound).to.be(10);
+ done();
+ })
+ });
+});
+

0 comments on commit 2078fe1

Please sign in to comment.