Skip to content

Commit

Permalink
introduce vitest-fixture (#219)
Browse files Browse the repository at this point in the history
* fixture: fetchExtensionItemIds

* fixture

* fixture: testWith

* vitest-fixture

* fix: undo skipped test

* minor: fix lint, tsc and review issues
  • Loading branch information
larsthorup authored Jul 15, 2022
1 parent 6a74c64 commit 8a8997e
Show file tree
Hide file tree
Showing 18 changed files with 19,801 additions and 4,614 deletions.
15,404 changes: 15,404 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"typescript": "4.7.4",
"vitepress": "^1.0.0-alpha.4",
"vitest": "0.17.0",
"vitest-fixture": "^0.4.0",
"vue": "^3.2.37"
},
"np": {}
Expand Down
13 changes: 3 additions & 10 deletions src/fetchExtensionItemIds.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import { expect, it } from 'vitest';
import { describe, expect } from 'vitest';

import fetchExtensionItemIds from './fetchExtensionItemIds';
import { describe } from './tests/fixture';
import useSchema from './tests/useSchema';
import useTestKnex from './tests/useTestKnex';
import { test } from './tests/useSchema';

describe('fetchExtensionItemIds', () => {
const [getKnex] = useTestKnex();
useSchema(getKnex, 'test');

// NOTE: be aware that this test depends on specifics of certain Postgres extensions.
// If it fails there is a chance that it's because the extensions themselves have changed,
// not necessarily the test.
it('should fetch extension item ids', async () => {
const db = getKnex();

test('it should fetch extension item ids', async ({ knex: [db] }) => {
await db.raw('create extension if not exists pg_trgm');
await db.raw('create extension if not exists pg_stat_statements');
await db.raw('create extension if not exists citext');
Expand Down
21 changes: 9 additions & 12 deletions src/kinds/extractCompositeType.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import * as R from 'ramda';
import { describe, expect } from 'vitest';

import { describe, expect, it } from '../tests/fixture';
import useSchema from '../tests/useSchema';
import useTestKnex from '../tests/useTestKnex';
import { test } from '../tests/useSchema';
import extractCompositeType, {
CompositeTypeAttribute,
CompositeTypeDetails,
Expand All @@ -20,11 +19,9 @@ const makePgType = (
});

describe('extractCompositeType', () => {
const [getKnex, databaseName] = useTestKnex();
useSchema(getKnex, 'test');

it('should extract simplified information', async () => {
const db = getKnex();
test('it should extract simplified information', async ({
knex: [db, databaseName],
}) => {
await db.raw('create type test.some_composite_type as (id integer)');

const result = await extractCompositeType(
Expand Down Expand Up @@ -107,8 +104,7 @@ describe('extractCompositeType', () => {
expect(result).toStrictEqual(expected);
});

it('should fetch column comments', async () => {
const db = getKnex();
test('it should fetch column comments', async ({ knex: [db] }) => {
await db.raw(
'create type test.some_composite_type as (id integer, name text)'
);
Expand All @@ -124,8 +120,9 @@ describe('extractCompositeType', () => {
expect(result.attributes[0].comment).toBe('id column');
});

it('should handle domains, composite types, ranges and enums as well as arrays of those', async () => {
const db = getKnex();
test('it should handle domains, composite types, ranges and enums as well as arrays of those', async ({
knex: [db],
}) => {
await db.raw('create domain test.some_domain as text');
await db.raw('create type test.some_composite as (id integer, name text)');
await db.raw('create type test.some_range as range(subtype=timestamptz)');
Expand Down
14 changes: 5 additions & 9 deletions src/kinds/extractDomain.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { expect, it } from 'vitest';
import { describe, expect } from 'vitest';

import { describe } from '../tests/fixture';
import useSchema from '../tests/useSchema';
import useTestKnex from '../tests/useTestKnex';
import { test } from '../tests/useSchema';
import extractDomain, { DomainDetails } from './extractDomain';
import PgType from './PgType';

Expand All @@ -17,11 +15,9 @@ const makePgType = (
});

describe('extractDomain', () => {
const [getKnex, databaseName] = useTestKnex();
useSchema(getKnex, 'test');

it('should extract simplified as well as full information_schema information', async () => {
const db = getKnex();
test('it should extract simplified as well as full information_schema information', async ({
knex: [db, databaseName],
}) => {
await db.raw('create domain test.some_domain as integer');

const result = await extractDomain(db, makePgType('some_domain'));
Expand Down
12 changes: 3 additions & 9 deletions src/kinds/extractEnum.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { expect, it } from 'vitest';
import { describe, expect } from 'vitest';

import { describe } from '../tests/fixture';
import useSchema from '../tests/useSchema';
import useTestKnex from '../tests/useTestKnex';
import { test } from '../tests/useSchema';
import extractEnum, { EnumDetails } from './extractEnum';
import PgType from './PgType';

Expand All @@ -17,11 +15,7 @@ const makePgType = (
});

describe('extractEnum', () => {
const [getKnex] = useTestKnex();
useSchema(getKnex, 'test');

it('should extract enum values', async () => {
const db = getKnex();
test('it should extract enum values', async ({ knex: [db] }) => {
await db.raw("create type test.some_enum as enum('a', 'b', 'c')");

const result = await extractEnum(db, makePgType('some_enum'));
Expand Down
22 changes: 9 additions & 13 deletions src/kinds/extractMaterializedView.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import * as R from 'ramda';
import { expect, it } from 'vitest';
import { describe, expect } from 'vitest';

import { describe } from '../tests/fixture';
import useSchema from '../tests/useSchema';
import useTestKnex from '../tests/useTestKnex';
import { test } from '../tests/useSchema';
import extractMaterializedView, {
MaterializedViewColumn,
MaterializedViewDetails,
Expand All @@ -21,11 +19,9 @@ const makePgType = (
});

describe('extractMaterializedView', () => {
const [getKnex, databaseName] = useTestKnex();
useSchema(getKnex, 'test');

it('should extract simplified information', async () => {
const db = getKnex();
test('it should extract simplified information', async ({
knex: [db, databaseName],
}) => {
await db.raw(
'create materialized view test.some_materialized_view as select 1 as id'
);
Expand Down Expand Up @@ -123,8 +119,7 @@ describe('extractMaterializedView', () => {
expect(result).toStrictEqual(expected);
});

it('should fetch column comments', async () => {
const db = getKnex();
test('it should fetch column comments', async ({ knex: [db] }) => {
await db.raw(
'create materialized view test.some_materialized_view as select 1 as id'
);
Expand All @@ -140,8 +135,9 @@ describe('extractMaterializedView', () => {
expect(result.columns[0].comment).toBe('id column');
});

it('should handle domains, composite types, ranges and enums as well as arrays of those', async () => {
const db = getKnex();
test('it should handle domains, composite types, ranges and enums as well as arrays of those', async ({
knex: [db],
}) => {
await db.raw('create domain test.some_domain as text');
await db.raw('create type test.some_composite as (id integer, name text)');
await db.raw('create type test.some_range as range(subtype=timestamptz)');
Expand Down
12 changes: 3 additions & 9 deletions src/kinds/extractRange.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { expect, it } from 'vitest';
import { describe, expect } from 'vitest';

import { describe } from '../tests/fixture';
import useSchema from '../tests/useSchema';
import useTestKnex from '../tests/useTestKnex';
import { test } from '../tests/useSchema';
import extractRange, { RangeDetails } from './extractRange';
import PgType from './PgType';

Expand All @@ -17,11 +15,7 @@ const makePgType = (
});

describe('extractRange', () => {
const [getKnex] = useTestKnex();
useSchema(getKnex, 'test');

it('should extract range values', async () => {
const db = getKnex();
test('it should extract range values', async ({ knex: [db] }) => {
await db.raw('create type test.some_range as range(subtype=timestamptz)');

const result = await extractRange(db, makePgType('some_range'));
Expand Down
44 changes: 19 additions & 25 deletions src/kinds/extractTable.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import * as R from 'ramda';
import { expect, it } from 'vitest';
import { describe, expect } from 'vitest';

import { describe } from '../tests/fixture';
import useSchema from '../tests/useSchema';
import useTestKnex from '../tests/useTestKnex';
import { test, testWith } from '../tests/useSchema';
import extractTable, {
ColumnReference,
TableColumn,
Expand All @@ -21,12 +19,11 @@ const makePgType = (
comment: null,
});

// const test = testWith({ schemaNames: ['test'] });
describe('extractTable', () => {
const [getKnex, databaseName] = useTestKnex();
useSchema(getKnex, 'test');

it('should extract simplified as well as full information_schema information', async () => {
const db = getKnex();
test('it should extract simplified as well as full information_schema information', async ({
knex: [db, databaseName],
}) => {
await db.raw('create table test.some_table (id integer)');

const result = await extractTable(db, makePgType('some_table'));
Expand Down Expand Up @@ -122,8 +119,7 @@ describe('extractTable', () => {
expect(result).toStrictEqual(expected);
});

it('should fetch column comments', async () => {
const db = getKnex();
test('it should fetch column comments', async ({ knex: [db] }) => {
await db.raw('create table test.some_table (id integer)');
await db.raw("comment on column test.some_table.id is 'id column'");

Expand All @@ -132,8 +128,7 @@ describe('extractTable', () => {
expect(result.columns[0].comment).toBe('id column');
});

it('should handle arrays of primitive types', async () => {
const db = getKnex();
test('it should handle arrays of primitive types', async ({ knex: [db] }) => {
await db.raw(
'create table test.some_table (array_of_ints integer[], array_of_strings text[], two_dimensional_array integer[][])'
);
Expand Down Expand Up @@ -167,8 +162,9 @@ describe('extractTable', () => {
expect(actual).toEqual(expected);
});

it('should handle domains, composite types, ranges and enums as well as arrays of those', async () => {
const db = getKnex();
test('it should handle domains, composite types, ranges and enums as well as arrays of those', async ({
knex: [db],
}) => {
await db.raw('create domain test.some_domain as text');
await db.raw('create type test.some_composite as (id integer, name text)');
await db.raw('create type test.some_range as range(subtype=timestamptz)');
Expand Down Expand Up @@ -260,11 +256,9 @@ describe('extractTable', () => {
});

describe('references', () => {
useSchema(getKnex, 'secondary_schema');

it('should extract a simple foreign key', async () => {
const db = getKnex();
const test = testWith({ schemaNames: ['test', 'secondary_schema'] });

test('it should extract a simple foreign key', async ({ knex: [db] }) => {
await db.raw('create table test.some_table (id integer primary key)');
await db.raw(
'create table test.linking_table (some_table_id integer references test.some_table(id))'
Expand All @@ -282,9 +276,9 @@ describe('extractTable', () => {
expect(result.columns[0].reference).toEqual(expected);
});

it('should extract a foreign key with a different schema', async () => {
const db = getKnex();

test('it should extract a foreign key with a different schema', async ({
knex: [db],
}) => {
await db.raw(
'create table secondary_schema.some_table (id integer primary key)'
);
Expand All @@ -304,9 +298,9 @@ describe('extractTable', () => {
expect(result.columns[0].reference).toEqual(expected);
});

it('should get the onDelete and onUpdate actions', async () => {
const db = getKnex();

test('it should get the onDelete and onUpdate actions', async ({
knex: [db],
}) => {
await db.raw('create table test.some_table (id integer primary key)');
await db.raw(
'create table test.linking_table (some_table_id integer references test.some_table(id) on delete cascade on update set null)'
Expand Down
22 changes: 9 additions & 13 deletions src/kinds/extractView.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import * as R from 'ramda';
import { expect, it } from 'vitest';
import { describe, expect } from 'vitest';

import { describe } from '../tests/fixture';
import useSchema from '../tests/useSchema';
import useTestKnex from '../tests/useTestKnex';
import { test } from '../tests/useSchema';
import extractView, { ViewColumn, ViewDetails } from './extractView';
import PgType from './PgType';

Expand All @@ -18,11 +16,9 @@ const makePgType = (
});

describe('extractView', () => {
const [getKnex, databaseName] = useTestKnex();
useSchema(getKnex, 'test');

it('should extract simplified as well as full information_schema information', async () => {
const db = getKnex();
test('it should extract simplified as well as full information_schema information', async ({
knex: [db, databaseName],
}) => {
await db.raw('create view test.some_view as select 1 as id');

const result = await extractView(db, makePgType('some_view'));
Expand Down Expand Up @@ -115,8 +111,7 @@ describe('extractView', () => {
expect(result).toStrictEqual(expected);
});

it('should fetch column comments', async () => {
const db = getKnex();
test('it should fetch column comments', async ({ knex: [db] }) => {
await db.raw('create view test.some_view as select 1 as id');
await db.raw("comment on column test.some_view.id is 'id column'");

Expand All @@ -125,8 +120,9 @@ describe('extractView', () => {
expect(result.columns[0].comment).toBe('id column');
});

it('should handle domains, composite types, ranges and enums as well as arrays of those', async () => {
const db = getKnex();
test('it should handle domains, composite types, ranges and enums as well as arrays of those', async ({
knex: [db],
}) => {
await db.raw('create domain test.some_domain as text');
await db.raw('create type test.some_composite as (id integer, name text)');
await db.raw('create type test.some_range as range(subtype=timestamptz)');
Expand Down
19 changes: 5 additions & 14 deletions src/kinds/fetchTypes.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import { expect, it } from 'vitest';
import { describe, expect } from 'vitest';

import { describe } from '../tests/fixture';
import useSchema from '../tests/useSchema';
import useTestKnex from '../tests/useTestKnex';
import { test } from '../tests/useSchema';
import fetchTypes from './fetchTypes';

describe('fetchTypes', () => {
const [getKnex] = useTestKnex();
useSchema(getKnex, 'test');

it('should fetch a simple type', async () => {
const db = getKnex();
test('it should fetch a simple type', async ({ knex: [db] }) => {
await db.raw('create table test.some_table (id integer)');

const types = await fetchTypes(db, ['test']);
Expand All @@ -24,8 +18,7 @@ describe('fetchTypes', () => {
});
});

it('should fetch all kinds', async () => {
const db = getKnex();
test('it should fetch all kinds', async ({ knex: [db] }) => {
await db.raw('create table test.some_table (id integer)');
await db.raw('create view test.some_view as select 1 as id');
await db.raw(
Expand Down Expand Up @@ -61,9 +54,7 @@ describe('fetchTypes', () => {
]);
});

it('should fetch comments', async () => {
const db = getKnex();

test('it should fetch comments', async ({ knex: [db] }) => {
// Tables are a "class" in postgres.
await db.raw('create table test.some_table (id integer)');
await db.raw("comment on table test.some_table is 'some table comment'");
Expand Down
Loading

0 comments on commit 8a8997e

Please sign in to comment.