123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509 |
- 'use strict';
- const chai = require('chai');
- const expect = chai.expect;
- const Support = require('../integration/support');
- const { DataTypes, sql } = require('@sequelize/core');
- const sinon = require('sinon');
- const dialect = Support.getTestDialect();
- describe(Support.getTestDialectTeaser('Smoke Tests'), () => {
- describe('getAssociations', () => {
- beforeEach(async function () {
- this.User = this.sequelize.define('User', { username: DataTypes.STRING });
- this.Task = this.sequelize.define('Task', {
- title: DataTypes.STRING,
- active: DataTypes.BOOLEAN,
- });
- this.User.belongsToMany(this.Task, { through: 'UserTasks' });
- this.Task.belongsToMany(this.User, { through: 'UserTasks' });
- await this.sequelize.sync({ force: true });
- const [john, task1, task2] = await Promise.all([
- this.User.create({ username: 'John' }),
- this.Task.create({ title: 'Get rich', active: true }),
- this.Task.create({ title: 'Die trying', active: false }),
- ]);
- this.tasks = [task1, task2];
- this.user = john;
- return john.setTasks([task1, task2]);
- });
- it('gets all associated objects with all fields', async function () {
- const john = await this.User.findOne({ where: { username: 'John' } });
- const tasks = await john.getTasks();
- for (const attr of Object.keys(tasks[0].getAttributes())) {
- expect(tasks[0]).to.have.property(attr);
- }
- });
- it('supports non primary key attributes for joins (custom through model)', async function () {
- const User = this.sequelize.define(
- 'User',
- {
- id: {
- type: DataTypes.UUID,
- allowNull: false,
- primaryKey: true,
- defaultValue: sql.uuidV4,
- field: 'user_id',
- },
- userSecondId: {
- type: DataTypes.UUID,
- allowNull: false,
- defaultValue: sql.uuidV4,
- field: 'user_second_id',
- },
- },
- {
- tableName: 'tbl_user',
- indexes: [
- {
- unique: true,
- fields: ['user_second_id'],
- },
- ],
- },
- );
- const Group = this.sequelize.define(
- 'Group',
- {
- id: {
- type: DataTypes.UUID,
- allowNull: false,
- primaryKey: true,
- defaultValue: sql.uuidV4,
- field: 'group_id',
- },
- groupSecondId: {
- type: DataTypes.UUID,
- allowNull: false,
- defaultValue: sql.uuidV4,
- field: 'group_second_id',
- },
- },
- {
- tableName: 'tbl_group',
- indexes: [
- {
- unique: true,
- fields: ['group_second_id'],
- },
- ],
- },
- );
- const User_has_Group = this.sequelize.define(
- 'User_has_Group',
- {},
- {
- tableName: 'tbl_user_has_group',
- indexes: [
- {
- unique: true,
- fields: ['UserUserSecondId', 'GroupGroupSecondId'],
- },
- ],
- },
- );
- User.belongsToMany(Group, { through: User_has_Group, sourceKey: 'userSecondId' });
- Group.belongsToMany(User, { through: User_has_Group, sourceKey: 'groupSecondId' });
- await this.sequelize.sync({ force: true });
- const [user1, user2, group1, group2] = await Promise.all([
- User.create(),
- User.create(),
- Group.create(),
- Group.create(),
- ]);
- await Promise.all([user1.addGroup(group1), user2.addGroup(group2)]);
- const [users, groups] = await Promise.all([
- User.findAll({
- where: {},
- include: [Group],
- }),
- Group.findAll({
- include: [User],
- }),
- ]);
- expect(users.length).to.equal(2);
- expect(users[0].Groups.length).to.equal(1);
- expect(users[1].Groups.length).to.equal(1);
- expect(users[0].Groups[0].User_has_Group.UserUserSecondId).to.be.ok;
- if (dialect === 'db2') {
- expect(users[0].Groups[0].User_has_Group.UserUserSecondId).to.deep.equal(
- users[0].userSecondId,
- );
- } else {
- expect(users[0].Groups[0].User_has_Group.UserUserSecondId).to.equal(users[0].userSecondId);
- }
- expect(users[0].Groups[0].User_has_Group.GroupGroupSecondId).to.be.ok;
- if (dialect === 'db2') {
- expect(users[0].Groups[0].User_has_Group.GroupGroupSecondId).to.deep.equal(
- users[0].Groups[0].groupSecondId,
- );
- } else {
- expect(users[0].Groups[0].User_has_Group.GroupGroupSecondId).to.equal(
- users[0].Groups[0].groupSecondId,
- );
- }
- expect(users[1].Groups[0].User_has_Group.UserUserSecondId).to.be.ok;
- if (dialect === 'db2') {
- expect(users[1].Groups[0].User_has_Group.UserUserSecondId).to.deep.equal(
- users[1].userSecondId,
- );
- } else {
- expect(users[1].Groups[0].User_has_Group.UserUserSecondId).to.equal(users[1].userSecondId);
- }
- expect(users[1].Groups[0].User_has_Group.GroupGroupSecondId).to.be.ok;
- if (dialect === 'db2') {
- expect(users[1].Groups[0].User_has_Group.GroupGroupSecondId).to.deep.equal(
- users[1].Groups[0].groupSecondId,
- );
- } else {
- expect(users[1].Groups[0].User_has_Group.GroupGroupSecondId).to.equal(
- users[1].Groups[0].groupSecondId,
- );
- }
- expect(groups.length).to.equal(2);
- expect(groups[0].Users.length).to.equal(1);
- expect(groups[1].Users.length).to.equal(1);
- expect(groups[0].Users[0].User_has_Group.GroupGroupSecondId).to.be.ok;
- if (dialect === 'db2') {
- expect(groups[0].Users[0].User_has_Group.GroupGroupSecondId).to.deep.equal(
- groups[0].groupSecondId,
- );
- } else {
- expect(groups[0].Users[0].User_has_Group.GroupGroupSecondId).to.equal(
- groups[0].groupSecondId,
- );
- }
- expect(groups[0].Users[0].User_has_Group.UserUserSecondId).to.be.ok;
- if (dialect === 'db2') {
- expect(groups[0].Users[0].User_has_Group.UserUserSecondId).to.deep.equal(
- groups[0].Users[0].userSecondId,
- );
- } else {
- expect(groups[0].Users[0].User_has_Group.UserUserSecondId).to.equal(
- groups[0].Users[0].userSecondId,
- );
- }
- expect(groups[1].Users[0].User_has_Group.GroupGroupSecondId).to.be.ok;
- if (dialect === 'db2') {
- expect(groups[1].Users[0].User_has_Group.GroupGroupSecondId).to.deep.equal(
- groups[1].groupSecondId,
- );
- } else {
- expect(groups[1].Users[0].User_has_Group.GroupGroupSecondId).to.equal(
- groups[1].groupSecondId,
- );
- }
- expect(groups[1].Users[0].User_has_Group.UserUserSecondId).to.be.ok;
- if (dialect === 'db2') {
- expect(groups[1].Users[0].User_has_Group.UserUserSecondId).to.deep.equal(
- groups[1].Users[0].userSecondId,
- );
- } else {
- expect(groups[1].Users[0].User_has_Group.UserUserSecondId).to.equal(
- groups[1].Users[0].userSecondId,
- );
- }
- });
- });
- describe('hasAssociations', () => {
- beforeEach(function () {
- this.Article = this.sequelize.define('Article', {
- pk: {
- type: DataTypes.INTEGER,
- autoIncrement: true,
- primaryKey: true,
- },
- title: DataTypes.STRING,
- });
- this.Label = this.sequelize.define('Label', {
- sk: {
- type: DataTypes.INTEGER,
- autoIncrement: true,
- primaryKey: true,
- },
- text: DataTypes.STRING,
- });
- this.ArticleLabel = this.sequelize.define('ArticleLabel');
- this.Article.belongsToMany(this.Label, { through: this.ArticleLabel });
- this.Label.belongsToMany(this.Article, { through: this.ArticleLabel });
- return this.sequelize.sync({ force: true });
- });
- it('answers false if only some labels have been assigned when passing a primary key instead of an object', async function () {
- const [article, label1, label2] = await Promise.all([
- this.Article.create({ title: 'Article' }),
- this.Label.create({ text: 'Awesomeness' }),
- this.Label.create({ text: 'Epicness' }),
- ]);
- await article.addLabels([label1]);
- const result = await article.hasLabels([
- label1[this.Label.primaryKeyAttribute],
- label2[this.Label.primaryKeyAttribute],
- ]);
- expect(result).to.be.false;
- });
- });
- describe('countAssociations', () => {
- beforeEach(async function () {
- this.User = this.sequelize.define('User', {
- username: DataTypes.STRING,
- });
- this.Task = this.sequelize.define('Task', {
- title: DataTypes.STRING,
- active: DataTypes.BOOLEAN,
- });
- this.UserTask = this.sequelize.define('UserTask', {
- id: {
- type: DataTypes.INTEGER,
- primaryKey: true,
- autoIncrement: true,
- },
- started: {
- type: DataTypes.BOOLEAN,
- defaultValue: false,
- },
- });
- this.User.belongsToMany(this.Task, { through: this.UserTask });
- this.Task.belongsToMany(this.User, { through: this.UserTask });
- await this.sequelize.sync({ force: true });
- const [john, task1, task2] = await Promise.all([
- this.User.create({ username: 'John' }),
- this.Task.create({ title: 'Get rich', active: true }),
- this.Task.create({ title: 'Die trying', active: false }),
- ]);
- this.tasks = [task1, task2];
- this.user = john;
- return john.setTasks([task1, task2]);
- });
- it('should count scoped through associations', async function () {
- this.User.belongsToMany(this.Task, {
- as: 'startedTasks',
- through: {
- model: this.UserTask,
- scope: {
- started: true,
- },
- },
- });
- for (let i = 0; i < 2; i++) {
- await this.user.addTask(await this.Task.create(), {
- through: { started: true },
- });
- }
- expect(await this.user.countStartedTasks({})).to.equal(2);
- });
- });
- describe('createAssociations', () => {
- it('creates a new associated object', async function () {
- const User = this.sequelize.define('User', { username: DataTypes.STRING });
- const Task = this.sequelize.define('Task', { title: DataTypes.STRING });
- User.belongsToMany(Task, { through: 'UserTasks' });
- Task.belongsToMany(User, { through: 'UserTasks' });
- await this.sequelize.sync({ force: true });
- const task = await Task.create({ title: 'task' });
- const createdUser = await task.createUser({ username: 'foo' });
- expect(createdUser).to.be.instanceof(User);
- expect(createdUser.username).to.equal('foo');
- const _users = await task.getUsers();
- expect(_users).to.have.length(1);
- });
- });
- describe('belongsTo and hasMany at once', () => {
- beforeEach(function () {
- this.A = this.sequelize.define('a', { name: DataTypes.STRING });
- this.B = this.sequelize.define('b', { name: DataTypes.STRING });
- });
- describe('target belongs to source', () => {
- beforeEach(function () {
- this.B.belongsTo(this.A, { as: 'relation1' });
- this.A.belongsToMany(this.B, { as: 'relation2', through: 'AB' });
- this.B.belongsToMany(this.A, { as: 'relation2', through: 'AB' });
- return this.sequelize.sync({ force: true });
- });
- it('correctly uses bId in A', async function () {
- const a1 = this.A.build({ name: 'a1' });
- const b1 = this.B.build({ name: 'b1' });
- await a1.save();
- await b1.save();
- await b1.setRelation1(a1);
- const b = await this.B.findOne({ where: { name: 'b1' } });
- expect(b.relation1Id).to.be.eq(a1.id);
- });
- });
- });
- });
- describe(Support.getTestDialectTeaser('Instance'), () => {
- before(function () {
- this.clock = sinon.useFakeTimers();
- });
- afterEach(function () {
- this.clock.reset();
- });
- after(function () {
- this.clock.restore();
- });
- beforeEach(async function () {
- this.User = this.sequelize.define('User', {
- username: { type: DataTypes.STRING },
- uuidv1: { type: DataTypes.UUID, defaultValue: sql.uuidV1 },
- uuidv4: { type: DataTypes.UUID, defaultValue: sql.uuidV4 },
- touchedAt: { type: DataTypes.DATE, defaultValue: DataTypes.NOW },
- aNumber: { type: DataTypes.INTEGER },
- bNumber: { type: DataTypes.INTEGER },
- aDate: { type: DataTypes.DATE },
- validateTest: {
- type: DataTypes.INTEGER,
- allowNull: true,
- validate: { isInt: true },
- },
- validateCustom: {
- type: DataTypes.STRING,
- allowNull: true,
- validate: { len: { msg: 'Length failed.', args: [1, 20] } },
- },
- dateAllowNullTrue: {
- type: DataTypes.DATE,
- allowNull: true,
- },
- isSuperUser: {
- type: DataTypes.BOOLEAN,
- defaultValue: false,
- },
- });
- await this.User.sync({ force: true });
- });
- describe('Escaping', () => {
- it('is done properly for special characters', async function () {
- // Ideally we should test more: "\0\n\r\b\t\\\'\"\x1a"
- // But this causes sqlite to fail and exits the entire test suite immediately
- const bio = `${dialect}'"\n`; // Need to add the dialect here so in case of failure I know what DB it failed for
- const u1 = await this.User.create({ username: bio });
- const u2 = await this.User.findByPk(u1.id);
- expect(u2.username).to.equal(bio);
- });
- });
- describe('values', () => {
- it('returns all values', async function () {
- const User = this.sequelize.define(
- 'UserHelper',
- {
- username: DataTypes.STRING,
- },
- { timestamps: false, logging: false },
- );
- await User.sync();
- const user = User.build({ username: 'foo' });
- expect(user.get({ plain: true })).to.deep.equal({ username: 'foo', id: null });
- });
- });
- describe(Support.getTestDialectTeaser('Model'), () => {
- before(function () {
- this.clock = sinon.useFakeTimers();
- });
- after(function () {
- this.clock.restore();
- });
- beforeEach(async function () {
- this.User = this.sequelize.define('User', {
- username: DataTypes.STRING,
- secretValue: DataTypes.STRING,
- data: DataTypes.STRING,
- intVal: DataTypes.INTEGER,
- theDate: DataTypes.DATE,
- aBool: DataTypes.BOOLEAN,
- });
- await this.User.sync({ force: true });
- });
- describe('save', () => {
- it('should map the correct fields when saving instance (#10589)', async function () {
- const User = this.sequelize.define('User', {
- id3: {
- field: 'id',
- type: DataTypes.INTEGER,
- primaryKey: true,
- },
- id: {
- field: 'id2',
- type: DataTypes.INTEGER,
- allowNull: false,
- },
- id2: {
- field: 'id3',
- type: DataTypes.INTEGER,
- allowNull: false,
- },
- });
- await this.sequelize.sync({ force: true });
- await User.create({ id3: 94, id: 87, id2: 943 });
- const user = await User.findByPk(94);
- await user.set('id2', 8877);
- await user.save({ id2: 8877 });
- expect((await User.findByPk(94)).id2).to.equal(8877);
- });
- });
- });
- });
|