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

Commit 35af6838 by Andy Edwards Committed by GitHub

test(integration/model): asyncify (#12287)

1 parent 796b6b7d
Showing with 587 additions and 689 deletions
......@@ -8,7 +8,7 @@ const chai = require('chai'),
describe(Support.getTestDialectTeaser('Model'), () => {
describe('attributes', () => {
describe('set', () => {
it('should only be called once when used on a join model called with an association getter', function() {
it('should only be called once when used on a join model called with an association getter', async function() {
let callCount = 0;
this.Student = this.sequelize.define('student', {
......@@ -44,33 +44,29 @@ describe(Support.getTestDialectTeaser('Model'), () => {
this.Student.belongsToMany(this.Course, { through: this.Score, foreignKey: 'StudentId' });
this.Course.belongsToMany(this.Student, { through: this.Score, foreignKey: 'CourseId' });
return this.sequelize.sync({ force: true }).then(() => {
return Promise.all([
await this.sequelize.sync({ force: true });
const [student, course] = await Promise.all([
this.Student.create({ no: 1, name: 'ryan' }),
this.Course.create({ no: 100, name: 'history' })
]).then(([student, course]) => {
return student.addCourse(course, { through: { score: 98, test_value: 1000 } });
}).then(() => {
]);
await student.addCourse(course, { through: { score: 98, test_value: 1000 } });
expect(callCount).to.equal(1);
return this.Score.findOne({ where: { StudentId: 1, CourseId: 100 } }).then(score => {
expect(score.test_value).to.equal(1001);
});
})
.then(() => {
return Promise.all([
const score0 = await this.Score.findOne({ where: { StudentId: 1, CourseId: 100 } });
expect(score0.test_value).to.equal(1001);
const [courses, score] = await 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);
expect(courses[0].score.toJSON().test_value).to.equal(1001);
expect(callCount).to.equal(1);
});
});
});
it('allows for an attribute to be called "toString"', function() {
it('allows for an attribute to be called "toString"', async function() {
const Person = this.sequelize.define('person', {
name: Sequelize.STRING,
nick: Sequelize.STRING
......@@ -78,9 +74,10 @@ describe(Support.getTestDialectTeaser('Model'), () => {
timestamps: false
});
return this.sequelize.sync({ force: true })
.then(() => Person.create({ name: 'Jozef', nick: 'Joe' }))
.then(() => Person.findOne({
await this.sequelize.sync({ force: true });
await Person.create({ name: 'Jozef', nick: 'Joe' });
const person = await Person.findOne({
attributes: [
'nick',
['name', 'toString']
......@@ -88,14 +85,13 @@ describe(Support.getTestDialectTeaser('Model'), () => {
where: {
name: 'Jozef'
}
}))
.then(person => {
});
expect(person.dataValues['toString']).to.equal('Jozef');
expect(person.get('toString')).to.equal('Jozef');
});
});
it('allows for an attribute to be called "toString" with associations', function() {
it('allows for an attribute to be called "toString" with associations', async function() {
const Person = this.sequelize.define('person', {
name: Sequelize.STRING,
nick: Sequelize.STRING
......@@ -107,10 +103,11 @@ describe(Support.getTestDialectTeaser('Model'), () => {
Person.hasMany(Computer);
return this.sequelize.sync({ force: true })
.then(() => Person.create({ name: 'Jozef', nick: 'Joe' }))
.then(person => person.createComputer({ hostname: 'laptop' }))
.then(() => Person.findAll({
await this.sequelize.sync({ force: true });
const person = await Person.create({ name: 'Jozef', nick: 'Joe' });
await person.createComputer({ hostname: 'laptop' });
const result = await Person.findAll({
attributes: [
'nick',
['name', 'toString']
......@@ -121,28 +118,25 @@ describe(Support.getTestDialectTeaser('Model'), () => {
where: {
name: 'Jozef'
}
}))
.then(result => {
});
expect(result.length).to.equal(1);
expect(result[0].dataValues['toString']).to.equal('Jozef');
expect(result[0].get('toString')).to.equal('Jozef');
expect(result[0].get('computers')[0].hostname).to.equal('laptop');
});
});
});
describe('quote', () => {
it('allows for an attribute with dots', function() {
it('allows for an attribute with dots', async function() {
const User = this.sequelize.define('user', {
'foo.bar.baz': Sequelize.TEXT
});
return this.sequelize.sync({ force: true })
.then(() => User.findAll())
.then(result => {
await this.sequelize.sync({ force: true });
const result = await User.findAll();
expect(result.length).to.equal(0);
});
});
});
});
});
......@@ -10,7 +10,7 @@ describe(Support.getTestDialectTeaser('Model'), () => {
describe('attributes', () => {
describe('types', () => {
describe('VIRTUAL', () => {
beforeEach(function() {
beforeEach(async function() {
this.User = this.sequelize.define('user', {
storage: Sequelize.STRING,
field1: {
......@@ -48,7 +48,7 @@ describe(Support.getTestDialectTeaser('Model'), () => {
expect(sql).to.not.include('field2');
};
return this.sequelize.sync({ force: true });
await this.sequelize.sync({ force: true });
});
it('should not be ignored in dataValues get', function() {
......@@ -60,14 +60,13 @@ describe(Support.getTestDialectTeaser('Model'), () => {
expect(user.get()).to.deep.equal({ storage: 'field1_value', field1: 'field1_value', virtualWithDefault: 'cake', field2: 42, id: null });
});
it('should be ignored in table creation', function() {
return this.sequelize.getQueryInterface().describeTable(this.User.tableName).then(fields => {
it('should be ignored in table creation', async function() {
const fields = await this.sequelize.getQueryInterface().describeTable(this.User.tableName);
expect(Object.keys(fields).length).to.equal(2);
});
});
it('should be ignored in find, findAll and includes', function() {
return Promise.all([
it('should be ignored in find, findAll and includes', async function() {
await Promise.all([
this.User.findOne({
logging: this.sqlAssert
}),
......@@ -89,7 +88,7 @@ describe(Support.getTestDialectTeaser('Model'), () => {
]);
});
it('should allow me to store selected values', function() {
it('should allow me to store selected values', async function() {
const Post = this.sequelize.define('Post', {
text: Sequelize.TEXT,
someBoolean: {
......@@ -97,94 +96,91 @@ describe(Support.getTestDialectTeaser('Model'), () => {
}
});
return this.sequelize.sync({ force: true }).then(() => {
return Post.bulkCreate([{ text: 'text1' }, { text: 'text2' }]);
}).then(() => {
await this.sequelize.sync({ force: true });
await Post.bulkCreate([{ text: 'text1' }, { text: 'text2' }]);
let boolQuery = 'EXISTS(SELECT 1) AS "someBoolean"';
if (dialect === 'mssql') {
boolQuery = 'CAST(CASE WHEN EXISTS(SELECT 1) THEN 1 ELSE 0 END AS BIT) AS "someBoolean"';
}
return Post.findOne({ attributes: ['id', 'text', Sequelize.literal(boolQuery)] });
}).then(post => {
const post = await Post.findOne({ attributes: ['id', 'text', Sequelize.literal(boolQuery)] });
expect(post.get('someBoolean')).to.be.ok;
expect(post.get().someBoolean).to.be.ok;
});
});
it('should be ignored in create and update', function() {
return this.User.create({
it('should be ignored in create and update', async function() {
const user0 = await this.User.create({
field1: 'something'
}).then(user => {
});
// We already verified that the virtual is not added to the table definition,
// so if this succeeds, were good
expect(user.virtualWithDefault).to.equal('cake');
expect(user.storage).to.equal('something');
return user.update({
expect(user0.virtualWithDefault).to.equal('cake');
expect(user0.storage).to.equal('something');
const user = await user0.update({
field1: 'something else'
}, {
fields: ['storage']
});
}).then(user => {
expect(user.virtualWithDefault).to.equal('cake');
expect(user.storage).to.equal('something else');
});
});
it('should be ignored in bulkCreate and and bulkUpdate', function() {
return this.User.bulkCreate([{
it('should be ignored in bulkCreate and and bulkUpdate', async function() {
await this.User.bulkCreate([{
field1: 'something'
}], {
logging: this.sqlAssert
}).then(() => {
return this.User.findAll();
}).then(users => {
expect(users[0].storage).to.equal('something');
});
const users = await this.User.findAll();
expect(users[0].storage).to.equal('something');
});
it('should be able to exclude with attributes', function() {
return this.User.bulkCreate([{
it('should be able to exclude with attributes', async function() {
await this.User.bulkCreate([{
field1: 'something'
}], {
logging: this.sqlAssert
}).then(() => {
return this.User.findAll({
});
const users0 = await this.User.findAll({
logging: this.sqlAssert
});
}).then(users => {
const user = users[0].get();
expect(user.storage).to.equal('something');
expect(user).to.include.all.keys(['field1', 'field2']);
const user0 = users0[0].get();
expect(user0.storage).to.equal('something');
expect(user0).to.include.all.keys(['field1', 'field2']);
return this.User.findAll({
const users = await this.User.findAll({
attributes: {
exclude: ['field1']
},
logging: this.sqlAssert
});
}).then(users => {
const user = users[0].get();
expect(user.storage).to.equal('something');
expect(user).not.to.include.all.keys(['field1']);
expect(user).to.include.all.keys(['field2']);
});
});
it('should be able to include model with virtual attributes', function() {
return this.User.create({}).then(user => {
return user.createTask();
}).then(() => {
return this.Task.findAll({
it('should be able to include model with virtual attributes', async function() {
const user0 = await this.User.create({});
await user0.createTask();
const tasks = await this.Task.findAll({
include: [{
attributes: ['field2', 'id'],
model: this.User
}]
});
}).then(tasks => {
const user = tasks[0].user.get();
expect(user.field2).to.equal(42);
......@@ -192,5 +188,4 @@ describe(Support.getTestDialectTeaser('Model'), () => {
});
});
});
});
});
......@@ -7,7 +7,7 @@ const chai = require('chai'),
describe(Support.getTestDialectTeaser('Model'), () => {
describe('count', () => {
beforeEach(function() {
beforeEach(async function() {
this.User = this.sequelize.define('User', {
username: DataTypes.STRING,
age: DataTypes.INTEGER
......@@ -19,112 +19,118 @@ describe(Support.getTestDialectTeaser('Model'), () => {
this.User.hasMany(this.Project);
this.Project.belongsTo(this.User);
return this.sequelize.sync({ force: true });
await this.sequelize.sync({ force: true });
});
it('should count rows', function() {
return this.User.bulkCreate([
it('should count rows', async function() {
await this.User.bulkCreate([
{ username: 'foo' },
{ username: 'bar' }
]).then(() => {
return expect(this.User.count()).to.eventually.equal(2);
});
]);
await expect(this.User.count()).to.eventually.equal(2);
});
it('should support include', function() {
return this.User.bulkCreate([
it('should support include', async function() {
await this.User.bulkCreate([
{ username: 'foo' },
{ username: 'bar' }
]).then(() => this.User.findOne())
.then(user => user.createProject({ name: 'project1' }))
.then(() => {
return expect(this.User.count({
]);
const user = await this.User.findOne();
await user.createProject({ name: 'project1' });
await expect(this.User.count({
include: [{
model: this.Project,
where: { name: 'project1' }
}]
})).to.eventually.equal(1);
});
});
it('should count groups correctly and return attributes', function() {
return this.User.bulkCreate([
it('should count groups correctly and return attributes', async function() {
await this.User.bulkCreate([
{ username: 'foo' },
{ username: 'bar' },
{
username: 'valak',
createdAt: new Date().setFullYear(2015)
}
]).then(() => this.User.count({
]);
const users = await this.User.count({
attributes: ['createdAt'],
group: ['createdAt']
})).then(users => {
});
expect(users.length).to.be.eql(2);
expect(users[0].createdAt).to.exist;
expect(users[1].createdAt).to.exist;
});
});
it('should not return NaN', function() {
return this.User.bulkCreate([
it('should not return NaN', async function() {
await this.User.bulkCreate([
{ username: 'valak', age: 10 },
{ username: 'conjuring', age: 20 },
{ username: 'scary', age: 10 }
]).then(() => this.User.count({
]);
const result = await this.User.count({
where: { age: 10 },
group: ['age'],
order: ['age']
})).then(result => {
});
// TODO: `parseInt` should not be needed, see #10533
expect(parseInt(result[0].count, 10)).to.be.eql(2);
return this.User.count({
const count0 = await this.User.count({
where: { username: 'fire' }
});
}).then(count => {
expect(count).to.be.eql(0);
return this.User.count({
expect(count0).to.be.eql(0);
const count = await this.User.count({
where: { username: 'fire' },
group: 'age'
});
}).then(count => {
expect(count).to.be.eql([]);
});
});
it('should be able to specify column for COUNT()', function() {
return this.User.bulkCreate([
it('should be able to specify column for COUNT()', async function() {
await this.User.bulkCreate([
{ username: 'ember', age: 10 },
{ username: 'angular', age: 20 },
{ username: 'mithril', age: 10 }
]).then(() => this.User.count({ col: 'username' }))
.then(count => {
expect(count).to.be.eql(3);
return this.User.count({
]);
const count0 = await this.User.count({ col: 'username' });
expect(count0).to.be.eql(3);
const count = await this.User.count({
col: 'age',
distinct: true
});
})
.then(count => {
expect(count).to.be.eql(2);
});
});
it('should be able to specify NO column for COUNT() with DISTINCT', function() {
return this.User.bulkCreate([
it('should be able to specify NO column for COUNT() with DISTINCT', async function() {
await this.User.bulkCreate([
{ username: 'ember', age: 10 },
{ username: 'angular', age: 20 },
{ username: 'mithril', age: 10 }
]).then(() => {
return this.User.count({
]);
const count = await this.User.count({
distinct: true
});
})
.then(count => {
expect(count).to.be.eql(3);
});
});
it('should be able to use where clause on included models', function() {
it('should be able to use where clause on included models', async function() {
const countOptions = {
col: 'username',
include: [this.Project],
......@@ -132,51 +138,53 @@ describe(Support.getTestDialectTeaser('Model'), () => {
'$Projects.name$': 'project1'
}
};
return this.User.bulkCreate([
await this.User.bulkCreate([
{ username: 'foo' },
{ username: 'bar' }
]).then(() => this.User.findOne())
.then(user => user.createProject({ name: 'project1' }))
.then(() => {
return this.User.count(countOptions).then(count => {
expect(count).to.be.eql(1);
]);
const user = await this.User.findOne();
await user.createProject({ name: 'project1' });
const count0 = await this.User.count(countOptions);
expect(count0).to.be.eql(1);
countOptions.where['$Projects.name$'] = 'project2';
return this.User.count(countOptions);
});
})
.then(count => {
const count = await this.User.count(countOptions);
expect(count).to.be.eql(0);
});
});
it('should be able to specify column for COUNT() with includes', function() {
return this.User.bulkCreate([
it('should be able to specify column for COUNT() with includes', async function() {
await this.User.bulkCreate([
{ username: 'ember', age: 10 },
{ username: 'angular', age: 20 },
{ username: 'mithril', age: 10 }
]).then(() => this.User.count({
]);
const count0 = await this.User.count({
col: 'username',
distinct: true,
include: [this.Project]
})).then(count => {
expect(count).to.be.eql(3);
return this.User.count({
});
expect(count0).to.be.eql(3);
const count = await this.User.count({
col: 'age',
distinct: true,
include: [this.Project]
});
}).then(count => {
expect(count).to.be.eql(2);
});
});
it('should work correctly with include and whichever raw option', function() {
it('should work correctly with include and whichever raw option', async function() {
const Post = this.sequelize.define('Post', {});
this.User.hasMany(Post);
return Post.sync({ force: true })
.then(() => Promise.all([this.User.create({}), Post.create({})]))
.then(([user, post]) => user.addPost(post))
.then(() => Promise.all([
await Post.sync({ force: true });
const [user, post] = await Promise.all([this.User.create({}), Post.create({})]);
await user.addPost(post);
const counts = await Promise.all([
this.User.count(),
this.User.count({ raw: undefined }),
this.User.count({ raw: false }),
......@@ -185,11 +193,10 @@ describe(Support.getTestDialectTeaser('Model'), () => {
this.User.count({ include: Post, raw: undefined }),
this.User.count({ include: Post, raw: false }),
this.User.count({ include: Post, raw: true })
]))
.then(counts => {
]);
expect(counts).to.deep.equal([1, 1, 1, 1, 1, 1, 1, 1]);
});
});
});
});
......@@ -10,8 +10,7 @@ const chai = require('chai'),
describe(Support.getTestDialectTeaser('Model'), () => {
describe('findAll', () => {
describe('group', () => {
it('should correctly group with attributes, #3009', () => {
it('should correctly group with attributes, #3009', async () => {
const Post = current.define('Post', {
id: { type: DataTypes.INTEGER, autoIncrement: true, primaryKey: true },
name: { type: DataTypes.STRING, allowNull: false }
......@@ -24,22 +23,23 @@ describe(Support.getTestDialectTeaser('Model'), () => {
Post.hasMany(Comment);
return current.sync({ force: true }).then(() => {
await current.sync({ force: true });
// Create an enviroment
return Post.bulkCreate([
await Post.bulkCreate([
{ name: 'post-1' },
{ name: 'post-2' }
]);
}).then(() => {
return Comment.bulkCreate([
await Comment.bulkCreate([
{ text: 'Market', PostId: 1 },
{ text: 'Text', PostId: 2 },
{ text: 'Abc', PostId: 2 },
{ text: 'Semaphor', PostId: 1 },
{ text: 'Text', PostId: 1 }
]);
}).then(() => {
return Post.findAll({
const posts = await Post.findAll({
attributes: [[Sequelize.fn('COUNT', Sequelize.col('Comments.id')), 'comment_count']],
include: [
{ model: Comment, attributes: [] }
......@@ -49,13 +49,12 @@ describe(Support.getTestDialectTeaser('Model'), () => {
['id']
]
});
}).then(posts => {
expect(parseInt(posts[0].get('comment_count'), 10)).to.be.equal(3);
expect(parseInt(posts[1].get('comment_count'), 10)).to.be.equal(2);
});
});
it('should not add primary key when grouping using a belongsTo association', () => {
it('should not add primary key when grouping using a belongsTo association', async () => {
const Post = current.define('Post', {
id: { type: DataTypes.INTEGER, autoIncrement: true, primaryKey: true },
name: { type: DataTypes.STRING, allowNull: false }
......@@ -69,21 +68,22 @@ describe(Support.getTestDialectTeaser('Model'), () => {
Post.hasMany(Comment);
Comment.belongsTo(Post);
return current.sync({ force: true }).then(() => {
return Post.bulkCreate([
await current.sync({ force: true });
await Post.bulkCreate([
{ name: 'post-1' },
{ name: 'post-2' }
]);
}).then(() => {
return Comment.bulkCreate([
await Comment.bulkCreate([
{ text: 'Market', PostId: 1 },
{ text: 'Text', PostId: 2 },
{ text: 'Abc', PostId: 2 },
{ text: 'Semaphor', PostId: 1 },
{ text: 'Text', PostId: 1 }
]);
}).then(() => {
return Comment.findAll({
const posts = await Comment.findAll({
attributes: ['PostId', [Sequelize.fn('COUNT', Sequelize.col('Comment.id')), 'comment_count']],
include: [
{ model: Post, attributes: [] }
......@@ -93,7 +93,7 @@ describe(Support.getTestDialectTeaser('Model'), () => {
['PostId']
]
});
}).then(posts => {
expect(posts[0].get().hasOwnProperty('id')).to.equal(false);
expect(posts[1].get().hasOwnProperty('id')).to.equal(false);
expect(parseInt(posts[0].get('comment_count'), 10)).to.be.equal(3);
......@@ -101,5 +101,4 @@ describe(Support.getTestDialectTeaser('Model'), () => {
});
});
});
});
});
......@@ -25,7 +25,7 @@ if (current.dialect.supports['UNION ALL']) {
this.clock.restore();
});
beforeEach(function() {
beforeEach(async function() {
this.User = this.sequelize.define('user', {
age: Sequelize.INTEGER
});
......@@ -49,17 +49,18 @@ if (current.dialect.supports['UNION ALL']) {
this.User.Tasks = this.User.hasMany(this.Task);
return this.sequelize.sync({ force: true }).then(() => {
return Promise.all([
await this.sequelize.sync({ force: true });
await 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]) => {
const [users, projects, tasks] = await Promise.all([this.User.findAll(), this.Project.findAll(), this.Task.findAll()]);
this.projects = projects;
return Promise.all([
await Promise.all([
projects[0].setMembers(users.slice(0, 4)),
projects[1].setMembers(users.slice(2)),
projects[0].setParanoidMembers(users.slice(0, 4)),
......@@ -67,17 +68,17 @@ if (current.dialect.supports['UNION ALL']) {
users[2].setTasks(tasks)
]);
});
});
describe('on: belongsToMany', () => {
it('maps attributes from a grouped limit to models', function() {
return this.User.findAll({
it('maps attributes from a grouped limit to models', async function() {
const users = await this.User.findAll({
groupedLimit: {
limit: 3,
on: this.User.Projects,
values: this.projects.map(item => item.get('id'))
}
}).then(users => {
});
expect(users).to.have.length(5);
users.filter(u => u.get('id') !== 3).forEach(u => {
expect(u.get('projects')).to.have.length(1);
......@@ -86,10 +87,9 @@ if (current.dialect.supports['UNION ALL']) {
expect(u.get('projects')).to.have.length(2);
});
});
});
it('maps attributes from a grouped limit to models with include', function() {
return this.User.findAll({
it('maps attributes from a grouped limit to models with include', async function() {
const users = await this.User.findAll({
groupedLimit: {
limit: 3,
on: this.User.Projects,
......@@ -97,7 +97,8 @@ if (current.dialect.supports['UNION ALL']) {
},
order: ['id'],
include: [this.User.Tasks]
}).then(users => {
});
/*
project1 - 1, 2, 3
project2 - 3, 4, 5
......@@ -113,10 +114,9 @@ if (current.dialect.supports['UNION ALL']) {
expect(u.get('projects')).to.have.length(2);
});
});
});
it('works with computed order', function() {
return this.User.findAll({
it('works with computed order', async function() {
const users = await this.User.findAll({
attributes: ['id'],
groupedLimit: {
limit: 3,
......@@ -127,7 +127,8 @@ if (current.dialect.supports['UNION ALL']) {
Sequelize.fn('ABS', Sequelize.col('age'))
],
include: [this.User.Tasks]
}).then(users => {
});
/*
project1 - 1, 3, 4
project2 - 3, 5, 4
......@@ -135,10 +136,9 @@ if (current.dialect.supports['UNION ALL']) {
expect(users).to.have.length(4);
expect(users.map(u => u.get('id'))).to.deep.equal([1, 3, 5, 4]);
});
});
it('works with multiple orders', function() {
return this.User.findAll({
it('works with multiple orders', async function() {
const users = await this.User.findAll({
attributes: ['id'],
groupedLimit: {
limit: 3,
......@@ -150,7 +150,8 @@ if (current.dialect.supports['UNION ALL']) {
['id', 'DESC']
],
include: [this.User.Tasks]
}).then(users => {
});
/*
project1 - 1, 3, 4
project2 - 3, 5, 7
......@@ -158,10 +159,9 @@ if (current.dialect.supports['UNION ALL']) {
expect(users).to.have.length(5);
expect(users.map(u => u.get('id'))).to.deep.equal([1, 3, 5, 7, 4]);
});
});
it('works with paranoid junction models', function() {
return this.User.findAll({
it('works with paranoid junction models', async function() {
const users0 = await this.User.findAll({
attributes: ['id'],
groupedLimit: {
limit: 3,
......@@ -173,20 +173,21 @@ if (current.dialect.supports['UNION ALL']) {
['id', 'DESC']
],
include: [this.User.Tasks]
}).then(users => {
});
/*
project1 - 1, 3, 4
project2 - 3, 5, 7
*/
expect(users).to.have.length(5);
expect(users.map(u => u.get('id'))).to.deep.equal([1, 3, 5, 7, 4]);
expect(users0).to.have.length(5);
expect(users0.map(u => u.get('id'))).to.deep.equal([1, 3, 5, 7, 4]);
return Promise.all([
this.projects[0].setParanoidMembers(users.slice(0, 2)),
this.projects[1].setParanoidMembers(users.slice(4))
await Promise.all([
this.projects[0].setParanoidMembers(users0.slice(0, 2)),
this.projects[1].setParanoidMembers(users0.slice(4))
]);
}).then(() => {
return this.User.findAll({
const users = await this.User.findAll({
attributes: ['id'],
groupedLimit: {
limit: 3,
......@@ -199,7 +200,7 @@ if (current.dialect.supports['UNION ALL']) {
],
include: [this.User.Tasks]
});
}).then(users => {
/*
project1 - 1, 3
project2 - 4
......@@ -208,33 +209,32 @@ if (current.dialect.supports['UNION ALL']) {
expect(users.map(u => u.get('id'))).to.deep.equal([1, 3, 4]);
});
});
});
describe('on: hasMany', () => {
beforeEach(function() {
beforeEach(async function() {
this.User = this.sequelize.define('user');
this.Task = this.sequelize.define('task');
this.User.Tasks = this.User.hasMany(this.Task);
return this.sequelize.sync({ force: true }).then(() => {
return Promise.all([
await this.sequelize.sync({ force: true });
await 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]) => {
const [users, tasks] = await Promise.all([this.User.findAll(), this.Task.findAll()]);
this.users = users;
return Promise.all([
await Promise.all([
users[0].setTasks(tasks[0]),
users[1].setTasks(tasks.slice(1, 4)),
users[2].setTasks(tasks.slice(4))
]);
});
});
it('Applies limit and order correctly', function() {
return this.Task.findAll({
it('Applies limit and order correctly', async function() {
const tasks = await this.Task.findAll({
order: [
['id', 'DESC']
],
......@@ -243,7 +243,8 @@ if (current.dialect.supports['UNION ALL']) {
on: this.User.Tasks,
values: this.users.map(item => item.get('id'))
}
}).then(tasks => {
});
const byUser = _.groupBy(tasks, _.property('userId'));
expect(Object.keys(byUser)).to.have.length(3);
......@@ -256,5 +257,4 @@ if (current.dialect.supports['UNION ALL']) {
});
});
});
});
}
......@@ -10,58 +10,58 @@ describe(Support.getTestDialectTeaser('Model'), () => {
describe('findAll', () => {
describe('order', () => {
describe('Sequelize.literal()', () => {
beforeEach(function() {
beforeEach(async function() {
this.User = this.sequelize.define('User', {
email: DataTypes.STRING
});
return this.User.sync({ force: true }).then(() => {
return this.User.create({
await this.User.sync({ force: true });
await this.User.create({
email: 'test@sequelizejs.com'
});
});
});
if (current.dialect.name !== 'mssql') {
it('should work with order: literal()', function() {
return this.User.findAll({
it('should work with order: literal()', async function() {
const users = await this.User.findAll({
order: this.sequelize.literal(`email = ${this.sequelize.escape('test@sequelizejs.com')}`)
}).then(users => {
});
expect(users.length).to.equal(1);
users.forEach(user => {
expect(user.get('email')).to.be.ok;
});
});
});
it('should work with order: [literal()]', function() {
return this.User.findAll({
it('should work with order: [literal()]', async function() {
const users = await this.User.findAll({
order: [this.sequelize.literal(`email = ${this.sequelize.escape('test@sequelizejs.com')}`)]
}).then(users => {
});
expect(users.length).to.equal(1);
users.forEach(user => {
expect(user.get('email')).to.be.ok;
});
});
});
it('should work with order: [[literal()]]', function() {
return this.User.findAll({
it('should work with order: [[literal()]]', async function() {
const users = await this.User.findAll({
order: [
[this.sequelize.literal(`email = ${this.sequelize.escape('test@sequelizejs.com')}`)]
]
}).then(users => {
});
expect(users.length).to.equal(1);
users.forEach(user => {
expect(user.get('email')).to.be.ok;
});
});
});
}
});
describe('injections', () => {
beforeEach(function() {
beforeEach(async function() {
this.User = this.sequelize.define('user', {
name: DataTypes.STRING
});
......@@ -69,12 +69,12 @@ describe(Support.getTestDialectTeaser('Model'), () => {
});
this.User.belongsTo(this.Group);
return this.sequelize.sync({ force: true });
await this.sequelize.sync({ force: true });
});
if (current.dialect.supports['ORDER NULLS']) {
it('should not throw with on NULLS LAST/NULLS FIRST', function() {
return this.User.findAll({
it('should not throw with on NULLS LAST/NULLS FIRST', async function() {
await this.User.findAll({
include: [this.Group],
order: [
['id', 'ASC NULLS LAST'],
......@@ -84,16 +84,16 @@ describe(Support.getTestDialectTeaser('Model'), () => {
});
}
it('should not throw on a literal', function() {
return this.User.findAll({
it('should not throw on a literal', async function() {
await this.User.findAll({
order: [
['id', this.sequelize.literal('ASC, name DESC')]
]
});
});
it('should not throw with include when last order argument is a field', function() {
return this.User.findAll({
it('should not throw with include when last order argument is a field', async function() {
await this.User.findAll({
include: [this.Group],
order: [
[this.Group, 'id']
......
......@@ -9,7 +9,7 @@ const current = Support.sequelize;
describe(Support.getTestDialectTeaser('Model'), () => {
describe('findAll', () => {
describe('separate with limit', () => {
it('should not throw syntax error (union)', () => {
it('should not throw syntax error (union)', async () => {
// #9813 testcase
const Project = current.define('Project', { name: DataTypes.STRING });
const LevelTwo = current.define('LevelTwo', { name: DataTypes.STRING });
......@@ -22,20 +22,23 @@ describe(Support.getTestDialectTeaser('Model'), () => {
LevelTwo.hasMany(LevelThree, { as: 'type_twos' });
LevelThree.belongsTo(LevelTwo);
return current.sync({ force: true }).then(() => {
return Promise.all([
try {
try {
await current.sync({ force: true });
const [project, level21, level22] = await Promise.all([
Project.create({ name: 'testProject' }),
LevelTwo.create({ name: 'testL21' }),
LevelTwo.create({ name: 'testL22' })
]);
}).then(([project, level21, level22]) => {
return Promise.all([
await Promise.all([
project.addLevelTwo(level21),
project.addLevelTwo(level22)
]);
}).then(() => {
// one include case
return Project.findAll({
const projects0 = await Project.findAll({
where: { name: 'testProject' },
include: [
{
......@@ -53,16 +56,17 @@ describe(Support.getTestDialectTeaser('Model'), () => {
}
]
});
}).then(projects => {
expect(projects).to.have.length(1);
expect(projects[0].LevelTwos).to.have.length(2);
expect(projects[0].LevelTwos[0].type_ones).to.have.length(0);
expect(projects[0].LevelTwos[1].type_ones).to.have.length(0);
}, () => {
expect(projects0).to.have.length(1);
expect(projects0[0].LevelTwos).to.have.length(2);
expect(projects0[0].LevelTwos[0].type_ones).to.have.length(0);
expect(projects0[0].LevelTwos[1].type_ones).to.have.length(0);
} catch (err) {
expect.fail();
}).then(() => {
}
// two includes case
return Project.findAll({
const projects = await Project.findAll({
where: { name: 'testProject' },
include: [
{
......@@ -88,14 +92,14 @@ describe(Support.getTestDialectTeaser('Model'), () => {
}
]
});
}).then(projects => {
expect(projects).to.have.length(1);
expect(projects[0].LevelTwos).to.have.length(2);
expect(projects[0].LevelTwos[0].type_ones).to.have.length(0);
expect(projects[0].LevelTwos[1].type_ones).to.have.length(0);
}, () => {
} catch (err) {
expect.fail();
});
}
});
});
});
......
......@@ -6,7 +6,7 @@ const chai = require('chai'),
DataTypes = require('../../../lib/data-types');
describe(Support.getTestDialectTeaser('Model'), () => {
beforeEach(function() {
beforeEach(async function() {
this.User = this.sequelize.define('User', {
username: DataTypes.STRING,
age: DataTypes.INTEGER
......@@ -18,21 +18,24 @@ describe(Support.getTestDialectTeaser('Model'), () => {
this.User.hasMany(this.Project);
this.Project.belongsTo(this.User);
return this.sequelize.sync({ force: true });
await this.sequelize.sync({ force: true });
});
describe('findOrBuild', () => {
it('initialize with includes', function() {
return this.User.bulkCreate([
it('initialize with includes', async function() {
const [, user2] = await this.User.bulkCreate([
{ username: 'Mello', age: 10 },
{ username: 'Mello', age: 20 }
], { returning: true }).then(([, user2]) => {
return this.Project.create({
], { returning: true });
const project = await this.Project.create({
name: 'Investigate'
}).then(project => user2.setProjects([project]));
}).then(() => {
return this.User.findOrBuild({
});
await user2.setProjects([project]);
const [user, created] = await this.User.findOrBuild({
defaults: {
username: 'Mello',
age: 10
......@@ -44,7 +47,7 @@ describe(Support.getTestDialectTeaser('Model'), () => {
model: this.Project
}]
});
}).then(([user, created]) => {
expect(created).to.be.false;
expect(user.get('id')).to.be.ok;
expect(user.get('username')).to.equal('Mello');
......@@ -54,5 +57,4 @@ describe(Support.getTestDialectTeaser('Model'), () => {
expect(user.Projects[0].get('name')).to.equal('Investigate');
});
});
});
});
......@@ -8,7 +8,7 @@ const expect = chai.expect;
describe(Support.getTestDialectTeaser('Model'), () => {
describe('optimistic locking', () => {
let Account;
beforeEach(function() {
beforeEach(async function() {
Account = this.sequelize.define('Account', {
number: {
type: DataTypes.INTEGER
......@@ -16,65 +16,54 @@ describe(Support.getTestDialectTeaser('Model'), () => {
}, {
version: true
});
return Account.sync({ force: true });
await Account.sync({ force: true });
});
it('should increment the version on save', () => {
return Account.create({ number: 1 }).then(account => {
account.number += 1;
expect(account.version).to.eq(0);
return account.save();
}).then(account => {
it('should increment the version on save', async () => {
const account0 = await Account.create({ number: 1 });
account0.number += 1;
expect(account0.version).to.eq(0);
const account = await account0.save();
expect(account.version).to.eq(1);
});
});
it('should increment the version on update', () => {
return Account.create({ number: 1 }).then(account => {
expect(account.version).to.eq(0);
return account.update({ number: 2 });
}).then(account => {
expect(account.version).to.eq(1);
account.number += 1;
return account.save();
}).then(account => {
it('should increment the version on update', async () => {
const account1 = await Account.create({ number: 1 });
expect(account1.version).to.eq(0);
const account0 = await account1.update({ number: 2 });
expect(account0.version).to.eq(1);
account0.number += 1;
const account = await account0.save();
expect(account.number).to.eq(3);
expect(account.version).to.eq(2);
});
});
it('prevents stale instances from being saved', () => {
return expect(Account.create({ number: 1 }).then(accountA => {
return Account.findByPk(accountA.id).then(accountB => {
it('prevents stale instances from being saved', async () => {
await expect((async () => {
const accountA = await Account.create({ number: 1 });
const accountB0 = await Account.findByPk(accountA.id);
accountA.number += 1;
return accountA.save().then(() => { return accountB; });
});
}).then(accountB => {
await accountA.save();
const accountB = await accountB0;
accountB.number += 1;
return accountB.save();
})).to.eventually.be.rejectedWith(Support.Sequelize.OptimisticLockError);
return await accountB.save();
})()).to.eventually.be.rejectedWith(Support.Sequelize.OptimisticLockError);
});
it('increment() also increments the version', () => {
return Account.create({ number: 1 }).then(account => {
expect(account.version).to.eq(0);
return account.increment('number', { by: 1 } );
}).then(account => {
return account.reload();
}).then(account => {
it('increment() also increments the version', async () => {
const account1 = await Account.create({ number: 1 });
expect(account1.version).to.eq(0);
const account0 = await account1.increment('number', { by: 1 } );
const account = await account0.reload();
expect(account.version).to.eq(1);
});
});
it('decrement() also increments the version', () => {
return Account.create({ number: 1 }).then(account => {
expect(account.version).to.eq(0);
return account.decrement('number', { by: 1 } );
}).then(account => {
return account.reload();
}).then(account => {
it('decrement() also increments the version', async () => {
const account1 = await Account.create({ number: 1 });
expect(account1.version).to.eq(0);
const account0 = await account1.decrement('number', { by: 1 } );
const account = await account0.reload();
expect(account.version).to.eq(1);
});
});
});
});
......@@ -17,7 +17,7 @@ describe(Support.getTestDialectTeaser('Model'), () => {
this.clock.restore();
});
it('should be able to soft delete with timestamps', function() {
it('should be able to soft delete with timestamps', async function() {
const Account = this.sequelize.define('Account', {
ownerId: {
type: DataTypes.INTEGER,
......@@ -32,32 +32,22 @@ describe(Support.getTestDialectTeaser('Model'), () => {
timestamps: true
});
return Account.sync({ force: true })
.then(() => Account.create({ ownerId: 12 }))
.then(() => Account.count())
.then(count => {
expect(count).to.be.equal(1);
return Account.destroy({ where: { ownerId: 12 } })
.then(result => {
await Account.sync({ force: true });
await Account.create({ ownerId: 12 });
const count2 = await Account.count();
expect(count2).to.be.equal(1);
const result = await Account.destroy({ where: { ownerId: 12 } });
expect(result).to.be.equal(1);
});
})
.then(() => Account.count())
.then(count => {
expect(count).to.be.equal(0);
return Account.count({ paranoid: false });
})
.then(count => {
expect(count).to.be.equal(1);
return Account.restore({ where: { ownerId: 12 } });
})
.then(() => Account.count())
.then(count => {
const count1 = await Account.count();
expect(count1).to.be.equal(0);
const count0 = await Account.count({ paranoid: false });
expect(count0).to.be.equal(1);
await Account.restore({ where: { ownerId: 12 } });
const count = await Account.count();
expect(count).to.be.equal(1);
});
});
it('should be able to soft delete without timestamps', function() {
it('should be able to soft delete without timestamps', async function() {
const Account = this.sequelize.define('Account', {
ownerId: {
type: DataTypes.INTEGER,
......@@ -80,27 +70,19 @@ describe(Support.getTestDialectTeaser('Model'), () => {
updatedAt: false
});
return Account.sync({ force: true })
.then(() => Account.create({ ownerId: 12 }))
.then(() => Account.count())
.then(count => {
expect(count).to.be.equal(1);
return Account.destroy({ where: { ownerId: 12 } });
})
.then(() => Account.count())
.then(count => {
expect(count).to.be.equal(0);
return Account.count({ paranoid: false });
})
.then(count => {
expect(count).to.be.equal(1);
return Account.restore({ where: { ownerId: 12 } });
})
.then(() => Account.count())
.then(count => {
await Account.sync({ force: true });
await Account.create({ ownerId: 12 });
const count2 = await Account.count();
expect(count2).to.be.equal(1);
await Account.destroy({ where: { ownerId: 12 } });
const count1 = await Account.count();
expect(count1).to.be.equal(0);
const count0 = await Account.count({ paranoid: false });
expect(count0).to.be.equal(1);
await Account.restore({ where: { ownerId: 12 } });
const count = await Account.count();
expect(count).to.be.equal(1);
});
});
if (current.dialect.supports.JSON) {
describe('JSON', () => {
......@@ -124,12 +106,12 @@ describe(Support.getTestDialectTeaser('Model'), () => {
});
});
beforeEach(function() {
return this.Model.sync({ force: true });
beforeEach(async function() {
await this.Model.sync({ force: true });
});
it('should soft delete with JSON condition', function() {
return this.Model.bulkCreate([{
it('should soft delete with JSON condition', async function() {
await this.Model.bulkCreate([{
name: 'One',
data: {
field: {
......@@ -143,7 +125,9 @@ describe(Support.getTestDialectTeaser('Model'), () => {
deep: false
}
}
}]).then(() => this.Model.destroy({
}]);
await this.Model.destroy({
where: {
data: {
field: {
......@@ -151,12 +135,13 @@ describe(Support.getTestDialectTeaser('Model'), () => {
}
}
}
})).then(() => this.Model.findAll()).then(records => {
});
const records = await this.Model.findAll();
expect(records.length).to.equal(1);
expect(records[0].get('name')).to.equal('Two');
});
});
});
}
});
});
......@@ -8,7 +8,7 @@ const chai = require('chai'),
describe(Support.getTestDialectTeaser('Model'), () => {
describe('scope', () => {
beforeEach(function() {
beforeEach(async function() {
this.ScopeMe = this.sequelize.define('ScopeMe', {
username: Sequelize.STRING,
email: Sequelize.STRING,
......@@ -57,66 +57,54 @@ describe(Support.getTestDialectTeaser('Model'), () => {
}
});
return this.sequelize.sync({ force: true }).then(() => {
await this.sequelize.sync({ force: true });
const records = [
{ username: 'tony', email: 'tony@sequelizejs.com', access_level: 3, other_value: 7 },
{ username: 'tobi', email: 'tobi@fakeemail.com', access_level: 10, other_value: 11 },
{ username: 'dan', email: 'dan@sequelizejs.com', access_level: 5, other_value: 10 },
{ username: 'fred', email: 'fred@foobar.com', access_level: 3, other_value: 7 }
];
return this.ScopeMe.bulkCreate(records);
});
await this.ScopeMe.bulkCreate(records);
});
it('should be able to merge attributes as array', function() {
return this.ScopeMe.scope('lowAccess', 'withName').findOne()
.then(record => {
it('should be able to merge attributes as array', async function() {
const record = await this.ScopeMe.scope('lowAccess', 'withName').findOne();
expect(record.other_value).to.exist;
expect(record.username).to.exist;
expect(record.access_level).to.exist;
});
});
it('should work with Symbol operators', function() {
return this.ScopeMe.scope('highAccess').findOne()
.then(record => {
it('should work with Symbol operators', async function() {
const record = await this.ScopeMe.scope('highAccess').findOne();
expect(record.username).to.equal('tobi');
return this.ScopeMe.scope('lessThanFour').findAll();
})
.then(records => {
expect(records).to.have.length(2);
expect(records[0].get('access_level')).to.equal(3);
expect(records[1].get('access_level')).to.equal(3);
return this.ScopeMe.scope('issue8473').findAll();
})
.then(records => {
const records0 = await this.ScopeMe.scope('lessThanFour').findAll();
expect(records0).to.have.length(2);
expect(records0[0].get('access_level')).to.equal(3);
expect(records0[1].get('access_level')).to.equal(3);
const records = await this.ScopeMe.scope('issue8473').findAll();
expect(records).to.have.length(1);
expect(records[0].get('access_level')).to.equal(5);
expect(records[0].get('other_value')).to.equal(10);
});
});
it('should keep symbols after default assignment', function() {
return this.ScopeMe.scope('highAccess').findOne()
.then(record => {
it('should keep symbols after default assignment', async function() {
const record = await this.ScopeMe.scope('highAccess').findOne();
expect(record.username).to.equal('tobi');
return this.ScopeMe.scope('lessThanFour').findAll({
const records = await this.ScopeMe.scope('lessThanFour').findAll({
where: {}
});
})
.then(records => {
expect(records).to.have.length(2);
expect(records[0].get('access_level')).to.equal(3);
expect(records[1].get('access_level')).to.equal(3);
return this.ScopeMe.scope('issue8473').findAll();
});
await this.ScopeMe.scope('issue8473').findAll();
});
it('should not throw error with sequelize.where', function() {
return this.ScopeMe.scope('like_t').findAll()
.then(records => {
it('should not throw error with sequelize.where', async function() {
const records = await this.ScopeMe.scope('like_t').findAll();
expect(records).to.have.length(2);
});
});
});
});
......@@ -9,7 +9,7 @@ const chai = require('chai'),
describe(Support.getTestDialectTeaser('Model'), () => {
describe('scope', () => {
describe('aggregate', () => {
beforeEach(function() {
beforeEach(async function() {
this.Child = this.sequelize.define('Child', {
priority: Sequelize.INTEGER
});
......@@ -50,18 +50,17 @@ describe(Support.getTestDialectTeaser('Model'), () => {
this.Child.belongsTo(this.ScopeMe);
this.ScopeMe.hasMany(this.Child);
return this.sequelize.sync({ force: true }).then(() => {
const records = [
await this.sequelize.sync({ force: true });
const records0 = [
{ username: 'tony', email: 'tony@sequelizejs.com', access_level: 3, other_value: 7 },
{ username: 'tobi', email: 'tobi@fakeemail.com', access_level: 10, other_value: 11 },
{ username: 'dan', email: 'dan@sequelizejs.com', access_level: 5, other_value: 10 },
{ username: 'fred', email: 'fred@foobar.com', access_level: 3, other_value: 7 }
];
return this.ScopeMe.bulkCreate(records);
}).then(() => {
return this.ScopeMe.findAll();
}).then(records => {
return Promise.all([
await this.ScopeMe.bulkCreate(records0);
const records = await this.ScopeMe.findAll();
await Promise.all([
records[0].createChild({
priority: 1
}),
......@@ -70,30 +69,29 @@ describe(Support.getTestDialectTeaser('Model'), () => {
})
]);
});
});
it('should apply defaultScope', function() {
return expect(this.ScopeMe.aggregate( '*', 'count' )).to.eventually.equal(2);
it('should apply defaultScope', async function() {
await expect(this.ScopeMe.aggregate( '*', 'count' )).to.eventually.equal(2);
});
it('should be able to override default scope', function() {
return expect(this.ScopeMe.aggregate( '*', 'count', { where: { access_level: { [Op.gt]: 5 } } })).to.eventually.equal(1);
it('should be able to override default scope', async function() {
await expect(this.ScopeMe.aggregate( '*', 'count', { where: { access_level: { [Op.gt]: 5 } } })).to.eventually.equal(1);
});
it('should be able to unscope', function() {
return expect(this.ScopeMe.unscoped().aggregate( '*', 'count' )).to.eventually.equal(4);
it('should be able to unscope', async function() {
await expect(this.ScopeMe.unscoped().aggregate( '*', 'count' )).to.eventually.equal(4);
});
it('should be able to apply other scopes', function() {
return expect(this.ScopeMe.scope('lowAccess').aggregate( '*', 'count' )).to.eventually.equal(3);
it('should be able to apply other scopes', async function() {
await expect(this.ScopeMe.scope('lowAccess').aggregate( '*', 'count' )).to.eventually.equal(3);
});
it('should be able to merge scopes with where', function() {
return expect(this.ScopeMe.scope('lowAccess').aggregate( '*', 'count', { where: { username: 'dan' } })).to.eventually.equal(1);
it('should be able to merge scopes with where', async function() {
await expect(this.ScopeMe.scope('lowAccess').aggregate( '*', 'count', { where: { username: 'dan' } })).to.eventually.equal(1);
});
it('should be able to use where on include', function() {
return expect(this.ScopeMe.scope('withInclude').aggregate( 'ScopeMe.id', 'count', {
it('should be able to use where on include', async function() {
await expect(this.ScopeMe.scope('withInclude').aggregate( 'ScopeMe.id', 'count', {
plain: true,
dataType: new Sequelize.INTEGER(),
includeIgnoreAttributes: false,
......@@ -105,27 +103,21 @@ describe(Support.getTestDialectTeaser('Model'), () => {
});
if (Support.sequelize.dialect.supports.schemas) {
it('aggregate with schema', function() {
it('aggregate with schema', async function() {
this.Hero = this.sequelize.define('Hero', {
codename: Sequelize.STRING
}, { schema: 'heroschema' });
return this.sequelize.createSchema('heroschema')
.then(() => {
return this.sequelize.sync({ force: true });
})
.then(() => {
await this.sequelize.createSchema('heroschema');
await this.sequelize.sync({ force: true });
const records = [
{ codename: 'hulk' },
{ codename: 'rantanplan' }
];
return this.Hero.bulkCreate(records);
})
.then(() => {
return expect(
await this.Hero.bulkCreate(records);
await expect(
this.Hero.unscoped().aggregate('*', 'count',
{ schema: 'heroschema' })).to.eventually.equal(
2);
});
{ schema: 'heroschema' })).to.eventually.equal(2);
});
}
});
......
......@@ -9,7 +9,7 @@ const chai = require('chai'),
describe(Support.getTestDialectTeaser('Model'), () => {
describe('scope', () => {
describe('count', () => {
beforeEach(function() {
beforeEach(async function() {
this.Child = this.sequelize.define('Child', {
priority: Sequelize.INTEGER
});
......@@ -80,18 +80,17 @@ describe(Support.getTestDialectTeaser('Model'), () => {
this.Child.belongsTo(this.ScopeMe);
this.ScopeMe.hasMany(this.Child);
return this.sequelize.sync({ force: true }).then(() => {
const records = [
await this.sequelize.sync({ force: true });
const records0 = [
{ username: 'tony', email: 'tony@sequelizejs.com', access_level: 3, other_value: 7, aliasValue: 12 },
{ username: 'tobi', email: 'tobi@fakeemail.com', access_level: 10, other_value: 11, aliasValue: 5 },
{ username: 'dan', email: 'dan@sequelizejs.com', access_level: 5, other_value: 10, aliasValue: 1 },
{ username: 'fred', email: 'fred@foobar.com', access_level: 3, other_value: 7, aliasValue: 10 }
];
return this.ScopeMe.bulkCreate(records);
}).then(() => {
return this.ScopeMe.findAll();
}).then(records => {
return Promise.all([
await this.ScopeMe.bulkCreate(records0);
const records = await this.ScopeMe.findAll();
await Promise.all([
records[0].createChild({
priority: 1
}),
......@@ -100,46 +99,45 @@ describe(Support.getTestDialectTeaser('Model'), () => {
})
]);
});
});
it('should apply defaultScope', function() {
return expect(this.ScopeMe.count()).to.eventually.equal(2);
it('should apply defaultScope', async function() {
await expect(this.ScopeMe.count()).to.eventually.equal(2);
});
it('should be able to override default scope', function() {
return expect(this.ScopeMe.count({ where: { access_level: { [Op.gt]: 5 } } })).to.eventually.equal(1);
it('should be able to override default scope', async function() {
await expect(this.ScopeMe.count({ where: { access_level: { [Op.gt]: 5 } } })).to.eventually.equal(1);
});
it('should be able to unscope', function() {
return expect(this.ScopeMe.unscoped().count()).to.eventually.equal(4);
it('should be able to unscope', async function() {
await expect(this.ScopeMe.unscoped().count()).to.eventually.equal(4);
});
it('should be able to apply other scopes', function() {
return expect(this.ScopeMe.scope('lowAccess').count()).to.eventually.equal(3);
it('should be able to apply other scopes', async function() {
await expect(this.ScopeMe.scope('lowAccess').count()).to.eventually.equal(3);
});
it('should be able to merge scopes with where', function() {
return expect(this.ScopeMe.scope('lowAccess').count({ where: { username: 'dan' } })).to.eventually.equal(1);
it('should be able to merge scopes with where', async function() {
await expect(this.ScopeMe.scope('lowAccess').count({ where: { username: 'dan' } })).to.eventually.equal(1);
});
it('should be able to merge scopes with where on aliased fields', function() {
return expect(this.ScopeMe.scope('withAliasedField').count({ where: { aliasValue: 5 } })).to.eventually.equal(1);
it('should be able to merge scopes with where on aliased fields', async function() {
await expect(this.ScopeMe.scope('withAliasedField').count({ where: { aliasValue: 5 } })).to.eventually.equal(1);
});
it('should ignore the order option if it is found within the scope', function() {
return expect(this.ScopeMe.scope('withOrder').count()).to.eventually.equal(4);
it('should ignore the order option if it is found within the scope', async function() {
await expect(this.ScopeMe.scope('withOrder').count()).to.eventually.equal(4);
});
it('should be able to use where on include', function() {
return expect(this.ScopeMe.scope('withInclude').count()).to.eventually.equal(1);
it('should be able to use where on include', async function() {
await expect(this.ScopeMe.scope('withInclude').count()).to.eventually.equal(1);
});
it('should be able to use include with function scope', function() {
return expect(this.ScopeMe.scope('withIncludeFunction').count()).to.eventually.equal(1);
it('should be able to use include with function scope', async function() {
await expect(this.ScopeMe.scope('withIncludeFunction').count()).to.eventually.equal(1);
});
it('should be able to use include with function scope and string association', function() {
return expect(this.ScopeMe.scope('withIncludeFunctionAndStringAssociation').count()).to.eventually.equal(1);
it('should be able to use include with function scope and string association', async function() {
await expect(this.ScopeMe.scope('withIncludeFunctionAndStringAssociation').count()).to.eventually.equal(1);
});
});
});
......
......@@ -9,7 +9,7 @@ const chai = require('chai'),
describe(Support.getTestDialectTeaser('Model'), () => {
describe('scope', () => {
describe('destroy', () => {
beforeEach(function() {
beforeEach(async function() {
this.ScopeMe = this.sequelize.define('ScopeMe', {
username: Sequelize.STRING,
email: Sequelize.STRING,
......@@ -34,71 +34,60 @@ describe(Support.getTestDialectTeaser('Model'), () => {
}
});
return this.sequelize.sync({ force: true }).then(() => {
await this.sequelize.sync({ force: true });
const records = [
{ username: 'tony', email: 'tony@sequelizejs.com', access_level: 3, other_value: 7 },
{ username: 'tobi', email: 'tobi@fakeemail.com', access_level: 10, other_value: 11 },
{ username: 'dan', email: 'dan@sequelizejs.com', access_level: 5, other_value: 10 },
{ username: 'fred', email: 'fred@foobar.com', access_level: 3, other_value: 7 }
];
return this.ScopeMe.bulkCreate(records);
});
await this.ScopeMe.bulkCreate(records);
});
it('should apply defaultScope', function() {
return this.ScopeMe.destroy({ where: {} }).then(() => {
return this.ScopeMe.unscoped().findAll();
}).then(users => {
it('should apply defaultScope', async function() {
await this.ScopeMe.destroy({ where: {} });
const users = await this.ScopeMe.unscoped().findAll();
expect(users).to.have.length(2);
expect(users[0].get('username')).to.equal('tony');
expect(users[1].get('username')).to.equal('fred');
});
});
it('should be able to override default scope', function() {
return this.ScopeMe.destroy({ where: { access_level: { [Op.lt]: 5 } } }).then(() => {
return this.ScopeMe.unscoped().findAll();
}).then(users => {
it('should be able to override default scope', async function() {
await this.ScopeMe.destroy({ where: { access_level: { [Op.lt]: 5 } } });
const users = await this.ScopeMe.unscoped().findAll();
expect(users).to.have.length(2);
expect(users[0].get('username')).to.equal('tobi');
expect(users[1].get('username')).to.equal('dan');
});
});
it('should be able to unscope destroy', function() {
return this.ScopeMe.unscoped().destroy({ where: {} }).then(() => {
return expect(this.ScopeMe.unscoped().findAll()).to.eventually.have.length(0);
});
it('should be able to unscope destroy', async function() {
await this.ScopeMe.unscoped().destroy({ where: {} });
await expect(this.ScopeMe.unscoped().findAll()).to.eventually.have.length(0);
});
it('should be able to apply other scopes', function() {
return this.ScopeMe.scope('lowAccess').destroy({ where: {} }).then(() => {
return this.ScopeMe.unscoped().findAll();
}).then(users => {
it('should be able to apply other scopes', async function() {
await this.ScopeMe.scope('lowAccess').destroy({ where: {} });
const users = await this.ScopeMe.unscoped().findAll();
expect(users).to.have.length(1);
expect(users[0].get('username')).to.equal('tobi');
});
});
it('should be able to merge scopes with where', function() {
return this.ScopeMe.scope('lowAccess').destroy({ where: { username: 'dan' } }).then(() => {
return this.ScopeMe.unscoped().findAll();
}).then(users => {
it('should be able to merge scopes with where', async function() {
await this.ScopeMe.scope('lowAccess').destroy({ where: { username: 'dan' } });
const users = await this.ScopeMe.unscoped().findAll();
expect(users).to.have.length(3);
expect(users[0].get('username')).to.equal('tony');
expect(users[1].get('username')).to.equal('tobi');
expect(users[2].get('username')).to.equal('fred');
});
});
it('should work with empty where', function() {
return this.ScopeMe.scope('lowAccess').destroy().then(() => {
return this.ScopeMe.unscoped().findAll();
}).then(users => {
it('should work with empty where', async function() {
await this.ScopeMe.scope('lowAccess').destroy();
const users = await this.ScopeMe.unscoped().findAll();
expect(users).to.have.length(1);
expect(users[0].get('username')).to.equal('tobi');
});
});
});
});
});
......@@ -8,7 +8,7 @@ const chai = require('chai'),
describe(Support.getTestDialectTeaser('Model'), () => {
describe('scopes', () => {
beforeEach(function() {
beforeEach(async function() {
this.ScopeMe = this.sequelize.define('ScopeMe', {
username: Sequelize.STRING,
email: Sequelize.STRING,
......@@ -62,89 +62,79 @@ describe(Support.getTestDialectTeaser('Model'), () => {
this.ScopeMe.hasMany(this.DefaultScopeExclude);
return this.sequelize.sync({ force: true }).then(() => {
await this.sequelize.sync({ force: true });
const records = [
{ username: 'tony', email: 'tony@sequelizejs.com', access_level: 3, other_value: 7, parent_id: 1 },
{ username: 'tobi', email: 'tobi@fakeemail.com', access_level: 10, other_value: 11, parent_id: 2 },
{ username: 'dan', email: 'dan@sequelizejs.com', access_level: 5, other_value: 10, parent_id: 1 },
{ username: 'fred', email: 'fred@foobar.com', access_level: 3, other_value: 7, parent_id: 1 }
];
return this.ScopeMe.bulkCreate(records);
});
await this.ScopeMe.bulkCreate(records);
});
it('should be able use where in scope', function() {
return this.ScopeMe.scope({ where: { parent_id: 2 } }).findAll().then(users => {
it('should be able use where in scope', async function() {
const users = await this.ScopeMe.scope({ where: { parent_id: 2 } }).findAll();
expect(users).to.have.length(1);
expect(users[0].username).to.equal('tobi');
});
});
it('should be able to combine scope and findAll where clauses', function() {
return this.ScopeMe.scope({ where: { parent_id: 1 } }).findAll({ where: { access_level: 3 } }).then(users => {
it('should be able to combine scope and findAll where clauses', async function() {
const users = await this.ScopeMe.scope({ where: { parent_id: 1 } }).findAll({ where: { access_level: 3 } });
expect(users).to.have.length(2);
expect(['tony', 'fred'].includes(users[0].username)).to.be.true;
expect(['tony', 'fred'].includes(users[1].username)).to.be.true;
});
});
it('should be able to use a defaultScope if declared', function() {
return this.ScopeMe.findAll().then(users => {
it('should be able to use a defaultScope if declared', async function() {
const users = await this.ScopeMe.findAll();
expect(users).to.have.length(2);
expect([10, 5].includes(users[0].access_level)).to.be.true;
expect([10, 5].includes(users[1].access_level)).to.be.true;
expect(['dan', 'tobi'].includes(users[0].username)).to.be.true;
expect(['dan', 'tobi'].includes(users[1].username)).to.be.true;
});
});
it('should be able to handle $and in scopes', function() {
return this.ScopeMe.scope('andScope').findAll().then(users => {
it('should be able to handle $and in scopes', async function() {
const users = await this.ScopeMe.scope('andScope').findAll();
expect(users).to.have.length(1);
expect(users[0].username).to.equal('tony');
});
});
describe('should not overwrite', () => {
it('default scope with values from previous finds', function() {
return this.ScopeMe.findAll({ where: { other_value: 10 } }).then(users => {
expect(users).to.have.length(1);
it('default scope with values from previous finds', async function() {
const users0 = await this.ScopeMe.findAll({ where: { other_value: 10 } });
expect(users0).to.have.length(1);
return this.ScopeMe.findAll();
}).then(users => {
const users = await this.ScopeMe.findAll();
// This should not have other_value: 10
expect(users).to.have.length(2);
});
});
it('other scopes with values from previous finds', async function() {
const users0 = await this.ScopeMe.scope('highValue').findAll({ where: { access_level: 10 } });
expect(users0).to.have.length(1);
it('other scopes with values from previous finds', function() {
return this.ScopeMe.scope('highValue').findAll({ where: { access_level: 10 } }).then(users => {
expect(users).to.have.length(1);
return this.ScopeMe.scope('highValue').findAll();
}).then(users => {
const users = await this.ScopeMe.scope('highValue').findAll();
// This should not have other_value: 10
expect(users).to.have.length(2);
});
});
});
it('should have no problem performing findOrCreate', function() {
return this.ScopeMe.findOrCreate({ where: { username: 'fake' } }).then(([user]) => {
it('should have no problem performing findOrCreate', async function() {
const [user] = await this.ScopeMe.findOrCreate({ where: { username: 'fake' } });
expect(user.username).to.equal('fake');
});
});
it('should work when included with default scope', function() {
return this.ScopeMe.findOne({
it('should work when included with default scope', async function() {
await this.ScopeMe.findOne({
include: [this.DefaultScopeExclude]
});
});
});
describe('scope in associations', () => {
it('should work when association with a virtual column queried with default scope', function() {
it('should work when association with a virtual column queried with default scope', async function() {
const Game = this.sequelize.define('Game', {
name: Sequelize.TEXT
});
......@@ -167,15 +157,15 @@ describe(Support.getTestDialectTeaser('Model'), () => {
Game.hasMany(User);
return this.sequelize.sync({ force: true }).then(() => {
return Game.findAll({
await this.sequelize.sync({ force: true });
const games = await Game.findAll({
include: [{
model: User
}]
});
}).then(games => {
expect(games).to.have.lengthOf(0);
});
});
});
});
......@@ -11,7 +11,7 @@ describe(Support.getTestDialectTeaser('Model'), () => {
describe('findAndCountAll', () => {
beforeEach(function() {
beforeEach(async function() {
this.ScopeMe = this.sequelize.define('ScopeMe', {
username: Sequelize.STRING,
email: Sequelize.STRING,
......@@ -40,60 +40,51 @@ describe(Support.getTestDialectTeaser('Model'), () => {
}
});
return this.sequelize.sync({ force: true }).then(() => {
await this.sequelize.sync({ force: true });
const records = [
{ username: 'tony', email: 'tony@sequelizejs.com', access_level: 3, other_value: 7 },
{ username: 'tobi', email: 'tobi@fakeemail.com', access_level: 10, other_value: 11 },
{ username: 'dan', email: 'dan@sequelizejs.com', access_level: 5, other_value: 10 },
{ username: 'fred', email: 'fred@foobar.com', access_level: 3, other_value: 7 }
];
return this.ScopeMe.bulkCreate(records);
});
await this.ScopeMe.bulkCreate(records);
});
it('should apply defaultScope', function() {
return this.ScopeMe.findAndCountAll().then(result => {
it('should apply defaultScope', async function() {
const result = await this.ScopeMe.findAndCountAll();
expect(result.count).to.equal(2);
expect(result.rows.length).to.equal(2);
});
});
it('should be able to override default scope', function() {
return this.ScopeMe.findAndCountAll({ where: { access_level: { [Op.gt]: 5 } } })
.then(result => {
it('should be able to override default scope', async function() {
const result = await this.ScopeMe.findAndCountAll({ where: { access_level: { [Op.gt]: 5 } } });
expect(result.count).to.equal(1);
expect(result.rows.length).to.equal(1);
});
});
it('should be able to unscope', function() {
return this.ScopeMe.unscoped().findAndCountAll({ limit: 1 })
.then(result => {
it('should be able to unscope', async function() {
const result = await this.ScopeMe.unscoped().findAndCountAll({ limit: 1 });
expect(result.count).to.equal(4);
expect(result.rows.length).to.equal(1);
});
});
it('should be able to apply other scopes', function() {
return this.ScopeMe.scope('lowAccess').findAndCountAll()
.then(result => {
it('should be able to apply other scopes', async function() {
const result = await this.ScopeMe.scope('lowAccess').findAndCountAll();
expect(result.count).to.equal(3);
});
});
it('should be able to merge scopes with where', function() {
return this.ScopeMe.scope('lowAccess')
.findAndCountAll({ where: { username: 'dan' } }).then(result => {
it('should be able to merge scopes with where', async function() {
const result = await this.ScopeMe.scope('lowAccess')
.findAndCountAll({ where: { username: 'dan' } });
expect(result.count).to.equal(1);
});
});
it('should ignore the order option if it is found within the scope', function() {
return this.ScopeMe.scope('withOrder').findAndCountAll()
.then(result => {
it('should ignore the order option if it is found within the scope', async function() {
const result = await this.ScopeMe.scope('withOrder').findAndCountAll();
expect(result.count).to.equal(4);
});
});
});
});
});
......@@ -9,8 +9,7 @@ const chai = require('chai'),
describe(Support.getTestDialectTeaser('Model'), () => {
describe('scope', () => {
describe('simple merge', () => {
beforeEach(function() {
beforeEach(async function() {
this.Foo = this.sequelize.define('foo', { name: Sequelize.STRING }, { timestamps: false });
this.Bar = this.sequelize.define('bar', { name: Sequelize.STRING }, { timestamps: false });
this.Baz = this.sequelize.define('baz', { name: Sequelize.STRING }, { timestamps: false });
......@@ -18,10 +17,10 @@ describe(Support.getTestDialectTeaser('Model'), () => {
this.Foo.belongsTo(this.Baz, { foreignKey: 'bazId' });
this.Foo.hasOne(this.Bar, { foreignKey: 'fooId' });
this.createEntries = () => {
return this.Baz.create({ name: 'The Baz' })
.then(baz => this.Foo.create({ name: 'The Foo', bazId: baz.id }))
.then(foo => this.Bar.create({ name: 'The Bar', fooId: foo.id }));
this.createEntries = async () => {
const baz = await this.Baz.create({ name: 'The Baz' });
const foo = await this.Foo.create({ name: 'The Foo', bazId: baz.id });
return this.Bar.create({ name: 'The Bar', fooId: foo.id });
};
this.scopes = {
......@@ -32,24 +31,21 @@ describe(Support.getTestDialectTeaser('Model'), () => {
this.Foo.addScope('includeBar', this.scopes.includeBar);
this.Foo.addScope('includeBaz', this.scopes.includeBaz);
return this.sequelize.sync({ force: true }).then(this.createEntries);
await this.createEntries(await this.sequelize.sync({ force: true }));
});
it('should merge simple scopes correctly', function() {
return this.Foo.scope('includeBar', 'includeBaz').findOne().then(result => {
it('should merge simple scopes correctly', async function() {
const result = await this.Foo.scope('includeBar', 'includeBaz').findOne();
const json = result.toJSON();
expect(json.bar).to.be.ok;
expect(json.baz).to.be.ok;
expect(json.bar.name).to.equal('The Bar');
expect(json.baz.name).to.equal('The Baz');
});
});
});
describe('complex merge', () => {
beforeEach(function() {
beforeEach(async function() {
this.Foo = this.sequelize.define('foo', { name: Sequelize.STRING }, { timestamps: false });
this.Bar = this.sequelize.define('bar', { name: Sequelize.STRING }, { timestamps: false });
this.Baz = this.sequelize.define('baz', { name: Sequelize.STRING }, { timestamps: false });
......@@ -142,36 +138,36 @@ describe(Support.getTestDialectTeaser('Model'), () => {
'excludeBazName'
]).toArray();
return this.sequelize.sync({ force: true }).then(this.createFooWithDescendants);
await this.createFooWithDescendants(await this.sequelize.sync({ force: true }));
});
it('should merge complex scopes correctly regardless of their order', function() {
return Promise.all(this.scopePermutations.map(scopes => this.Foo.scope(...scopes).findOne())).then(results => {
it('should merge complex scopes correctly regardless of their order', async function() {
const results = await Promise.all(this.scopePermutations.map(scopes => this.Foo.scope(...scopes).findOne()));
const first = results.shift().toJSON();
for (const result of results) {
expect(result.toJSON()).to.deep.equal(first);
}
});
});
it('should merge complex scopes with findAll options correctly regardless of their order', function() {
return Promise.all(this.scopePermutations.map(([a, b, c, d]) => this.Foo.scope(a, b, c).findAll(this.scopes[d]).then(x => x[0]))).then(results => {
it('should merge complex scopes with findAll options correctly regardless of their order', async function() {
const results = await Promise.all(this.scopePermutations.map(async ([a, b, c, d]) => {
const x = await this.Foo.scope(a, b, c).findAll(this.scopes[d]);
return x[0];
}));
const first = results.shift().toJSON();
for (const result of results) {
expect(result.toJSON()).to.deep.equal(first);
}
});
});
it('should merge complex scopes with findOne options correctly regardless of their order', function() {
return Promise.all(this.scopePermutations.map(([a, b, c, d]) => this.Foo.scope(a, b, c).findOne(this.scopes[d]))).then(results => {
it('should merge complex scopes with findOne options correctly regardless of their order', async function() {
const results = await Promise.all(this.scopePermutations.map(([a, b, c, d]) => this.Foo.scope(a, b, c).findOne(this.scopes[d])));
const first = results.shift().toJSON();
for (const result of results) {
expect(result.toJSON()).to.deep.equal(first);
}
});
});
});
});
......
......@@ -9,7 +9,7 @@ const chai = require('chai'),
describe(Support.getTestDialectTeaser('Model'), () => {
describe('scope', () => {
describe('update', () => {
beforeEach(function() {
beforeEach(async function() {
this.ScopeMe = this.sequelize.define('ScopeMe', {
username: Sequelize.STRING,
email: Sequelize.STRING,
......@@ -34,69 +34,59 @@ describe(Support.getTestDialectTeaser('Model'), () => {
}
});
return this.sequelize.sync({ force: true }).then(() => {
await this.sequelize.sync({ force: true });
const records = [
{ username: 'tony', email: 'tony@sequelizejs.com', access_level: 3, other_value: 7 },
{ username: 'tobi', email: 'tobi@fakeemail.com', access_level: 10, other_value: 11 },
{ username: 'dan', email: 'dan@sequelizejs.com', access_level: 5, other_value: 10 },
{ username: 'fred', email: 'fred@foobar.com', access_level: 3, other_value: 7 }
];
return this.ScopeMe.bulkCreate(records);
});
await this.ScopeMe.bulkCreate(records);
});
it('should apply defaultScope', function() {
return this.ScopeMe.update({ username: 'ruben' }, { where: {} }).then(() => {
return this.ScopeMe.unscoped().findAll({ where: { username: 'ruben' } });
}).then(users => {
it('should apply defaultScope', async function() {
await this.ScopeMe.update({ username: 'ruben' }, { where: {} });
const users = await this.ScopeMe.unscoped().findAll({ where: { username: 'ruben' } });
expect(users).to.have.length(2);
expect(users[0].get('email')).to.equal('tobi@fakeemail.com');
expect(users[1].get('email')).to.equal('dan@sequelizejs.com');
});
});
it('should be able to override default scope', function() {
return this.ScopeMe.update({ username: 'ruben' }, { where: { access_level: { [Op.lt]: 5 } } }).then(() => {
return this.ScopeMe.unscoped().findAll({ where: { username: 'ruben' } });
}).then(users => {
it('should be able to override default scope', async function() {
await this.ScopeMe.update({ username: 'ruben' }, { where: { access_level: { [Op.lt]: 5 } } });
const users = await this.ScopeMe.unscoped().findAll({ where: { username: 'ruben' } });
expect(users).to.have.length(2);
expect(users[0].get('email')).to.equal('tony@sequelizejs.com');
expect(users[1].get('email')).to.equal('fred@foobar.com');
});
});
it('should be able to unscope destroy', function() {
return this.ScopeMe.unscoped().update({ username: 'ruben' }, { where: {} }).then(() => {
return this.ScopeMe.unscoped().findAll();
}).then(rubens => {
it('should be able to unscope destroy', async function() {
await this.ScopeMe.unscoped().update({ username: 'ruben' }, { where: {} });
const rubens = await this.ScopeMe.unscoped().findAll();
expect(rubens.every(r => r.get('username') === 'ruben')).to.be.true;
});
});
it('should be able to apply other scopes', function() {
return this.ScopeMe.scope('lowAccess').update({ username: 'ruben' }, { where: {} }).then(() => {
return this.ScopeMe.unscoped().findAll({ where: { username: { [Op.ne]: 'ruben' } } });
}).then(users => {
it('should be able to apply other scopes', async function() {
await this.ScopeMe.scope('lowAccess').update({ username: 'ruben' }, { where: {} });
const users = await this.ScopeMe.unscoped().findAll({ where: { username: { [Op.ne]: 'ruben' } } });
expect(users).to.have.length(1);
expect(users[0].get('email')).to.equal('tobi@fakeemail.com');
});
});
it('should be able to merge scopes with where', function() {
return this.ScopeMe.scope('lowAccess').update({ username: 'ruben' }, { where: { username: 'dan' } }).then(() => {
return this.ScopeMe.unscoped().findAll({ where: { username: 'ruben' } });
}).then(users => {
it('should be able to merge scopes with where', async function() {
await this.ScopeMe.scope('lowAccess').update({ username: 'ruben' }, { where: { username: 'dan' } });
const users = await this.ScopeMe.unscoped().findAll({ where: { username: 'ruben' } });
expect(users).to.have.length(1);
expect(users[0].get('email')).to.equal('dan@sequelizejs.com');
});
});
it('should work with empty where', function() {
return this.ScopeMe.scope('lowAccess').update({
it('should work with empty where', async function() {
await this.ScopeMe.scope('lowAccess').update({
username: 'ruby'
}).then(() => {
return this.ScopeMe.unscoped().findAll({ where: { username: 'ruby' } });
}).then(users => {
});
const users = await this.ScopeMe.unscoped().findAll({ where: { username: 'ruby' } });
expect(users).to.have.length(3);
users.forEach(user => {
expect(user.get('username')).to.equal('ruby');
......@@ -104,5 +94,4 @@ describe(Support.getTestDialectTeaser('Model'), () => {
});
});
});
});
});
......@@ -6,7 +6,7 @@ const chai = require('chai'),
DataTypes = require('../../../lib/data-types');
describe(Support.getTestDialectTeaser('Model'), () => {
beforeEach(function() {
beforeEach(async function() {
this.Payment = this.sequelize.define('Payment', {
amount: DataTypes.DECIMAL,
mood: {
......@@ -15,28 +15,28 @@ describe(Support.getTestDialectTeaser('Model'), () => {
}
});
return this.sequelize.sync({ force: true }).then(() => {
return this.Payment.bulkCreate([
await this.sequelize.sync({ force: true });
await this.Payment.bulkCreate([
{ amount: 5, mood: 'neutral' },
{ amount: -5, mood: 'neutral' },
{ amount: 10, mood: 'happy' },
{ amount: 90, mood: 'happy' }
]);
});
});
describe('sum', () => {
it('should sum without rows', function() {
return expect(this.Payment.sum('amount', { where: { mood: 'sad' } })).to.eventually.be.equal(0);
it('should sum without rows', async function() {
await expect(this.Payment.sum('amount', { where: { mood: 'sad' } })).to.eventually.be.equal(0);
});
it('should sum when is 0', function() {
return expect(this.Payment.sum('amount', { where: { mood: 'neutral' } })).to.eventually.be.equal(0);
it('should sum when is 0', async function() {
await expect(this.Payment.sum('amount', { where: { mood: 'neutral' } })).to.eventually.be.equal(0);
});
it('should sum', function() {
return expect(this.Payment.sum('amount', { where: { mood: 'happy' } })).to.eventually.be.equal(100);
it('should sum', async function() {
await expect(this.Payment.sum('amount', { where: { mood: 'happy' } })).to.eventually.be.equal(100);
});
});
});
......@@ -10,7 +10,7 @@ const _ = require('lodash');
describe(Support.getTestDialectTeaser('Model'), () => {
describe('update', () => {
beforeEach(function() {
beforeEach(async function() {
this.Account = this.sequelize.define('Account', {
ownerId: {
type: DataTypes.INTEGER,
......@@ -21,27 +21,29 @@ describe(Support.getTestDialectTeaser('Model'), () => {
type: DataTypes.STRING
}
});
return this.Account.sync({ force: true });
await this.Account.sync({ force: true });
});
it('should only update the passed fields', function() {
return this.Account
.create({ ownerId: 2 })
.then(account => this.Account.update({
it('should only update the passed fields', async function() {
const account = await this.Account
.create({ ownerId: 2 });
await this.Account.update({
name: Math.random().toString()
}, {
where: {
id: account.get('id')
}
}));
});
});
describe('skips update query', () => {
it('if no data to update', function() {
it('if no data to update', async function() {
const spy = sinon.spy();
return this.Account.create({ ownerId: 3 }).then(() => {
return this.Account.update({
await this.Account.create({ ownerId: 3 });
const result = await this.Account.update({
unknownField: 'haha'
}, {
where: {
......@@ -49,13 +51,12 @@ describe(Support.getTestDialectTeaser('Model'), () => {
},
logging: spy
});
}).then(result => {
expect(result[0]).to.equal(0);
expect(spy.called, 'Update query was issued when no data to update').to.be.false;
});
});
it('skips when timestamps disabled', function() {
it('skips when timestamps disabled', async function() {
const Model = this.sequelize.define('Model', {
ownerId: {
type: DataTypes.INTEGER,
......@@ -70,10 +71,10 @@ describe(Support.getTestDialectTeaser('Model'), () => {
});
const spy = sinon.spy();
return Model.sync({ force: true })
.then(() => Model.create({ ownerId: 3 }))
.then(() => {
return Model.update({
await Model.sync({ force: true });
await Model.create({ ownerId: 3 });
const result = await Model.update({
unknownField: 'haha'
}, {
where: {
......@@ -81,95 +82,84 @@ describe(Support.getTestDialectTeaser('Model'), () => {
},
logging: spy
});
})
.then(result => {
expect(result[0]).to.equal(0);
expect(spy.called, 'Update query was issued when no data to update').to.be.false;
});
});
});
it('changed should be false after reload', function() {
return this.Account.create({ ownerId: 2, name: 'foo' })
.then(account => {
account.name = 'bar';
expect(account.changed()[0]).to.equal('name');
return account.reload();
})
.then(account => {
it('changed should be false after reload', async function() {
const account0 = await this.Account.create({ ownerId: 2, name: 'foo' });
account0.name = 'bar';
expect(account0.changed()[0]).to.equal('name');
const account = await account0.reload();
expect(account.changed()).to.equal(false);
});
});
it('should ignore undefined values without throwing not null validation', function() {
it('should ignore undefined values without throwing not null validation', async function() {
const ownerId = 2;
return this.Account.create({
const account0 = await this.Account.create({
ownerId,
name: Math.random().toString()
}).then(account => {
return this.Account.update({
});
await this.Account.update({
name: Math.random().toString(),
ownerId: undefined
}, {
where: {
id: account.get('id')
id: account0.get('id')
}
});
}).then(() => {
return this.Account.findOne();
}).then(account => {
const account = await this.Account.findOne();
expect(account.ownerId).to.be.equal(ownerId);
});
});
if (_.get(current.dialect.supports, 'returnValues.returning')) {
it('should return the updated record', function() {
return this.Account.create({ ownerId: 2 }).then(account => {
return this.Account.update({ name: 'FooBar' }, {
it('should return the updated record', async function() {
const account = await this.Account.create({ ownerId: 2 });
const [, accounts] = await this.Account.update({ name: 'FooBar' }, {
where: {
id: account.get('id')
},
returning: true
}).then(([, accounts]) => {
});
const firstAcc = accounts[0];
expect(firstAcc.ownerId).to.be.equal(2);
expect(firstAcc.name).to.be.equal('FooBar');
});
});
});
}
if (current.dialect.supports['LIMIT ON UPDATE']) {
it('should only update one row', function() {
return this.Account.create({
it('should only update one row', async function() {
await this.Account.create({
ownerId: 2,
name: 'Account Name 1'
})
.then(() => {
return this.Account.create({
});
await this.Account.create({
ownerId: 2,
name: 'Account Name 2'
});
})
.then(() => {
return this.Account.create({
await this.Account.create({
ownerId: 2,
name: 'Account Name 3'
});
})
.then(() => {
const options = {
where: {
ownerId: 2
},
limit: 1
};
return this.Account.update({ name: 'New Name' }, options);
})
.then(account => {
const account = await this.Account.update({ name: 'New Name' }, options);
expect(account[0]).to.equal(1);
});
});
}
});
});
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!