Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
public
/
sequelize
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
不要怂,就是干,撸起袖子干!
Commit ebd2b5d1
authored
Feb 02, 2015
by
Jan Aagaard Meier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix and refactor tests for raw query
1 parent
68b5fb5b
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
79 additions
and
108 deletions
lib/dialects/mssql/query.js
lib/dialects/postgres/query-generator.js
test/integration/model.test.js
test/integration/sequelize.test.js
test/integration/sequelize.transaction.test.js
test/integration/transaction.test.js
lib/dialects/mssql/query.js
View file @
ebd2b5d
...
...
@@ -153,7 +153,10 @@ module.exports = (function() {
result
=
data
[
0
]
&&
data
[
0
].
AFFECTEDROWS
;
}
else
if
(
this
.
isVersionQuery
())
{
result
=
data
[
0
].
version
;
}
}
else
if
(
this
.
isRawQuery
())
{
// MSSQL returns row data and metadata (affected rows etc) in a single object - let's standarize it, sorta
result
=
[
data
,
data
];
}
return
result
;
};
...
...
lib/dialects/postgres/query-generator.js
View file @
ebd2b5d
...
...
@@ -869,8 +869,6 @@ module.exports = (function() {
return
dataType
;
},
quoteIdentifier
:
function
(
identifier
,
force
)
{
var
_
=
Utils
.
_
;
if
(
identifier
===
'*'
)
return
identifier
;
...
...
test/integration/model.test.js
View file @
ebd2b5d
...
...
@@ -1229,7 +1229,7 @@ describe(Support.getTestDialectTeaser('Model'), function() {
it
(
'sets deletedAt to the current timestamp if paranoid is true'
,
function
()
{
var
self
=
this
,
qi
=
this
.
sequelize
.
queryInterface
.
QueryGenerator
.
quoteIdentifier
,
qi
=
this
.
sequelize
.
queryInterface
.
QueryGenerator
.
quoteIdentifier
.
bind
(
this
.
sequelize
.
queryInterface
.
QueryGenerator
)
,
ParanoidUser
=
self
.
sequelize
.
define
(
'ParanoidUser'
,
{
username
:
Sequelize
.
STRING
,
secretValue
:
Sequelize
.
STRING
,
...
...
test/integration/sequelize.test.js
View file @
ebd2b5d
...
...
@@ -335,64 +335,64 @@ describe(Support.getTestDialectTeaser('Sequelize'), function() {
});
it
(
'replaces token with the passed array'
,
function
(
done
)
{
this
.
sequelize
.
query
(
'select ? as foo, ? as bar'
,
null
,
{
type
:
this
.
sequelize
.
QueryTypes
.
SELECT
},
[
1
,
2
]
).
success
(
function
(
result
)
{
this
.
sequelize
.
query
(
'select ? as foo, ? as bar'
,
null
,
{
type
:
this
.
sequelize
.
QueryTypes
.
SELECT
,
replacements
:
[
1
,
2
]
}
).
success
(
function
(
result
)
{
expect
(
result
).
to
.
deep
.
equal
([{
foo
:
1
,
bar
:
2
}]);
done
();
});
});
it
(
'replaces named parameters with the passed object'
,
function
()
{
return
expect
(
this
.
sequelize
.
query
(
'select :one as foo, :two as bar'
,
null
,
{
raw
:
true
},
{
one
:
1
,
two
:
2
}).
get
(
0
))
return
expect
(
this
.
sequelize
.
query
(
'select :one as foo, :two as bar'
,
null
,
{
raw
:
true
,
replacements
:
{
one
:
1
,
two
:
2
}
}).
get
(
0
))
.
to
.
eventually
.
deep
.
equal
([{
foo
:
1
,
bar
:
2
}]);
});
it
(
'replaces named parameters with the passed object and ignore those which does not qualify'
,
function
()
{
return
expect
(
this
.
sequelize
.
query
(
'select :one as foo, :two as bar, \'00:00\' as baz'
,
null
,
{
raw
:
true
},
{
one
:
1
,
two
:
2
}).
get
(
0
))
return
expect
(
this
.
sequelize
.
query
(
'select :one as foo, :two as bar, \'00:00\' as baz'
,
null
,
{
raw
:
true
,
replacements
:
{
one
:
1
,
two
:
2
}
}).
get
(
0
))
.
to
.
eventually
.
deep
.
equal
([{
foo
:
1
,
bar
:
2
,
baz
:
'00:00'
}]);
});
it
(
'replaces named parameters with the passed object using the same key twice'
,
function
()
{
return
expect
(
this
.
sequelize
.
query
(
'select :one as foo, :two as bar, :one as baz'
,
null
,
{
raw
:
true
},
{
one
:
1
,
two
:
2
}).
get
(
0
))
return
expect
(
this
.
sequelize
.
query
(
'select :one as foo, :two as bar, :one as baz'
,
null
,
{
raw
:
true
,
replacements
:
{
one
:
1
,
two
:
2
}
}).
get
(
0
))
.
to
.
eventually
.
deep
.
equal
([{
foo
:
1
,
bar
:
2
,
baz
:
1
}]);
});
it
(
'replaces named parameters with the passed object having a null property'
,
function
()
{
return
expect
(
this
.
sequelize
.
query
(
'select :one as foo, :two as bar'
,
null
,
{
raw
:
true
},
{
one
:
1
,
two
:
null
}).
get
(
0
))
return
expect
(
this
.
sequelize
.
query
(
'select :one as foo, :two as bar'
,
null
,
{
raw
:
true
,
replacements
:
{
one
:
1
,
two
:
null
}
}).
get
(
0
))
.
to
.
eventually
.
deep
.
equal
([{
foo
:
1
,
bar
:
null
}]);
});
it
(
'throw an exception when key is missing in the passed object'
,
function
()
{
var
self
=
this
;
expect
(
function
()
{
self
.
sequelize
.
query
(
'select :one as foo, :two as bar, :three as baz'
,
null
,
{
raw
:
true
},
{
one
:
1
,
two
:
2
});
self
.
sequelize
.
query
(
'select :one as foo, :two as bar, :three as baz'
,
null
,
{
raw
:
true
,
replacements
:
{
one
:
1
,
two
:
2
}
});
}).
to
.
throw
(
Error
,
/Named parameter ":
\w
+" has no value in the given object
\.
/g
);
});
it
(
'throw an exception with the passed number'
,
function
()
{
var
self
=
this
;
expect
(
function
()
{
self
.
sequelize
.
query
(
'select :one as foo, :two as bar'
,
null
,
{
raw
:
true
},
2
);
self
.
sequelize
.
query
(
'select :one as foo, :two as bar'
,
null
,
{
raw
:
true
,
replacements
:
2
}
);
}).
to
.
throw
(
Error
,
/Named parameter ":
\w
+" has no value in the given object
\.
/g
);
});
it
(
'throw an exception with the passed empty object'
,
function
()
{
var
self
=
this
;
expect
(
function
()
{
self
.
sequelize
.
query
(
'select :one as foo, :two as bar'
,
null
,
{
raw
:
true
},
{
});
self
.
sequelize
.
query
(
'select :one as foo, :two as bar'
,
null
,
{
raw
:
true
,
replacements
:
{}
});
}).
to
.
throw
(
Error
,
/Named parameter ":
\w
+" has no value in the given object
\.
/g
);
});
it
(
'throw an exception with the passed string'
,
function
()
{
var
self
=
this
;
expect
(
function
()
{
self
.
sequelize
.
query
(
'select :one as foo, :two as bar'
,
null
,
{
raw
:
true
},
'foobar'
);
self
.
sequelize
.
query
(
'select :one as foo, :two as bar'
,
null
,
{
raw
:
true
,
replacements
:
'foobar'
}
);
}).
to
.
throw
(
Error
,
/Named parameter ":
\w
+" has no value in the given object
\.
/g
);
});
it
(
'throw an exception with the passed date'
,
function
()
{
var
self
=
this
;
expect
(
function
()
{
self
.
sequelize
.
query
(
'select :one as foo, :two as bar'
,
null
,
{
raw
:
true
},
new
Date
()
);
self
.
sequelize
.
query
(
'select :one as foo, :two as bar'
,
null
,
{
raw
:
true
,
replacements
:
new
Date
()}
);
}).
to
.
throw
(
Error
,
/Named parameter ":
\w
+" has no value in the given object
\.
/g
);
});
...
...
@@ -428,7 +428,7 @@ describe(Support.getTestDialectTeaser('Sequelize'), function() {
}.
bind
(
this
)).
to
.
throw
(
TypeError
,
'options.transaction is required'
);
});
it
.
only
(
'one value'
,
function
()
{
it
(
'one value'
,
function
()
{
return
this
.
sequelize
.
transaction
().
bind
(
this
).
then
(
function
(
t
)
{
this
.
t
=
t
;
return
this
.
sequelize
.
set
({
foo
:
'bar'
},
{
transaction
:
t
});
...
...
@@ -873,87 +873,71 @@ describe(Support.getTestDialectTeaser('Sequelize'), function() {
});
if
(
dialect
===
'sqlite'
)
{
it
(
'correctly scopes transaction from other connections'
,
function
(
done
)
{
it
(
'correctly scopes transaction from other connections'
,
function
()
{
var
TransactionTest
=
this
.
sequelizeWithTransaction
.
define
(
'TransactionTest'
,
{
name
:
DataTypes
.
STRING
},
{
timestamps
:
false
})
,
self
=
this
;
var
count
=
function
(
transaction
,
callback
)
{
var
count
=
function
(
transaction
)
{
var
sql
=
self
.
sequelizeWithTransaction
.
getQueryInterface
().
QueryGenerator
.
selectQuery
(
'TransactionTests'
,
{
attributes
:
[[
'count(*)'
,
'cnt'
]]
});
self
.
sequelizeWithTransaction
.
query
(
sql
,
null
,
{
plain
:
true
,
raw
:
true
,
transaction
:
transaction
,
type
:
self
.
sequelize
.
QueryTypes
.
SELECT
})
.
then
(
function
(
result
)
{
callback
(
result
.
cnt
);
});
return
self
.
sequelizeWithTransaction
.
query
(
sql
,
{
plain
:
true
,
transaction
:
transaction
}).
then
(
function
(
result
)
{
return
result
.
cnt
;
});
};
TransactionTest
.
sync
({
force
:
true
}).
success
(
function
()
{
self
.
sequelizeWithTransaction
.
transaction
().
then
(
function
(
t1
)
{
self
.
sequelizeWithTransaction
.
query
(
'INSERT INTO '
+
qq
(
'TransactionTests'
)
+
' ('
+
qq
(
'name'
)
+
') VALUES (\'foo\');'
,
null
,
{
plain
:
true
,
raw
:
true
,
transaction
:
t1
}).
success
(
function
()
{
count
(
null
,
function
(
cnt
)
{
expect
(
cnt
).
to
.
equal
(
0
);
count
(
t1
,
function
(
cnt
)
{
expect
(
cnt
).
to
.
equal
(
1
);
t1
.
commit
().
success
(
function
()
{
count
(
null
,
function
(
cnt
)
{
expect
(
cnt
).
to
.
equal
(
1
);
done
();
});
});
});
});
});
});
return
TransactionTest
.
sync
({
force
:
true
}).
bind
(
this
).
then
(
function
()
{
return
self
.
sequelizeWithTransaction
.
transaction
();
}).
then
(
function
(
t1
)
{
this
.
t1
=
t1
;
return
self
.
sequelizeWithTransaction
.
query
(
'INSERT INTO '
+
qq
(
'TransactionTests'
)
+
' ('
+
qq
(
'name'
)
+
') VALUES (\'foo\');'
,
{
transaction
:
t1
});
}).
then
(
function
()
{
return
expect
(
count
()).
to
.
eventually
.
equal
(
0
);
}).
then
(
function
(
cnt
)
{
return
expect
(
count
(
this
.
t1
)).
to
.
eventually
.
equal
(
1
);
}).
then
(
function
()
{
return
this
.
t1
.
commit
();
}).
then
(
function
()
{
return
expect
(
count
()).
to
.
eventually
.
equal
(
1
);
});
});
}
else
{
it
(
'correctly handles multiple transactions'
,
function
(
done
)
{
it
(
'correctly handles multiple transactions'
,
function
()
{
var
TransactionTest
=
this
.
sequelizeWithTransaction
.
define
(
'TransactionTest'
,
{
name
:
DataTypes
.
STRING
},
{
timestamps
:
false
})
,
self
=
this
;
var
count
=
function
(
transaction
,
callback
)
{
var
count
=
function
(
transaction
)
{
var
sql
=
self
.
sequelizeWithTransaction
.
getQueryInterface
().
QueryGenerator
.
selectQuery
(
'TransactionTests'
,
{
attributes
:
[[
'count(*)'
,
'cnt'
]]
});
self
.
sequelizeWithTransaction
.
query
(
sql
,
{
plain
:
true
,
transaction
:
transaction
})
.
success
(
function
(
result
)
{
callback
(
parseInt
(
result
.
cnt
,
10
));
});
return
self
.
sequelizeWithTransaction
.
query
(
sql
,
{
plain
:
true
,
transaction
:
transaction
}).
then
(
function
(
result
)
{
return
parseInt
(
result
.
cnt
,
10
);
});
};
TransactionTest
.
sync
({
force
:
true
}).
success
(
function
()
{
self
.
sequelizeWithTransaction
.
transaction
().
then
(
function
(
t1
)
{
self
.
sequelizeWithTransaction
.
query
(
'INSERT INTO '
+
qq
(
'TransactionTests'
)
+
' ('
+
qq
(
'name'
)
+
') VALUES (\'foo\');'
,
null
,
{
plain
:
true
,
raw
:
true
,
transaction
:
t1
}).
success
(
function
()
{
self
.
sequelizeWithTransaction
.
transaction
().
then
(
function
(
t2
)
{
self
.
sequelizeWithTransaction
.
query
(
'INSERT INTO '
+
qq
(
'TransactionTests'
)
+
' ('
+
qq
(
'name'
)
+
') VALUES (\'bar\');'
,
null
,
{
plain
:
true
,
raw
:
true
,
transaction
:
t2
}).
success
(
function
()
{
count
(
null
,
function
(
cnt
)
{
expect
(
cnt
).
to
.
equal
(
0
);
count
(
t1
,
function
(
cnt
)
{
expect
(
cnt
).
to
.
equal
(
1
);
count
(
t2
,
function
(
cnt
)
{
expect
(
cnt
).
to
.
equal
(
1
);
t2
.
rollback
().
success
(
function
()
{
count
(
null
,
function
(
cnt
)
{
expect
(
cnt
).
to
.
equal
(
0
);
t1
.
commit
().
success
(
function
()
{
count
(
null
,
function
(
cnt
)
{
expect
(
cnt
).
to
.
equal
(
1
);
done
();
});
});
});
});
});
});
});
});
});
});
});
return
TransactionTest
.
sync
({
force
:
true
}).
bind
(
this
).
then
(
function
()
{
return
self
.
sequelizeWithTransaction
.
transaction
();
}).
then
(
function
(
t1
)
{
this
.
t1
=
t1
;
return
self
.
sequelizeWithTransaction
.
query
(
'INSERT INTO '
+
qq
(
'TransactionTests'
)
+
' ('
+
qq
(
'name'
)
+
') VALUES (\'foo\');'
,
null
,
{
transaction
:
t1
});
}).
then
(
function
()
{
return
self
.
sequelizeWithTransaction
.
transaction
();
}).
then
(
function
(
t2
)
{
this
.
t2
=
t2
;
return
self
.
sequelizeWithTransaction
.
query
(
'INSERT INTO '
+
qq
(
'TransactionTests'
)
+
' ('
+
qq
(
'name'
)
+
') VALUES (\'bar\');'
,
null
,
{
transaction
:
t2
});
}).
then
(
function
()
{
return
expect
(
count
()).
to
.
eventually
.
equal
(
0
);
}).
then
(
function
()
{
return
expect
(
count
(
this
.
t1
)).
to
.
eventually
.
equal
(
1
);
}).
then
(
function
()
{
return
expect
(
count
(
this
.
t2
)).
to
.
eventually
.
equal
(
1
);
}).
then
(
function
()
{
return
this
.
t2
.
rollback
();
}).
then
(
function
()
{
return
expect
(
count
()).
to
.
eventually
.
equal
(
0
);
}).
then
(
function
(
cnt
)
{
return
this
.
t1
.
commit
();
}).
then
(
function
()
{
return
expect
(
count
()).
to
.
eventually
.
equal
(
1
);
});
});
}
...
...
test/integration/sequelize.transaction.test.js
View file @
ebd2b5d
...
...
@@ -20,7 +20,7 @@ describe(Support.getTestDialectTeaser('Sequelize#transaction'), function() {
.
success
(
function
()
{
done
();
});
});
it
(
'gets triggered once a transaction has been successfully roll
backed
'
,
function
(
done
)
{
it
(
'gets triggered once a transaction has been successfully roll
ed back
'
,
function
(
done
)
{
this
.
sequelize
.
transaction
().
then
(
function
(
t
)
{
t
.
rollback
();
})
...
...
@@ -28,16 +28,15 @@ describe(Support.getTestDialectTeaser('Sequelize#transaction'), function() {
});
if
(
Support
.
getTestDialect
()
!==
'sqlite'
)
{
it
(
'works for long running transactions'
,
function
(
done
)
{
Support
.
prepareTransactionTest
(
this
.
sequelize
,
function
(
sequelize
)
{
it
(
'works for long running transactions'
,
function
()
{
return
Support
.
prepareTransactionTest
(
this
.
sequelize
,
function
(
sequelize
)
{
var
User
=
sequelize
.
define
(
'User'
,
{
name
:
Support
.
Sequelize
.
STRING
},
{
timestamps
:
false
});
return
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
return
sequelize
.
sync
({
force
:
true
}).
then
(
function
()
{
return
sequelize
.
transaction
();
}).
then
(
function
(
t
)
{
expect
(
t
).
to
.
be
.
ok
;
var
query
=
'select sleep(2);'
;
switch
(
Support
.
getTestDialect
())
{
...
...
@@ -51,36 +50,23 @@ describe(Support.getTestDialectTeaser('Sequelize#transaction'), function() {
break
;
}
return
sequelize
.
query
(
query
,
null
,
{
raw
:
true
,
plain
:
true
,
transaction
:
t
}).
then
(
function
()
{
return
sequelize
.
query
(
query
,
{
transaction
:
t
}).
then
(
function
()
{
var
dao
=
User
.
build
({
name
:
'foo'
});
// this.QueryGenerator.insertQuery(tableName, values, dao.daoFactory.rawAttributes)
return
query
=
sequelize
.
getQueryInterface
()
.
QueryGenerator
.
insertQuery
(
User
.
tableName
,
dao
.
values
,
User
.
rawAttributes
);
return
sequelize
.
getQueryInterface
().
QueryGenerator
.
insertQuery
(
User
.
tableName
,
dao
.
values
,
User
.
rawAttributes
);
}).
then
(
function
()
{
return
Promise
.
delay
(
1000
);
}).
then
(
function
()
{
return
sequelize
.
query
(
query
,
null
,
{
raw
:
true
,
plain
:
true
,
transaction
:
t
});
return
sequelize
.
query
(
query
,
{
transaction
:
t
});
}).
then
(
function
()
{
return
t
.
commit
();
});
}).
then
(
function
()
{
return
User
.
all
().
success
(
function
(
users
)
{
expect
(
users
.
length
).
to
.
equal
(
1
);
expect
(
users
[
0
].
name
).
to
.
equal
(
'foo'
);
done
();
});
}).
catch
(
done
);
return
User
.
all
();
}).
then
(
function
(
users
)
{
expect
(
users
.
length
).
to
.
equal
(
1
);
expect
(
users
[
0
].
name
).
to
.
equal
(
'foo'
);
});
});
});
}
...
...
test/integration/transaction.test.js
View file @
ebd2b5d
...
...
@@ -11,7 +11,7 @@ var chai = require('chai')
if
(
current
.
dialect
.
supports
.
transactions
)
{
describe
(
Support
.
getTestDialectTeaser
(
'Transaction'
),
function
()
{
describe
.
skip
(
Support
.
getTestDialectTeaser
(
'Transaction'
),
function
()
{
this
.
timeout
(
4000
);
describe
(
'constructor'
,
function
()
{
it
(
'stores options'
,
function
()
{
...
...
@@ -98,7 +98,7 @@ describe(Support.getTestDialectTeaser('Transaction'), function() {
.
then
(
function
(
t
)
{
return
sequelize
.
sync
({
transaction
:
t
})
.
then
(
function
(
)
{
return
t
;
return
t
;
});
})
.
then
(
function
(
t
)
{
...
...
@@ -116,7 +116,7 @@ describe(Support.getTestDialectTeaser('Transaction'), function() {
.
then
(
function
(
users
)
{
return
expect
(
users
.
length
).
to
.
equal
(
1
);
});
});
});
}
if
(
current
.
dialect
.
supports
.
lock
)
{
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment