Skip to content

Commit

Permalink
test: reorganize tests (#1051)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 authored Apr 3, 2024
1 parent 38fa3c3 commit 730c237
Show file tree
Hide file tree
Showing 81 changed files with 906 additions and 667 deletions.
1 change: 1 addition & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ jobs:

- name: Test
run: pnpm run test:coverage
continue-on-error: true

- name: Upload Coverage
if: ${{ matrix.branch != 'main' }}
Expand Down
10 changes: 5 additions & 5 deletions test/db.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ vi.mock('pg', () => ({
Client: vi.fn().mockImplementation(() => hoisted.client),
}));

describe('lib/db', () => {
describe('db', () => {
const log: Logger = {
debug: vi.fn(),
error: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
};

describe('.constructor(connection)', () => {
describe('constructor', () => {
let db: DBConnection;

afterEach(() => {
Expand All @@ -33,7 +33,7 @@ describe('lib/db', () => {
}
});

it('pg.Client should be called with connection string', () => {
it('should call pg.Client with connection string', () => {
db = Db('connection_string');

expect(Client).toBeCalledWith('connection_string');
Expand All @@ -52,7 +52,7 @@ describe('lib/db', () => {
});
});

describe('.query(query)', () => {
describe('query', () => {
let db: DBConnection;

beforeEach(() => {
Expand Down Expand Up @@ -118,7 +118,7 @@ describe('lib/db', () => {
});
});

describe('.close()', () => {
describe('close', () => {
it('should call client.end', async () => {
// @ts-expect-error: JS test
const db = Db();
Expand Down
111 changes: 0 additions & 111 deletions test/indexes.spec.ts

This file was deleted.

12 changes: 6 additions & 6 deletions test/migration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const migrationsTable = 'pgmigrations';
const actionsCallback = require(`./${callbackMigration}`);
const actionsPromise = require(`./${promiseMigration}`);

describe('lib/migration', () => {
describe('migration', () => {
const dbMock = {} as DBConnection;

const logger: Logger = {
Expand Down Expand Up @@ -46,7 +46,7 @@ describe('lib/migration', () => {
});

describe('self.applyUp', () => {
it('normal operations: db.query should be called', async () => {
it('should call db.query on normal operations', async () => {
const migration = new Migration(
dbMock,
callbackMigration,
Expand All @@ -61,7 +61,7 @@ describe('lib/migration', () => {
expect(queryMock).toHaveBeenCalled();
});

it('normal operations: db.query should be called when returning promise', async () => {
it('should call db.query when returning promise on normal operations', async () => {
const migration = new Migration(
dbMock,
promiseMigration,
Expand All @@ -76,7 +76,7 @@ describe('lib/migration', () => {
expect(queryMock).toHaveBeenCalled();
});

it('--dry-run option: db.query should not be called', async () => {
it('should not call db.query on --dry-run', async () => {
const migration = new Migration(
dbMock,
callbackMigration,
Expand Down Expand Up @@ -139,7 +139,7 @@ describe('lib/migration', () => {
});

describe('self.applyDown', () => {
it('normal operations: db.query should be called', async () => {
it('should call db.query on normal operations', async () => {
const migration = new Migration(
dbMock,
callbackMigration,
Expand All @@ -154,7 +154,7 @@ describe('lib/migration', () => {
expect(queryMock).toHaveBeenCalled();
});

it('--dry-run option: db.query should not be called', async () => {
it('should not call db.query on --dry-run', async () => {
const migration = new Migration(
dbMock,
callbackMigration,
Expand Down
2 changes: 1 addition & 1 deletion test/operations/columns/addColumns.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect, it } from 'vitest';
import { PgType } from '../../../src';
import { addColumns } from '../../../src/operations/tables';
import { options1 } from '../../utils';
import { options1 } from '../../presetMigrationOptions';

describe('operations', () => {
describe('columns', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/operations/columns/alterColumn.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest';
import { alterColumn } from '../../../src/operations/tables';
import { options1 } from '../../utils';
import { options1 } from '../../presetMigrationOptions';

describe('operations', () => {
describe('columns', () => {
Expand Down
32 changes: 31 additions & 1 deletion test/operations/columns/dropColumns.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest';
import { dropColumns } from '../../../src/operations/tables';
import { options1 } from '../../utils';
import { options1, options2 } from '../../presetMigrationOptions';

describe('operations', () => {
describe('columns', () => {
Expand Down Expand Up @@ -53,6 +53,36 @@ describe('operations', () => {
expect(statement).toBe(`ALTER TABLE "myschema"."distributors"
DROP "address";`);
});

it.each([
// should drop multiple columns
[
'should drop multiple columns 1',
options1,
['myTableName', ['colC1', 'colC2']],
`ALTER TABLE "myTableName"
DROP "colC1",
DROP "colC2";`,
],
[
'should drop multiple columns 2',
options2,
['myTableName', ['colC1', 'colC2']],
`ALTER TABLE "my_table_name"
DROP "col_c1",
DROP "col_c2";`,
],
] as const)('%s', (_, optionPreset, [tableName, columns], expected) => {
const dropColumnsFn = dropColumns(optionPreset);
const statement = dropColumnsFn(
tableName,
// @ts-expect-error: ignore readonly
columns
);

expect(statement).toBeTypeOf('string');
expect(statement).toBe(expected);
});
});
});
});
2 changes: 1 addition & 1 deletion test/operations/columns/renameColumn.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest';
import { renameColumn } from '../../../src/operations/tables';
import { options1 } from '../../utils';
import { options1 } from '../../presetMigrationOptions';

describe('operations', () => {
describe('columns', () => {
Expand Down
84 changes: 83 additions & 1 deletion test/operations/constraints/addConstraint.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest';
import { addConstraint } from '../../../src/operations/tables';
import { options1 } from '../../utils';
import { options1, options2 } from '../../presetMigrationOptions';

describe('operations', () => {
describe('constraints', () => {
Expand Down Expand Up @@ -51,6 +51,88 @@ describe('operations', () => {
);
});

it.each([
// should works with strings
[
'should works with strings 1',
options1,
['myTableName', 'myConstraintName', 'CHECK name IS NOT NULL'],
`ALTER TABLE "myTableName"
ADD CONSTRAINT "myConstraintName" CHECK name IS NOT NULL;`,
],
[
'should works with strings 2',
options2,
['myTableName', 'myConstraintName', 'CHECK name IS NOT NULL'],
`ALTER TABLE "my_table_name"
ADD CONSTRAINT "my_constraint_name" CHECK name IS NOT NULL;`,
],
// should not add constraint name if not defined
[
'should not add constraint name if not defined 1',
options1,
['myTableName', null, 'CHECK name IS NOT NULL'],
`ALTER TABLE "myTableName"
ADD CHECK name IS NOT NULL;`,
],
[
'should not add constraint name if not defined 2',
options2,
['myTableName', null, 'CHECK name IS NOT NULL'],
`ALTER TABLE "my_table_name"
ADD CHECK name IS NOT NULL;`,
],
// should create comments
[
'should create comments 1',
options1,
[
'myTableName',
'myConstraintName',
{
primaryKey: 'colA',
comment: 'this is an important primary key',
},
],
`ALTER TABLE "myTableName"
ADD CONSTRAINT "myConstraintName" PRIMARY KEY ("colA");
COMMENT ON CONSTRAINT "myConstraintName" ON "myTableName" IS $pga$this is an important primary key$pga$;`,
],
[
'should create comments 2',
options2,
[
'myTableName',
'myConstraintName',
{
primaryKey: 'colA',
comment: 'this is an important primary key',
},
],
`ALTER TABLE "my_table_name"
ADD CONSTRAINT "my_constraint_name" PRIMARY KEY ("col_a");
COMMENT ON CONSTRAINT "my_constraint_name" ON "my_table_name" IS $pga$this is an important primary key$pga$;`,
],
] as const)(
'%s',
(
_,
optionPreset,
[tableName, constraintName, expression],
expected
) => {
const addConstraintFn = addConstraint(optionPreset);
const statement = addConstraintFn(
tableName,
constraintName,
expression
);

expect(statement).toBeTypeOf('string');
expect(statement).toBe(expected);
}
);

describe('reverse', () => {
it('should contain a reverse function', () => {
expect(addConstraintFn.reverse).toBeTypeOf('function');
Expand Down
2 changes: 1 addition & 1 deletion test/operations/constraints/dropConstraint.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest';
import { dropConstraint } from '../../../src/operations/tables';
import { options1 } from '../../utils';
import { options1 } from '../../presetMigrationOptions';

describe('operations', () => {
describe('constraints', () => {
Expand Down
Loading

0 comments on commit 730c237

Please sign in to comment.