Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: overhaul test suite of attribute(s)ToSQL #17121

Draft
wants to merge 28 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
0930b4b
meta: remove unused second parameter of attributeTypeToSql
WikiRik Feb 23, 2024
9498084
meta: remove Db2QueryInterface#createTable
WikiRik Feb 23, 2024
2d9e2fe
meta: add optional chaining options
WikiRik Feb 23, 2024
4101781
meta: inspect instead of String
WikiRik Feb 23, 2024
522f08c
fix: update type._binary
WikiRik Feb 25, 2024
1056d31
meta: removed dialect specific tests (apart from sqlite)
WikiRik Feb 25, 2024
efd97db
fix: properly convert ARRAY(ENUM)
WikiRik Feb 25, 2024
b69c31b
fix: split part of attributesToSQL to attributeToSQL
WikiRik Feb 25, 2024
be61a93
fix(sqlite): standardize addColumnQuery more
WikiRik Feb 25, 2024
073d4e0
fix: properly escape comments on db2 and mssql
WikiRik Feb 25, 2024
b84a0f0
Merge branch 'main' into WikiRik/attributes-to-sql-2
WikiRik Feb 25, 2024
2bb01f3
meta: push initial test suites
WikiRik Feb 25, 2024
6e0290e
meta(mssql): fix test assertions
WikiRik Feb 25, 2024
17e0cac
meta(db2): fixed assertions
WikiRik Feb 25, 2024
f2d5b0d
Update attribute-to-sql.test.ts
WikiRik Feb 25, 2024
98e16b1
meta: add TODO to unescape comment later
WikiRik Feb 26, 2024
8677746
meta: update Object.create(null)
WikiRik Feb 26, 2024
70eedc5
Merge branch 'main' into WikiRik/attributes-to-sql-2
WikiRik Mar 22, 2024
dcf799b
meta: fix prettier
WikiRik Mar 22, 2024
9950fd4
Merge remote-tracking branch 'origin/main' into WikiRik/attributes-to…
WikiRik Mar 30, 2024
8ac74c3
meta(sqlite): fix merge conflict
WikiRik Mar 30, 2024
7647a56
fix: overhaul test suite
WikiRik Mar 30, 2024
d108700
Merge branch 'main' into WikiRik/attributes-to-sql-2
WikiRik Mar 30, 2024
6db5793
meta(postgres): fix tests
WikiRik Mar 30, 2024
3327490
meta(postgres): add TODO for weird expectation
WikiRik Mar 30, 2024
0073494
Merge branch 'main' into WikiRik/attributes-to-sql-2
WikiRik Apr 12, 2024
6565ef2
meta: update to sqlite3
WikiRik Apr 12, 2024
d41f2fa
meta: fix test assertions
WikiRik Apr 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,6 +1,7 @@
import type { Nullish } from '@sequelize/utils';
import type { Literal } from '../expression-builders/literal.js';
import type { BindOrReplacements } from '../sequelize.js';
import type { TableOrModel } from './query-generator.types.js';

export interface AddLimitOffsetOptions {
limit?: number | Literal | Nullish;
Expand All @@ -10,7 +11,7 @@ export interface AddLimitOffsetOptions {

export interface AttributeToSqlOptions {
context: 'addColumn' | 'changeColumn' | 'createTable';
schema?: string;
table: string;
table?: TableOrModel;
withoutForeignKeyConstraints?: boolean;
foreignKey?: string;
}
2 changes: 1 addition & 1 deletion packages/core/src/abstract-dialect/query-generator.js
Expand Up @@ -1891,7 +1891,7 @@ export class AbstractQueryGenerator extends AbstractQueryGeneratorTypeScript {
// To capture output rows when there is a trigger on MSSQL DB
if (options.hasTrigger && this.dialect.supports.tmpTableTrigger) {
const tmpColumns = returnFields.map((field, i) => {
return `${field} ${attributeTypeToSql(returnTypes[i], { dialect: this.dialect })}`;
return `${field} ${attributeTypeToSql(returnTypes[i])}`;
});

tmpTable = `DECLARE @tmp TABLE (${tmpColumns.join(',')}); `;
Expand Down
Expand Up @@ -265,7 +265,12 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => {
});

const describedTable2 = await this.queryInterface.describeTable({ tableName: 'users' });
expect(describedTable2.level_id.comment).to.equal('FooBar');
if (dialect === 'mssql') {
// TODO: this should be unescaped by query.formatResult
expect(describedTable2.level_id.comment).to.equal("N'FooBar'");
} else {
expect(describedTable2.level_id.comment).to.equal('FooBar');
}
});
}
});
Expand Down
Expand Up @@ -170,9 +170,13 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => {
expect(enumVals.type).to.eql("ENUM('hello','world')");
}

if (['postgres', 'mysql', 'mssql'].includes(dialect)) {
if (['postgres', 'mysql'].includes(dialect)) {
expect(city.comment).to.equal('Users City');
expect(username.comment).to.equal(null);
} else if (dialect === 'mssql') {
// TODO: this should be unescaped by query.formatResult
expect(city.comment).to.equal("N'Users City'");
expect(username.comment).to.equal(null);
}
});

Expand Down
2 changes: 1 addition & 1 deletion packages/core/test/support.ts
Expand Up @@ -64,7 +64,7 @@ function withInlineCause(cb: () => any): () => void {

export function inlineErrorCause(error: unknown): string {
if (!(error instanceof Error)) {
return String(error);
return inspect(error);
}

let message = error.message;
Expand Down
98 changes: 0 additions & 98 deletions packages/core/test/unit/dialects/db2/query-generator.test.js
Expand Up @@ -15,104 +15,6 @@ const { createSequelizeInstance } = require('../../../support');
if (dialect === 'db2') {
describe('[DB2 Specific] QueryGenerator', () => {
const suites = {
attributesToSQL: [
{
arguments: [{ id: 'INTEGER' }],
expectation: { id: 'INTEGER' },
},
{
arguments: [{ id: 'INTEGER', foo: 'VARCHAR(255)' }],
expectation: { id: 'INTEGER', foo: 'VARCHAR(255)' },
},
{
arguments: [{ id: { type: 'INTEGER' } }],
expectation: { id: 'INTEGER' },
},
{
arguments: [{ id: { type: 'INTEGER', allowNull: false } }],
expectation: { id: 'INTEGER NOT NULL' },
},
{
arguments: [{ id: { type: 'INTEGER', allowNull: true } }],
expectation: { id: 'INTEGER' },
},
{
arguments: [{ id: { type: 'INTEGER', primaryKey: true, autoIncrement: true } }],
expectation: {
id: 'INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY(START WITH 1, INCREMENT BY 1) PRIMARY KEY',
},
},
{
arguments: [{ id: { type: 'INTEGER', defaultValue: 0 } }],
expectation: { id: 'INTEGER DEFAULT 0' },
},
{
title: 'Add column level comment',
arguments: [{ id: { type: 'INTEGER', comment: 'Test' } }],
expectation: { id: 'INTEGER COMMENT Test' },
},
{
arguments: [{ id: { type: 'INTEGER', unique: true } }],
expectation: { id: 'INTEGER UNIQUE' },
},
{
arguments: [{ id: { type: 'INTEGER', after: 'Bar' } }],
expectation: { id: 'INTEGER' },
},
// No Default Values allowed for certain types
// TODO: this test is broken. It adds the default value because the data type is a string, which can't have special behaviors.
// change type to an actual Sequelize DataType & re-enable
// {
// title: 'No Default value for DB2 BLOB allowed',
// arguments: [{ id: { type: 'BLOB', defaultValue: Buffer.from([]) } }],
// expectation: { id: 'BLOB DEFAULT ' },
// },
{
title: 'No Default value for DB2 TEXT allowed',
arguments: [{ id: { type: 'TEXT', defaultValue: 'abc' } }],
expectation: { id: 'TEXT' },
},
// New references style
{
arguments: [{ id: { type: 'INTEGER', references: { table: 'Bar' } } }],
expectation: { id: 'INTEGER REFERENCES "Bar" ("id")' },
},
{
arguments: [{ id: { type: 'INTEGER', references: { table: 'Bar', key: 'pk' } } }],
expectation: { id: 'INTEGER REFERENCES "Bar" ("pk")' },
},
{
arguments: [
{ id: { type: 'INTEGER', references: { table: 'Bar' }, onDelete: 'CASCADE' } },
],
expectation: { id: 'INTEGER REFERENCES "Bar" ("id") ON DELETE CASCADE' },
},
{
arguments: [
{ id: { type: 'INTEGER', references: { table: 'Bar' }, onUpdate: 'RESTRICT' } },
],
expectation: { id: 'INTEGER REFERENCES "Bar" ("id") ON UPDATE RESTRICT' },
},
{
arguments: [
{
id: {
type: 'INTEGER',
allowNull: false,
autoIncrement: true,
defaultValue: 1,
references: { table: 'Bar' },
onDelete: 'CASCADE',
onUpdate: 'RESTRICT',
},
},
],
expectation: {
id: 'INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY(START WITH 1, INCREMENT BY 1) DEFAULT 1 REFERENCES "Bar" ("id") ON DELETE CASCADE ON UPDATE RESTRICT',
},
},
],

selectQuery: [
{
arguments: ['myTable'],
Expand Down
104 changes: 0 additions & 104 deletions packages/core/test/unit/dialects/mariadb/query-generator.test.js
Expand Up @@ -15,110 +15,6 @@ const { createSequelizeInstance } = require('../../../support');
if (dialect === 'mariadb') {
describe('[MARIADB Specific] QueryGenerator', () => {
const suites = {
attributesToSQL: [
{
arguments: [{ id: 'INTEGER' }],
expectation: { id: 'INTEGER' },
},
{
arguments: [{ id: 'INTEGER', foo: 'VARCHAR(255)' }],
expectation: { id: 'INTEGER', foo: 'VARCHAR(255)' },
},
{
arguments: [{ id: { type: 'INTEGER' } }],
expectation: { id: 'INTEGER' },
},
{
arguments: [{ id: { type: 'INTEGER', allowNull: false } }],
expectation: { id: 'INTEGER NOT NULL' },
},
{
arguments: [{ id: { type: 'INTEGER', allowNull: true } }],
expectation: { id: 'INTEGER' },
},
{
arguments: [{ id: { type: 'INTEGER', primaryKey: true, autoIncrement: true } }],
expectation: { id: 'INTEGER auto_increment PRIMARY KEY' },
},
{
arguments: [{ id: { type: 'INTEGER', defaultValue: 0 } }],
expectation: { id: 'INTEGER DEFAULT 0' },
},
{
title: 'Add column level comment',
arguments: [{ id: { type: 'INTEGER', comment: 'Test' } }],
expectation: { id: "INTEGER COMMENT 'Test'" },
},
{
arguments: [{ id: { type: 'INTEGER', unique: true } }],
expectation: { id: 'INTEGER UNIQUE' },
},
{
arguments: [{ id: { type: 'INTEGER', after: 'Bar' } }],
expectation: { id: 'INTEGER AFTER `Bar`' },
},
// No Default Values allowed for certain types
{
title: 'No Default value for MariaDB BLOB allowed',
arguments: [{ id: { type: 'BLOB', defaultValue: [] } }],
expectation: { id: 'BLOB' },
},
{
title: 'No Default value for MariaDB TEXT allowed',
arguments: [{ id: { type: 'TEXT', defaultValue: [] } }],
expectation: { id: 'TEXT' },
},
{
title: 'No Default value for MariaDB GEOMETRY allowed',
arguments: [{ id: { type: 'GEOMETRY', defaultValue: [] } }],
expectation: { id: 'GEOMETRY' },
},
{
title: 'No Default value for MariaDB JSON allowed',
arguments: [{ id: { type: 'JSON', defaultValue: [] } }],
expectation: { id: 'JSON' },
},
// New references style
{
arguments: [{ id: { type: 'INTEGER', references: { table: 'Bar' } } }],
expectation: { id: 'INTEGER REFERENCES `Bar` (`id`)' },
},
{
arguments: [{ id: { type: 'INTEGER', references: { table: 'Bar', key: 'pk' } } }],
expectation: { id: 'INTEGER REFERENCES `Bar` (`pk`)' },
},
{
arguments: [
{ id: { type: 'INTEGER', references: { table: 'Bar' }, onDelete: 'CASCADE' } },
],
expectation: { id: 'INTEGER REFERENCES `Bar` (`id`) ON DELETE CASCADE' },
},
{
arguments: [
{ id: { type: 'INTEGER', references: { table: 'Bar' }, onUpdate: 'RESTRICT' } },
],
expectation: { id: 'INTEGER REFERENCES `Bar` (`id`) ON UPDATE RESTRICT' },
},
{
arguments: [
{
id: {
type: 'INTEGER',
allowNull: false,
autoIncrement: true,
defaultValue: 1,
references: { table: 'Bar' },
onDelete: 'CASCADE',
onUpdate: 'RESTRICT',
},
},
],
expectation: {
id: 'INTEGER NOT NULL auto_increment DEFAULT 1 REFERENCES `Bar` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT',
},
},
],

selectQuery: [
{
arguments: ['myTable'],
Expand Down
104 changes: 0 additions & 104 deletions packages/core/test/unit/dialects/mysql/query-generator.test.js
Expand Up @@ -15,110 +15,6 @@ const { createSequelizeInstance } = require('../../../support');
if (dialect === 'mysql') {
describe('[MYSQL Specific] QueryGenerator', () => {
const suites = {
attributesToSQL: [
{
arguments: [{ id: 'INTEGER' }],
expectation: { id: 'INTEGER' },
},
{
arguments: [{ id: 'INTEGER', foo: 'VARCHAR(255)' }],
expectation: { id: 'INTEGER', foo: 'VARCHAR(255)' },
},
{
arguments: [{ id: { type: 'INTEGER' } }],
expectation: { id: 'INTEGER' },
},
{
arguments: [{ id: { type: 'INTEGER', allowNull: false } }],
expectation: { id: 'INTEGER NOT NULL' },
},
{
arguments: [{ id: { type: 'INTEGER', allowNull: true } }],
expectation: { id: 'INTEGER' },
},
{
arguments: [{ id: { type: 'INTEGER', primaryKey: true, autoIncrement: true } }],
expectation: { id: 'INTEGER auto_increment PRIMARY KEY' },
},
{
arguments: [{ id: { type: 'INTEGER', defaultValue: 0 } }],
expectation: { id: 'INTEGER DEFAULT 0' },
},
{
title: 'Add column level comment',
arguments: [{ id: { type: 'INTEGER', comment: 'Test' } }],
expectation: { id: "INTEGER COMMENT 'Test'" },
},
{
arguments: [{ id: { type: 'INTEGER', unique: true } }],
expectation: { id: 'INTEGER UNIQUE' },
},
{
arguments: [{ id: { type: 'INTEGER', after: 'Bar' } }],
expectation: { id: 'INTEGER AFTER `Bar`' },
},
// No Default Values allowed for certain types
{
title: 'No Default value for MySQL BLOB allowed',
arguments: [{ id: { type: 'BLOB', defaultValue: [] } }],
expectation: { id: 'BLOB' },
},
{
title: 'No Default value for MySQL TEXT allowed',
arguments: [{ id: { type: 'TEXT', defaultValue: [] } }],
expectation: { id: 'TEXT' },
},
{
title: 'No Default value for MySQL GEOMETRY allowed',
arguments: [{ id: { type: 'GEOMETRY', defaultValue: [] } }],
expectation: { id: 'GEOMETRY' },
},
{
title: 'No Default value for MySQL JSON allowed',
arguments: [{ id: { type: 'JSON', defaultValue: [] } }],
expectation: { id: 'JSON' },
},
// New references style
{
arguments: [{ id: { type: 'INTEGER', references: { table: 'Bar' } } }],
expectation: { id: 'INTEGER REFERENCES `Bar` (`id`)' },
},
{
arguments: [{ id: { type: 'INTEGER', references: { table: 'Bar', key: 'pk' } } }],
expectation: { id: 'INTEGER REFERENCES `Bar` (`pk`)' },
},
{
arguments: [
{ id: { type: 'INTEGER', references: { table: 'Bar' }, onDelete: 'CASCADE' } },
],
expectation: { id: 'INTEGER REFERENCES `Bar` (`id`) ON DELETE CASCADE' },
},
{
arguments: [
{ id: { type: 'INTEGER', references: { table: 'Bar' }, onUpdate: 'RESTRICT' } },
],
expectation: { id: 'INTEGER REFERENCES `Bar` (`id`) ON UPDATE RESTRICT' },
},
{
arguments: [
{
id: {
type: 'INTEGER',
allowNull: false,
autoIncrement: true,
defaultValue: 1,
references: { table: 'Bar' },
onDelete: 'CASCADE',
onUpdate: 'RESTRICT',
},
},
],
expectation: {
id: 'INTEGER NOT NULL auto_increment DEFAULT 1 REFERENCES `Bar` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT',
},
},
],

selectQuery: [
{
arguments: ['myTable'],
Expand Down