Skip to content

Commit

Permalink
Test for changeColumn on composite primary keys
Browse files Browse the repository at this point in the history
  • Loading branch information
Hornwitser committed Oct 30, 2024
1 parent 2b38801 commit 7415f47
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 52 deletions.
41 changes: 0 additions & 41 deletions src/sscce-sequelize-6.ts

This file was deleted.

35 changes: 24 additions & 11 deletions src/sscce-sequelize-7.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CreationOptional, DataTypes, InferAttributes, InferCreationAttributes, Model } from '@sequelize/core';
import { Attribute, NotNull } from '@sequelize/core/decorators-legacy';
import { Attribute, PrimaryKey } from '@sequelize/core/decorators-legacy';
import { createSequelize7Instance } from '../dev/create-sequelize-instance';
import { expect } from 'chai';
import sinon from 'sinon';
Expand All @@ -23,21 +23,34 @@ export async function run() {
});

class Foo extends Model<InferAttributes<Foo>, InferCreationAttributes<Foo>> {
declare id: CreationOptional<number>;

@Attribute(DataTypes.TEXT)
@NotNull
@PrimaryKey
declare name: string;

@Attribute(DataTypes.TEXT)
@PrimaryKey
declare rank: string;

@Attribute(DataTypes.TEXT)
declare role: CreationOptional<string>;
}

sequelize.addModels([Foo]);

// You can use sinon and chai assertions directly in your SSCCE.
const spy = sinon.spy();
sequelize.afterBulkSync(() => spy());
await sequelize.sync({ force: true });
expect(spy).to.have.been.called;

console.log(await Foo.create({ name: 'TS foo' }));
expect(await Foo.count()).to.equal(1);
// Inserting two entries with different name and rank succeeds
await expect(Foo.create({ name: "fish", rank: "novice" })).to.eventually.be.fulfilled;
await expect(Foo.create({ name: "fish", rank: "expert" })).to.eventually.be.fulfilled;

// Inserting two entries with same name and rank fails
await expect(Foo.create({ name: "cat", rank: "expert" })).to.eventually.be.fulfilled;
await expect(Foo.create({ name: "cat", rank: "expert" })).to.eventually.be.rejected;

// Altering an unrelated column should not change the above behaviour
await Foo.truncate();
await sequelize.queryInterface.changeColumn("Foos", "role", { type: DataTypes.INTEGER });
await expect(Foo.create({ name: "bear", rank: "novice" })).to.eventually.be.fulfilled;
await expect(Foo.create({ name: "bear", rank: "expert" })).to.eventually.be.fulfilled;
await expect(Foo.create({ name: "dog", rank: "expert" })).to.eventually.be.fulfilled;
await expect(Foo.create({ name: "dog", rank: "expert" })).to.eventually.be.rejected;
}

0 comments on commit 7415f47

Please sign in to comment.