diff --git a/core/server/data/import/000.js b/core/server/data/import/000.js index 9c2bdd8fa8..874ce4db0c 100644 --- a/core/server/data/import/000.js +++ b/core/server/data/import/000.js @@ -81,7 +81,12 @@ function preProcessPostTags(tableData) { function importTags(ops, tableData) { tableData = stripProperties(['id'], tableData); _.each(tableData, function (tag) { - ops.push(models.Tag.add(tag)); + ops.push(models.Tag.read({name: tag.name}).then(function (_tag) { + if (!_tag) { + return models.Tag.add(tag); + } + return when.resolve(_tag); + })); }); } diff --git a/core/test/unit/import_spec.js b/core/test/unit/import_spec.js index d52370e295..bd23779a6c 100644 --- a/core/test/unit/import_spec.js +++ b/core/test/unit/import_spec.js @@ -1,202 +1,98 @@ -///*globals describe, beforeEach, it*/ -//var testUtils = require('./testUtils'), -// should = require('should'), -// sinon = require('sinon'), -// when = require('when'), -// _ = require("underscore"), -// errors = require('../../server/errorHandling'), -// -// // Stuff we are testing -// knex = require("../../server/models/base").Knex, -// migration = require('../../server/data/migration'), -// exporter = require('../../server/data/export'), -// importer = require('../../server/data/import'), -// Importer001 = require('../../server/data/import/001'), -// Importer002 = require('../../server/data/import/002'), -// Settings = require('../../server/models/settings').Settings; -// -//describe("Import", function () { -// -// should.exist(exporter); -// should.exist(importer); -// -// beforeEach(function (done) { -// // clear database... we need to initialise it manually for each test -// testUtils.clearData().then(function () { -// done(); -// }, done); -// }); -// -// it("resolves 001", function (done) { -// var importStub = sinon.stub(Importer001, "importData", function () { -// return when.resolve(); -// }), -// fakeData = { test: true }; -// -// importer("001", fakeData).then(function () { -// importStub.calledWith(fakeData).should.equal(true); -// -// importStub.restore(); -// -// done(); -// }).then(null, done); -// }); -// -// describe("001", function () { -// this.timeout(4000); -// -// should.exist(Importer001); -// -// it("imports data from 001", function (done) { -// var exportData; -// -// // initialise database to version 001 - confusingly we have to set the max version to be one higher -// // than the migration version we want -// migration.migrateUpFromVersion('001', '002').then(function () { -// return Settings.populateDefaults(); -// }).then(function () { -// // export the version 001 data ready to import -// // TODO: Should have static test data here? -// return exporter("001"); -// }).then(function (exported) { -// exportData = exported; -// -// // Version 001 exporter required the database be empty... -// var tables = [ -// 'posts', 'users', 'roles', 'roles_users', 'permissions', 'permissions_roles', -// 'settings' -// ], -// truncateOps = _.map(tables, function (name) { -// return knex(name).truncate(); -// }); -// -// return when.all(truncateOps); -// }).then(function () { -// return importer("001", exportData); -// }).then(function () { -// // Grab the data from tables -// return when.all([ -// knex("users").select(), -// knex("posts").select(), -// knex("settings").select() -// ]); -// }).then(function (importedData) { -// -// should.exist(importedData); -// importedData.length.should.equal(3); -// -// // we always have 0 users as there isn't one in fixtures -// importedData[0].length.should.equal(0); -// importedData[1].length.should.equal(exportData.data.posts.length); -// importedData[2].length.should.be.above(0); -// -// _.findWhere(exportData.data.settings, {key: "databaseVersion"}).value.should.equal("001"); -// -// done(); -// }).then(null, done); -// }); -// }); -// -// it("resolves 002", function (done) { -// var importStub = sinon.stub(Importer002, "importData", function () { -// return when.resolve(); -// }), -// fakeData = { test: true }; -// -// importer("002", fakeData).then(function () { -// importStub.calledWith(fakeData).should.equal(true); -// -// importStub.restore(); -// -// done(); -// }).then(null, done); -// }); -// -// describe("002", function () { -// this.timeout(4000); -// -// should.exist(Importer002); -// -// it("imports data from 001", function (done) { -// var exportData; -// -// // initialise database to version 001 - confusingly we have to set the max version to be one higher -// // than the migration version we want -// migration.migrateUpFromVersion('001', '002').then(function () { -// return Settings.populateDefaults(); -// }).then(function () { -// // export the version 001 data ready to import -// // TODO: Should have static test data here? -// return exporter("001"); -// }).then(function (exported) { -// exportData = exported; -// -// // now migrate up to the proper version ready for importing - confusingly we have to set the max version -// // to be one higher than the migration version we want -// return migration.migrateUpFromVersion('002', '003'); -// }).then(function () { -// return importer("002", exportData); -// }).then(function () { -// // Grab the data from tables -// return when.all([ -// knex("users").select(), -// knex("posts").select(), -// knex("settings").select() -// ]); -// }).then(function (importedData) { -// -// should.exist(importedData); -// importedData.length.should.equal(3); -// -// // we always have 0 users as there isn't one in fixtures -// importedData[0].length.should.equal(0); -// // import no longer requires all data to be dropped, and adds posts -// importedData[1].length.should.equal(exportData.data.posts.length + 1); -// importedData[2].length.should.be.above(0); -// -// _.findWhere(importedData[2], {key: "databaseVersion"}).value.should.equal("002"); -// -// done(); -// }).then(null, done); -// }); -// -// it("imports data from 002", function (done) { -// var exportData; -// -// // initialise database to version 001 - confusingly we have to set the max version to be one higher -// // than the migration version we want -// migration.migrateUpFromVersion('001', '003').then(function () { -// return Settings.populateDefaults(); -// }).then(function () { -// // export the version 002 data ready to import -// // TODO: Should have static test data here? -// return exporter("002"); -// }).then(function (exported) { -// exportData = exported; -// -// return importer("002", exportData); -// }).then(function () { -// // Grab the data from tables -// return when.all([ -// knex("users").select(), -// knex("posts").select(), -// knex("settings").select() -// ]); -// }).then(function (importedData) { -// -// should.exist(importedData); -// importedData.length.should.equal(3); -// -// // we always have 0 users as there isn't one in fixtures -// importedData[0].length.should.equal(0); -// // import no longer requires all data to be dropped, and adds posts -// importedData[1].length.should.equal(exportData.data.posts.length + 1); -// importedData[2].length.should.be.above(0); -// -// _.findWhere(importedData[2], {key: "databaseVersion"}).value.should.equal("002"); -// -// done(); -// }).then(null, done); -// }); -// }); -//}); +/*globals describe, beforeEach, it*/ +var testUtils = require('./testUtils'), + should = require('should'), + sinon = require('sinon'), + when = require('when'), + _ = require("underscore"), + errors = require('../../server/errorHandling'), + + // Stuff we are testing + knex = require("../../server/models/base").Knex, + migration = require('../../server/data/migration'), + exporter = require('../../server/data/export'), + importer = require('../../server/data/import'), + Importer000 = require('../../server/data/import/000'), + fixtures = require('../../server/data/fixtures'), + Settings = require('../../server/models/settings').Settings; + +describe("Import", function () { + + should.exist(exporter); + should.exist(importer); + + beforeEach(function (done) { + // clear database... we need to initialise it manually for each test + testUtils.clearData().then(function () { + done(); + }, done); + }); + + it("resolves 000", function (done) { + var importStub = sinon.stub(Importer000, "importData", function () { + return when.resolve(); + }), + fakeData = { test: true }; + + importer("000", fakeData).then(function () { + importStub.calledWith(fakeData).should.equal(true); + + importStub.restore(); + + done(); + }).then(null, done); + }); + + describe("000", function () { + this.timeout(4000); + + should.exist(Importer000); + + it("imports data from 000", function (done) { + var exportData; + + // initialise database to version 000 - confusingly we have to set the max version to be one higher + // than the migration version we want. Could just use migrate from fresh here... but this is more explicit + migration.migrateUpFromVersion('000', '001').then(function () { + // Load the fixtures + return fixtures.populateFixtures(); + }).then(function () { + // Initialise the default settings + return Settings.populateDefaults(); + }).then(function () { + // export the version 000 data ready to import + // TODO: Should have static test data here? + return exporter(); + }).then(function (exported) { + exportData = exported; + + return importer("000", exportData); + }).then(function () { + // Grab the data from tables + return when.all([ + knex("users").select(), + knex("posts").select(), + knex("settings").select(), + knex("tags").select() + ]); + }).then(function (importedData) { + + should.exist(importedData); + importedData.length.should.equal(4, 'Did not get data successfully'); + + // we always have 0 users as there isn't one in fixtures + importedData[0].length.should.equal(0, 'There should not be a user'); + // import no longer requires all data to be dropped, and adds posts + importedData[1].length.should.equal(exportData.data.posts.length + 1, 'Wrong number of posts'); + + // test settings + importedData[2].length.should.be.above(0, 'Wrong number of settings'); + _.findWhere(importedData[2], {key: "databaseVersion"}).value.should.equal("000", 'Wrong database version'); + + // test tags + importedData[3].length.should.equal(exportData.data.tags.length, 'no new tags'); + + done(); + }).then(null, done); + }); + }); +});