Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add integration test for type overrides #481

Merged
merged 1 commit into from
Jan 27, 2023
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
3 changes: 3 additions & 0 deletions packages/example/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
"emitTemplate": "{{dir}}/{{name}}.types.ts"
}
],
"typesOverrides": {
"date": "string"
},
"srcDir": "./src/",
"dbUrl": "postgres://postgres:password@localhost/postgres"
}
2 changes: 1 addition & 1 deletion packages/example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"test": "docker-compose run build && docker-compose run test && docker-compose run test-cjs",
"typegen": "pgtyped -c config.json",
"build": "echo 'No build step required. Use npm test instead'",
"watch": "tsc --watch --preserveWatchOutput",
"watch": "echo 'No build step required. Use npm test instead'",
"check": "tsc --noEmit"
},
"dependencies": {
Expand Down
3 changes: 2 additions & 1 deletion packages/example/sql/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ CREATE TABLE notifications (
id SERIAL PRIMARY KEY,
user_id INTEGER REFERENCES users,
payload jsonb NOT NULL,
type notification_type NOT NULL DEFAULT 'notification'
type notification_type NOT NULL DEFAULT 'notification',
created_at DATE NOT NULL DEFAULT CURRENT_DATE
);

CREATE TABLE authors (
Expand Down
24 changes: 22 additions & 2 deletions packages/example/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ import {
insertNotifications,
} from './notifications/notifications.js';
import {
sendNotifications,
thresholdFrogs,
getNotifications,
sendNotifications,
thresholdFrogs,
} from './notifications/notifications.queries.js';
import {getUsersWithComment} from "./users/sample.js";

const dbConfig = {
host: process.env.PGHOST ?? '127.0.0.1',
Expand All @@ -32,6 +34,12 @@ const dbConfig = {
// Connect to the database once before all tests
let client: any;
beforeAll(async () => {
// Parse dates as strings for demo and testing purposes
pg.types.setTypeParser(pg.types.builtins.DATE, function(val) {
return val;
})

// Create a new client and connect to the database
client = new Client(dbConfig);
await client.connect();
});
Expand All @@ -55,6 +63,18 @@ test('select query with parameters', async () => {
expect(comments).toMatchSnapshot();
})

test('select query with date type override (TS)', async () => {
const comments = await getUsersWithComment(0, client);
const dateAsString: string = comments.registration_date;
expect(typeof dateAsString).toBe("string");
})

test('select query with date type override (SQL)', async () => {
const notifications = await getNotifications.run({userId: 1}, client);
const dateAsString: string = notifications[0].created_at;
expect(typeof dateAsString).toBe("string");
})

test('insert query with parameter spread', async () => {
const [{ book_id: insertedBookId }] = await insertBooks.run(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export interface IGetNotificationsParams {

/** 'GetNotifications' return type */
export interface IGetNotificationsResult {
created_at: string;
id: number;
payload: Json;
type: notification_type;
Expand Down
1 change: 1 addition & 0 deletions packages/example/src/notifications/notifications.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export type IGetAllNotificationsParams = void;

/** 'GetAllNotifications' return type */
export interface IGetAllNotificationsResult {
created_at: string;
id: number;
payload: Json;
type: notification_type;
Expand Down
2 changes: 1 addition & 1 deletion packages/example/src/users/sample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ export async function getUsersWithComment(
GROUP BY u.id
HAVING count(bc.id) > $minCommentCount!::int`;
const result = await getUsersWithComments.run({ minCommentCount }, client);
return result[0].user_name;
return result[0];
}
2 changes: 1 addition & 1 deletion packages/example/src/users/sample.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface IGetUsersWithCommentsResult {
first_name: string | null;
id: number;
last_name: string | null;
registration_date: Date;
registration_date: string;
user_name: string;
}

Expand Down