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 9512ab91
authored
Jan 25, 2020
by
Pedro Augusto de Paula Barbosa
Committed by
Sushant
Jan 25, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor(tests): use async/await (#11876)
1 parent
f843582c
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
148 additions
and
255 deletions
test/integration/operators.test.js
test/integration/pool.test.js
test/integration/operators.test.js
View file @
9512ab9
...
...
@@ -3,7 +3,6 @@
const
chai
=
require
(
'chai'
),
Sequelize
=
require
(
'../../index'
),
Op
=
Sequelize
.
Op
,
Promise
=
Sequelize
.
Promise
,
expect
=
chai
.
expect
,
Support
=
require
(
'../support'
),
DataTypes
=
require
(
'../../lib/data-types'
),
...
...
@@ -11,7 +10,7 @@ const chai = require('chai'),
describe
(
Support
.
getTestDialectTeaser
(
'Operators'
),
()
=>
{
describe
(
'REGEXP'
,
()
=>
{
beforeEach
(
function
()
{
beforeEach
(
async
function
()
{
this
.
User
=
this
.
sequelize
.
define
(
'user'
,
{
id
:
{
type
:
DataTypes
.
INTEGER
,
...
...
@@ -29,145 +28,93 @@ describe(Support.getTestDialectTeaser('Operators'), () => {
timestamps
:
false
});
return
Promise
.
all
([
this
.
sequelize
.
getQueryInterface
().
createTable
(
'users'
,
{
userId
:
{
type
:
DataTypes
.
INTEGER
,
allowNull
:
false
,
primaryKey
:
true
,
autoIncrement
:
true
},
full_name
:
{
type
:
DataTypes
.
STRING
}
})
]);
await
this
.
sequelize
.
getQueryInterface
().
createTable
(
'users'
,
{
userId
:
{
type
:
DataTypes
.
INTEGER
,
allowNull
:
false
,
primaryKey
:
true
,
autoIncrement
:
true
},
full_name
:
{
type
:
DataTypes
.
STRING
}
});
});
if
(
dialect
===
'mysql'
||
dialect
===
'postgres'
)
{
describe
(
'case sensitive'
,
()
=>
{
it
(
'should work with a regexp where'
,
function
()
{
return
this
.
User
.
create
({
name
:
'Foobar'
}).
then
(()
=>
{
return
this
.
User
.
findOne
({
where
:
{
name
:
{
[
Op
.
regexp
]:
'^Foo'
}
}
});
}).
then
(
user
=>
{
expect
(
user
).
to
.
be
.
ok
;
it
(
'should work with a regexp where'
,
async
function
()
{
await
this
.
User
.
create
({
name
:
'Foobar'
});
const
user
=
await
this
.
User
.
findOne
({
where
:
{
name
:
{
[
Op
.
regexp
]:
'^Foo'
}
}
});
expect
(
user
).
to
.
be
.
ok
;
});
it
(
'should work with a not regexp where'
,
function
()
{
return
this
.
User
.
create
({
name
:
'Foobar'
}).
then
(()
=>
{
return
this
.
User
.
findOne
({
where
:
{
name
:
{
[
Op
.
notRegexp
]:
'^Foo'
}
}
});
}).
then
(
user
=>
{
expect
(
user
).
to
.
not
.
be
.
ok
;
it
(
'should work with a not regexp where'
,
async
function
()
{
await
this
.
User
.
create
({
name
:
'Foobar'
});
const
user
=
await
this
.
User
.
findOne
({
where
:
{
name
:
{
[
Op
.
notRegexp
]:
'^Foo'
}
}
});
expect
(
user
).
to
.
not
.
be
.
ok
;
});
it
(
'should properly escape regular expressions'
,
function
()
{
return
this
.
User
.
bulkCreate
([{
name
:
'John'
},
{
name
:
'Bob'
}]).
then
(()
=>
{
return
this
.
User
.
findAll
({
where
:
{
name
:
{
[
Op
.
notRegexp
]:
"Bob'; drop table users --"
}
}
});
}).
then
(()
=>
{
return
this
.
User
.
findAll
({
where
:
{
name
:
{
[
Op
.
regexp
]:
"Bob'; drop table users --"
}
}
});
}).
then
(()
=>
{
return
this
.
User
.
findAll
();
}).
then
(
users
=>
{
expect
(
users
).
length
(
2
);
it
(
'should properly escape regular expressions'
,
async
function
()
{
await
this
.
User
.
bulkCreate
([{
name
:
'John'
},
{
name
:
'Bob'
}]);
await
this
.
User
.
findAll
({
where
:
{
name
:
{
[
Op
.
notRegexp
]:
"Bob'; drop table users --"
}
}
});
await
this
.
User
.
findAll
({
where
:
{
name
:
{
[
Op
.
regexp
]:
"Bob'; drop table users --"
}
}
});
expect
(
await
this
.
User
.
findAll
()).
to
.
have
.
length
(
2
);
});
});
}
if
(
dialect
===
'postgres'
)
{
describe
(
'case insensitive'
,
()
=>
{
it
(
'should work with a case-insensitive regexp where'
,
function
()
{
return
this
.
User
.
create
({
name
:
'Foobar'
}).
then
(()
=>
{
return
this
.
User
.
findOne
({
where
:
{
name
:
{
[
Op
.
iRegexp
]:
'^foo'
}
}
});
}).
then
(
user
=>
{
expect
(
user
).
to
.
be
.
ok
;
it
(
'should work with a case-insensitive regexp where'
,
async
function
()
{
await
this
.
User
.
create
({
name
:
'Foobar'
});
const
user
=
await
this
.
User
.
findOne
({
where
:
{
name
:
{
[
Op
.
iRegexp
]:
'^foo'
}
}
});
expect
(
user
).
to
.
be
.
ok
;
});
it
(
'should work with a case-insensitive not regexp where'
,
function
()
{
return
this
.
User
.
create
({
name
:
'Foobar'
}).
then
(()
=>
{
return
this
.
User
.
findOne
({
where
:
{
name
:
{
[
Op
.
notIRegexp
]:
'^foo'
}
}
});
}).
then
(
user
=>
{
expect
(
user
).
to
.
not
.
be
.
ok
;
it
(
'should work with a case-insensitive not regexp where'
,
async
function
()
{
await
this
.
User
.
create
({
name
:
'Foobar'
});
const
user
=
await
this
.
User
.
findOne
({
where
:
{
name
:
{
[
Op
.
notIRegexp
]:
'^foo'
}
}
});
expect
(
user
).
to
.
not
.
be
.
ok
;
});
it
(
'should properly escape regular expressions'
,
function
()
{
return
this
.
User
.
bulkCreate
([{
name
:
'John'
},
{
name
:
'Bob'
}]).
then
(()
=>
{
return
this
.
User
.
findAll
({
where
:
{
name
:
{
[
Op
.
iRegexp
]:
"Bob'; drop table users --"
}
}
});
}).
then
(()
=>
{
return
this
.
User
.
findAll
({
where
:
{
name
:
{
[
Op
.
notIRegexp
]:
"Bob'; drop table users --"
}
}
});
}).
then
(()
=>
{
return
this
.
User
.
findAll
();
}).
then
(
users
=>
{
expect
(
users
).
length
(
2
);
it
(
'should properly escape regular expressions'
,
async
function
()
{
await
this
.
User
.
bulkCreate
([{
name
:
'John'
},
{
name
:
'Bob'
}]);
await
this
.
User
.
findAll
({
where
:
{
name
:
{
[
Op
.
iRegexp
]:
"Bob'; drop table users --"
}
}
});
await
this
.
User
.
findAll
({
where
:
{
name
:
{
[
Op
.
notIRegexp
]:
"Bob'; drop table users --"
}
}
});
expect
(
await
this
.
User
.
findAll
()).
to
.
have
.
length
(
2
);
});
});
}
...
...
test/integration/pool.test.js
View file @
9512ab9
...
...
@@ -69,175 +69,118 @@ describe(Support.getTestDialectTeaser('Pooling'), () => {
});
describe
(
'network / connection errors'
,
()
=>
{
it
(
'should obtain new connection when old connection is abruptly closed'
,
()
=>
{
const
sequelize
=
Support
.
createSequelizeInstance
(
{
pool
:
{
max
:
1
,
idle
:
5000
it
(
'should obtain new connection when old connection is abruptly closed'
,
async
()
=>
{
function
simulateUnexpectedError
(
connection
)
{
// should never be returned again
if
(
dialect
===
'mssql'
)
{
connection
=
unwrapAndAttachMSSQLUniqueId
(
connection
);
}
});
connection
.
emit
(
'error'
,
{
code
:
'ECONNRESET'
});
}
const
sequelize
=
Support
.
createSequelizeInstance
({
pool
:
{
max
:
1
,
idle
:
5000
}
});
const
cm
=
sequelize
.
connectionManager
;
let
conn
;
return
sequelize
.
sync
()
.
then
(()
=>
cm
.
getConnection
())
.
then
(
connection
=>
{
// Save current connection
conn
=
connection
;
if
(
dialect
===
'mssql'
)
{
connection
=
unwrapAndAttachMSSQLUniqueId
(
connection
);
}
// simulate an unexpected error
// should never be returned again
connection
.
emit
(
'error'
,
{
code
:
'ECONNRESET'
});
})
.
then
(()
=>
{
// Get next available connection
return
cm
.
getConnection
();
})
.
then
(
connection
=>
{
assertNewConnection
(
connection
,
conn
);
await
sequelize
.
sync
();
expect
(
sequelize
.
connectionManager
.
pool
.
size
).
to
.
equal
(
1
);
expect
(
cm
.
validate
(
conn
)).
to
.
be
.
not
.
ok
;
const
firstConnection
=
await
cm
.
getConnection
();
simulateUnexpectedError
(
firstConnection
);
const
secondConnection
=
await
cm
.
getConnection
();
return
cm
.
releaseConnection
(
connection
);
});
assertNewConnection
(
secondConnection
,
firstConnection
);
expect
(
cm
.
pool
.
size
).
to
.
equal
(
1
);
expect
(
cm
.
validate
(
firstConnection
)).
to
.
be
.
not
.
ok
;
await
cm
.
releaseConnection
(
secondConnection
);
});
it
(
'should obtain new connection when released connection dies inside pool'
,
()
=>
{
const
sequelize
=
Support
.
createSequelizeInstance
({
pool
:
{
max
:
1
,
idle
:
5000
it
(
'should obtain new connection when released connection dies inside pool'
,
async
()
=>
{
function
simulateUnexpectedError
(
connection
)
{
// should never be returned again
if
(
dialect
===
'mssql'
)
{
unwrapAndAttachMSSQLUniqueId
(
connection
).
close
();
}
else
if
(
dialect
===
'postgres'
)
{
connection
.
end
();
}
else
{
connection
.
close
();
}
}
);
}
const
sequelize
=
Support
.
createSequelizeInstance
({
pool
:
{
max
:
1
,
idle
:
5000
}
});
const
cm
=
sequelize
.
connectionManager
;
let
oldConnection
;
await
sequelize
.
sync
()
;
return
sequelize
.
sync
()
.
then
(()
=>
cm
.
getConnection
())
.
then
(
connection
=>
{
// Save current connection
oldConnection
=
connection
;
return
cm
.
releaseConnection
(
connection
);
})
.
then
(()
=>
{
let
connection
=
oldConnection
;
if
(
dialect
===
'mssql'
)
{
connection
=
unwrapAndAttachMSSQLUniqueId
(
connection
);
}
// simulate an unexpected error
// should never be returned again
if
(
dialect
.
match
(
/postgres/
))
{
connection
.
end
();
}
else
{
connection
.
close
();
}
})
.
then
(()
=>
{
// Get next available connection
return
cm
.
getConnection
();
})
.
then
(
connection
=>
{
assertNewConnection
(
connection
,
oldConnection
);
const
oldConnection
=
await
cm
.
getConnection
();
await
cm
.
releaseConnection
(
oldConnection
);
simulateUnexpectedError
(
oldConnection
);
const
newConnection
=
await
cm
.
getConnection
();
expect
(
sequelize
.
connectionManager
.
pool
.
size
).
to
.
equal
(
1
);
expect
(
cm
.
validate
(
oldConnection
)).
to
.
be
.
not
.
ok
;
assertNewConnection
(
newConnection
,
oldConnection
);
expect
(
cm
.
pool
.
size
).
to
.
equal
(
1
);
expect
(
cm
.
validate
(
oldConnection
)).
to
.
be
.
not
.
ok
;
return
cm
.
releaseConnection
(
connection
);
});
await
cm
.
releaseConnection
(
newConnection
);
});
});
describe
(
'idle'
,
()
=>
{
it
(
'should maintain connection within idle range'
,
()
=>
{
it
(
'should maintain connection within idle range'
,
async
()
=>
{
const
sequelize
=
Support
.
createSequelizeInstance
({
pool
:
{
max
:
1
,
idle
:
10
}
pool
:
{
max
:
1
,
idle
:
100
}
});
const
cm
=
sequelize
.
connectionManager
;
let
conn
;
await
sequelize
.
sync
()
;
return
sequelize
.
sync
()
.
then
(()
=>
cm
.
getConnection
())
.
then
(
connection
=>
{
// Save current connection
conn
=
connection
;
const
firstConnection
=
await
cm
.
getConnection
();
if
(
dialect
===
'mssql'
)
{
connection
=
unwrapAndAttachMSSQLUniqueId
(
connection
);
}
// TODO - Do we really need this call?
unwrapAndAttachMSSQLUniqueId
(
firstConnection
);
// returning connection back to pool
return
cm
.
releaseConnection
(
conn
);
})
.
then
(()
=>
{
// Get next available connection
return
Sequelize
.
Promise
.
delay
(
9
).
then
(()
=>
cm
.
getConnection
());
})
.
then
(
connection
=>
{
assertSameConnection
(
connection
,
conn
);
expect
(
cm
.
validate
(
conn
)).
to
.
be
.
ok
;
// returning connection back to pool
await
cm
.
releaseConnection
(
firstConnection
);
// Wait a little and then get next available connection
await
Sequelize
.
Promise
.
delay
(
90
);
const
secondConnection
=
await
cm
.
getConnection
();
return
cm
.
releaseConnection
(
connection
);
});
assertSameConnection
(
secondConnection
,
firstConnection
);
expect
(
cm
.
validate
(
firstConnection
)).
to
.
be
.
ok
;
await
cm
.
releaseConnection
(
secondConnection
);
});
it
(
'should get new connection beyond idle range'
,
()
=>
{
it
(
'should get new connection beyond idle range'
,
async
()
=>
{
const
sequelize
=
Support
.
createSequelizeInstance
({
pool
:
{
max
:
1
,
idle
:
100
,
evict
:
10
}
pool
:
{
max
:
1
,
idle
:
100
,
evict
:
10
}
});
const
cm
=
sequelize
.
connectionManager
;
let
conn
;
await
sequelize
.
sync
()
;
return
sequelize
.
sync
()
.
then
(()
=>
cm
.
getConnection
())
.
then
(
connection
=>
{
// Save current connection
conn
=
connection
;
const
firstConnection
=
await
cm
.
getConnection
();
if
(
dialect
===
'mssql'
)
{
connection
=
unwrapAndAttachMSSQLUniqueId
(
connection
);
}
// TODO - Do we really need this call?
unwrapAndAttachMSSQLUniqueId
(
firstConnection
);
// returning connection back to pool
return
cm
.
releaseConnection
(
conn
);
})
.
then
(()
=>
{
// Get next available connection
return
Sequelize
.
Promise
.
delay
(
110
).
then
(()
=>
cm
.
getConnection
());
})
.
then
(
connection
=>
{
assertNewConnection
(
connection
,
conn
);
expect
(
cm
.
validate
(
conn
)).
not
.
to
.
be
.
ok
;
// returning connection back to pool
await
cm
.
releaseConnection
(
firstConnection
);
return
cm
.
releaseConnection
(
connection
);
});
// Wait a little and then get next available connection
await
Sequelize
.
Promise
.
delay
(
110
);
const
secondConnection
=
await
cm
.
getConnection
();
assertNewConnection
(
secondConnection
,
firstConnection
);
expect
(
cm
.
validate
(
firstConnection
)).
not
.
to
.
be
.
ok
;
await
cm
.
releaseConnection
(
secondConnection
);
});
});
describe
(
'acquire'
,
()
=>
{
it
(
'should reject with ConnectionAcquireTimeoutError when unable to acquire connection'
,
function
()
{
it
(
'should reject with ConnectionAcquireTimeoutError when unable to acquire connection'
,
async
function
()
{
this
.
testInstance
=
new
Sequelize
(
'localhost'
,
'ffd'
,
'dfdf'
,
{
dialect
,
databaseVersion
:
'1.2.3'
,
...
...
@@ -249,11 +192,12 @@ describe(Support.getTestDialectTeaser('Pooling'), () => {
this
.
sinon
.
stub
(
this
.
testInstance
.
connectionManager
,
'_connect'
)
.
returns
(
new
Sequelize
.
Promise
(()
=>
{}));
return
expect
(
this
.
testInstance
.
authenticate
())
.
to
.
eventually
.
be
.
rejectedWith
(
Sequelize
.
ConnectionAcquireTimeoutError
);
await
expect
(
this
.
testInstance
.
authenticate
()
).
to
.
eventually
.
be
.
rejectedWith
(
Sequelize
.
ConnectionAcquireTimeoutError
);
});
it
(
'should reject with ConnectionAcquireTimeoutError when unable to acquire connection for transaction'
,
function
()
{
it
(
'should reject with ConnectionAcquireTimeoutError when unable to acquire connection for transaction'
,
async
function
()
{
this
.
testInstance
=
new
Sequelize
(
'localhost'
,
'ffd'
,
'dfdf'
,
{
dialect
,
databaseVersion
:
'1.2.3'
,
...
...
@@ -266,9 +210,11 @@ describe(Support.getTestDialectTeaser('Pooling'), () => {
this
.
sinon
.
stub
(
this
.
testInstance
.
connectionManager
,
'_connect'
)
.
returns
(
new
Sequelize
.
Promise
(()
=>
{}));
return
expect
(
this
.
testInstance
.
transaction
(()
=>
{
return
this
.
testInstance
.
transaction
(()
=>
{});
})).
to
.
eventually
.
be
.
rejectedWith
(
Sequelize
.
ConnectionAcquireTimeoutError
);
await
expect
(
this
.
testInstance
.
transaction
(
async
()
=>
{
await
this
.
testInstance
.
transaction
(()
=>
{});
})
).
to
.
eventually
.
be
.
rejectedWith
(
Sequelize
.
ConnectionAcquireTimeoutError
);
});
});
});
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