Skip to content

Commit

Permalink
test: add test for #6950 (#7272)
Browse files Browse the repository at this point in the history
This commit adds tests to demonstrate the bug found in #6950
  • Loading branch information
vegerot authored Feb 8, 2021
1 parent c0899eb commit 0a5100d
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 0 deletions.
28 changes: 28 additions & 0 deletions test/github-issues/6950/entity/post_with_null_1.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Category } from "../../../functional/migrations/generate-command/entity";
import { Entity, BaseEntity, PrimaryGeneratedColumn, Column, ManyToMany, JoinTable } from "../../../../src";
import "reflect-metadata";

@Entity("post_test", { schema: "public" })
export class Post extends BaseEntity {

@PrimaryGeneratedColumn()
id: number;

@Column()
title: string;

@Column({
default: "This is default text."
})
text: string;

@Column({
default: null,
})
comments: string;

@ManyToMany(type => Category)
@JoinTable()
categories: Category[];

}
29 changes: 29 additions & 0 deletions test/github-issues/6950/entity/post_with_null_2.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Category } from "../../../functional/migrations/generate-command/entity";
import { Entity, BaseEntity, PrimaryGeneratedColumn, Column, ManyToMany, JoinTable } from "../../../../src";

@Entity("post_test", { schema: "public" })
export class Post extends BaseEntity {

@PrimaryGeneratedColumn()
id: number;

@Column()
title: string;

@Column({
default: "This is default text."
})
text: string;

@Column({
nullable: true,
default: null,
type: 'text'
})
comments: string | null;

@ManyToMany(type => Category)
@JoinTable()
categories: Category[];

}
71 changes: 71 additions & 0 deletions test/github-issues/6950/issue-6950.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import "reflect-metadata";
import { Connection } from "../../../src";

import { Category } from "../../functional/migrations/generate-command/entity";
import { createTestingConnections, closeTestingConnections } from "../../utils/test-utils";

import { Post as Post1 } from './entity/post_with_null_1.entity'
import { Post as Post2 } from './entity/post_with_null_1.entity'

describe("github issues > #6950 postgres: Inappropiate migration generated for 'default: null'", () => {
describe('null default', () => {
let connections: Connection[];
before(async () => connections = await createTestingConnections({
migrations: [],
enabledDrivers: ["postgres", "cockroachdb", "mysql"],
schemaCreate: false,
dropSchema: true,
entities: [Post1, Category],
schema: "public",
}));
// beforeEach(() => reloadTestingDatabases(connections));
after(() => closeTestingConnections(connections));

it("can recognize model changes", () => Promise.all(connections.map(async connection => {
const sqlInMemory = await connection.driver.createSchemaBuilder().log();
sqlInMemory.upQueries.length.should.be.greaterThan(0);
sqlInMemory.downQueries.length.should.be.greaterThan(0);
})));

it.skip("does not generate when no model changes", () => Promise.all(connections.map(async connection => {
await connection.driver.createSchemaBuilder().build();

const sqlInMemory = await connection.driver.createSchemaBuilder().log();

sqlInMemory.upQueries.length.should.be.equal(0);
sqlInMemory.downQueries.length.should.be.equal(0);

})));

})
describe('null default and nullable ', () => {
let connections: Connection[];
before(async () => connections = await createTestingConnections({
migrations: [],
enabledDrivers: ["postgres", "cockroachdb", "mysql"],
schemaCreate: false,
dropSchema: true,
entities: [Post2, Category],
schema: "public",
}));
// beforeEach(() => reloadTestingDatabases(connections));
after(() => closeTestingConnections(connections));

it("can recognize model changes", () => Promise.all(connections.map(async connection => {
const sqlInMemory = await connection.driver.createSchemaBuilder().log();
sqlInMemory.upQueries.length.should.be.greaterThan(0);
sqlInMemory.downQueries.length.should.be.greaterThan(0);
})));

it.skip("does not generate when no model changes", () => Promise.all(connections.map(async connection => {
await connection.driver.createSchemaBuilder().build();

const sqlInMemory = await connection.driver.createSchemaBuilder().log();

sqlInMemory.upQueries.length.should.be.equal(0);
sqlInMemory.downQueries.length.should.be.equal(0);

})));

})
});

0 comments on commit 0a5100d

Please sign in to comment.