Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
import '../test-utils/env';
import { GraphQLQueryFn, getConnections, seed, snapshot } from 'graphile-test';
import { join } from 'path';
import type { PgTestClient } from 'pgsql-test/test-client';

import { PgSimpleInflector } from '../src';
import { IntrospectionQuery } from '../test-utils/queries';

const SCHEMA = 'app_public';
const sql = (file: string) => join(__dirname, '../sql', file);

// Helper function to create a test case with its own database setup
function createTestCase(
testName: string,
sqlFile: string,
expectedTypes: {
orderBy?: string[];
connection?: string[];
edge?: string[];
queryField?: string;
}
) {
describe(testName, () => {
let teardown: () => Promise<void>;
let query: GraphQLQueryFn;
let db: PgTestClient;

beforeAll(async () => {
const connections = await getConnections(
{
schemas: [SCHEMA],
authRole: 'authenticated',
graphile: {
overrideSettings: {
appendPlugins: [PgSimpleInflector]
}
}
},
[
seed.sqlfile([
sql(sqlFile)
])
]
);

({ db, query, teardown } = connections);
});

beforeEach(() => db.beforeEach());
beforeEach(async () => {
db.setContext({ role: 'authenticated' });
});
afterEach(() => db.afterEach());
afterAll(async () => {
await teardown();
});

it(`should correctly handle ${testName}`, async () => {
const data = await query(IntrospectionQuery);

// Use snapshot to verify the entire schema structure
expect(snapshot(data)).toMatchSnapshot(`${testName.replace(/\s+/g, '-').toLowerCase()}`);
});
});
}

describe('Inflection Special Cases', () => {
describe('Singular Table Names', () => {
// Test case 1: regimen (singular table name)
createTestCase(
'regimen table',
'test-regimen.sql',
{
orderBy: ['RegimensOrderBy', 'RegimenOrderBy'],
connection: ['RegimensConnection', 'RegimenConnection'],
edge: ['RegimensEdge', 'RegimenEdge'],
queryField: 'regimens'
}
);

// Test case 2: child (singular table name)
createTestCase(
'child table',
'test-child.sql',
{
orderBy: ['ChildrenOrderBy', 'ChildOrderBy'],
connection: ['ChildrenConnection', 'ChildConnection'],
edge: ['ChildrenEdge', 'ChildEdge'],
queryField: 'children'
}
);

// Test case 3: man (singular table name)
createTestCase(
'man table',
'test-man.sql',
{
orderBy: ['MenOrderBy', 'ManOrderBy'],
connection: ['MenConnection', 'ManConnection'],
edge: ['MenEdge', 'ManEdge'],
queryField: 'men'
}
);

// Test case 4: user_login (singular compound table name)
createTestCase(
'user_login table',
'test-user_login.sql',
{
orderBy: ['UserLoginsOrderBy', 'UserLoginOrderBy'],
connection: ['UserLoginsConnection', 'UserLoginConnection'],
edge: ['UserLoginsEdge', 'UserLoginEdge'],
queryField: 'userLogins'
}
);
});

describe('Plural Table Names', () => {
// Test case 1: regimens (plural table name)
createTestCase(
'regimens table',
'test-regimens.sql',
{
orderBy: ['RegimensOrderBy', 'RegimenOrderBy'],
connection: ['RegimensConnection', 'RegimenConnection'],
edge: ['RegimensEdge', 'RegimenEdge'],
queryField: 'regimens'
}
);

// Test case 2: children (plural table name)
createTestCase(
'children table',
'test-children.sql',
{
orderBy: ['ChildrenOrderBy', 'ChildOrderBy'],
connection: ['ChildrenConnection', 'ChildConnection'],
edge: ['ChildrenEdge', 'ChildEdge'],
queryField: 'children'
}
);

// Test case 3: men (plural table name)
createTestCase(
'men table',
'test-men.sql',
{
orderBy: ['MenOrderBy', 'ManOrderBy'],
connection: ['MenConnection', 'ManConnection'],
edge: ['MenEdge', 'ManEdge'],
queryField: 'men'
}
);

// Test case 4: user_logins (plural compound table name)
createTestCase(
'user_logins table',
'test-user_logins.sql',
{
orderBy: ['UserLoginsOrderBy', 'UserLoginOrderBy'],
connection: ['UserLoginsConnection', 'UserLoginConnection'],
edge: ['UserLoginsEdge', 'UserLoginEdge'],
queryField: 'userLogins'
}
);
});

describe('Compound Table Names', () => {
// Test case 1: user_regimen (singular compound table name)
createTestCase(
'user_regimen table',
'test-user_regimen.sql',
{
orderBy: ['UserRegimensOrderBy', 'UserRegimenOrderBy'],
connection: ['UserRegimensConnection', 'UserRegimenConnection'],
edge: ['UserRegimensEdge', 'UserRegimenEdge'],
queryField: 'userRegimens'
}
);

// Test case 2: user_regimens (plural compound table name)
createTestCase(
'user_regimens table',
'test-user_regimens.sql',
{
orderBy: ['UserRegimensOrderBy', 'UserRegimenOrderBy'],
connection: ['UserRegimensConnection', 'UserRegimenConnection'],
edge: ['UserRegimensEdge', 'UserRegimenEdge'],
queryField: 'userRegimens'
}
);
});
});

3 changes: 2 additions & 1 deletion graphile/graphile-simple-inflector/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"pgsql-test": "workspace:^"
},
"dependencies": {
"graphile-build": "^4.14.1"
"graphile-build": "^4.14.1",
"inflekt": "^0.1.2"
}
}
14 changes: 14 additions & 0 deletions graphile/graphile-simple-inflector/sql/test-child.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- Test table for child inflection (testing singular table name)
BEGIN;
CREATE EXTENSION IF NOT EXISTS citext;
DROP SCHEMA IF EXISTS app_public CASCADE;
CREATE SCHEMA app_public;

CREATE TABLE app_public.child (
id serial PRIMARY KEY,
name text NOT NULL,
parent_id bigint
);

COMMIT;

14 changes: 14 additions & 0 deletions graphile/graphile-simple-inflector/sql/test-children.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- Test table for children inflection (testing plural table name)
BEGIN;
CREATE EXTENSION IF NOT EXISTS citext;
DROP SCHEMA IF EXISTS app_public CASCADE;
CREATE SCHEMA app_public;

CREATE TABLE app_public.children (
id serial PRIMARY KEY,
name text NOT NULL,
parent_id bigint
);

COMMIT;

13 changes: 13 additions & 0 deletions graphile/graphile-simple-inflector/sql/test-man.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- Test table for man inflection (testing singular table name)
BEGIN;
CREATE EXTENSION IF NOT EXISTS citext;
DROP SCHEMA IF EXISTS app_public CASCADE;
CREATE SCHEMA app_public;

CREATE TABLE app_public.man (
id serial PRIMARY KEY,
name text NOT NULL
);

COMMIT;

13 changes: 13 additions & 0 deletions graphile/graphile-simple-inflector/sql/test-men.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- Test table for men inflection (testing plural table name)
BEGIN;
CREATE EXTENSION IF NOT EXISTS citext;
DROP SCHEMA IF EXISTS app_public CASCADE;
CREATE SCHEMA app_public;

CREATE TABLE app_public.men (
id serial PRIMARY KEY,
name text NOT NULL
);

COMMIT;

13 changes: 13 additions & 0 deletions graphile/graphile-simple-inflector/sql/test-regimen.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- Test table for regimen inflection (testing singular table name)
BEGIN;
CREATE EXTENSION IF NOT EXISTS citext;
DROP SCHEMA IF EXISTS app_public CASCADE;
CREATE SCHEMA app_public;

CREATE TABLE app_public.regimen (
id serial PRIMARY KEY,
name text NOT NULL
);

COMMIT;

13 changes: 13 additions & 0 deletions graphile/graphile-simple-inflector/sql/test-regimens.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- Test table for regimens inflection (testing plural table name)
BEGIN;
CREATE EXTENSION IF NOT EXISTS citext;
DROP SCHEMA IF EXISTS app_public CASCADE;
CREATE SCHEMA app_public;

CREATE TABLE app_public.regimens (
id serial PRIMARY KEY,
name text NOT NULL
);

COMMIT;

14 changes: 14 additions & 0 deletions graphile/graphile-simple-inflector/sql/test-user_login.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- Test table for user_login inflection (testing singular compound table name)
BEGIN;
CREATE EXTENSION IF NOT EXISTS citext;
DROP SCHEMA IF EXISTS app_public CASCADE;
CREATE SCHEMA app_public;

CREATE TABLE app_public.user_login (
id serial PRIMARY KEY,
user_id bigint NOT NULL,
login_time timestamptz NOT NULL DEFAULT now()
);

COMMIT;

14 changes: 14 additions & 0 deletions graphile/graphile-simple-inflector/sql/test-user_logins.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- Test table for user_logins inflection (testing plural table name)
BEGIN;
CREATE EXTENSION IF NOT EXISTS citext;
DROP SCHEMA IF EXISTS app_public CASCADE;
CREATE SCHEMA app_public;

CREATE TABLE app_public.user_logins (
id serial PRIMARY KEY,
user_id bigint NOT NULL,
login_time timestamptz NOT NULL DEFAULT now()
);

COMMIT;

14 changes: 14 additions & 0 deletions graphile/graphile-simple-inflector/sql/test-user_regimen.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- Test table for user_regimen inflection (testing singular compound table name)
BEGIN;
CREATE EXTENSION IF NOT EXISTS citext;
DROP SCHEMA IF EXISTS app_public CASCADE;
CREATE SCHEMA app_public;

CREATE TABLE app_public.user_regimen (
id serial PRIMARY KEY,
user_id bigint NOT NULL,
name text NOT NULL
);

COMMIT;

14 changes: 14 additions & 0 deletions graphile/graphile-simple-inflector/sql/test-user_regimens.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- Test table for user_regimens inflection (testing plural compound table name)
BEGIN;
CREATE EXTENSION IF NOT EXISTS citext;
DROP SCHEMA IF EXISTS app_public CASCADE;
CREATE SCHEMA app_public;

CREATE TABLE app_public.user_regimens (
id serial PRIMARY KEY,
user_id bigint NOT NULL,
name text NOT NULL
);

COMMIT;

Loading