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

Commit a51e55b4 by Matt Broadstone

clean up some tests missed with last weeks progress

There was some refactoring on attributes test where mssql dialect
branching was missed. Also added databaseVersion support for mssql
dialect
1 parent 381751a6
......@@ -30,6 +30,10 @@ module.exports = (function() {
")", "AND", '"s"."name" NOT LIKE', "'db_%'"].join(' ');
},
versionQuery: function() {
return "SELECT @@VERSION as 'version'";
},
createTableQuery: function(tableName, attributes, options) {
var query = "IF OBJECT_ID('[<%= escapedTable %>]', 'U') IS NULL CREATE TABLE <%= table %> (<%= attributes %>)"
, primaryKeys = []
......@@ -182,7 +186,7 @@ module.exports = (function() {
, newName = Object.keys(attributes)[0];
return Utils._.template(query)({
tableName: tableName,
tableName: this.quoteTable(tableName),
before: attrBefore,
after: newName
});
......
......@@ -38,7 +38,6 @@ module.exports = (function() {
if (Utils._.contains(self.sql, 'BEGIN TRANSACTION')) {
self.connection.beginTransaction(function(err) {
if (!!err) {
console.log(err);
reject(self.formatError(err));
} else {
resolve(self.formatResults());
......@@ -47,7 +46,6 @@ module.exports = (function() {
} else if (Utils._.contains(self.sql, 'COMMIT TRANSACTION')) {
self.connection.commitTransaction(function(err) {
if (!!err) {
console.log(err);
reject(self.formatError(err));
} else {
resolve(self.formatResults());
......@@ -56,7 +54,6 @@ module.exports = (function() {
} else if (Utils._.contains(self.sql, 'ROLLBACK TRANSACTION')) {
self.connection.rollbackTransaction(function(err) {
if (!!err) {
console.log(err);
reject(self.formatError(err));
} else {
resolve(self.formatResults());
......@@ -108,7 +105,7 @@ module.exports = (function() {
return promise;
};
/**
/**
* High level function that handles the results of a query execution.
*
*
......@@ -128,6 +125,15 @@ module.exports = (function() {
var result = this.callee;
if (this.isInsertQuery(data)) {
this.handleInsertQuery(data);
if (!this.callee) {
// NOTE: super contrived. This just passes the newly added query-interface
// test returning only the PK. There isn't a way in MSSQL to identify
// that a given return value is the PK, and we have no schema information
// because there was no calling Model.
var record = data[0];
result = record[Object.keys(record)[0]];
}
}
if (this.isShowTableQuery()) {
......@@ -157,6 +163,8 @@ module.exports = (function() {
result = data.length;
} else if (this.isBulkDeleteQuery()){
result = data[0].AFFECTEDROWS;
} else if (this.isVersionQuery()) {
result = data[0].version;
}
return result;
......@@ -167,8 +175,6 @@ module.exports = (function() {
match = err.message.match(/Violation of UNIQUE KEY constraint '(.*)'. Cannot insert duplicate key in object '?(.*?)$/);
match = match || err.message.match(/Cannot insert duplicate key row in object .* with unique index '(.*)'/);
if (match && match.length > 1) {
console.log(err);
return new sequelizeErrors.UniqueConstraintError({
name: 'SequelizeUniqueConstraintError',
fields: null,
......@@ -181,7 +187,6 @@ module.exports = (function() {
match = err.message.match(/Failed on step '(.*)'.Could not create constraint. See previous errors./);
match = err.message.match(/The DELETE statement conflicted with the REFERENCE constraint "(.*)". The conflict occurred in database "(.*)", table "(.*)", column '(.*)'./);
if (match && match.length > 0) {
console.log(err);
return new sequelizeErrors.ForeignKeyConstraintError({
fields: null,
index: match[1],
......@@ -189,7 +194,6 @@ module.exports = (function() {
});
}
console.log(err);
return new sequelizeErrors.DatabaseError(err);
};
......
......@@ -402,12 +402,23 @@ describe(Support.getTestDialectTeaser("Model"), function () {
return this.sequelize.sync({ force: true }).then(function () {
return Test.create({});
}).then(function () {
return Test.findAll({
attributes: [
var findAttributes;
if (dialect === 'mssql') {
findAttributes = [
Sequelize.literal('CAST(CASE WHEN EXISTS(SELECT 1) THEN 1 ELSE 0 END AS BIT) AS "someProperty"'),
[Sequelize.literal('CAST(CASE WHEN EXISTS(SELECT 1) THEN 1 ELSE 0 END AS BIT)'), 'someProperty2']
];
} else {
findAttributes = [
Sequelize.literal('EXISTS(SELECT 1) AS "someProperty"'),
[Sequelize.literal('EXISTS(SELECT 1)'), 'someProperty2']
]
];
}
return Test.findAll({
attributes: findAttributes
});
}).then(function (tests) {
expect(tests[0].get('someProperty')).to.be.ok;
expect(tests[0].get('someProperty2')).to.be.ok;
......@@ -476,4 +487,4 @@ describe(Support.getTestDialectTeaser("Model"), function () {
});
});
});
});
\ No newline at end of file
});
......@@ -101,7 +101,12 @@ describe(Support.getTestDialectTeaser("Model"), function () {
return this.sequelize.sync({ force: true}).then(function () {
return Post.bulkCreate([{ text: 'text1' },{ text: 'text2' }]);
}).then(function () {
return Post.find({ attributes: ['id','text',Sequelize.literal('EXISTS(SELECT 1) AS "someBoolean"')] });
var boolQuery = 'EXISTS(SELECT 1) AS "someBoolean"';
if (dialect === 'mssql') {
boolQuery = 'CAST(CASE WHEN EXISTS(SELECT 1) THEN 1 ELSE 0 END AS BIT) AS "someBoolean"';
}
return Post.find({ attributes: ['id','text', Sequelize.literal(boolQuery)] });
}).then(function (post) {
expect(post.get('someBoolean')).to.be.ok;
expect(post.get().someBoolean).to.be.ok;
......@@ -138,4 +143,4 @@ describe(Support.getTestDialectTeaser("Model"), function () {
});
});
});
});
\ No newline at end of file
});
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!