Skip to content

Commit

Permalink
test: simplify some tests a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
B4nan committed Dec 27, 2023
1 parent b523ae4 commit d347b3a
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Entity, Index, MikroORM, PrimaryKey, Property } from '@mikro-orm/core';
import { PostgreSqlDriver, WeightedFullTextValue, SchemaGenerator, FullTextType } from '@mikro-orm/postgresql';
import { FullTextType, PostgreSqlDriver, WeightedFullTextValue } from '@mikro-orm/postgresql';
import { mockLogger } from '../../helpers';

const createWeightedValue = (book: Book): WeightedFullTextValue => ({ A: book.title!, B: book.description! });
Expand Down Expand Up @@ -41,18 +41,16 @@ export class Book {
describe('full text search tsvector in postgres', () => {

let orm: MikroORM<PostgreSqlDriver>;
let generator: SchemaGenerator;

beforeAll(async () => {
orm = await MikroORM.init({
entities: [Book],
dbName: `mikro_orm_test_tsvector`,
driver: PostgreSqlDriver,
});
generator = orm.schema;
await generator.ensureDatabase();
await generator.execute('drop table if exists book');
await generator.createSchema();
await orm.schema.ensureDatabase();
await orm.schema.execute('drop table if exists book');
await orm.schema.createSchema();
});

beforeEach(() => orm.schema.clearDatabase());
Expand Down
9 changes: 4 additions & 5 deletions tests/features/migrations/Migrator.postgres.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,10 @@ describe('Migrator (postgres)', () => {
extensions: [Migrator],
});

const schemaGenerator = orm.schema;
await schemaGenerator.refreshDatabase();
await schemaGenerator.execute('alter table "custom"."book2" add column "foo" varchar null default \'lol\';');
await schemaGenerator.execute('alter table "custom"."book2" alter column "double" type numeric using ("double"::numeric);');
await schemaGenerator.execute('alter table "custom"."test2" add column "path" polygon null default null;');
await orm.schema.refreshDatabase();
await orm.schema.execute('alter table "custom"."book2" add column "foo" varchar null default \'lol\';');
await orm.schema.execute('alter table "custom"."book2" alter column "double" type numeric using ("double"::numeric);');
await orm.schema.execute('alter table "custom"."test2" add column "path" polygon null default null;');
await remove(process.cwd() + '/temp/migrations-456');
});
beforeEach(() => orm.config.resetServiceCache());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'reflect-metadata';
import type { Constructor } from '@mikro-orm/core';
import { Entity, PrimaryKey, t } from '@mikro-orm/core';
import { MikroORM, SchemaGenerator } from '@mikro-orm/mysql';
import { MikroORM } from '@mikro-orm/mysql';

@Entity({ tableName: 'user' })
class User0 {
Expand Down Expand Up @@ -57,17 +57,14 @@ class User5 {
describe('changing PK column type [mysql] (GH 1480)', () => {

let orm: MikroORM;
let generator: SchemaGenerator;

beforeAll(async () => {
orm = await MikroORM.init({
entities: [User0],
dbName: 'mikro_orm_test_gh_1480',
port: 3308,
});
generator = orm.schema;
await generator.ensureDatabase();
await generator.dropSchema();
await orm.schema.ensureDatabase();
await orm.schema.dropSchema();
});

afterAll(() => orm.close(true));
Expand Down
30 changes: 13 additions & 17 deletions tests/features/schema-generator/length-diffing.mysql.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Entity, MikroORM, PrimaryKey, Property, t } from '@mikro-orm/core';
import type { SchemaGenerator } from '@mikro-orm/postgresql';
import { MySqlDriver } from '@mikro-orm/mysql';
import { Entity, PrimaryKey, Property, t } from '@mikro-orm/core';
import { MikroORM } from '@mikro-orm/mysql';

@Entity({ tableName: 'book' })
export class Book0 {
Expand Down Expand Up @@ -89,47 +88,44 @@ export class Book4 {

describe('length diffing in mysql', () => {

let orm: MikroORM<MySqlDriver>;
let generator: SchemaGenerator;
let orm: MikroORM;

beforeAll(async () => {
orm = await MikroORM.init({
entities: [Book0],
dbName: `mikro_orm_test_length_diffing`,
driver: MySqlDriver,
port: 3308,
});
generator = orm.schema;
await generator.ensureDatabase();
await generator.execute('drop table if exists book');
await generator.createSchema();
await orm.schema.ensureDatabase();
await orm.schema.execute('drop table if exists book');
await orm.schema.createSchema();
});

afterAll(() => orm.close(true));

test('schema generator updates column types when length changes (varchar, decimal, ...)', async () => {
orm.getMetadata().reset('Book0');
await orm.discoverEntity(Book1);
const diff1 = await generator.getUpdateSchemaSQL({ wrap: false });
const diff1 = await orm.schema.getUpdateSchemaSQL({ wrap: false });
expect(diff1).toMatchSnapshot();
await generator.execute(diff1);
await orm.schema.execute(diff1);

orm.getMetadata().reset('Book1');
await orm.discoverEntity(Book2);
const diff2 = await generator.getUpdateSchemaSQL({ wrap: false });
const diff2 = await orm.schema.getUpdateSchemaSQL({ wrap: false });
expect(diff2).toMatchSnapshot();
await generator.execute(diff2);
await orm.schema.execute(diff2);

orm.getMetadata().reset('Book2');
await orm.discoverEntity(Book3);
const diff3 = await generator.getUpdateSchemaSQL({ wrap: false });
const diff3 = await orm.schema.getUpdateSchemaSQL({ wrap: false });
expect(diff3).toMatchSnapshot();
await generator.execute(diff3);
await orm.schema.execute(diff3);

orm.getMetadata().reset('Book3');
await orm.discoverEntity(Book4);

await expect(generator.getUpdateSchemaSQL({ wrap: false })).resolves.toBe('');
await expect(orm.schema.getUpdateSchemaSQL({ wrap: false })).resolves.toBe('');
});

});
3 changes: 1 addition & 2 deletions tests/features/unit-of-work/GH4426.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ beforeAll(async () => {
dbName: ':memory:',
});

const generator = orm.schema;
await generator.createSchema();
await orm.schema.createSchema();
});

afterAll(async () => {
Expand Down

0 comments on commit d347b3a

Please sign in to comment.