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

You need to sign in or sign up before continuing.
Commit c013246b by Andy Edwards Committed by GitHub

test: asyncify integration/associations (#12227)

1 parent c14972b9
......@@ -5,78 +5,68 @@ const chai = require('chai'),
Support = require('../support');
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', {}),
Task = this.sequelize.define('task', {});
User.hasMany(Task, { as: 'assignments', foreignKey: 'userId' });
Task.belongsTo(User, { as: 'owner', foreignKey: 'userId' });
return this.sequelize.sync({ force: true }).then(() => {
return User.create({ id: 1 });
}).then(user => {
expect(user.getAssignments).to.be.ok;
return Task.create({ id: 1, userId: 1 });
}).then(task => {
expect(task.getOwner).to.be.ok;
return Promise.all([
User.findOne({ where: { id: 1 }, include: [{ model: Task, as: 'assignments' }] }),
Task.findOne({ where: { id: 1 }, include: [{ model: User, as: 'owner' }] })
]);
}).then(([user, task]) => {
expect(user.assignments).to.be.ok;
expect(task.owner).to.be.ok;
});
await this.sequelize.sync({ force: true });
const user0 = await User.create({ id: 1 });
expect(user0.getAssignments).to.be.ok;
const task0 = await Task.create({ id: 1, userId: 1 });
expect(task0.getOwner).to.be.ok;
const [user, task] = await Promise.all([
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;
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', {}),
Task = this.sequelize.define('task', {});
User.hasMany(Task, { as: 'ASSIGNMENTS', foreignKey: 'userId' });
Task.belongsTo(User, { as: 'OWNER', foreignKey: 'userId' });
return this.sequelize.sync({ force: true }).then(() => {
return User.create({ id: 1 });
}).then(user => {
expect(user.getASSIGNMENTS).to.be.ok;
return Task.create({ id: 1, userId: 1 });
}).then(task => {
expect(task.getOWNER).to.be.ok;
return Promise.all([
User.findOne({ where: { id: 1 }, include: [{ model: Task, as: 'ASSIGNMENTS' }] }),
Task.findOne({ where: { id: 1 }, include: [{ model: User, as: 'OWNER' }] })
]);
}).then(([user, task]) => {
expect(user.ASSIGNMENTS).to.be.ok;
expect(task.OWNER).to.be.ok;
});
await this.sequelize.sync({ force: true });
const user0 = await User.create({ id: 1 });
expect(user0.getASSIGNMENTS).to.be.ok;
const task0 = await Task.create({ id: 1, userId: 1 });
expect(task0.getOWNER).to.be.ok;
const [user, task] = await Promise.all([
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;
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', {}),
Task = this.sequelize.define('task', {});
User.hasMany(Task, { as: { singular: 'task', plural: 'taskz' } });
return this.sequelize.sync({ force: true }).then(() => {
return User.create({ id: 1 });
}).then(user => {
expect(user.getTaskz).to.be.ok;
expect(user.addTask).to.be.ok;
expect(user.addTaskz).to.be.ok;
}).then(() => {
return User.findOne({ where: { id: 1 }, include: [{ model: Task, as: 'taskz' }] });
}).then(user => {
expect(user.taskz).to.be.ok;
});
await this.sequelize.sync({ force: true });
const user0 = await User.create({ id: 1 });
expect(user0.getTaskz).to.be.ok;
expect(user0.addTask).to.be.ok;
expect(user0.addTaskz).to.be.ok;
const user = await User.findOne({ where: { id: 1 }, include: [{ model: Task, as: 'taskz' }] });
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', {}),
Task = this.sequelize.define('task', {}, {
name: {
......@@ -87,16 +77,12 @@ describe(Support.getTestDialectTeaser('Alias'), () => {
User.hasMany(Task);
return this.sequelize.sync({ force: true }).then(() => {
return User.create({ id: 1 });
}).then(user => {
expect(user.getAssignments).to.be.ok;
expect(user.addAssignment).to.be.ok;
expect(user.addAssignments).to.be.ok;
}).then(() => {
return User.findOne({ where: { id: 1 }, include: [Task] });
}).then(user => {
expect(user.assignments).to.be.ok;
});
await this.sequelize.sync({ force: true });
const user0 = await User.create({ id: 1 });
expect(user0.getAssignments).to.be.ok;
expect(user0.addAssignment).to.be.ok;
expect(user0.addAssignments).to.be.ok;
const user = await User.findOne({ where: { id: 1 }, include: [Task] });
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'),
DataTypes = require('../../../lib/data-types');
describe(Support.getTestDialectTeaser('Self'), () => {
it('supports freezeTableName', function() {
it('supports freezeTableName', async function() {
const Group = this.sequelize.define('Group', {}, {
tableName: 'user_group',
timestamps: false,
......@@ -15,35 +15,35 @@ describe(Support.getTestDialectTeaser('Self'), () => {
});
Group.belongsTo(Group, { as: 'Parent', foreignKey: 'parent_id' });
return Group.sync({ force: true }).then(() => {
return Group.findAll({
include: [{
model: Group,
as: 'Parent'
}]
});
await Group.sync({ force: true });
await Group.findAll({
include: [{
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 });
Person.hasMany(Person, { as: 'Children', foreignKey: 'parent_id' });
expect(Person.rawAttributes.parent_id).to.be.ok;
return this.sequelize.sync({ force: true }).then(() => {
return Promise.all([
Person.create({ name: 'Mary' }),
Person.create({ name: 'John' }),
Person.create({ name: 'Chris' })
]);
}).then(([mary, john, chris]) => {
return mary.setChildren([john, chris]);
});
await this.sequelize.sync({ force: true });
const [mary, john, chris] = await Promise.all([
Person.create({ name: 'Mary' }),
Person.create({ name: 'John' }),
Person.create({ name: '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 });
Person.belongsToMany(Person, { as: 'Parents', through: 'Family', foreignKey: 'ChildId', otherKey: 'PersonId' });
......@@ -58,24 +58,21 @@ describe(Support.getTestDialectTeaser('Self'), () => {
expect(foreignIdentifiers).to.have.members(['PersonId', 'ChildId']);
expect(rawAttributes).to.have.members(['createdAt', 'updatedAt', 'PersonId', 'ChildId']);
return this.sequelize.sync({ force: true }).then(() => {
return Promise.all([
Person.create({ name: 'Mary' }),
Person.create({ name: 'John' }),
Person.create({ name: 'Chris' })
]).then(([mary, john, chris]) => {
return mary.setParents([john]).then(() => {
return chris.addParent(john);
}).then(() => {
return john.getChilds();
}).then(children => {
expect(children.map(v => v.id)).to.have.members([mary.id, chris.id]);
});
});
});
await this.sequelize.sync({ force: true });
const [mary, john, chris] = await Promise.all([
Person.create({ name: 'Mary' }),
Person.create({ name: 'John' }),
Person.create({ name: 'Chris' })
]);
await mary.setParents([john]);
await chris.addParent(john);
const children = await john.getChilds();
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 Family = this.sequelize.define('Family', {
preexisting_child: {
......@@ -101,47 +98,49 @@ describe(Support.getTestDialectTeaser('Self'), () => {
expect(rawAttributes).to.have.members(['preexisting_parent', 'preexisting_child']);
let count = 0;
return this.sequelize.sync({ force: true }).then(() => {
return Promise.all([
Person.create({ name: 'Mary' }),
Person.create({ name: 'John' }),
Person.create({ name: 'Chris' })
]);
}).then(([mary, john, chris]) => {
this.mary = mary;
this.chris = chris;
this.john = john;
return mary.setParents([john], {
logging(sql) {
if (sql.match(/INSERT/)) {
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');
}
await this.sequelize.sync({ force: true });
const [mary, john, chris] = await Promise.all([
Person.create({ name: 'Mary' }),
Person.create({ name: 'John' }),
Person.create({ name: 'Chris' })
]);
this.mary = mary;
this.chris = chris;
this.john = john;
await mary.setParents([john], {
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++;
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(sql).to.have.string('preexisting_child');
expect(sql).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!