2013-12-29 00:13:47 +04:00
|
|
|
var knex = require('../../server/models/base').knex,
|
|
|
|
when = require('when'),
|
2014-03-16 20:21:02 +04:00
|
|
|
sequence = require('when/sequence'),
|
2013-12-29 00:13:47 +04:00
|
|
|
nodefn = require('when/node/function'),
|
2014-02-13 07:26:56 +04:00
|
|
|
_ = require('lodash'),
|
2013-12-29 00:13:47 +04:00
|
|
|
fs = require('fs-extra'),
|
|
|
|
path = require('path'),
|
|
|
|
migration = require("../../server/data/migration/"),
|
|
|
|
Settings = require('../../server/models/settings').Settings,
|
2013-11-07 17:26:47 +04:00
|
|
|
DataGenerator = require('./fixtures/data-generator'),
|
2013-12-29 00:13:47 +04:00
|
|
|
API = require('./api');
|
2013-05-24 08:02:41 +04:00
|
|
|
|
2013-10-08 05:39:33 +04:00
|
|
|
function initData() {
|
|
|
|
return migration.init();
|
|
|
|
}
|
2013-06-15 02:12:04 +04:00
|
|
|
|
2013-10-08 05:39:33 +04:00
|
|
|
function clearData() {
|
|
|
|
// we must always try to delete all tables
|
|
|
|
return migration.reset();
|
|
|
|
}
|
2013-06-15 02:12:04 +04:00
|
|
|
|
2013-10-08 05:39:33 +04:00
|
|
|
function insertPosts() {
|
2013-11-05 19:02:57 +04:00
|
|
|
return when(knex('posts').insert(DataGenerator.forKnex.posts).then(function () {
|
2013-12-29 00:13:47 +04:00
|
|
|
return knex('tags').insert(DataGenerator.forKnex.tags).then(function () {
|
|
|
|
return knex('posts_tags').insert(DataGenerator.forKnex.posts_tags);
|
|
|
|
});
|
|
|
|
}));
|
2013-10-08 05:39:33 +04:00
|
|
|
}
|
2013-06-15 02:12:04 +04:00
|
|
|
|
2013-12-31 21:09:49 +04:00
|
|
|
function insertMorePosts(max) {
|
2013-10-08 05:39:33 +04:00
|
|
|
var lang,
|
|
|
|
status,
|
2014-03-16 20:21:02 +04:00
|
|
|
posts = [],
|
2013-10-08 05:39:33 +04:00
|
|
|
promises = [],
|
|
|
|
i, j, k = 0;
|
2013-06-15 02:12:04 +04:00
|
|
|
|
2013-12-31 21:09:49 +04:00
|
|
|
max = max || 50;
|
|
|
|
|
2013-10-08 05:39:33 +04:00
|
|
|
for (i = 0; i < 2; i += 1) {
|
|
|
|
lang = i % 2 ? 'en' : 'fr';
|
|
|
|
posts.push(DataGenerator.forKnex.createGenericPost(k++, null, lang));
|
2013-06-15 02:12:04 +04:00
|
|
|
|
2013-12-31 21:09:49 +04:00
|
|
|
for (j = 0; j < max; j += 1) {
|
|
|
|
status = j % 2 ? 'draft' : 'published';
|
2013-10-08 05:39:33 +04:00
|
|
|
posts.push(DataGenerator.forKnex.createGenericPost(k++, status, lang));
|
|
|
|
}
|
2013-06-25 15:43:15 +04:00
|
|
|
}
|
2013-08-25 00:51:58 +04:00
|
|
|
|
2014-03-16 20:21:02 +04:00
|
|
|
return sequence(_.times(posts.length, function(index) {
|
|
|
|
return function() {
|
|
|
|
return knex('posts').insert(posts[index]);
|
|
|
|
}
|
|
|
|
}));
|
2013-10-08 05:39:33 +04:00
|
|
|
}
|
|
|
|
|
2014-02-13 07:26:56 +04:00
|
|
|
function insertMorePostsTags(max) {
|
|
|
|
max = max || 50;
|
|
|
|
|
|
|
|
return when.all([
|
2014-03-16 20:21:02 +04:00
|
|
|
// PostgreSQL can return results in any order
|
|
|
|
knex('posts').orderBy('id', 'asc').select('id'),
|
2014-02-13 07:26:56 +04:00
|
|
|
knex('tags').select('id', 'name')
|
|
|
|
]).then(function (results) {
|
|
|
|
var posts = _.pluck(results[0], 'id'),
|
|
|
|
injectionTagId = _.chain(results[1])
|
|
|
|
.where({name: 'injection'})
|
|
|
|
.pluck('id')
|
|
|
|
.value()[0],
|
|
|
|
promises = [],
|
|
|
|
i;
|
|
|
|
|
|
|
|
if (max > posts.length) {
|
|
|
|
throw new Error('Trying to add more posts_tags than the number of posts.');
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < max; i += 1) {
|
|
|
|
promises.push(DataGenerator.forKnex.createPostsTags(posts[i], injectionTagId));
|
|
|
|
}
|
|
|
|
|
2014-03-16 20:21:02 +04:00
|
|
|
return sequence(_.times(promises.length, function(index) {
|
|
|
|
return function() {
|
|
|
|
return knex('posts_tags').insert(promises[index]);
|
|
|
|
};
|
|
|
|
}));
|
2014-02-13 07:26:56 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-10-08 05:39:33 +04:00
|
|
|
function insertDefaultUser() {
|
|
|
|
var users = [],
|
2013-11-03 18:12:09 +04:00
|
|
|
userRoles = [];
|
2013-05-24 08:02:41 +04:00
|
|
|
|
2013-10-08 05:39:33 +04:00
|
|
|
users.push(DataGenerator.forKnex.createUser(DataGenerator.Content.users[0]));
|
|
|
|
userRoles.push(DataGenerator.forKnex.createUserRole(1, 1));
|
2014-02-12 07:40:39 +04:00
|
|
|
return knex('users')
|
|
|
|
.insert(users)
|
|
|
|
.then(function () {
|
|
|
|
return knex('roles_users').insert(userRoles);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function insertDefaultApp() {
|
|
|
|
var apps = [];
|
|
|
|
|
|
|
|
apps.push(DataGenerator.forKnex.createApp(DataGenerator.Content.apps[0]));
|
|
|
|
|
|
|
|
return knex('apps')
|
|
|
|
.insert(apps)
|
|
|
|
.then(function () {
|
|
|
|
return knex('permissions_apps')
|
|
|
|
.insert({
|
|
|
|
app_id: 1,
|
|
|
|
permission_id: 1
|
|
|
|
});
|
|
|
|
});
|
2013-12-29 00:13:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function insertDefaultFixtures() {
|
|
|
|
return when(insertDefaultUser().then(function () {
|
|
|
|
return insertPosts();
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
function loadExportFixture(filename) {
|
|
|
|
var filepath = path.resolve(__dirname + '/fixtures/' + filename + '.json');
|
|
|
|
|
|
|
|
return nodefn.call(fs.readFile, filepath).then(function (fileContents) {
|
|
|
|
var data;
|
|
|
|
|
|
|
|
// Parse the json data
|
|
|
|
try {
|
|
|
|
data = JSON.parse(fileContents);
|
|
|
|
} catch (e) {
|
|
|
|
return when.reject(new Error("Failed to parse the file"));
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
});
|
2013-10-08 05:39:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
initData: initData,
|
|
|
|
clearData: clearData,
|
|
|
|
insertDefaultFixtures: insertDefaultFixtures,
|
|
|
|
insertPosts: insertPosts,
|
|
|
|
insertMorePosts: insertMorePosts,
|
2014-02-13 07:26:56 +04:00
|
|
|
insertMorePostsTags: insertMorePostsTags,
|
2013-10-08 05:39:33 +04:00
|
|
|
insertDefaultUser: insertDefaultUser,
|
2014-02-12 07:40:39 +04:00
|
|
|
insertDefaultApp: insertDefaultApp,
|
2013-10-08 05:39:33 +04:00
|
|
|
|
2013-12-29 00:13:47 +04:00
|
|
|
loadExportFixture: loadExportFixture,
|
|
|
|
|
2013-10-08 05:39:33 +04:00
|
|
|
DataGenerator: DataGenerator,
|
|
|
|
API: API
|
|
|
|
};
|