Skip to content

Commit 5497904

Browse files
committed
Migration related test improvements
refs #6301 - Don't hardcode the model fields in utils/api -> use the schema + modify the lists = We can now easily see what the differences between the schema and the API result are - Don't hardcode the default DB version in the import/export tools = We don't have to update this every time we update the database version
1 parent 21c272b commit 5497904

File tree

3 files changed

+69
-62
lines changed

3 files changed

+69
-62
lines changed

core/test/integration/export_spec.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ var testUtils = require('../utils/index'),
99
// Stuff we are testing
1010
versioning = require('../../server/data/schema').versioning,
1111
exporter = require('../../server/data/export/index'),
12+
13+
DEF_DB_VERSION = versioning.getDefaultDatabaseVersion(),
1214
sandbox = sinon.sandbox.create();
1315

1416
describe('Exporter', function () {
@@ -22,9 +24,9 @@ describe('Exporter', function () {
2224
should.exist(exporter);
2325

2426
it('exports data', function (done) {
25-
// Stub migrations to return 000 as the current database version
27+
// Stub migrations to return DEF_DB_VERSION as the current database version
2628
var versioningStub = sandbox.stub(versioning, 'getDatabaseVersion', function () {
27-
return Promise.resolve('004');
29+
return Promise.resolve(DEF_DB_VERSION);
2830
});
2931

3032
exporter().then(function (exportData) {
@@ -37,13 +39,13 @@ describe('Exporter', function () {
3739
should.exist(exportData.meta);
3840
should.exist(exportData.data);
3941

40-
exportData.meta.version.should.equal('004');
42+
exportData.meta.version.should.equal(DEF_DB_VERSION);
4143

4244
dbVersionSetting = _.findWhere(exportData.data.settings, {key: 'databaseVersion'});
4345

4446
should.exist(dbVersionSetting);
4547

46-
dbVersionSetting.value.should.equal('004');
48+
dbVersionSetting.value.should.equal(DEF_DB_VERSION);
4749

4850
_.each(tables, function (name) {
4951
should.exist(exportData.data[name]);

core/test/integration/import_spec.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ var testUtils = require('../utils/index'),
1212
// Stuff we are testing
1313
db = require('../../server/data/db'),
1414
config = require('../../server/config'),
15+
versioning = require('../../server/data/schema').versioning,
1516
defaultConfig = rewire('../../../config.example')[process.env.NODE_ENV],
1617
migration = rewire('../../server/data/migration'),
1718
exporter = require('../../server/data/export'),
1819
importer = require('../../server/data/import'),
1920
DataImporter = require('../../server/data/import/data-importer'),
2021

22+
DEF_DB_VERSION = versioning.getDefaultDatabaseVersion(),
2123
knex = db.knex,
2224
sandbox = sinon.sandbox.create();
2325

@@ -155,7 +157,7 @@ describe('Import', function () {
155157

156158
// test settings
157159
settings.length.should.be.above(0, 'Wrong number of settings');
158-
_.findWhere(settings, {key: 'databaseVersion'}).value.should.equal('004', 'Wrong database version');
160+
_.findWhere(settings, {key: 'databaseVersion'}).value.should.equal(DEF_DB_VERSION, 'Wrong database version');
159161

160162
// test tags
161163
tags.length.should.equal(exportData.data.tags.length, 'no new tags');
@@ -211,7 +213,7 @@ describe('Import', function () {
211213

212214
// test settings
213215
settings.length.should.be.above(0, 'Wrong number of settings');
214-
_.findWhere(settings, {key: 'databaseVersion'}).value.should.equal('004', 'Wrong database version');
216+
_.findWhere(settings, {key: 'databaseVersion'}).value.should.equal(DEF_DB_VERSION, 'Wrong database version');
215217

216218
// activeTheme should NOT have been overridden
217219
_.findWhere(settings, {key: 'activeTheme'}).value.should.equal('casper', 'Wrong theme');
@@ -272,7 +274,7 @@ describe('Import', function () {
272274

273275
// test settings
274276
settings.length.should.be.above(0, 'Wrong number of settings');
275-
_.findWhere(settings, {key: 'databaseVersion'}).value.should.equal('004', 'Wrong database version');
277+
_.findWhere(settings, {key: 'databaseVersion'}).value.should.equal(DEF_DB_VERSION, 'Wrong database version');
276278

277279
done();
278280
});
@@ -317,7 +319,7 @@ describe('Import', function () {
317319

318320
// test settings
319321
settings.length.should.be.above(0, 'Wrong number of settings');
320-
_.findWhere(settings, {key: 'databaseVersion'}).value.should.equal('004', 'Wrong database version');
322+
_.findWhere(settings, {key: 'databaseVersion'}).value.should.equal(DEF_DB_VERSION, 'Wrong database version');
321323

322324
done();
323325
});
@@ -375,7 +377,7 @@ describe('Import', function () {
375377

376378
// test settings
377379
settings.length.should.be.above(0, 'Wrong number of settings');
378-
_.findWhere(settings, {key: 'databaseVersion'}).value.should.equal('004', 'Wrong database version');
380+
_.findWhere(settings, {key: 'databaseVersion'}).value.should.equal(DEF_DB_VERSION, 'Wrong database version');
379381

380382
// activeTheme should NOT have been overridden
381383
_.findWhere(settings, {key: 'activeTheme'}).value.should.equal('casper', 'Wrong theme');
@@ -435,7 +437,7 @@ describe('Import', function () {
435437

436438
// test settings
437439
settings.length.should.be.above(0, 'Wrong number of settings');
438-
_.findWhere(settings, {key: 'databaseVersion'}).value.should.equal('004', 'Wrong database version');
440+
_.findWhere(settings, {key: 'databaseVersion'}).value.should.equal(DEF_DB_VERSION, 'Wrong database version');
439441

440442
done();
441443
});
@@ -479,7 +481,7 @@ describe('Import', function () {
479481

480482
// test settings
481483
settings.length.should.be.above(0, 'Wrong number of settings');
482-
_.findWhere(settings, {key: 'databaseVersion'}).value.should.equal('004', 'Wrong database version');
484+
_.findWhere(settings, {key: 'databaseVersion'}).value.should.equal(DEF_DB_VERSION, 'Wrong database version');
483485

484486
done();
485487
});
@@ -529,7 +531,7 @@ describe('Import', function () {
529531

530532
// test settings
531533
settings.length.should.be.above(0, 'Wrong number of settings');
532-
_.findWhere(settings, {key: 'databaseVersion'}).value.should.equal('004', 'Wrong database version');
534+
_.findWhere(settings, {key: 'databaseVersion'}).value.should.equal(DEF_DB_VERSION, 'Wrong database version');
533535

534536
done();
535537
}).catch(done);
@@ -719,7 +721,7 @@ describe('Import (new test structure)', function () {
719721

720722
// test settings
721723
settings.length.should.be.above(0, 'Wrong number of settings');
722-
_.findWhere(settings, {key: 'databaseVersion'}).value.should.equal('004', 'Wrong database version');
724+
_.findWhere(settings, {key: 'databaseVersion'}).value.should.equal(DEF_DB_VERSION, 'Wrong database version');
723725

724726
done();
725727
}).catch(done);
@@ -944,7 +946,7 @@ describe('Import (new test structure)', function () {
944946

945947
// test settings
946948
settings.length.should.be.above(0, 'Wrong number of settings');
947-
_.findWhere(settings, {key: 'databaseVersion'}).value.should.equal('004', 'Wrong database version');
949+
_.findWhere(settings, {key: 'databaseVersion'}).value.should.equal(DEF_DB_VERSION, 'Wrong database version');
948950

949951
done();
950952
}).catch(done);
@@ -1181,7 +1183,7 @@ describe('Import (new test structure)', function () {
11811183

11821184
// test settings
11831185
settings.length.should.be.above(0, 'Wrong number of settings');
1184-
_.findWhere(settings, {key: 'databaseVersion'}).value.should.equal('004', 'Wrong database version');
1186+
_.findWhere(settings, {key: 'databaseVersion'}).value.should.equal(DEF_DB_VERSION, 'Wrong database version');
11851187

11861188
done();
11871189
}).catch(done);

core/test/utils/api.js

Lines changed: 50 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,66 +2,77 @@ var _ = require('lodash'),
22
url = require('url'),
33
moment = require('moment'),
44
config = require('../../server/config'),
5+
schema = require('../../server/data/schema').tables,
56
ApiRouteBase = '/ghost/api/v0.1/',
67
host = config.server.host,
78
port = config.server.port,
8-
schema = 'http://',
9+
protocol = 'http://',
910
expectedProperties = {
11+
// API top level
1012
configuration: ['key', 'value', 'type'],
11-
posts: ['posts', 'meta'],
12-
tags: ['tags', 'meta'],
13-
users: ['users', 'meta'],
14-
roles: ['roles'],
15-
pagination: ['page', 'limit', 'pages', 'total', 'next', 'prev'],
16-
post: ['id', 'uuid', 'title', 'slug', 'markdown', 'html', 'meta_title', 'meta_description',
17-
'featured', 'image', 'status', 'language', 'created_at', 'created_by', 'updated_at',
18-
'updated_by', 'published_at', 'published_by', 'page', 'author', 'url'
19-
],
20-
settings: ['settings', 'meta'],
21-
setting: ['id', 'uuid', 'key', 'value', 'type', 'created_at', 'created_by', 'updated_at', 'updated_by'],
22-
tag: ['id', 'uuid', 'name', 'slug', 'description', 'parent', 'image', 'hidden',
23-
'meta_title', 'meta_description', 'created_at', 'created_by', 'updated_at', 'updated_by'
24-
],
25-
theme: ['uuid', 'name', 'version', 'active'],
26-
user: ['id', 'uuid', 'name', 'slug', 'email', 'image', 'cover', 'bio', 'website',
27-
'location', 'accessibility', 'status', 'language', 'meta_title', 'meta_description', 'tour', 'last_login',
28-
'created_at', 'created_by', 'updated_at', 'updated_by'
29-
],
13+
posts: ['posts', 'meta'],
14+
tags: ['tags', 'meta'],
15+
users: ['users', 'meta'],
16+
settings: ['settings', 'meta'],
17+
roles: ['roles'],
18+
pagination: ['page', 'limit', 'pages', 'total', 'next', 'prev'],
19+
slugs: ['slugs'],
20+
slug: ['slug'],
21+
// object / model level
22+
// Post API swaps author_id to author, and always returns a computed 'url' property
23+
post: _(schema.posts).keys().without('author_id').concat('author', 'url').value(),
24+
// User API always removes the password field
25+
user: _(schema.users).keys().without('password').value(),
26+
// Tag API swaps parent_id to parent
27+
tag: _(schema.tags).keys().without('parent_id').concat('parent').value(),
28+
setting: _.keys(schema.settings),
29+
accesstoken: _.keys(schema.accesstokens),
30+
role: _.keys(schema.roles),
31+
permission: _.keys(schema.permissions),
3032
notification: ['type', 'message', 'status', 'id', 'dismissible', 'location'],
31-
slugs: ['slugs'],
32-
slug: ['slug'],
33-
accesstoken: ['access_token', 'refresh_token', 'expires_in', 'token_type'],
34-
role: ['id', 'uuid', 'name', 'description', 'created_at', 'created_by', 'updated_at', 'updated_by'],
35-
permission: ['id', 'uuid', 'name', 'object_type', 'action_type', 'object_id', 'created_at', 'created_by',
36-
'updated_at', 'updated_by'
37-
]
33+
theme: ['uuid', 'name', 'version', 'active']
3834
};
3935

4036
function getApiQuery(route) {
4137
return url.resolve(ApiRouteBase, route);
4238
}
4339

4440
function getApiURL(route) {
45-
var baseURL = url.resolve(schema + host + ':' + port, ApiRouteBase);
41+
var baseURL = url.resolve(protocol + host + ':' + port, ApiRouteBase);
4642
return url.resolve(baseURL, route);
4743
}
44+
45+
function getURL() {
46+
return protocol + host;
47+
}
48+
4849
function getSigninURL() {
49-
return url.resolve(schema + host + ':' + port, 'ghost/signin/');
50+
return url.resolve(protocol + host + ':' + port, 'ghost/signin/');
5051
}
52+
5153
function getAdminURL() {
52-
return url.resolve(schema + host + ':' + port, 'ghost/');
54+
return url.resolve(protocol + host + ':' + port, 'ghost/');
55+
}
56+
57+
function isISO8601(date) {
58+
return moment(date).parsingFlags().iso;
5359
}
5460

5561
// make sure the API only returns expected properties only
56-
function checkResponseValue(jsonResponse, properties) {
57-
for (var i = 0; i < properties.length; i = i + 1) {
58-
// For some reason, settings response objects do not have the 'hasOwnProperty' method
59-
if (Object.prototype.hasOwnProperty.call(jsonResponse, properties[i])) {
60-
continue;
61-
}
62-
jsonResponse.should.have.property(properties[i]);
63-
}
64-
Object.keys(jsonResponse).length.should.eql(properties.length);
62+
function checkResponseValue(jsonResponse, expectedProperties) {
63+
var providedProperties = _.keys(jsonResponse),
64+
missing = _.difference(expectedProperties, providedProperties),
65+
unexpected = _.difference(providedProperties, expectedProperties);
66+
67+
_.each(missing, function (prop) {
68+
jsonResponse.should.have.property(prop);
69+
});
70+
71+
_.each(unexpected, function (prop) {
72+
jsonResponse.should.not.have.property(prop);
73+
});
74+
75+
providedProperties.length.should.eql(expectedProperties.length);
6576
}
6677

6778
function checkResponse(jsonResponse, objectType, additionalProperties, missingProperties) {
@@ -72,14 +83,6 @@ function checkResponse(jsonResponse, objectType, additionalProperties, missingPr
7283
checkResponseValue(jsonResponse, checkProperties);
7384
}
7485

75-
function isISO8601(date) {
76-
return moment(date).parsingFlags().iso;
77-
}
78-
79-
function getURL() {
80-
return schema + host;
81-
}
82-
8386
module.exports = {
8487
getApiURL: getApiURL,
8588
getApiQuery: getApiQuery,

0 commit comments

Comments
 (0)