0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-17 23:44:39 -05:00

Tests: extended knex mock

no issue

- support more cases
  - e.g. multiple where matches
- @TODO
  - take time to look for NPM module, which does this already
  - test sqlite3 :memory: mode again
This commit is contained in:
kirrg001 2018-04-16 00:29:17 +02:00
parent 5762e400a8
commit defe65c2de
2 changed files with 66 additions and 18 deletions

View file

@ -293,7 +293,10 @@ describe('Unit: models/post', function () {
events.tag.push(event);
});
return models.Post.findOne({id: testUtils.DataGenerator.forKnex.posts[3].id}, {withRelated: ['tags']})
return models.Post.findOne({
id: testUtils.DataGenerator.forKnex.posts[3].id,
status: 'draft'
}, {withRelated: ['tags']})
.then((post) => {
// post will be updated, tags relation not
return models.Post.edit({
@ -324,7 +327,10 @@ describe('Unit: models/post', function () {
events.tag.push(event);
});
return models.Post.findOne({id: testUtils.DataGenerator.forKnex.posts[3].id}, {withRelated: ['tags']})
return models.Post.findOne({
id: testUtils.DataGenerator.forKnex.posts[3].id,
status: 'draft'
}, {withRelated: ['tags']})
.then((post) => {
// post will be updated, tags relation not
return models.Post.edit({
@ -775,7 +781,10 @@ describe('Unit: models/post', function () {
describe('findOne', function () {
it('withRelated: []', function () {
return models.Post.findOne({id: testUtils.DataGenerator.forKnex.posts[3].id}, {withRelated: []})
return models.Post.findOne({
id: testUtils.DataGenerator.forKnex.posts[3].id,
status: 'draft'
}, {withRelated: []})
.then(function (post) {
post = post.toJSON();
post.author.should.eql(testUtils.DataGenerator.forKnex.users[0].id);
@ -785,7 +794,10 @@ describe('Unit: models/post', function () {
});
it('withRelated: [author]', function () {
return models.Post.findOne({id: testUtils.DataGenerator.forKnex.posts[3].id}, {withRelated: ['author']})
return models.Post.findOne({
id: testUtils.DataGenerator.forKnex.posts[3].id,
status: 'draft'
}, {withRelated: ['author']})
.then(function (post) {
post = post.toJSON();
post.author.id.should.eql(testUtils.DataGenerator.forKnex.users[0].id);
@ -794,7 +806,10 @@ describe('Unit: models/post', function () {
});
it('withRelated: [authors]', function () {
return models.Post.findOne({id: testUtils.DataGenerator.forKnex.posts[3].id}, {withRelated: ['authors']})
return models.Post.findOne({
id: testUtils.DataGenerator.forKnex.posts[3].id,
status: 'draft'
}, {withRelated: ['authors']})
.then(function (post) {
post = post.toJSON();
post.author.should.eql(testUtils.DataGenerator.forKnex.users[0].id);
@ -805,7 +820,10 @@ describe('Unit: models/post', function () {
});
it('withRelated: [authors, author]', function () {
return models.Post.findOne({id: testUtils.DataGenerator.forKnex.posts[3].id}, {withRelated: ['authors', 'author']})
return models.Post.findOne({
id: testUtils.DataGenerator.forKnex.posts[3].id,
status: 'draft'
}, {withRelated: ['authors', 'author']})
.then(function (post) {
post = post.toJSON();
post.author.id.should.eql(testUtils.DataGenerator.forKnex.users[0].id);
@ -911,7 +929,10 @@ describe('Unit: models/post', function () {
post = post.toJSON();
post.author.should.eql(testUtils.DataGenerator.forKnex.users[1].id);
should.not.exist(post.authors);
return models.Post.findOne({id: testUtils.DataGenerator.forKnex.posts[3].id}, {withRelated: ['authors']});
return models.Post.findOne({
id: testUtils.DataGenerator.forKnex.posts[3].id,
status: 'draft'
}, {withRelated: ['authors']});
}).then(function (post) {
post = post.toJSON();
post.authors.length.should.eql(2);

View file

@ -1,4 +1,5 @@
'use strict';
/* eslint-disable */
const mockKnex = require('mock-knex'),
_ = require('lodash'),
@ -70,14 +71,20 @@ class KnexMock {
joinAttribute = query.sql.match(/on\s\"\w+\"\.\"\w+\"\s\=\s\"\w+\"\.(\"\w+\")/)[1],
joinTable = query.sql.match(/on\s\"\w+\"\.\"\w+\"\s\=\s(\"\w+\")/)[1],
targetIdentifier = query.sql.match(/(\"\w+\")\sin\s\(\?\)/),
value = query.bindings[0],
values = query.bindings,
targetEntries,
toReturn = [];
if (!targetIdentifier) {
targetIdentifier = query.sql.match(/where\s\"\w+\"\.\"(\w+)\"\s\=/);
}
if (!targetIdentifier) {
targetIdentifier = query.sql.match(/where\s\"\w+\"\.\"(\w+)\"\s\in\s/);
}
if (targetIdentifier) {
targetIdentifier = targetIdentifier[1];
} else {
targetIdentifier = query.sql.match(/where\s\"\w+\"\.\"(\w+)\"\s\=/)[1];
}
targetTable = targetTable.replace(/"/g, '');
@ -89,18 +96,18 @@ class KnexMock {
debug(targetTable, targetIdentifier, targetAttribute, joinTable, joinAttribute);
targetEntries = _.filter(this.db[targetTable], ((existing) => {
if (existing[targetIdentifier] === value) {
if (values.indexOf(existing[targetIdentifier]) !== -1) {
return true;
}
}));
if (targetEntries && targetEntries.length) {
_.each(targetEntries, ((target) => {
const found = _.find(this.db[joinTable], ((joinEntry) => {
const found = _.cloneDeep(_.find(this.db[joinTable], ((joinEntry) => {
if (joinEntry[joinAttribute] === target[targetAttribute]) {
return true;
}
}));
})));
_.each(target, function (value, key) {
let match = query.sql.match(new RegExp('\\"' + targetTable + '\\"\\.\\"' + key + '"\\sas\\s(\\"\\w+\\")'));
@ -129,20 +136,40 @@ class KnexMock {
} else {
let tableName = query.sql.match(/from\s\"(\w+)\"/)[1],
where = query.sql.match(/\"(\w+)\"\s\=\s\?/),
value = query.bindings[0],
dbEntry;
values = query.bindings,
dbEntry,
wheres = [];
// where "users"."id" in ('1')
if (!where) {
where = query.sql.match(/\"\w+\"\.\"(\w+)\"\sin\s\(\?\)/)[1];
} else {
where = where[1];
// 3 wheres
let wheresMatch = query.sql.match(/\"(\w+)\"\s\=\s\?\sand\s\"\w+\"\.\"(\w+)\"\s\=\s\?\sand\s\"\w+\"\.\"(\w+)\"\s\=\s\?/);
if (wheresMatch) {
wheres.push(wheresMatch[1]);
wheres.push(wheresMatch[2]);
wheres.push(wheresMatch[3]);
} else {
// 2 wheres
let wheresMatch = query.sql.match(/\"(\w+)\"\s\=\s\?\sand\s\"\w+\"\.\"(\w+)\"\s\=\s\?/);
if (wheresMatch) {
wheres.push(wheresMatch[1]);
wheres.push(wheresMatch[2]);
} else {
wheres.push(where[1]);
}
}
}
debug(tableName, where, value);
values = query.bindings.slice(0, wheres.length);
debug(tableName, wheres, values);
dbEntry = _.filter(this.db[tableName], ((existing) => {
if (existing[where] === value) {
if (_.isEqual(_.values(_.pick(existing, wheres)), values)) {
return true;
}
}));