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

Commit 9d3be832 by Pedro Augusto de Paula Barbosa Committed by Sushant

refactor(*): use async/await (#11864)

1 parent 6837a174
......@@ -1437,19 +1437,15 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), () => {
});
});
it('should count all associations', function() {
return expect(this.user.countTasks({})).to.eventually.equal(2);
it('should count all associations', async function() {
expect(await this.user.countTasks({})).to.equal(2);
});
it('should count filtered associations', function() {
return expect(this.user.countTasks({
where: {
active: true
}
})).to.eventually.equal(1);
it('should count filtered associations', async function() {
expect(await this.user.countTasks({ where: { active: true } })).to.equal(1);
});
it('should count scoped associations', function() {
it('should count scoped associations', async function() {
this.User.belongsToMany(this.Task, {
as: 'activeTasks',
through: this.UserTask,
......@@ -1458,12 +1454,10 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), () => {
}
});
return expect(this.user.countActiveTasks({})).to.eventually.equal(1);
expect(await this.user.countActiveTasks({})).to.equal(1);
});
it('should count scoped through associations', function() {
const user = this.user;
it('should count scoped through associations', async function() {
this.User.belongsToMany(this.Task, {
as: 'startedTasks',
through: {
......@@ -1474,20 +1468,13 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), () => {
}
});
return Promise.join(
this.Task.create().then(task => {
return user.addTask(task, {
through: { started: true }
});
}),
this.Task.create().then(task => {
return user.addTask(task, {
for (let i = 0; i < 2; i++) {
await this.user.addTask(await this.Task.create(), {
through: { started: true }
});
})
).then(() => {
return expect(user.countStartedTasks({})).to.eventually.equal(2);
});
}
expect(await this.user.countStartedTasks({})).to.equal(2);
});
});
......
......@@ -381,27 +381,19 @@ describe(Support.getTestDialectTeaser('BelongsTo'), () => {
});
});
it('supports setting same association twice', function() {
const Home = this.sequelize.define('home', {}),
User = this.sequelize.define('user');
it('supports setting same association twice', async function() {
const Home = this.sequelize.define('home', {});
const User = this.sequelize.define('user');
Home.belongsTo(User);
const ctx = {};
return this.sequelize.sync({ force: true }).then(() => {
return Promise.all([
await this.sequelize.sync({ force: true });
const [home, user] = await Promise.all([
Home.create(),
User.create()
]);
}).then(([home, user]) => {
ctx.home = home;
ctx.user = user;
return home.setUser(user);
}).then(() => {
return ctx.home.setUser(ctx.user);
}).then(() => {
return expect(ctx.home.getUser()).to.eventually.have.property('id', ctx.user.get('id'));
});
await home.setUser(user);
expect(await home.getUser()).to.have.property('id', user.id);
});
});
......
......@@ -19,46 +19,37 @@ if (current.dialect.supports.transactions) {
delete Sequelize._cls;
});
beforeEach(function() {
return Support.prepareTransactionTest(this.sequelize).then(sequelize => {
this.sequelize = sequelize;
beforeEach(async function() {
this.sequelize = await Support.prepareTransactionTest(this.sequelize);
this.ns = cls.getNamespace('sequelize');
this.User = this.sequelize.define('user', {
name: Sequelize.STRING
});
return this.sequelize.sync({ force: true });
});
await this.sequelize.sync({ force: true });
});
describe('context', () => {
it('does not use continuation storage on manually managed transactions', function() {
return Sequelize._clsRun(() => {
return this.sequelize.transaction().then(transaction => {
return Sequelize._clsRun(async () => {
const transaction = await this.sequelize.transaction();
expect(this.ns.get('transaction')).not.to.be.ok;
return transaction.rollback();
});
await transaction.rollback();
});
});
it('supports several concurrent transactions', function() {
it('supports several concurrent transactions', async function() {
let t1id, t2id;
return Promise.join(
this.sequelize.transaction(() => {
await Promise.all([
this.sequelize.transaction(async () => {
t1id = this.ns.get('transaction').id;
return Promise.resolve();
}),
this.sequelize.transaction(() => {
this.sequelize.transaction(async () => {
t2id = this.ns.get('transaction').id;
return Promise.resolve();
}),
() => {
})
]);
expect(t1id).to.be.ok;
expect(t2id).to.be.ok;
expect(t1id).not.to.equal(t2id);
}
);
});
it('supports nested promise chains', function() {
......@@ -145,8 +136,8 @@ if (current.dialect.supports.transactions) {
it('automagically uses the transaction in all calls with async/await', function() {
return this.sequelize.transaction(async () => {
await this.User.create({ name: 'bob' });
await expect(this.User.findAll({ transaction: null })).to.eventually.have.length(0);
await expect(this.User.findAll({})).to.eventually.have.length(1);
expect(await this.User.findAll({ transaction: null })).to.have.length(0);
expect(await this.User.findAll({})).to.have.length(1);
});
});
});
......
This diff could not be displayed because it is too large.
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!