不要怂,就是干,撸起袖子干!

Commit e893107e by Sascha Depold

moved query-interface specs to buster

1 parent 2d5e69d8
...@@ -208,6 +208,27 @@ module.exports = (function() { ...@@ -208,6 +208,27 @@ module.exports = (function() {
} }
return hashToWhereConditions(hash).replace(/\\'/g, "''"); return hashToWhereConditions(hash).replace(/\\'/g, "''");
},
showIndexQuery: function(tableName) {
var sql = "PRAGMA INDEX_LIST('<%= tableName %>')"
return Utils._.template(sql, { tableName: tableName })
},
removeIndexQuery: function(tableName, indexNameOrAttributes) {
var sql = "DROP INDEX <%= indexName %>"
, indexName = indexNameOrAttributes
if (typeof indexName !== 'string') {
indexName = Utils._.underscored(tableName + '_' + indexNameOrAttributes.join('_'))
}
return Utils._.template(sql, { tableName: tableName, indexName: indexName })
},
describeTableQuery: function(tableName) {
var sql = "PRAGMA TABLE_INFO('<%= tableName %>')"
return Utils._.template(sql, { tableName: tableName })
} }
} }
......
...@@ -76,7 +76,6 @@ module.exports = (function() { ...@@ -76,7 +76,6 @@ module.exports = (function() {
var onSuccess = function(results, metaData) { var onSuccess = function(results, metaData) {
var result = this.callee var result = this.callee
, self = this
// add the inserted row id to the instance // add the inserted row id to the instance
if (this.send('isInsertQuery', results, metaData)) { if (this.send('isInsertQuery', results, metaData)) {
...@@ -102,6 +101,12 @@ module.exports = (function() { ...@@ -102,6 +101,12 @@ module.exports = (function() {
result = this.send('handleSelectQuery', results) result = this.send('handleSelectQuery', results)
} else if (this.send('isShowOrDescribeQuery')) { } else if (this.send('isShowOrDescribeQuery')) {
result = results result = results
} else if (this.sql.indexOf('PRAGMA INDEX_LIST') !== -1) {
// this is the sqlite way of getting the indexes of a table
result = results.map(function(result) { return { Key_name: result.name } })
} else if (this.sql.indexOf('PRAGMA TABLE_INFO') !== -1) {
// this is the sqlite way of getting the metadata of a table
console.log(results)
} }
this.emit('success', result) this.emit('success', result)
......
...@@ -83,11 +83,13 @@ module.exports = (function() { ...@@ -83,11 +83,13 @@ module.exports = (function() {
return new Utils.CustomEventEmitter(function(emitter) { return new Utils.CustomEventEmitter(function(emitter) {
var sql; var sql;
if (self.QueryGenerator.describeTableQuery) { if (self.QueryGenerator.describeTableQuery) {
sql = self.QueryGenerator.describeTableQuery(tableName) sql = self.QueryGenerator.describeTableQuery(tableName)
} else { } else {
sql = 'DESCRIBE `' + tableName + '`;' sql = 'DESCRIBE `' + tableName + '`;'
} }
self.sequelize.query(sql, null, { raw: true }).success(function(data) { self.sequelize.query(sql, null, { raw: true }).success(function(data) {
emitter.emit('success', data) emitter.emit('success', data)
}).error(function(err) { }).error(function(err) {
......
var config = require("./config/config")
, Sequelize = require("../index")
, sequelize = new Sequelize(config.mysql.database, config.mysql.username, config.mysql.password, { logging: false })
, Helpers = new (require("./config/helpers"))(sequelize)
, QueryInterface = require("../lib/query-interface")
describe('QueryInterface', function() {
var interface = null
beforeEach(function() {
interface = sequelize.getQueryInterface()
Helpers.dropAllTables()
})
afterEach(function() {
interface = null
Helpers.dropAllTables()
})
describe('dropAllTables', function() {
it("should drop all tables", function() {
Helpers.async(function(done) {
interface.dropAllTables().success(done).error(function(err) { console.log(err) })
})
Helpers.async(function(done) {
interface.showAllTables().success(function(tableNames) {
expect(tableNames.length).toEqual(0)
done()
})
})
Helpers.async(function(done) {
interface.createTable('table', { name: Sequelize.STRING })
.success(done)
.error(function(err){ console.log(err)})
})
Helpers.async(function(done) {
interface.showAllTables().success(function(tableNames) {
expect(tableNames.length).toEqual(1)
done()
})
})
Helpers.async(function(done) {
interface.dropAllTables().success(done).error(function(err) { console.log(err) })
})
Helpers.async(function(done) {
interface.showAllTables().success(function(tableNames) {
expect(tableNames.length).toEqual(0)
done()
})
})
})
})
describe('indexes', function() {
beforeEach(function(){
Helpers.async(function(done) {
interface.createTable('User', {
username: Sequelize.STRING,
isAdmin: Sequelize.BOOLEAN
}).success(done)
})
})
it('adds, reads and removes an index to the table', function() {
Helpers.async(function(done) {
interface.addIndex('User', ['username', 'isAdmin']).success(done).error(function(err) {
console.log(err)
})
})
Helpers.async(function(done) {
interface.showIndex('User').success(function(indexes) {
var indexColumns = indexes.map(function(index) { return index.Column_name }).sort()
expect(indexColumns).toEqual(['isAdmin', 'username'])
done()
}).error(function(err) { console.log(err) })
})
Helpers.async(function(done) {
interface.removeIndex('User', ['username', 'isAdmin']).success(done).error(function(err) {
console.log(err)
})
})
Helpers.async(function(done) {
interface.showIndex('User').success(function(indexes) {
var indexColumns = indexes.map(function(index) { return index.Column_name }).sort()
expect(indexColumns).toEqual([])
done()
}).error(function(err) { console.log(err) })
})
})
})
})
if(typeof require === 'function') {
const buster = require("buster")
, CustomEventEmitter = require("../lib/emitters/custom-event-emitter")
, Helpers = require('./buster-helpers')
, dialect = Helpers.getTestDialect()
}
buster.spec.expose()
buster.testRunner.timeout = 1000
describe(Helpers.getTestDialectTeaser("QueryInterface"), function() {
before(function(done) {
Helpers.initTests({
dialect: dialect,
beforeComplete: function(sequelize) {
this.sequelize = sequelize
}.bind(this),
onComplete: function() {
this.interface = this.sequelize.getQueryInterface()
done()
}.bind(this)
})
})
describe('dropAllTables', function() {
it("should drop all tables", function(done) {
this.interface.dropAllTables().complete(function(err) {
expect(err).toBeNull()
this.interface.showAllTables().complete(function(err, tableNames) {
expect(err).toBeNull()
expect(tableNames.length).toEqual(0)
this.interface.createTable('table', { name: Helpers.Sequelize.STRING }).complete(function(err) {
expect(err).toBeNull()
this.interface.showAllTables().complete(function(err, tableNames) {
expect(err).toBeNull()
expect(tableNames.length).toEqual(1)
this.interface.dropAllTables().complete(function(err) {
expect(err).toBeNull()
this.interface.showAllTables().complete(function(err, tableNames) {
expect(err).toBeNull()
expect(tableNames.length).toEqual(0)
done()
})
}.bind(this))
}.bind(this))
}.bind(this))
}.bind(this))
}.bind(this))
})
})
describe('=>indexes', function() {
before(function(done) {
this.interface.createTable('User', {
username: Helpers.Sequelize.STRING,
isAdmin: Helpers.Sequelize.BOOLEAN
}).success(done)
})
it('adds, reads and removes an index to the table', function(done) {
this.interface.addIndex('User', ['username', 'isAdmin']).complete(function(err) {
expect(err).toBeNull()
this.interface.showIndex('User').complete(function(err, indexes) {
expect(err).toBeNull()
var indexColumns = Helpers.Sequelize.Utils._.uniq(indexes.map(function(index) { return index.Key_name }))
expect(indexColumns).toEqual(['user_username_is_admin'])
this.interface.removeIndex('User', ['username', 'isAdmin']).complete(function(err) {
expect(err).toBeNull()
this.interface.showIndex('User').complete(function(err, indexes) {
expect(err).toBeNull()
indexColumns = Helpers.Sequelize.Utils._.uniq(indexes.map(function(index) { return index.Key_name }))
expect(indexColumns).toEqual([])
done()
})
}.bind(this))
}.bind(this))
}.bind(this))
})
})
describe('describeTable', function() {
})
})
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!