blob: 490192ae728fe5376166880824e596dff894bf75 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
import { test, expect, describe } from "bun:test";
import { Pool } from "pg";
import { parse } from "pg-connection-string";
import postgres from "postgres";
const CONNECTION_STRING = process.env.TLS_POSTGRES_DATABASE_URL;
const it = CONNECTION_STRING ? test : test.skip;
describe("pg", () => {
it("should connect using TLS", async () => {
const pool = new Pool(parse(CONNECTION_STRING as string));
try {
const { rows } = await pool.query("SELECT version()", []);
const [{ version }] = rows;
expect(version).toMatch(/PostgreSQL/);
} finally {
pool.end();
}
});
});
describe("postgres", () => {
it("should connect using TLS", async () => {
const sql = postgres(CONNECTION_STRING as string);
try {
const [{ version }] = await sql`SELECT version()`;
expect(version).toMatch(/PostgreSQL/);
} finally {
sql.end();
}
});
it("should insert, select and delete", async () => {
const sql = postgres(CONNECTION_STRING as string);
try {
await sql`CREATE TABLE IF NOT EXISTS users (
user_id serial PRIMARY KEY,
username VARCHAR ( 50 ) NOT NULL
);`;
const [{ user_id, username }] = await sql`insert into users (username) values ('bun') returning *`;
expect(username).toBe("bun");
const [{ user_id: user_id2, username: username2 }] = await sql`select * from users where user_id = ${user_id}`;
expect(username2).toBe("bun");
expect(user_id2).toBe(user_id);
const [{ username: username3 }] = await sql`delete from users where user_id = ${user_id} returning *`;
expect(username3).toBe("bun");
} finally {
sql.end();
}
});
});
|