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

Adds interval data type related test cases #553

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/example/sql/schema.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
BEGIN;
CREATE TABLE users (
id SERIAL PRIMARY KEY,
email TEXT NOT NULL,
Expand Down Expand Up @@ -35,13 +36,21 @@ CREATE TABLE books (
categories category[]
);

CREATE TABLE audio_books (
id SERIAL PRIMARY KEY,
text_book_id INTEGER REFERENCES books,
duration INTERVAL NOT NULL
);

CREATE TABLE book_comments (
id SERIAL PRIMARY KEY,
user_id INTEGER REFERENCES users,
book_id INTEGER REFERENCES books,
body TEXT
);



INSERT INTO users (email, user_name, first_name, last_name, age)
VALUES ('alex.doe@example.com', 'alexd', 'Alex', 'Doe', 35),
('jane.holmes@example.com', 'jane67', 'Jane', 'Holmes', 23),
Expand Down Expand Up @@ -78,6 +87,12 @@ INSERT INTO book_comments (user_id, book_id, body)
VALUES (1, 1, 'Fantastic read, recommend it!'),
(1, 2, 'Did not like it, expected much more...');

INSERT INTO audio_books (text_book_id, duration)
VALUES (1, '01:23:45'),
(2, '02:34:56'),
(3, '03:45:57'),
(4, '04:56:48');

CREATE TYPE "Iso31661Alpha2" AS ENUM (
'AD', 'AE', 'AF', 'AG', 'AI', 'AL', 'AM', 'AO', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AW', 'AX', 'AZ', 'BA', 'BB', 'BD', 'BE', 'BF',
'BG', 'BH', 'BI', 'BJ', 'BL', 'BM', 'BN', 'BO', 'BQ', 'BR', 'BS', 'BT', 'BV', 'BW', 'BY', 'BZ', 'CA', 'CC', 'CD', 'CF', 'CG',
Expand All @@ -101,3 +116,5 @@ CREATE TABLE book_country (

INSERT INTO book_country (country)
VALUES ('CZ'), ('DE');

COMMIT;
25 changes: 25 additions & 0 deletions packages/example/src/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,31 @@ Array [
]
`;

exports[`select query with interval field 1`] = `
Array [
Object {
"duration": "0 years 0 mons 0 days 1 hours 23 mins 45.0 secs",
"id": 1,
"text_book_id": 1,
},
Object {
"duration": "0 years 0 mons 0 days 2 hours 34 mins 56.0 secs",
"id": 2,
"text_book_id": 2,
},
Object {
"duration": "0 years 0 mons 0 days 3 hours 45 mins 57.0 secs",
"id": 3,
"text_book_id": 3,
},
Object {
"duration": "0 years 0 mons 0 days 4 hours 56 mins 48.0 secs",
"id": 4,
"text_book_id": 4,
},
]
`;

exports[`select query with join and a parameter override 1`] = `
Array [
Object {
Expand Down
30 changes: 30 additions & 0 deletions packages/example/src/audio_books/audio_books.queries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/** Types generated for queries found in "src/audio_books/audio_books.sql" */
import { PreparedQuery } from '@pgtyped/runtime';

/** 'GetAudioBooks' parameters type */
export type IGetAudioBooksParams = void;

/** 'GetAudioBooks' return type */
export interface IGetAudioBooksResult {
duration: string;
id: number;
text_book_id: number | null;
}

/** 'GetAudioBooks' query type */
export interface IGetAudioBooksQuery {
params: IGetAudioBooksParams;
result: IGetAudioBooksResult;
}

const getAudioBooksIR: any = {"usedParamSet":{},"params":[],"statement":"select * from audio_books"};

/**
* Query generated from SQL:
* ```
* select * from audio_books
* ```
*/
export const getAudioBooks = new PreparedQuery<IGetAudioBooksParams,IGetAudioBooksResult>(getAudioBooksIR);


2 changes: 2 additions & 0 deletions packages/example/src/audio_books/audio_books.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* @name getAudioBooks */
select * from audio_books;
6 changes: 5 additions & 1 deletion packages/example/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
import { getUsersWithComment } from './users/sample.js';
import { Category } from './customTypes.js';
import { sql } from './sql/index.js'
import { getAudioBooks } from "./audio_books/audio_books.queries.js";

const { Client } = pg;

Expand Down Expand Up @@ -232,7 +233,10 @@ test('select query with a bigint field', async () => {
expect(row.book_count).toBe(BigInt(4));
});


test('select query with interval field', async () => {
const rows = await getAudioBooks.run(undefined, client);
expect(rows).toMatchSnapshot();
})
test('ts-implicit mode query', async () => {
const books = await sql(`SELECT * FROM books WHERE id = $id`).run({id: 1}, client);
expect(books).toMatchSnapshot();
Expand Down