From 428fb9427839acb2227078828e499459d07648d3 Mon Sep 17 00:00:00 2001 From: Fabian Becker Date: Sun, 16 Mar 2014 16:21:02 +0000 Subject: [PATCH] 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 --- core/server/models/settings.js | 4 ++- .../integration/model/model_posts_spec.js | 2 +- .../integration/model/model_settings_spec.js | 6 +++-- .../test/integration/model/model_tags_spec.js | 26 ++++++++++++++----- core/test/utils/index.js | 23 +++++++++------- package.json | 4 +-- 6 files changed, 44 insertions(+), 21 deletions(-) diff --git a/core/server/models/settings.js b/core/server/models/settings.js index 74a5557925..7c183f77e4 100644 --- a/core/server/models/settings.js +++ b/core/server/models/settings.js @@ -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) { diff --git a/core/test/integration/model/model_posts_spec.js b/core/test/integration/model/model_posts_spec.js index e702019150..15b5983977 100644 --- a/core/test/integration/model/model_posts_spec.js +++ b/core/test/integration/model/model_posts_spec.js @@ -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'); diff --git a/core/test/integration/model/model_settings_spec.js b/core/test/integration/model/model_settings_spec.js index 676c5f53ea..c6ba6eddf1 100644 --- a/core/test/integration/model/model_settings_spec.js +++ b/core/test/integration/model/model_settings_spec.js @@ -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 diff --git a/core/test/integration/model/model_tags_spec.js b/core/test/integration/model/model_tags_spec.js index e26b3174a9..2b8ebc84ae 100644 --- a/core/test/integration/model/model_tags_spec.js +++ b/core/test/integration/model/model_tags_spec.js @@ -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); diff --git a/core/test/utils/index.js b/core/test/utils/index.js index 67ecf2b8da..ddbe52c386 100644 --- a/core/test/utils/index.js +++ b/core/test/utils/index.js @@ -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]); + }; + })); }); } diff --git a/package.json b/package.json index 441f6b56de..e7064beaa1 100644 --- a/package.json +++ b/package.json @@ -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",