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

Commit c013246b by Andy Edwards Committed by GitHub

test: asyncify integration/associations (#12227)

1 parent c14972b9
...@@ -5,78 +5,68 @@ const chai = require('chai'), ...@@ -5,78 +5,68 @@ const chai = require('chai'),
Support = require('../support'); Support = require('../support');
describe(Support.getTestDialectTeaser('Alias'), () => { describe(Support.getTestDialectTeaser('Alias'), () => {
it('should uppercase the first letter in alias getter, but not in eager loading', function() { it('should uppercase the first letter in alias getter, but not in eager loading', async function() {
const User = this.sequelize.define('user', {}), const User = this.sequelize.define('user', {}),
Task = this.sequelize.define('task', {}); Task = this.sequelize.define('task', {});
User.hasMany(Task, { as: 'assignments', foreignKey: 'userId' }); User.hasMany(Task, { as: 'assignments', foreignKey: 'userId' });
Task.belongsTo(User, { as: 'owner', foreignKey: 'userId' }); Task.belongsTo(User, { as: 'owner', foreignKey: 'userId' });
return this.sequelize.sync({ force: true }).then(() => { await this.sequelize.sync({ force: true });
return User.create({ id: 1 }); const user0 = await User.create({ id: 1 });
}).then(user => { expect(user0.getAssignments).to.be.ok;
expect(user.getAssignments).to.be.ok;
return Task.create({ id: 1, userId: 1 }); const task0 = await Task.create({ id: 1, userId: 1 });
}).then(task => { expect(task0.getOwner).to.be.ok;
expect(task.getOwner).to.be.ok;
return Promise.all([ const [user, task] = await Promise.all([
User.findOne({ where: { id: 1 }, include: [{ model: Task, as: 'assignments' }] }), User.findOne({ where: { id: 1 }, include: [{ model: Task, as: 'assignments' }] }),
Task.findOne({ where: { id: 1 }, include: [{ model: User, as: 'owner' }] }) Task.findOne({ where: { id: 1 }, include: [{ model: User, as: 'owner' }] })
]); ]);
}).then(([user, task]) => {
expect(user.assignments).to.be.ok; expect(user.assignments).to.be.ok;
expect(task.owner).to.be.ok; expect(task.owner).to.be.ok;
}); });
});
it('shouldnt touch the passed alias', function() { it('shouldnt touch the passed alias', async function() {
const User = this.sequelize.define('user', {}), const User = this.sequelize.define('user', {}),
Task = this.sequelize.define('task', {}); Task = this.sequelize.define('task', {});
User.hasMany(Task, { as: 'ASSIGNMENTS', foreignKey: 'userId' }); User.hasMany(Task, { as: 'ASSIGNMENTS', foreignKey: 'userId' });
Task.belongsTo(User, { as: 'OWNER', foreignKey: 'userId' }); Task.belongsTo(User, { as: 'OWNER', foreignKey: 'userId' });
return this.sequelize.sync({ force: true }).then(() => { await this.sequelize.sync({ force: true });
return User.create({ id: 1 }); const user0 = await User.create({ id: 1 });
}).then(user => { expect(user0.getASSIGNMENTS).to.be.ok;
expect(user.getASSIGNMENTS).to.be.ok;
return Task.create({ id: 1, userId: 1 }); const task0 = await Task.create({ id: 1, userId: 1 });
}).then(task => { expect(task0.getOWNER).to.be.ok;
expect(task.getOWNER).to.be.ok;
return Promise.all([ const [user, task] = await Promise.all([
User.findOne({ where: { id: 1 }, include: [{ model: Task, as: 'ASSIGNMENTS' }] }), User.findOne({ where: { id: 1 }, include: [{ model: Task, as: 'ASSIGNMENTS' }] }),
Task.findOne({ where: { id: 1 }, include: [{ model: User, as: 'OWNER' }] }) Task.findOne({ where: { id: 1 }, include: [{ model: User, as: 'OWNER' }] })
]); ]);
}).then(([user, task]) => {
expect(user.ASSIGNMENTS).to.be.ok; expect(user.ASSIGNMENTS).to.be.ok;
expect(task.OWNER).to.be.ok; expect(task.OWNER).to.be.ok;
}); });
});
it('should allow me to pass my own plural and singular forms to hasMany', function() { it('should allow me to pass my own plural and singular forms to hasMany', async function() {
const User = this.sequelize.define('user', {}), const User = this.sequelize.define('user', {}),
Task = this.sequelize.define('task', {}); Task = this.sequelize.define('task', {});
User.hasMany(Task, { as: { singular: 'task', plural: 'taskz' } }); User.hasMany(Task, { as: { singular: 'task', plural: 'taskz' } });
return this.sequelize.sync({ force: true }).then(() => { await this.sequelize.sync({ force: true });
return User.create({ id: 1 }); const user0 = await User.create({ id: 1 });
}).then(user => { expect(user0.getTaskz).to.be.ok;
expect(user.getTaskz).to.be.ok; expect(user0.addTask).to.be.ok;
expect(user.addTask).to.be.ok; expect(user0.addTaskz).to.be.ok;
expect(user.addTaskz).to.be.ok; const user = await User.findOne({ where: { id: 1 }, include: [{ model: Task, as: 'taskz' }] });
}).then(() => {
return User.findOne({ where: { id: 1 }, include: [{ model: Task, as: 'taskz' }] });
}).then(user => {
expect(user.taskz).to.be.ok; expect(user.taskz).to.be.ok;
}); });
});
it('should allow me to define plural and singular forms on the model', function() { it('should allow me to define plural and singular forms on the model', async function() {
const User = this.sequelize.define('user', {}), const User = this.sequelize.define('user', {}),
Task = this.sequelize.define('task', {}, { Task = this.sequelize.define('task', {}, {
name: { name: {
...@@ -87,16 +77,12 @@ describe(Support.getTestDialectTeaser('Alias'), () => { ...@@ -87,16 +77,12 @@ describe(Support.getTestDialectTeaser('Alias'), () => {
User.hasMany(Task); User.hasMany(Task);
return this.sequelize.sync({ force: true }).then(() => { await this.sequelize.sync({ force: true });
return User.create({ id: 1 }); const user0 = await User.create({ id: 1 });
}).then(user => { expect(user0.getAssignments).to.be.ok;
expect(user.getAssignments).to.be.ok; expect(user0.addAssignment).to.be.ok;
expect(user.addAssignment).to.be.ok; expect(user0.addAssignments).to.be.ok;
expect(user.addAssignments).to.be.ok; const user = await User.findOne({ where: { id: 1 }, include: [Task] });
}).then(() => {
return User.findOne({ where: { id: 1 }, include: [Task] });
}).then(user => {
expect(user.assignments).to.be.ok; expect(user.assignments).to.be.ok;
}); });
});
}); });
This diff could not be displayed because it is too large.
...@@ -6,7 +6,7 @@ const chai = require('chai'), ...@@ -6,7 +6,7 @@ const chai = require('chai'),
DataTypes = require('../../../lib/data-types'); DataTypes = require('../../../lib/data-types');
describe(Support.getTestDialectTeaser('Multiple Level Filters'), () => { describe(Support.getTestDialectTeaser('Multiple Level Filters'), () => {
it('can filter through belongsTo', function() { it('can filter through belongsTo', async function() {
const User = this.sequelize.define('User', { username: DataTypes.STRING }), const User = this.sequelize.define('User', { username: DataTypes.STRING }),
Task = this.sequelize.define('Task', { title: DataTypes.STRING }), Task = this.sequelize.define('Task', { title: DataTypes.STRING }),
Project = this.sequelize.define('Project', { title: DataTypes.STRING }); Project = this.sequelize.define('Project', { title: DataTypes.STRING });
...@@ -17,20 +17,23 @@ describe(Support.getTestDialectTeaser('Multiple Level Filters'), () => { ...@@ -17,20 +17,23 @@ describe(Support.getTestDialectTeaser('Multiple Level Filters'), () => {
Task.belongsTo(Project); Task.belongsTo(Project);
Project.hasMany(Task); Project.hasMany(Task);
return this.sequelize.sync({ force: true }).then(() => { await this.sequelize.sync({ force: true });
return User.bulkCreate([{
await User.bulkCreate([{
username: 'leia' username: 'leia'
}, { }, {
username: 'vader' username: 'vader'
}]).then(() => { }]);
return Project.bulkCreate([{
await Project.bulkCreate([{
UserId: 1, UserId: 1,
title: 'republic' title: 'republic'
}, { }, {
UserId: 2, UserId: 2,
title: 'empire' title: 'empire'
}]).then(() => { }]);
return Task.bulkCreate([{
await Task.bulkCreate([{
ProjectId: 1, ProjectId: 1,
title: 'fight empire' title: 'fight empire'
}, { }, {
...@@ -42,8 +45,9 @@ describe(Support.getTestDialectTeaser('Multiple Level Filters'), () => { ...@@ -42,8 +45,9 @@ describe(Support.getTestDialectTeaser('Multiple Level Filters'), () => {
}, { }, {
ProjectId: 2, ProjectId: 2,
title: 'rule everything' title: 'rule everything'
}]).then(() => { }]);
return Task.findAll({
const tasks = await Task.findAll({
include: [ include: [
{ {
model: Project, model: Project,
...@@ -53,19 +57,14 @@ describe(Support.getTestDialectTeaser('Multiple Level Filters'), () => { ...@@ -53,19 +57,14 @@ describe(Support.getTestDialectTeaser('Multiple Level Filters'), () => {
required: true required: true
} }
] ]
}).then(tasks => { });
expect(tasks.length).to.be.equal(2); expect(tasks.length).to.be.equal(2);
expect(tasks[0].title).to.be.equal('fight empire'); expect(tasks[0].title).to.be.equal('fight empire');
expect(tasks[1].title).to.be.equal('stablish republic'); expect(tasks[1].title).to.be.equal('stablish republic');
}); });
});
});
});
});
});
it('avoids duplicated tables in query', function() { it('avoids duplicated tables in query', async function() {
const User = this.sequelize.define('User', { username: DataTypes.STRING }), const User = this.sequelize.define('User', { username: DataTypes.STRING }),
Task = this.sequelize.define('Task', { title: DataTypes.STRING }), Task = this.sequelize.define('Task', { title: DataTypes.STRING }),
Project = this.sequelize.define('Project', { title: DataTypes.STRING }); Project = this.sequelize.define('Project', { title: DataTypes.STRING });
...@@ -76,20 +75,23 @@ describe(Support.getTestDialectTeaser('Multiple Level Filters'), () => { ...@@ -76,20 +75,23 @@ describe(Support.getTestDialectTeaser('Multiple Level Filters'), () => {
Task.belongsTo(Project); Task.belongsTo(Project);
Project.hasMany(Task); Project.hasMany(Task);
return this.sequelize.sync({ force: true }).then(() => { await this.sequelize.sync({ force: true });
return User.bulkCreate([{
await User.bulkCreate([{
username: 'leia' username: 'leia'
}, { }, {
username: 'vader' username: 'vader'
}]).then(() => { }]);
return Project.bulkCreate([{
await Project.bulkCreate([{
UserId: 1, UserId: 1,
title: 'republic' title: 'republic'
}, { }, {
UserId: 2, UserId: 2,
title: 'empire' title: 'empire'
}]).then(() => { }]);
return Task.bulkCreate([{
await Task.bulkCreate([{
ProjectId: 1, ProjectId: 1,
title: 'fight empire' title: 'fight empire'
}, { }, {
...@@ -101,8 +103,9 @@ describe(Support.getTestDialectTeaser('Multiple Level Filters'), () => { ...@@ -101,8 +103,9 @@ describe(Support.getTestDialectTeaser('Multiple Level Filters'), () => {
}, { }, {
ProjectId: 2, ProjectId: 2,
title: 'rule everything' title: 'rule everything'
}]).then(() => { }]);
return Task.findAll({
const tasks = await Task.findAll({
include: [ include: [
{ {
model: Project, model: Project,
...@@ -115,18 +118,14 @@ describe(Support.getTestDialectTeaser('Multiple Level Filters'), () => { ...@@ -115,18 +118,14 @@ describe(Support.getTestDialectTeaser('Multiple Level Filters'), () => {
required: true required: true
} }
] ]
}).then(tasks => { });
expect(tasks.length).to.be.equal(2); expect(tasks.length).to.be.equal(2);
expect(tasks[0].title).to.be.equal('fight empire'); expect(tasks[0].title).to.be.equal('fight empire');
expect(tasks[1].title).to.be.equal('stablish republic'); expect(tasks[1].title).to.be.equal('stablish republic');
}); });
});
});
});
});
});
it('can filter through hasMany', function() { it('can filter through hasMany', async function() {
const User = this.sequelize.define('User', { username: DataTypes.STRING }), const User = this.sequelize.define('User', { username: DataTypes.STRING }),
Task = this.sequelize.define('Task', { title: DataTypes.STRING }), Task = this.sequelize.define('Task', { title: DataTypes.STRING }),
Project = this.sequelize.define('Project', { title: DataTypes.STRING }); Project = this.sequelize.define('Project', { title: DataTypes.STRING });
...@@ -137,20 +136,23 @@ describe(Support.getTestDialectTeaser('Multiple Level Filters'), () => { ...@@ -137,20 +136,23 @@ describe(Support.getTestDialectTeaser('Multiple Level Filters'), () => {
Task.belongsTo(Project); Task.belongsTo(Project);
Project.hasMany(Task); Project.hasMany(Task);
return this.sequelize.sync({ force: true }).then(() => { await this.sequelize.sync({ force: true });
return User.bulkCreate([{
await User.bulkCreate([{
username: 'leia' username: 'leia'
}, { }, {
username: 'vader' username: 'vader'
}]).then(() => { }]);
return Project.bulkCreate([{
await Project.bulkCreate([{
UserId: 1, UserId: 1,
title: 'republic' title: 'republic'
}, { }, {
UserId: 2, UserId: 2,
title: 'empire' title: 'empire'
}]).then(() => { }]);
return Task.bulkCreate([{
await Task.bulkCreate([{
ProjectId: 1, ProjectId: 1,
title: 'fight empire' title: 'fight empire'
}, { }, {
...@@ -162,8 +164,9 @@ describe(Support.getTestDialectTeaser('Multiple Level Filters'), () => { ...@@ -162,8 +164,9 @@ describe(Support.getTestDialectTeaser('Multiple Level Filters'), () => {
}, { }, {
ProjectId: 2, ProjectId: 2,
title: 'rule everything' title: 'rule everything'
}]).then(() => { }]);
return User.findAll({
const users = await User.findAll({
include: [ include: [
{ {
model: Project, model: Project,
...@@ -173,56 +176,47 @@ describe(Support.getTestDialectTeaser('Multiple Level Filters'), () => { ...@@ -173,56 +176,47 @@ describe(Support.getTestDialectTeaser('Multiple Level Filters'), () => {
required: true required: true
} }
] ]
}).then(users => { });
expect(users.length).to.be.equal(1); expect(users.length).to.be.equal(1);
expect(users[0].username).to.be.equal('leia'); expect(users[0].username).to.be.equal('leia');
}); });
});
});
});
});
});
it('can filter through hasMany connector', function() { it('can filter through hasMany connector', async function() {
const User = this.sequelize.define('User', { username: DataTypes.STRING }), const User = this.sequelize.define('User', { username: DataTypes.STRING }),
Project = this.sequelize.define('Project', { title: DataTypes.STRING }); Project = this.sequelize.define('Project', { title: DataTypes.STRING });
Project.belongsToMany(User, { through: 'user_project' }); Project.belongsToMany(User, { through: 'user_project' });
User.belongsToMany(Project, { through: 'user_project' }); User.belongsToMany(Project, { through: 'user_project' });
return this.sequelize.sync({ force: true }).then(() => { await this.sequelize.sync({ force: true });
return User.bulkCreate([{
await User.bulkCreate([{
username: 'leia' username: 'leia'
}, { }, {
username: 'vader' username: 'vader'
}]).then(() => { }]);
return Project.bulkCreate([{
await Project.bulkCreate([{
title: 'republic' title: 'republic'
}, { }, {
title: 'empire' title: 'empire'
}]).then(() => { }]);
return User.findByPk(1).then(user => {
return Project.findByPk(1).then(project => { const user = await User.findByPk(1);
return user.setProjects([project]).then(() => { const project = await Project.findByPk(1);
return User.findByPk(2).then(user => { await user.setProjects([project]);
return Project.findByPk(2).then(project => { const user0 = await User.findByPk(2);
return user.setProjects([project]).then(() => { const project0 = await Project.findByPk(2);
return User.findAll({ await user0.setProjects([project0]);
const users = await User.findAll({
include: [ include: [
{ model: Project, where: { title: 'republic' } } { model: Project, where: { title: 'republic' } }
] ]
}).then(users => { });
expect(users.length).to.be.equal(1); expect(users.length).to.be.equal(1);
expect(users[0].username).to.be.equal('leia'); expect(users[0].username).to.be.equal('leia');
}); });
});
});
});
});
});
});
});
});
});
});
}); });
...@@ -6,7 +6,7 @@ const chai = require('chai'), ...@@ -6,7 +6,7 @@ const chai = require('chai'),
DataTypes = require('../../../lib/data-types'); DataTypes = require('../../../lib/data-types');
describe(Support.getTestDialectTeaser('Self'), () => { describe(Support.getTestDialectTeaser('Self'), () => {
it('supports freezeTableName', function() { it('supports freezeTableName', async function() {
const Group = this.sequelize.define('Group', {}, { const Group = this.sequelize.define('Group', {}, {
tableName: 'user_group', tableName: 'user_group',
timestamps: false, timestamps: false,
...@@ -15,35 +15,35 @@ describe(Support.getTestDialectTeaser('Self'), () => { ...@@ -15,35 +15,35 @@ describe(Support.getTestDialectTeaser('Self'), () => {
}); });
Group.belongsTo(Group, { as: 'Parent', foreignKey: 'parent_id' }); Group.belongsTo(Group, { as: 'Parent', foreignKey: 'parent_id' });
return Group.sync({ force: true }).then(() => { await Group.sync({ force: true });
return Group.findAll({
await Group.findAll({
include: [{ include: [{
model: Group, model: Group,
as: 'Parent' as: 'Parent'
}] }]
}); });
}); });
});
it('can handle 1:m associations', function() { it('can handle 1:m associations', async function() {
const Person = this.sequelize.define('Person', { name: DataTypes.STRING }); const Person = this.sequelize.define('Person', { name: DataTypes.STRING });
Person.hasMany(Person, { as: 'Children', foreignKey: 'parent_id' }); Person.hasMany(Person, { as: 'Children', foreignKey: 'parent_id' });
expect(Person.rawAttributes.parent_id).to.be.ok; expect(Person.rawAttributes.parent_id).to.be.ok;
return this.sequelize.sync({ force: true }).then(() => { await this.sequelize.sync({ force: true });
return Promise.all([
const [mary, john, chris] = await Promise.all([
Person.create({ name: 'Mary' }), Person.create({ name: 'Mary' }),
Person.create({ name: 'John' }), Person.create({ name: 'John' }),
Person.create({ name: 'Chris' }) Person.create({ name: 'Chris' })
]); ]);
}).then(([mary, john, chris]) => {
return mary.setChildren([john, chris]); await mary.setChildren([john, chris]);
});
}); });
it('can handle n:m associations', function() { it('can handle n:m associations', async function() {
const Person = this.sequelize.define('Person', { name: DataTypes.STRING }); const Person = this.sequelize.define('Person', { name: DataTypes.STRING });
Person.belongsToMany(Person, { as: 'Parents', through: 'Family', foreignKey: 'ChildId', otherKey: 'PersonId' }); Person.belongsToMany(Person, { as: 'Parents', through: 'Family', foreignKey: 'ChildId', otherKey: 'PersonId' });
...@@ -58,24 +58,21 @@ describe(Support.getTestDialectTeaser('Self'), () => { ...@@ -58,24 +58,21 @@ describe(Support.getTestDialectTeaser('Self'), () => {
expect(foreignIdentifiers).to.have.members(['PersonId', 'ChildId']); expect(foreignIdentifiers).to.have.members(['PersonId', 'ChildId']);
expect(rawAttributes).to.have.members(['createdAt', 'updatedAt', 'PersonId', 'ChildId']); expect(rawAttributes).to.have.members(['createdAt', 'updatedAt', 'PersonId', 'ChildId']);
return this.sequelize.sync({ force: true }).then(() => { await this.sequelize.sync({ force: true });
return Promise.all([
const [mary, john, chris] = await Promise.all([
Person.create({ name: 'Mary' }), Person.create({ name: 'Mary' }),
Person.create({ name: 'John' }), Person.create({ name: 'John' }),
Person.create({ name: 'Chris' }) Person.create({ name: 'Chris' })
]).then(([mary, john, chris]) => { ]);
return mary.setParents([john]).then(() => {
return chris.addParent(john); await mary.setParents([john]);
}).then(() => { await chris.addParent(john);
return john.getChilds(); const children = await john.getChilds();
}).then(children => {
expect(children.map(v => v.id)).to.have.members([mary.id, chris.id]); expect(children.map(v => v.id)).to.have.members([mary.id, chris.id]);
}); });
});
});
});
it('can handle n:m associations with pre-defined through table', function() { it('can handle n:m associations with pre-defined through table', async function() {
const Person = this.sequelize.define('Person', { name: DataTypes.STRING }); const Person = this.sequelize.define('Person', { name: DataTypes.STRING });
const Family = this.sequelize.define('Family', { const Family = this.sequelize.define('Family', {
preexisting_child: { preexisting_child: {
...@@ -101,17 +98,19 @@ describe(Support.getTestDialectTeaser('Self'), () => { ...@@ -101,17 +98,19 @@ describe(Support.getTestDialectTeaser('Self'), () => {
expect(rawAttributes).to.have.members(['preexisting_parent', 'preexisting_child']); expect(rawAttributes).to.have.members(['preexisting_parent', 'preexisting_child']);
let count = 0; let count = 0;
return this.sequelize.sync({ force: true }).then(() => { await this.sequelize.sync({ force: true });
return Promise.all([
const [mary, john, chris] = await Promise.all([
Person.create({ name: 'Mary' }), Person.create({ name: 'Mary' }),
Person.create({ name: 'John' }), Person.create({ name: 'John' }),
Person.create({ name: 'Chris' }) Person.create({ name: 'Chris' })
]); ]);
}).then(([mary, john, chris]) => {
this.mary = mary; this.mary = mary;
this.chris = chris; this.chris = chris;
this.john = john; this.john = john;
return mary.setParents([john], {
await mary.setParents([john], {
logging(sql) { logging(sql) {
if (sql.match(/INSERT/)) { if (sql.match(/INSERT/)) {
count++; count++;
...@@ -120,8 +119,8 @@ describe(Support.getTestDialectTeaser('Self'), () => { ...@@ -120,8 +119,8 @@ describe(Support.getTestDialectTeaser('Self'), () => {
} }
} }
}); });
}).then(() => {
return this.mary.addParent(this.chris, { await this.mary.addParent(this.chris, {
logging(sql) { logging(sql) {
if (sql.match(/INSERT/)) { if (sql.match(/INSERT/)) {
count++; count++;
...@@ -130,18 +129,18 @@ describe(Support.getTestDialectTeaser('Self'), () => { ...@@ -130,18 +129,18 @@ describe(Support.getTestDialectTeaser('Self'), () => {
} }
} }
}); });
}).then(() => {
return this.john.getChildren({ const children = await this.john.getChildren({
logging(sql) { logging(sql) {
count++; count++;
const whereClause = sql.split('FROM')[1]; // look only in the whereClause const whereClause = sql.split('FROM')[1];
// look only in the whereClause
expect(whereClause).to.have.string('preexisting_child'); expect(whereClause).to.have.string('preexisting_child');
expect(whereClause).to.have.string('preexisting_parent'); expect(whereClause).to.have.string('preexisting_parent');
} }
}); });
}).then(children => {
expect(count).to.be.equal(3); expect(count).to.be.equal(3);
expect(children.map(v => v.id)).to.have.members([this.mary.id]); expect(children.map(v => v.id)).to.have.members([this.mary.id]);
}); });
});
}); });
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!