Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DjellalAbdou committed Mar 4, 2024
1 parent 32fe9a7 commit db7d36a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
18 changes: 11 additions & 7 deletions packages/core/test/integration/model/upsert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1058,7 +1058,7 @@ describe('Model', () => {
onConflictUpdateWhere: {
intVal: { [Op.gte]: 15 },
},
isNewRecord: false,
isNewRecord: true,
};
const vars = beforeEach2(async () => {
const User = sequelize.define('users', {
Expand All @@ -1073,14 +1073,18 @@ describe('Model', () => {
return { User };
});

it('Should throw SequelizeEmptyResultError when conflict if where condition is not met', async () => {
it('Should keep old entry when conflict if where condition is not met', async () => {
const { User } = vars;
await expect(User.create(
{ name: 'Abdou', intVal: 20, uniqueId: 'Abdou-1' },
options,
)).to.be.rejectedWith(Sequelize.EmptyResultError);
try {
// sqlite won't throw an error
await User.create({ name: 'Abdou', intVal: 20, uniqueId: 'Abdou-1' }, options);
} catch (error) {
// postgres will throw a unique constraint error when on conflict update where condition is not met
expect(error).to.be.an.instanceOf(Sequelize.EmptyResultError);
}

const user = await User.findOne({ where: { uniqueId: 'Abdou-1' } });
expect(user.intVal).to.eq(10);
expect(user.intVal).to.eq(10);
});

it('Should update exisiting record on conflict if where condition is met', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ if (dialect.startsWith('postgres')) {
{
arguments: ['myTable', { name: 'foo' }, {}, { updateOnDuplicate: ['name'], upsertKeys: ['name'], onConflictUpdateWhere: { id: 10 } }],
expectation: {
query: 'INSERT INTO "myTable" ("name") VALUES ($sequelize_1) ON CONFLICT ("name") DO UPDATE SET "name"=EXCLUDED."name" WHERE "id" = 10;',
query: 'INSERT INTO "myTable" ("name") VALUES ($sequelize_1) ON CONFLICT ("name") DO UPDATE SET "name"=EXCLUDED."name" WHERE "myTable"."id" = 10;',
bind: { sequelize_1: 'foo' },
},
},
Expand Down Expand Up @@ -719,7 +719,7 @@ if (dialect.startsWith('postgres')) {
{ updateOnDuplicate: ['name'], upsertKeys: ['name'], onConflictUpdateWhere: { id: 10 } },
],
expectation:
'INSERT INTO "mySchema"."myTable" ("name") VALUES (\'foo\'),(\'bar\') ON CONFLICT ("name") DO UPDATE SET "name"=EXCLUDED."name" WHERE "id" = 10;',
'INSERT INTO "mySchema"."myTable" ("name") VALUES (\'foo\'),(\'bar\') ON CONFLICT ("name") DO UPDATE SET "name"=EXCLUDED."name" WHERE "myTable"."id" = 10;',
},

// Variants when quoteIdentifiers is false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ if (dialect === 'sqlite') {
{
arguments: ['myTable', { name: 'foo' }, {}, { updateOnDuplicate: ['name'], upsertKeys: ['name'], onConflictUpdateWhere: { id: 10 } }],
expectation: {
query: 'INSERT INTO `myTable` (`name`) VALUES ($sequelize_1) ON CONFLICT (`name`) DO UPDATE SET `name`=EXCLUDED.`name` WHERE `id` = 10;',
query: 'INSERT INTO `myTable` (`name`) VALUES ($sequelize_1) ON CONFLICT (`name`) DO UPDATE SET `name`=EXCLUDED.`name` WHERE `myTable`.`id` = 10;',
bind: { sequelize_1: 'foo' },
},
},
Expand Down Expand Up @@ -431,7 +431,7 @@ if (dialect === 'sqlite') {
{ updateOnDuplicate: ['name'], upsertKeys: ['name'], onConflictUpdateWhere: { id: 10 } },
],
expectation:
"INSERT INTO `myTable` (`name`) VALUES ('foo'),('bar') ON CONFLICT (`name`) DO UPDATE SET `name`=EXCLUDED.`name` WHERE `id` = 10;",
"INSERT INTO `myTable` (`name`) VALUES ('foo'),('bar') ON CONFLICT (`name`) DO UPDATE SET `name`=EXCLUDED.`name` WHERE `myTable`.`id` = 10;",
},
],

Expand Down

0 comments on commit db7d36a

Please sign in to comment.