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

Commit a1ec8a18 by Andy Edwards Committed by GitHub

test: replace Promise.join calls with Promise.all (#12134)

1 parent 39a1f11f
......@@ -35,23 +35,19 @@ describe(Support.getTestDialectTeaser('BelongsTo'), () => {
Task.User = Task.belongsTo(User, { as: 'user' });
return this.sequelize.sync({ force: true }).then(() => {
return Promise.join(
Task.create({
id: 1,
user: { id: 1 }
}, {
include: [Task.User]
}),
Task.create({
id: 2,
user: { id: 2 }
}, {
include: [Task.User]
}),
Task.create({
id: 3
})
);
return Promise.all([Task.create({
id: 1,
user: { id: 1 }
}, {
include: [Task.User]
}), Task.create({
id: 2,
user: { id: 2 }
}, {
include: [Task.User]
}), Task.create({
id: 3
})]);
}).then(tasks => {
return Task.User.get(tasks).then(result => {
expect(result[tasks[0].id].id).to.equal(tasks[0].user.id);
......@@ -366,10 +362,7 @@ describe(Support.getTestDialectTeaser('BelongsTo'), () => {
Comment.belongsTo(Post, { foreignKey: 'post_id' });
return this.sequelize.sync({ force: true }).then(() => {
return Promise.join(
Post.create(),
Comment.create()
).then(async ([post, comment]) => {
return Promise.all([Post.create(), Comment.create()]).then(async ([post, comment]) => {
expect(comment.get('post_id')).not.to.be.ok;
const setter = await comment.setPost(post, { save: false });
......@@ -1029,9 +1022,10 @@ describe(Support.getTestDialectTeaser('BelongsTo'), () => {
it('should load with an alias', function() {
return this.sequelize.sync({ force: true }).then(() => {
return Promise.join(
return Promise.all([
this.Individual.create({ name: 'Foo Bar' }),
this.Hat.create({ name: 'Baz' }));
this.Hat.create({ name: 'Baz' })
]);
}).then(([individual, hat]) => {
return individual.setPersonwearinghat(hat);
}).then(() => {
......@@ -1058,9 +1052,10 @@ describe(Support.getTestDialectTeaser('BelongsTo'), () => {
it('should load all', function() {
return this.sequelize.sync({ force: true }).then(() => {
return Promise.join(
return Promise.all([
this.Individual.create({ name: 'Foo Bar' }),
this.Hat.create({ name: 'Baz' }));
this.Hat.create({ name: 'Baz' })
]);
}).then(([individual, hat]) => {
return individual.setPersonwearinghat(hat);
}).then(() => {
......
......@@ -33,23 +33,19 @@ describe(Support.getTestDialectTeaser('HasOne'), () => {
Player.User = Player.hasOne(User, { as: 'user' });
return this.sequelize.sync({ force: true }).then(() => {
return Promise.join(
Player.create({
id: 1,
user: {}
}, {
include: [Player.User]
}),
Player.create({
id: 2,
user: {}
}, {
include: [Player.User]
}),
Player.create({
id: 3
})
);
return Promise.all([Player.create({
id: 1,
user: {}
}, {
include: [Player.User]
}), Player.create({
id: 2,
user: {}
}, {
include: [Player.User]
}), Player.create({
id: 3
})]);
}).then(players => {
return Player.User.get(players).then(result => {
expect(result[players[0].id].id).to.equal(players[0].user.id);
......@@ -900,9 +896,10 @@ describe(Support.getTestDialectTeaser('HasOne'), () => {
it('should load with an alias', function() {
return this.sequelize.sync({ force: true }).then(() => {
return Promise.join(
return Promise.all([
this.Individual.create({ name: 'Foo Bar' }),
this.Hat.create({ name: 'Baz' }));
this.Hat.create({ name: 'Baz' })
]);
}).then(([individual, hat]) => {
return individual.setPersonwearinghat(hat);
}).then(() => {
......@@ -929,9 +926,10 @@ describe(Support.getTestDialectTeaser('HasOne'), () => {
it('should load all', function() {
return this.sequelize.sync({ force: true }).then(() => {
return Promise.join(
return Promise.all([
this.Individual.create({ name: 'Foo Bar' }),
this.Hat.create({ name: 'Baz' }));
this.Hat.create({ name: 'Baz' })
]);
}).then(([individual, hat]) => {
return individual.setPersonwearinghat(hat);
}).then(() => {
......
......@@ -168,10 +168,7 @@ describe(Support.getTestDialectTeaser('Include'), () => {
};
return this.sequelize.sync({ force: true }).then(() => {
return Promise.join(
Person.create(),
Company.create()
).then(([person, company]) => {
return Promise.all([Person.create(), Company.create()]).then(([person, company]) => {
return person.setEmployer(company);
});
}).then(() => {
......@@ -214,14 +211,11 @@ describe(Support.getTestDialectTeaser('Include'), () => {
return this.sequelize.sync({ force: true }).then(() => {
return User.create().then(user => {
return Promise.join(
user.createTask({
title: 'trivial'
}),
user.createTask({
title: 'pursuit'
})
);
return Promise.all([user.createTask({
title: 'trivial'
}), user.createTask({
title: 'pursuit'
})]);
}).then(() => {
return User.findOne({
include: [
......@@ -271,10 +265,7 @@ describe(Support.getTestDialectTeaser('Include'), () => {
user: User.create(),
group: Group.create()
}).then(props => {
return Promise.join(
props.task.setUser(props.user),
props.user.setGroup(props.group)
).then(() => props);
return Promise.all([props.task.setUser(props.user), props.user.setGroup(props.group)]).then(() => props);
}).then(props => {
return Task.findOne({
where: {
......
......@@ -32,28 +32,24 @@ describe(Support.getTestDialectTeaser('Include'), () => {
// Sync them
return this.sequelize.sync({ force: true }).then(() => {
// Create an enviroment
return Promise.join(
Project.bulkCreate([
{ id: 1, name: 'No tasks' },
{ id: 2, name: 'No tasks no employees' },
{ id: 3, name: 'No employees' },
{ id: 4, name: 'In progress A' },
{ id: 5, name: 'In progress B' },
{ id: 6, name: 'In progress C' }
]),
Task.bulkCreate([
{ name: 'Important task', fk: 3 },
{ name: 'Important task', fk: 4 },
{ name: 'Important task', fk: 5 },
{ name: 'Important task', fk: 6 }
]),
Employee.bulkCreate([
{ name: 'Jane Doe', fk: 1 },
{ name: 'John Doe', fk: 4 },
{ name: 'Jane John Doe', fk: 5 },
{ name: 'John Jane Doe', fk: 6 }
])
).then(() =>{
return Promise.all([Project.bulkCreate([
{ id: 1, name: 'No tasks' },
{ id: 2, name: 'No tasks no employees' },
{ id: 3, name: 'No employees' },
{ id: 4, name: 'In progress A' },
{ id: 5, name: 'In progress B' },
{ id: 6, name: 'In progress C' }
]), Task.bulkCreate([
{ name: 'Important task', fk: 3 },
{ name: 'Important task', fk: 4 },
{ name: 'Important task', fk: 5 },
{ name: 'Important task', fk: 6 }
]), Employee.bulkCreate([
{ name: 'Jane Doe', fk: 1 },
{ name: 'John Doe', fk: 4 },
{ name: 'Jane John Doe', fk: 5 },
{ name: 'John Jane Doe', fk: 6 }
])]).then(() =>{
//Find all projects with tasks and employees
const availableProjects = 3;
const limit = 2;
......@@ -101,55 +97,49 @@ describe(Support.getTestDialectTeaser('Include'), () => {
return this.sequelize.sync({ force: true }).then(() => {
// Create an enviroment
return Promise.join(
User.bulkCreate([
{ name: 'Youtube' },
{ name: 'Facebook' },
{ name: 'Google' },
{ name: 'Yahoo' },
{ name: '404' }
]),
SomeConnection.bulkCreate([ // Lets count, m: A and u: 1
{ u: 1, m: 'A', fk: 1 }, // 1 // Will be deleted
{ u: 2, m: 'A', fk: 1 },
{ u: 3, m: 'A', fk: 1 },
{ u: 4, m: 'A', fk: 1 },
{ u: 5, m: 'A', fk: 1 },
{ u: 1, m: 'B', fk: 1 },
{ u: 2, m: 'B', fk: 1 },
{ u: 3, m: 'B', fk: 1 },
{ u: 4, m: 'B', fk: 1 },
{ u: 5, m: 'B', fk: 1 },
{ u: 1, m: 'C', fk: 1 },
{ u: 2, m: 'C', fk: 1 },
{ u: 3, m: 'C', fk: 1 },
{ u: 4, m: 'C', fk: 1 },
{ u: 5, m: 'C', fk: 1 },
{ u: 1, m: 'A', fk: 2 }, // 2 // Will be deleted
{ u: 4, m: 'A', fk: 2 },
{ u: 2, m: 'A', fk: 2 },
{ u: 1, m: 'A', fk: 3 }, // 3
{ u: 2, m: 'A', fk: 3 },
{ u: 3, m: 'A', fk: 3 },
{ u: 2, m: 'B', fk: 2 },
{ u: 1, m: 'A', fk: 4 }, // 4
{ u: 4, m: 'A', fk: 2 }
]),
A.bulkCreate([
{ name: 'Just' },
{ name: 'for' },
{ name: 'testing' },
{ name: 'proposes' },
{ name: 'only' }
]),
B.bulkCreate([
{ name: 'this should not' },
{ name: 'be loaded' }
]),
C.bulkCreate([
{ name: 'because we only want A' }
])
).then(() => {
return Promise.all([User.bulkCreate([
{ name: 'Youtube' },
{ name: 'Facebook' },
{ name: 'Google' },
{ name: 'Yahoo' },
{ name: '404' }
]), SomeConnection.bulkCreate([ // Lets count, m: A and u: 1
{ u: 1, m: 'A', fk: 1 }, // 1 // Will be deleted
{ u: 2, m: 'A', fk: 1 },
{ u: 3, m: 'A', fk: 1 },
{ u: 4, m: 'A', fk: 1 },
{ u: 5, m: 'A', fk: 1 },
{ u: 1, m: 'B', fk: 1 },
{ u: 2, m: 'B', fk: 1 },
{ u: 3, m: 'B', fk: 1 },
{ u: 4, m: 'B', fk: 1 },
{ u: 5, m: 'B', fk: 1 },
{ u: 1, m: 'C', fk: 1 },
{ u: 2, m: 'C', fk: 1 },
{ u: 3, m: 'C', fk: 1 },
{ u: 4, m: 'C', fk: 1 },
{ u: 5, m: 'C', fk: 1 },
{ u: 1, m: 'A', fk: 2 }, // 2 // Will be deleted
{ u: 4, m: 'A', fk: 2 },
{ u: 2, m: 'A', fk: 2 },
{ u: 1, m: 'A', fk: 3 }, // 3
{ u: 2, m: 'A', fk: 3 },
{ u: 3, m: 'A', fk: 3 },
{ u: 2, m: 'B', fk: 2 },
{ u: 1, m: 'A', fk: 4 }, // 4
{ u: 4, m: 'A', fk: 2 }
]), A.bulkCreate([
{ name: 'Just' },
{ name: 'for' },
{ name: 'testing' },
{ name: 'proposes' },
{ name: 'only' }
]), B.bulkCreate([
{ name: 'this should not' },
{ name: 'be loaded' }
]), C.bulkCreate([
{ name: 'because we only want A' }
])]).then(() => {
// Delete some of conns to prove the concept
return SomeConnection.destroy({ where: {
m: 'A',
......
......@@ -151,10 +151,7 @@ describe(Support.getTestDialectTeaser('Include'), () => {
return this.sequelize
.sync({ force: true })
.then(() => {
return Promise.join(
A.create({}),
B.create({})
);
return Promise.all([A.create({}), B.create({})]);
})
.then(([a, b]) => {
return a.addB(b, { through: { name: 'Foobar' } });
......@@ -293,39 +290,36 @@ describe(Support.getTestDialectTeaser('Include'), () => {
G.belongsTo(H);
return this.sequelize.sync({ force: true }).then(() => {
return Promise.join(
A.create({}),
(function(singles) {
let promise = Promise.resolve(),
previousInstance,
b;
singles.forEach(model => {
const values = {};
if (model.name === 'g') {
values.name = 'yolo';
}
return Promise.all([A.create({}), (function(singles) {
let promise = Promise.resolve(),
previousInstance,
b;
promise = promise.then(() => {
return model.create(values).then(instance => {
if (previousInstance) {
return previousInstance[`set${_.upperFirst(model.name)}`](instance).then(() => {
previousInstance = instance;
});
}
previousInstance = b = instance;
});
});
});
singles.forEach(model => {
const values = {};
if (model.name === 'g') {
values.name = 'yolo';
}
promise = promise.then(() => {
return b;
return model.create(values).then(instance => {
if (previousInstance) {
return previousInstance[`set${_.upperFirst(model.name)}`](instance).then(() => {
previousInstance = instance;
});
}
previousInstance = b = instance;
});
});
});
promise = promise.then(() => {
return b;
});
return promise;
})([B, C, D, E, F, G, H])
).then(([a, b]) => {
return promise;
})([B, C, D, E, F, G, H])]).then(([a, b]) => {
return a.setB(b);
}).then(() => {
return A.findOne({
......
......@@ -1014,12 +1014,12 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), () => {
return User.findAll();
})
}).then(results => {
return Promise.join(
return Promise.all([
results.users[1].setGroup(results.groups[0]),
results.users[2].setGroup(results.groups[0]),
results.users[3].setGroup(results.groups[1]),
results.users[0].setGroup(results.groups[0])
);
]);
}).then(() => {
return User.findAll({
include: [
......
......@@ -21,26 +21,23 @@ if (current.dialect.supports.groupedLimit) {
User.Tasks = User.hasMany(Task, { as: 'tasks' });
return this.sequelize.sync({ force: true }).then(() => {
return Promise.join(
User.create({
id: 1,
tasks: [
{},
{},
{}
]
}, {
include: [User.Tasks]
}),
User.create({
id: 2,
tasks: [
{}
]
}, {
include: [User.Tasks]
})
).then(() => {
return Promise.all([User.create({
id: 1,
tasks: [
{},
{},
{}
]
}, {
include: [User.Tasks]
}), User.create({
id: 2,
tasks: [
{}
]
}, {
include: [User.Tasks]
})]).then(() => {
return User.findAll({
include: [
{ association: User.Tasks, separate: true }
......@@ -193,29 +190,26 @@ if (current.dialect.supports.groupedLimit) {
User.Tasks = User.hasMany(Task, { as: 'tasks', foreignKey: 'userId' });
return this.sequelize.sync({ force: true }).then(() => {
return Promise.join(
User.create({
id: 1,
tasks: [
{},
{},
{}
]
}, {
include: [User.Tasks]
}),
User.create({
id: 2,
tasks: [
{},
{},
{},
{}
]
}, {
include: [User.Tasks]
})
).then(() => {
return Promise.all([User.create({
id: 1,
tasks: [
{},
{},
{}
]
}, {
include: [User.Tasks]
}), User.create({
id: 2,
tasks: [
{},
{},
{},
{}
]
}, {
include: [User.Tasks]
})]).then(() => {
return User.findAll({
include: [
{ association: User.Tasks, limit: 2 }
......@@ -245,34 +239,31 @@ if (current.dialect.supports.groupedLimit) {
Company.Tasks = Company.hasMany(Task, { as: 'tasks' });
return this.sequelize.sync({ force: true }).then(() => {
return Promise.join(
User.create({
id: 1,
company: {
tasks: [
{},
{},
{}
]
}
}, {
include: [
{ association: User.Company, include: [Company.Tasks] }
return Promise.all([User.create({
id: 1,
company: {
tasks: [
{},
{},
{}
]
}),
User.create({
id: 2,
company: {
tasks: [
{}
]
}
}, {
include: [
{ association: User.Company, include: [Company.Tasks] }
}
}, {
include: [
{ association: User.Company, include: [Company.Tasks] }
]
}), User.create({
id: 2,
company: {
tasks: [
{}
]
})
).then(() => {
}
}, {
include: [
{ association: User.Company, include: [Company.Tasks] }
]
})]).then(() => {
return User.findAll({
include: [
{ association: User.Company, include: [
......@@ -306,28 +297,26 @@ if (current.dialect.supports.groupedLimit) {
Task.Project = Task.belongsTo(Project, { as: 'project' });
return this.sequelize.sync({ force: true }).then(() => {
return Promise.join(
Company.create({
id: 1,
users: [
{
tasks: [
{ project: {} },
{ project: {} },
{ project: {} }
]
}
]
}, {
include: [
{ association: Company.Users, include: [
{ association: User.Tasks, include: [
Task.Project
] }
return Promise.all([Company.create({
id: 1,
users: [
{
tasks: [
{ project: {} },
{ project: {} },
{ project: {} }
]
}
]
}, {
include: [
{ association: Company.Users, include: [
{ association: User.Tasks, include: [
Task.Project
] }
]
})
).then(() => {
] }
]
})]).then(() => {
return Company.findAll({
include: [
{ association: Company.Users, include: [
......@@ -359,47 +348,44 @@ if (current.dialect.supports.groupedLimit) {
Project.Tasks = Project.hasMany(Task, { as: 'tasks' });
return this.sequelize.sync({ force: true }).then(() => {
return Promise.join(
User.create({
id: 1,
projects: [
{
id: 1,
tasks: [
{},
{},
{}
]
},
{
id: 2,
tasks: [
{}
]
}
]
}, {
include: [
{ association: User.Projects, include: [Project.Tasks] }
]
}),
User.create({
id: 2,
projects: [
{
id: 3,
tasks: [
{},
{}
]
}
]
}, {
include: [
{ association: User.Projects, include: [Project.Tasks] }
]
})
).then(() => {
return Promise.all([User.create({
id: 1,
projects: [
{
id: 1,
tasks: [
{},
{},
{}
]
},
{
id: 2,
tasks: [
{}
]
}
]
}, {
include: [
{ association: User.Projects, include: [Project.Tasks] }
]
}), User.create({
id: 2,
projects: [
{
id: 3,
tasks: [
{},
{}
]
}
]
}, {
include: [
{ association: User.Projects, include: [Project.Tasks] }
]
})]).then(() => {
return User.findAll({
include: [
{ association: User.Projects, separate: true, include: [
......@@ -445,29 +431,26 @@ if (current.dialect.supports.groupedLimit) {
return Support.dropTestSchemas(this.sequelize).then(() => {
return this.sequelize.createSchema('archive').then(() => {
return this.sequelize.sync({ force: true }).then(() => {
return Promise.join(
User.create({
id: 1,
tasks: [
{ id: 1, title: 'b' },
{ id: 2, title: 'd' },
{ id: 3, title: 'c' },
{ id: 4, title: 'a' }
]
}, {
include: [User.Tasks]
}),
User.create({
id: 2,
tasks: [
{ id: 5, title: 'a' },
{ id: 6, title: 'c' },
{ id: 7, title: 'b' }
]
}, {
include: [User.Tasks]
})
);
return Promise.all([User.create({
id: 1,
tasks: [
{ id: 1, title: 'b' },
{ id: 2, title: 'd' },
{ id: 3, title: 'c' },
{ id: 4, title: 'a' }
]
}, {
include: [User.Tasks]
}), User.create({
id: 2,
tasks: [
{ id: 5, title: 'a' },
{ id: 6, title: 'c' },
{ id: 7, title: 'b' }
]
}, {
include: [User.Tasks]
})]);
}).then(() => {
return User.findAll({
include: [{ model: Task, limit: 2, as: 'tasks', order: [['id', 'ASC']] }],
......
......@@ -46,10 +46,10 @@ describe(Support.getTestDialectTeaser('Model'), () => {
this.Course.belongsToMany(this.Student, { through: this.Score, foreignKey: 'CourseId' });
return this.sequelize.sync({ force: true }).then(() => {
return Promise.join(
return Promise.all([
this.Student.create({ no: 1, name: 'ryan' }),
this.Course.create({ no: 100, name: 'history' })
).then(([student, course]) => {
]).then(([student, course]) => {
return student.addCourse(course, { through: { score: 98, test_value: 1000 } });
}).then(() => {
expect(callCount).to.equal(1);
......@@ -58,10 +58,10 @@ describe(Support.getTestDialectTeaser('Model'), () => {
});
})
.then(() => {
return Promise.join(
return Promise.all([
this.Student.build({ no: 1 }).getCourses({ where: { no: 100 } }),
this.Score.findOne({ where: { StudentId: 1, CourseId: 100 } })
);
]);
})
.then(([courses, score]) => {
expect(score.test_value).to.equal(1001);
......
......@@ -542,14 +542,11 @@ describe(Support.getTestDialectTeaser('Model'), () => {
it('should work with a belongsTo association getter', function() {
const userId = Math.floor(Math.random() * 100000);
return Promise.join(
this.User.create({
id: userId
}),
this.Task.create({
user_id: userId
})
).then(([user, task]) => {
return Promise.all([this.User.create({
id: userId
}), this.Task.create({
user_id: userId
})]).then(([user, task]) => {
return Promise.all([user, task.getUser()]);
}).then(([userA, userB]) => {
expect(userA.get('id')).to.equal(userB.get('id'));
......
......@@ -432,26 +432,25 @@ describe(Support.getTestDialectTeaser('Model'), () => {
if (current.dialect.supports.transactions) {
it('works with a transaction', function() {
return this.sequelize.transaction().then(transaction => {
return Promise.join(
return Promise.all([
this.User.findOrCreate({ where: { uniqueName: 'winner' }, transaction }),
this.User.findOrCreate({ where: { uniqueName: 'winner' }, transaction }),
(first, second) => {
const firstInstance = first[0],
firstCreated = first[1],
secondInstance = second[0],
secondCreated = second[1];
this.User.findOrCreate({ where: { uniqueName: 'winner' }, transaction })
]).then(([first, second]) => {
const firstInstance = first[0],
firstCreated = first[1],
secondInstance = second[0],
secondCreated = second[1];
// Depending on execution order and MAGIC either the first OR the second call should return true
expect(firstCreated ? !secondCreated : secondCreated).to.be.ok; // XOR
// Depending on execution order and MAGIC either the first OR the second call should return true
expect(firstCreated ? !secondCreated : secondCreated).to.be.ok; // XOR
expect(firstInstance).to.be.ok;
expect(secondInstance).to.be.ok;
expect(firstInstance).to.be.ok;
expect(secondInstance).to.be.ok;
expect(firstInstance.id).to.equal(secondInstance.id);
expect(firstInstance.id).to.equal(secondInstance.id);
return transaction.commit();
}
);
return transaction.commit();
});
});
});
}
......@@ -512,87 +511,82 @@ describe(Support.getTestDialectTeaser('Model'), () => {
username: 'gottlieb'
});
}).then(() => {
return Promise.join(
User.findOrCreate({
where: {
objectId: 'asdasdasd'
},
defaults: {
username: 'gottlieb'
}
}).then(() => {
throw new Error('I should have ben rejected');
}).catch(err => {
expect(err instanceof Sequelize.UniqueConstraintError).to.be.ok;
expect(err.fields).to.be.ok;
}),
User.findOrCreate({
where: {
objectId: 'asdasdasd'
},
defaults: {
username: 'gottlieb'
}
}).then(() => {
throw new Error('I should have ben rejected');
}).catch(err => {
expect(err instanceof Sequelize.UniqueConstraintError).to.be.ok;
expect(err.fields).to.be.ok;
})
);
return Promise.all([User.findOrCreate({
where: {
objectId: 'asdasdasd'
},
defaults: {
username: 'gottlieb'
}
}).then(() => {
throw new Error('I should have ben rejected');
}).catch(err => {
expect(err instanceof Sequelize.UniqueConstraintError).to.be.ok;
expect(err.fields).to.be.ok;
}), User.findOrCreate({
where: {
objectId: 'asdasdasd'
},
defaults: {
username: 'gottlieb'
}
}).then(() => {
throw new Error('I should have ben rejected');
}).catch(err => {
expect(err instanceof Sequelize.UniqueConstraintError).to.be.ok;
expect(err.fields).to.be.ok;
})]);
});
});
// Creating two concurrent transactions and selecting / inserting from the same table throws sqlite off
(dialect !== 'sqlite' ? it : it.skip)('works without a transaction', function() {
return Promise.join(
return Promise.all([
this.User.findOrCreate({ where: { uniqueName: 'winner' } }),
this.User.findOrCreate({ where: { uniqueName: 'winner' } }),
(first, second) => {
const firstInstance = first[0],
firstCreated = first[1],
secondInstance = second[0],
secondCreated = second[1];
this.User.findOrCreate({ where: { uniqueName: 'winner' } })
]).then(([first, second]) => {
const firstInstance = first[0],
firstCreated = first[1],
secondInstance = second[0],
secondCreated = second[1];
// Depending on execution order and MAGIC either the first OR the second call should return true
expect(firstCreated ? !secondCreated : secondCreated).to.be.ok; // XOR
// Depending on execution order and MAGIC either the first OR the second call should return true
expect(firstCreated ? !secondCreated : secondCreated).to.be.ok; // XOR
expect(firstInstance).to.be.ok;
expect(secondInstance).to.be.ok;
expect(firstInstance).to.be.ok;
expect(secondInstance).to.be.ok;
expect(firstInstance.id).to.equal(secondInstance.id);
}
);
expect(firstInstance.id).to.equal(secondInstance.id);
});
});
});
});
describe('findCreateFind', () => {
(dialect !== 'sqlite' ? it : it.skip)('should work with multiple concurrent calls', function() {
return Promise.join(
return Promise.all([
this.User.findOrCreate({ where: { uniqueName: 'winner' } }),
this.User.findOrCreate({ where: { uniqueName: 'winner' } }),
this.User.findOrCreate({ where: { uniqueName: 'winner' } }),
(first, second, third) => {
const firstInstance = first[0],
firstCreated = first[1],
secondInstance = second[0],
secondCreated = second[1],
thirdInstance = third[0],
thirdCreated = third[1];
expect([firstCreated, secondCreated, thirdCreated].filter(value => {
return value;
}).length).to.equal(1);
expect(firstInstance).to.be.ok;
expect(secondInstance).to.be.ok;
expect(thirdInstance).to.be.ok;
expect(firstInstance.id).to.equal(secondInstance.id);
expect(secondInstance.id).to.equal(thirdInstance.id);
}
);
this.User.findOrCreate({ where: { uniqueName: 'winner' } })
]).then(([first, second, third]) => {
const firstInstance = first[0],
firstCreated = first[1],
secondInstance = second[0],
secondCreated = second[1],
thirdInstance = third[0],
thirdCreated = third[1];
expect([firstCreated, secondCreated, thirdCreated].filter(value => {
return value;
}).length).to.equal(1);
expect(firstInstance).to.be.ok;
expect(secondInstance).to.be.ok;
expect(thirdInstance).to.be.ok;
expect(firstInstance.id).to.equal(secondInstance.id);
expect(secondInstance.id).to.equal(thirdInstance.id);
});
});
});
......@@ -656,13 +650,10 @@ describe(Support.getTestDialectTeaser('Model'), () => {
Log.removeAttribute('id');
return this.sequelize.sync({ force: true }).then(() => {
return Promise.join(
Log.create({ level: 'info' }),
Log.bulkCreate([
{ level: 'error' },
{ level: 'debug' }
])
);
return Promise.all([Log.create({ level: 'info' }), Log.bulkCreate([
{ level: 'error' },
{ level: 'debug' }
])]);
}).then(() => {
return Log.findAll();
}).then(logs => {
......
......@@ -51,22 +51,22 @@ if (current.dialect.supports['UNION ALL']) {
this.User.Tasks = this.User.hasMany(this.Task);
return this.sequelize.sync({ force: true }).then(() => {
return Promise.join(
return Promise.all([
this.User.bulkCreate([{ age: -5 }, { age: 45 }, { age: 7 }, { age: -9 }, { age: 8 }, { age: 15 }, { age: -9 }]),
this.Project.bulkCreate([{}, {}]),
this.Task.bulkCreate([{}, {}])
);
]);
})
.then(() => Promise.all([this.User.findAll(), this.Project.findAll(), this.Task.findAll()]))
.then(([users, projects, tasks]) => {
this.projects = projects;
return Promise.join(
return Promise.all([
projects[0].setMembers(users.slice(0, 4)),
projects[1].setMembers(users.slice(2)),
projects[0].setParanoidMembers(users.slice(0, 4)),
projects[1].setParanoidMembers(users.slice(2)),
users[2].setTasks(tasks)
);
]);
});
});
......@@ -218,19 +218,19 @@ if (current.dialect.supports['UNION ALL']) {
this.User.Tasks = this.User.hasMany(this.Task);
return this.sequelize.sync({ force: true }).then(() => {
return Promise.join(
return Promise.all([
this.User.bulkCreate([{}, {}, {}]),
this.Task.bulkCreate([{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }, { id: 5 }, { id: 6 }])
);
]);
})
.then(() => Promise.all([this.User.findAll(), this.Task.findAll()]))
.then(([users, tasks]) => {
this.users = users;
return Promise.join(
return Promise.all([
users[0].setTasks(tasks[0]),
users[1].setTasks(tasks.slice(1, 4)),
users[2].setTasks(tasks.slice(4))
);
]);
});
});
......
......@@ -487,7 +487,7 @@ if (current.dialect.supports.transactions) {
});
});
};
return Promise.join(newTransactionFunc(), newTransactionFunc()).then(() => {
return Promise.all([newTransactionFunc(), newTransactionFunc()]).then(() => {
return User.findAll().then(users => {
expect(users.length).to.equal(2);
});
......@@ -510,7 +510,7 @@ if (current.dialect.supports.transactions) {
});
});
};
return expect(Promise.join(newTransactionFunc(), newTransactionFunc())).to.be.rejectedWith('SQLITE_BUSY: database is locked');
return expect(Promise.all([newTransactionFunc(), newTransactionFunc()])).to.be.rejectedWith('SQLITE_BUSY: database is locked');
});
});
});
......@@ -570,16 +570,14 @@ if (current.dialect.supports.transactions) {
}).then(() => {
return this.sequelize.transaction({ isolationLevel: Transaction.ISOLATION_LEVELS.SERIALIZABLE }).then(transaction => {
return User.findAll( { transaction } )
.then(() => Promise.join(
.then(() => Promise.all([// Update should not succeed before transaction has committed
User.update({ username: 'joe' }, {
where: {
username: 'jan'
}
}).then(() => expect(transactionSpy).to.have.been.called ), // Update should not succeed before transaction has committed
delay(2000)
}).then(() => expect(transactionSpy).to.have.been.called ), delay(2000)
.then(() => transaction.commit())
.then(transactionSpy)
));
.then(transactionSpy)]));
});
});
});
......@@ -612,31 +610,27 @@ if (current.dialect.supports.transactions) {
return this.sequelize.transaction({
isolationLevel: Transaction.ISOLATION_LEVELS.READ_COMMITTED
}).then(t2 => {
return Promise.join(
User.findOne({
where: {
username: 'jan'
},
lock: t2.LOCK.UPDATE,
transaction: t2
}).then(() => {
t2Spy();
return t2.commit().then(() => {
expect(t2Spy).to.have.been.calledAfter(t1Spy); // Find should not succeed before t1 has committed
});
}),
t1Jan.update({
awesome: true
}, {
transaction: t1
}).then(() => {
t1Spy();
return delay(2000).then(() => {
return t1.commit();
});
})
);
return Promise.all([User.findOne({
where: {
username: 'jan'
},
lock: t2.LOCK.UPDATE,
transaction: t2
}).then(() => {
t2Spy();
return t2.commit().then(() => {
expect(t2Spy).to.have.been.calledAfter(t1Spy); // Find should not succeed before t1 has committed
});
}), t1Jan.update({
awesome: true
}, {
transaction: t1
}).then(() => {
t1Spy();
return delay(2000).then(() => {
return t1.commit();
});
})]);
});
});
});
......@@ -695,12 +689,12 @@ if (current.dialect.supports.transactions) {
Task.belongsToMany(User, { through: 'UserTasks' });
return this.sequelize.sync({ force: true }).then(() => {
return Promise.join(
return Promise.all([
User.create({ username: 'John' }),
Task.create({ title: 'Get rich', active: false }),
(john, task1) => {
return john.setTasks([task1]);
})
Task.create({ title: 'Get rich', active: false })
]).then(([john, task1]) => {
return john.setTasks([task1]);
})
.then(() => {
return this.sequelize.transaction(t1 => {
......@@ -738,13 +732,13 @@ if (current.dialect.supports.transactions) {
Task.belongsToMany(User, { through: 'UserTasks' });
return this.sequelize.sync({ force: true }).then(() => {
return Promise.join(
return Promise.all([
User.create({ username: 'John' }),
Task.create({ title: 'Get rich', active: false }),
Task.create({ title: 'Die trying', active: false }),
(john, task1) => {
return john.setTasks([task1]);
})
Task.create({ title: 'Die trying', active: false })
]).then(([john, task1]) => {
return john.setTasks([task1]);
})
.then(() => {
return this.sequelize.transaction(t1 => {
return User.findOne({
......@@ -801,29 +795,26 @@ if (current.dialect.supports.transactions) {
transaction: t1
}).then(t1Jan => {
return this.sequelize.transaction().then(t2 => {
return Promise.join(
User.findOne({
where: {
username: 'jan'
},
lock: t2.LOCK.KEY_SHARE,
transaction: t2
}).then(() => {
t2Spy();
return t2.commit();
}),
t1Jan.update({
awesome: true
}, {
transaction: t1
}).then(() => {
return delay(2000).then(() => {
t1Spy();
expect(t1Spy).to.have.been.calledAfter(t2Spy);
return t1.commit();
});
})
);
return Promise.all([User.findOne({
where: {
username: 'jan'
},
lock: t2.LOCK.KEY_SHARE,
transaction: t2
}).then(() => {
t2Spy();
return t2.commit();
}), t1Jan.update({
awesome: true
}, {
transaction: t1
}).then(() => {
return delay(2000).then(() => {
t1Spy();
expect(t1Spy).to.have.been.calledAfter(t2Spy);
return t1.commit();
});
})]);
});
});
});
......@@ -854,38 +845,34 @@ if (current.dialect.supports.transactions) {
return this.sequelize.transaction({
isolationLevel: Transaction.ISOLATION_LEVELS.READ_COMMITTED
}).then(t2 => {
return Promise.join(
User.findOne({
where: {
username: 'jan'
},
transaction: t2
}).then(t2Jan => {
t2FindSpy();
return t2Jan.update({
awesome: false
}, {
transaction: t2
}).then(() => {
t2UpdateSpy();
return t2.commit().then(() => {
expect(t2FindSpy).to.have.been.calledBefore(t1Spy); // The find call should have returned
expect(t2UpdateSpy).to.have.been.calledAfter(t1Spy); // But the update call should not happen before the first transaction has committed
});
});
}),
t1Jan.update({
awesome: true
return Promise.all([User.findOne({
where: {
username: 'jan'
},
transaction: t2
}).then(t2Jan => {
t2FindSpy();
return t2Jan.update({
awesome: false
}, {
transaction: t1
transaction: t2
}).then(() => {
return delay(2000).then(() => {
t1Spy();
return t1.commit();
t2UpdateSpy();
return t2.commit().then(() => {
expect(t2FindSpy).to.have.been.calledBefore(t1Spy); // The find call should have returned
expect(t2UpdateSpy).to.have.been.calledAfter(t1Spy); // But the update call should not happen before the first transaction has committed
});
})
);
});
}), t1Jan.update({
awesome: true
}, {
transaction: t1
}).then(() => {
return delay(2000).then(() => {
t1Spy();
return t1.commit();
});
})]);
});
});
});
......
......@@ -309,17 +309,13 @@ describe(Support.getTestDialectTeaser('InstanceValidator'), () => {
});
it('should allow decimal as scientific notation', () => {
return Promise.join(
expect(User.create({
number: '2321312301230128391820e219'
})).not.to.be.rejected,
expect(User.create({
number: '2321312301230128391820e+219'
})).not.to.be.rejected,
expect(User.create({
number: '2321312301230128391820f219'
})).to.be.rejected
);
return Promise.all([expect(User.create({
number: '2321312301230128391820e219'
})).not.to.be.rejected, expect(User.create({
number: '2321312301230128391820e+219'
})).not.to.be.rejected, expect(User.create({
number: '2321312301230128391820f219'
})).to.be.rejected]);
});
it('should allow string as a number', () => {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!