Skip to content

Commit

Permalink
Updated tests to run without globals, and fixed code that was relying…
Browse files Browse the repository at this point in the history
… on globals being on
  • Loading branch information
sgress454 committed Sep 10, 2015
1 parent 0d57de3 commit 189937d
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 28 deletions.
22 changes: 11 additions & 11 deletions lib/hooks/blueprints/actionUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ var actionUtil = {

/**
* Given a Waterline query and an express request, populate
* the appropriate/specified association attributes and
* return it so it can be chained further ( i.e. so you can
* the appropriate/specified association attributes and
* return it so it can be chained further ( i.e. so you can
* .exec() it )
*
* @param {Query} query [waterline query object]
* @param {Request} req
* @return {Query}
*/
populateRequest: function(query, req) {
var DEFAULT_POPULATE_LIMIT = sails.config.blueprints.defaultLimit || 30;
var DEFAULT_POPULATE_LIMIT = req._sails.config.blueprints.defaultLimit || 30;
var _options = req.options;
var aliasFilter = req.param('populate');
var shouldPopulate = _options.populate;
Expand Down Expand Up @@ -75,11 +75,11 @@ var actionUtil = {
}
});

return actionUtil.populateQuery(query, associations);
return actionUtil.populateQuery(query, associations, req._sails);
},

/**
* Given a Waterline query and Waterline model, populate the
* Given a Waterline query and Waterline model, populate the
* appropriate/specified association attributes and return it
* so it can be chained further ( i.e. so you can .exec() it )
*
Expand All @@ -90,7 +90,7 @@ var actionUtil = {
populateModel: function(query, model) {
return actionUtil.populateQuery(query, model.associations);
},


/**
* Given a Waterline query, populate the appropriate/specified
Expand All @@ -101,9 +101,9 @@ var actionUtil = {
* @param {Array} associations [array of objects with an alias
* and (optional) limit key]
* @return {Query}
*/
populateQuery: function(query, associations) {
var DEFAULT_POPULATE_LIMIT = sails.config.blueprints.defaultLimit || 30;
*/
populateQuery: function(query, associations, sails) {
var DEFAULT_POPULATE_LIMIT = (sails && sails.config.blueprints.defaultLimit) || 30;

return _.reduce(associations, function(query, association) {
return query.populate(association.alias, {
Expand All @@ -124,7 +124,7 @@ var actionUtil = {

// Look up identity of associated model
var ident = assoc[assoc.type];
var AssociatedModel = sails.models[ident];
var AssociatedModel = req._sails.models[ident];

if (req.options.autoWatch) {
AssociatedModel.watch(req);
Expand Down Expand Up @@ -328,7 +328,7 @@ var actionUtil = {
* @param {Request} req
*/
parseLimit: function (req) {
var DEFAULT_LIMIT = sails.config.blueprints.defaultLimit || 30;
var DEFAULT_LIMIT = req._sails.config.blueprints.defaultLimit || 30;
var limit = req.param('limit') || (typeof req.options.limit !== 'undefined' ? req.options.limit : DEFAULT_LIMIT);
if (limit) { limit = +limit; }
return limit;
Expand Down
2 changes: 1 addition & 1 deletion lib/hooks/blueprints/actions/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ module.exports = function addToCollection (req, res) {
// Get the model class of the child in order to figure out the name of
// the primary key attribute.
var associationAttr = _.findWhere(Model.associations, { alias: relation });
var ChildModel = sails.models[associationAttr.collection];
var ChildModel = req._sails.models[associationAttr.collection];
var childPkAttr = ChildModel.primaryKey;


Expand Down
4 changes: 2 additions & 2 deletions lib/hooks/blueprints/actions/destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ module.exports = function destroyOneRecord (req, res) {
Model.destroy(pk).exec(function destroyedRecord (err) {
if (err) return res.negotiate(err);

if (sails.hooks.pubsub) {
Model.publishDestroy(pk, !sails.config.blueprints.mirror && req, {previous: record});
if (req._sails.hooks.pubsub) {
Model.publishDestroy(pk, !req._sails.config.blueprints.mirror && req, {previous: record});
if (req.isSocket) {
Model.unsubscribe(req, record);
Model.retire(record);
Expand Down
2 changes: 1 addition & 1 deletion lib/hooks/blueprints/actions/findOne.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module.exports = function findOneRecord (req, res) {
if (err) return res.serverError(err);
if(!matchingRecord) return res.notFound('No record found with the specified `id`.');

if (sails.hooks.pubsub && req.isSocket) {
if (req._sails.hooks.pubsub && req.isSocket) {
Model.subscribe(req, matchingRecord);
actionUtil.subscribeDeep(req, matchingRecord);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/hooks/blueprints/actions/remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ module.exports = function remove(req, res) {

// If we have the pubsub hook, use the model class's publish method
// to notify all subscribers about the removed item
if (sails.hooks.pubsub) {
Model.publishRemove(parentRecord[Model.primaryKey], relation, childPk, !sails.config.blueprints.mirror && req);
if (req._sails.hooks.pubsub) {
Model.publishRemove(parentRecord[Model.primaryKey], relation, childPk, !req._sails.config.blueprints.mirror && req);
}

return res.ok(parentRecord);
Expand Down
2 changes: 1 addition & 1 deletion lib/hooks/blueprints/actions/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module.exports = function updateOneRecord (req, res) {
// No matter what, don't allow changing the PK via the update blueprint
// (you should just drop and re-add the record if that's what you really want)
if (typeof values[Model.primaryKey] !== 'undefined' && values[Model.primaryKey] != pk) {
sails.log.warn('Cannot change primary key via update blueprint; ignoring value sent for `' + Model.primaryKey + '`');
req._sails.log.warn('Cannot change primary key via update blueprint; ignoring value sent for `' + Model.primaryKey + '`');
}
// Make sure the primary key is unchanged
values[Model.primaryKey] = pk;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = {

watch: function(req, res) {
Pet.watch(req);
req._sails.models.pet.watch(req);
res.send(200);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
module.exports = {

watch: function(req, res) {
User.watch(req);
req._sails.models.user.watch(req);
res.send(200);
},

message: function(req, res) {
User.findOne({
req._sails.models.user.findOne({
user_id: 1
}, function(err, user) {
if (err) return res.json(500, {
Expand All @@ -17,7 +17,7 @@ module.exports = {
error: 'Expected specified user (with user_id=1) to exist...'
});
} else {
User.message(user, {
req._sails.models.user.message(user, {
greeting: 'hello'
}, req);
return res.send(200);
Expand All @@ -27,7 +27,7 @@ module.exports = {

subscribe: function(req, res) {

User.subscribe(req, {
req._sails.models.user.subscribe(req, {
user_id: req.param('id')
}, req.param('context'));
res.send(200);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {};
3 changes: 2 additions & 1 deletion test/integration/fixtures/sampleapp/config/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ module.exports = {
},
models: {
migrate: 'alter'
}
},
globals: false
};
4 changes: 2 additions & 2 deletions test/integration/fixtures/users.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var async = require('async');
module.exports = function(cb) {
module.exports = function(sails, cb) {

var users = [];
for (var userId = 1; userId <= 11; userId++) {
Expand All @@ -15,6 +15,6 @@ module.exports = function(cb) {
users.push(user);
}

async.forEach(users, function create(user, cb) {User.create(user).exec(cb)}, cb);
async.forEach(users, function create(user, cb) {sails.models.user.create(user).exec(cb)}, cb);

};
62 changes: 62 additions & 0 deletions test/integration/globals.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Test dependencies
*/
var assert = require('assert');
var httpHelper = require('./helpers/httpHelper.js');
var appHelper = require('./helpers/appHelper');

describe('globals :: ', function() {

describe('with default settings', function() {

var sailsprocess;
var appName = 'testApp';

before(function(done) {
this.timeout(15000);
// Build the app
appHelper.buildAndLift(appName, {globals: null}, function(err, sails) {

sailsprocess = sails;
return done(err);

});


});

after(function() {

sailsprocess.kill();
// console.log('before `chdir ../`' + ', cwd was :: ' + process.cwd());
process.chdir('../');
// console.log('after `chdir ../`' + ', cwd was :: ' + process.cwd());
appHelper.teardown();
});

it('lodash should be globalized', function() {
assert(_);
assert.equal(_.name, 'lodash');
});

it('async should be globalized', function() {
assert(async);
});

it('sails should be globalized', function() {
assert(sails);
});

it('services should be globalized', function() {
assert(TestService);
});

it('models should be globalized', function() {
assert(User);
});


});


});
2 changes: 1 addition & 1 deletion test/integration/router.APIScaffold.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ describe('router :: ', function() {

before(function(done) {

User.create([
sailsprocess.models.user.create([
{name:'scott'},
{name:'abby'},
{name:'joe'},
Expand Down
3 changes: 2 additions & 1 deletion test/integration/router.blueprintOptions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var appHelper = require('./helpers/appHelper');
var util = require('util');
var async = require('async');
var fixture = require('./fixtures/users.js');
var _ = require('lodash');

/**
* Errors
Expand Down Expand Up @@ -75,7 +76,7 @@ describe('router :: ', function() {
sailsprocess = sails;

// Add 31 users with 31 pets each
fixture(done);
fixture(sails, done);

});

Expand Down

0 comments on commit 189937d

Please sign in to comment.