|
| 1 | +const expect = require('expect.js'); |
| 2 | +const Support = require(__dirname + '/../support'); |
| 3 | +const helpers = require(__dirname + '/../support/helpers'); |
| 4 | +const gulp = require('gulp'); |
| 5 | +const _ = require('lodash'); |
| 6 | + |
| 7 | +const prepare = function (flag, callback, options) { |
| 8 | + options = _.assign({ config: {} }, options || {}); |
| 9 | + |
| 10 | + const configPath = 'config/config.json'; |
| 11 | + const config = _.assign({}, helpers.getTestConfig(), options.config); |
| 12 | + const configContent = JSON.stringify(config); |
| 13 | + |
| 14 | + gulp |
| 15 | + .src(Support.resolveSupportPath('tmp')) |
| 16 | + .pipe(helpers.clearDirectory()) |
| 17 | + .pipe(helpers.runCli('init')) |
| 18 | + .pipe(helpers.removeFile(configPath)) |
| 19 | + .pipe(helpers.overwriteFile(configContent, configPath)) |
| 20 | + .pipe(helpers.runCli('db:create')) |
| 21 | + .pipe(helpers.runCli(flag, { pipeStdout: true })) |
| 22 | + .pipe(helpers.teardown(callback)); |
| 23 | +}; |
| 24 | + |
| 25 | +describe(Support.getTestDialectTeaser('db:drop'), () => { |
| 26 | + if (Support.dialectIsPostgres()) { |
| 27 | + it('correctly drops database', function (done) { |
| 28 | + const databaseName = `my_test_db_${_.random(10000, 99999)}`; |
| 29 | + prepare( |
| 30 | + 'db:drop', |
| 31 | + () => { |
| 32 | + this.sequelize.query(`SELECT 1 as exists FROM pg_database WHERE datname = '${databaseName}';`, { |
| 33 | + type: this.sequelize.QueryTypes.SELECT |
| 34 | + }).then(result => { |
| 35 | + expect(result).to.be.empty; |
| 36 | + done(); |
| 37 | + }); |
| 38 | + }, { |
| 39 | + config: { |
| 40 | + database: databaseName |
| 41 | + } |
| 42 | + }); |
| 43 | + }); |
| 44 | + } |
| 45 | + |
| 46 | + if (Support.dialectIsMySQL()) { |
| 47 | + it('correctly drops database', function (done) { |
| 48 | + const databaseName = `my_test_db_${_.random(10000, 99999)}`; |
| 49 | + prepare( |
| 50 | + 'db:drop', |
| 51 | + () => { |
| 52 | + this.sequelize.query(`SELECT IF('${databaseName}' IN(SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA), 1, 0) AS found;`, { |
| 53 | + type: this.sequelize.QueryTypes.SELECT |
| 54 | + }).then(result => { |
| 55 | + expect(result[0].found).to.eql(0); |
| 56 | + done(); |
| 57 | + }); |
| 58 | + }, { |
| 59 | + config: { |
| 60 | + database: databaseName |
| 61 | + } |
| 62 | + }); |
| 63 | + }); |
| 64 | + } |
| 65 | +}); |
0 commit comments