Fixes random pg failures.

fixes #2075
- Updates knex to latest version
- Fixes expected value in page test
- Change fixture insertion to be sequential
- Add orderBy to insertMorePostsTags fixture helper
This commit is contained in:
Fabian Becker 2014-03-16 16:21:02 +00:00
parent dfcd93359d
commit 428fb94278
6 changed files with 44 additions and 21 deletions

View File

@ -64,7 +64,9 @@ Settings = ghostBookshelf.Model.extend({
if (!_.isObject(_key)) {
_key = { key: _key };
}
return ghostBookshelf.Model.read.call(this, _key);
return when(ghostBookshelf.Model.read.call(this, _key)).then(function (element) {
return element;
});
},
edit: function (_data, t) {

View File

@ -419,7 +419,7 @@ describe('Post Model', function () {
}).then(function (paginationResult) {
paginationResult.page.should.equal(2);
paginationResult.limit.should.equal(15);
paginationResult.posts.length.should.equal(9);
paginationResult.posts.length.should.equal(11);
paginationResult.pages.should.equal(2);
paginationResult.aspect.tag.name.should.equal('injection');
paginationResult.aspect.tag.slug.should.equal('injection');

View File

@ -65,7 +65,7 @@ describe('Settings Model', function () {
should.exist(found);
found.attributes.value.should.equal(firstSetting.attributes.value);
found.get('value').should.equal(firstSetting.attributes.value);
done();
@ -206,7 +206,9 @@ describe('Settings Model', function () {
return SettingsModel.findAll();
}).then(function (allSettings) {
allSettings.length.should.be.above(0);
return SettingsModel.read('description');
return SettingsModel.read('description').then(function (descriptionSetting) {
return descriptionSetting;
});
}).then(function (descriptionSetting) {
// Testing against the actual value in default-settings.json feels icky,
// but it's easier to fix the test if that ever changes than to mock out that behaviour

View File

@ -177,9 +177,13 @@ describe('Tag Model', function () {
return PostModel.read({id: postModel.id, status: 'all'}, { withRelated: ['tags']});
}).then(function (reloadedPost) {
var tagModels = reloadedPost.related('tags').models,
tagNames = tagModels.map(function (t) { return t.attributes.name; });
tagNames = tagModels.map(function (t) { return t.attributes.name; }),
tagIds = _.pluck(tagModels, 'id');
tagNames.sort().should.eql(['tag1', 'tag2', 'tag3']);
tagModels[2].id.should.eql(4); // make sure it hasn't just added a new tag with the same name
// make sure it hasn't just added a new tag with the same name
// Don't expect a certain order in results - check for number of items!
Math.max.apply(Math, tagIds).should.eql(4);
done();
}).then(null, done);
@ -248,9 +252,14 @@ describe('Tag Model', function () {
return PostModel.read({id: postModel.id, status: 'all'}, { withRelated: ['tags']});
}).then(function (reloadedPost) {
var tagModels = reloadedPost.related('tags').models,
tagNames = tagModels.map(function (t) { return t.attributes.name; });
tagNames = tagModels.map(function (t) { return t.attributes.name; }),
tagIds = _.pluck(tagModels, 'id');
tagNames.sort().should.eql(['tag1', 'tag2', 'tag3']);
tagModels[2].id.should.eql(4); // make sure it hasn't just added a new tag with the same name
// make sure it hasn't just added a new tag with the same name
// Don't expect a certain order in results - check for number of items!
Math.max.apply(Math, tagIds).should.eql(4);
done();
}).then(null, done);
@ -279,9 +288,14 @@ describe('Tag Model', function () {
return PostModel.read({id: postModel.id, status: 'all'}, { withRelated: ['tags']});
}).then(function (reloadedPost) {
var tagModels = reloadedPost.related('tags').models,
tagNames = tagModels.map(function (t) { return t.attributes.name; });
tagNames = tagModels.map(function (t) { return t.get('name'); }),
tagIds = _.pluck(tagModels, 'id');
tagNames.sort().should.eql(['tag1', 'tag2', 'tag3', 'tag4']);
tagModels[2].id.should.eql(4); // make sure it hasn't just added a new tag with the same name
// make sure it hasn't just added a new tag with the same name
// Don't expect a certain order in results - check for number of items!
Math.max.apply(Math, tagIds).should.eql(5);
done();
}).then(null, done);

View File

@ -1,5 +1,6 @@
var knex = require('../../server/models/base').knex,
when = require('when'),
sequence = require('when/sequence'),
nodefn = require('when/node/function'),
_ = require('lodash'),
fs = require('fs-extra'),
@ -29,14 +30,13 @@ function insertPosts() {
function insertMorePosts(max) {
var lang,
status,
posts,
posts = [],
promises = [],
i, j, k = 0;
max = max || 50;
for (i = 0; i < 2; i += 1) {
posts = [];
lang = i % 2 ? 'en' : 'fr';
posts.push(DataGenerator.forKnex.createGenericPost(k++, null, lang));
@ -44,18 +44,21 @@ function insertMorePosts(max) {
status = j % 2 ? 'draft' : 'published';
posts.push(DataGenerator.forKnex.createGenericPost(k++, status, lang));
}
promises.push(knex('posts').insert(posts));
}
return when.all(promises);
return sequence(_.times(posts.length, function(index) {
return function() {
return knex('posts').insert(posts[index]);
}
}));
}
function insertMorePostsTags(max) {
max = max || 50;
return when.all([
knex('posts').select('id'),
// PostgreSQL can return results in any order
knex('posts').orderBy('id', 'asc').select('id'),
knex('tags').select('id', 'name')
]).then(function (results) {
var posts = _.pluck(results[0], 'id'),
@ -74,9 +77,11 @@ function insertMorePostsTags(max) {
promises.push(DataGenerator.forKnex.createPostsTags(posts[i], injectionTagId));
}
promises.push(knex('posts_tags').insert(promises));
return when.all(promises);
return sequence(_.times(promises.length, function(index) {
return function() {
return knex('posts_tags').insert(promises[index]);
};
}));
});
}

View File

@ -40,7 +40,7 @@
"express": "3.4.6",
"express-hbs": "0.7.9",
"fs-extra": "0.8.1",
"knex": "0.5.0",
"knex": "0.5.8",
"lodash": "2.4.1",
"moment": "2.4.0",
"node-polyglot": "0.3.0",
@ -55,7 +55,7 @@
"when": "2.7.0"
},
"optionalDependencies": {
"mysql": "2.0.0-alpha9"
"mysql": "2.1.1"
},
"devDependencies": {
"blanket": "~1.1.5",