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

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;
const task0 = await Task.create({ id: 1, userId: 1 });
return Task.create({ id: 1, userId: 1 }); expect(task0.getOwner).to.be.ok;
}).then(task => {
expect(task.getOwner).to.be.ok; const [user, task] = await Promise.all([
User.findOne({ where: { id: 1 }, include: [{ model: Task, as: 'assignments' }] }),
return Promise.all([ Task.findOne({ where: { id: 1 }, include: [{ model: User, as: 'owner' }] })
User.findOne({ where: { id: 1 }, include: [{ model: Task, as: 'assignments' }] }), ]);
Task.findOne({ where: { id: 1 }, include: [{ model: User, as: 'owner' }] })
]); expect(user.assignments).to.be.ok;
}).then(([user, task]) => { expect(task.owner).to.be.ok;
expect(user.assignments).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;
const task0 = await Task.create({ id: 1, userId: 1 });
return Task.create({ id: 1, userId: 1 }); expect(task0.getOWNER).to.be.ok;
}).then(task => {
expect(task.getOWNER).to.be.ok; const [user, task] = await Promise.all([
User.findOne({ where: { id: 1 }, include: [{ model: Task, as: 'ASSIGNMENTS' }] }),
return Promise.all([ Task.findOne({ where: { id: 1 }, include: [{ model: User, as: 'OWNER' }] })
User.findOne({ where: { id: 1 }, include: [{ model: Task, as: 'ASSIGNMENTS' }] }), ]);
Task.findOne({ where: { id: 1 }, include: [{ model: User, as: 'OWNER' }] })
]); expect(user.ASSIGNMENTS).to.be.ok;
}).then(([user, task]) => { expect(task.OWNER).to.be.ok;
expect(user.ASSIGNMENTS).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(() => { expect(user.taskz).to.be.ok;
return User.findOne({ where: { id: 1 }, include: [{ model: Task, as: 'taskz' }] });
}).then(user => {
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(() => { expect(user.assignments).to.be.ok;
return User.findOne({ where: { id: 1 }, include: [Task] });
}).then(user => {
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('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({
include: [{ await Group.findAll({
model: Group, include: [{
as: 'Parent' model: Group,
}] 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([
Person.create({ name: 'Mary' }), const [mary, john, chris] = await Promise.all([
Person.create({ name: 'John' }), Person.create({ name: 'Mary' }),
Person.create({ name: 'Chris' }) Person.create({ name: 'John' }),
]); 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([
Person.create({ name: 'Mary' }), const [mary, john, chris] = await Promise.all([
Person.create({ name: 'John' }), Person.create({ name: 'Mary' }),
Person.create({ name: 'Chris' }) Person.create({ name: 'John' }),
]).then(([mary, john, chris]) => { Person.create({ name: 'Chris' })
return mary.setParents([john]).then(() => { ]);
return chris.addParent(john);
}).then(() => { await mary.setParents([john]);
return john.getChilds(); await chris.addParent(john);
}).then(children => { const children = await john.getChilds();
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,47 +98,49 @@ describe(Support.getTestDialectTeaser('Self'), () => { ...@@ -101,47 +98,49 @@ 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([
Person.create({ name: 'Mary' }), const [mary, john, chris] = await Promise.all([
Person.create({ name: 'John' }), Person.create({ name: 'Mary' }),
Person.create({ name: 'Chris' }) Person.create({ name: 'John' }),
]); Person.create({ name: 'Chris' })
}).then(([mary, john, chris]) => { ]);
this.mary = mary;
this.chris = chris; this.mary = mary;
this.john = john; this.chris = chris;
return mary.setParents([john], { this.john = john;
logging(sql) {
if (sql.match(/INSERT/)) { await mary.setParents([john], {
count++; logging(sql) {
expect(sql).to.have.string('preexisting_child'); if (sql.match(/INSERT/)) {
expect(sql).to.have.string('preexisting_parent'); count++;
} expect(sql).to.have.string('preexisting_child');
} expect(sql).to.have.string('preexisting_parent');
});
}).then(() => {
return this.mary.addParent(this.chris, {
logging(sql) {
if (sql.match(/INSERT/)) {
count++;
expect(sql).to.have.string('preexisting_child');
expect(sql).to.have.string('preexisting_parent');
}
} }
}); }
}).then(() => { });
return this.john.getChildren({
logging(sql) { await this.mary.addParent(this.chris, {
logging(sql) {
if (sql.match(/INSERT/)) {
count++; count++;
const whereClause = sql.split('FROM')[1]; // look only in the whereClause expect(sql).to.have.string('preexisting_child');
expect(whereClause).to.have.string('preexisting_child'); expect(sql).to.have.string('preexisting_parent');
expect(whereClause).to.have.string('preexisting_parent');
} }
}); }
}).then(children => {
expect(count).to.be.equal(3);
expect(children.map(v => v.id)).to.have.members([this.mary.id]);
}); });
const children = await this.john.getChildren({
logging(sql) {
count++;
const whereClause = sql.split('FROM')[1];
// look only in the whereClause
expect(whereClause).to.have.string('preexisting_child');
expect(whereClause).to.have.string('preexisting_parent');
}
});
expect(count).to.be.equal(3);
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!