From 29bf8a505d438f7e095e42e9caefd93ef8d2371e Mon Sep 17 00:00:00 2001 From: Ciro Spaciari Date: Wed, 23 Oct 2024 18:01:06 -0700 Subject: [PATCH] fix(tests) pq -> pg + populate before (#14748) --- test/js/third_party/{pq => pg}/package.json | 2 +- test/js/third_party/pg/pg.test.ts | 75 +++++++++++++++++++++ test/js/third_party/pq/pq.test.ts | 32 --------- 3 files changed, 76 insertions(+), 33 deletions(-) rename test/js/third_party/{pq => pg}/package.json (74%) create mode 100644 test/js/third_party/pg/pg.test.ts delete mode 100644 test/js/third_party/pq/pq.test.ts diff --git a/test/js/third_party/pq/package.json b/test/js/third_party/pg/package.json similarity index 74% rename from test/js/third_party/pq/package.json rename to test/js/third_party/pg/package.json index b42b99ed32..20b0102e47 100644 --- a/test/js/third_party/pq/package.json +++ b/test/js/third_party/pg/package.json @@ -1,5 +1,5 @@ { - "name": "pq", + "name": "pg", "dependencies": { "pg": "8.11.1" } diff --git a/test/js/third_party/pg/pg.test.ts b/test/js/third_party/pg/pg.test.ts new file mode 100644 index 0000000000..5b7f03f72c --- /dev/null +++ b/test/js/third_party/pg/pg.test.ts @@ -0,0 +1,75 @@ +import { describe, expect, test } from "bun:test"; +import { getSecret } from "harness"; +import { Client, Pool } from "pg"; +import { parse } from "pg-connection-string"; + +const databaseUrl = getSecret("TLS_POSTGRES_DATABASE_URL"); + +// Function to insert 1000 users +async function insertUsers(client: Client) { + // Generate an array of users + const users = Array.from({ length: 300 }, (_, i) => ({ + name: `User ${i + 1}`, + email: `user${i + 1}@example.com`, + age: Math.floor(Math.random() * 50) + 20, // Random age between 20 and 70 + })); + + // Prepare the query to insert multiple rows + const insertQuery = ` + INSERT INTO users (name, email, age) + VALUES ${users.map((_, i) => `($${i * 3 + 1}, $${i * 3 + 2}, $${i * 3 + 3})`).join(", ")}; + `; + + // Flatten the users array for parameterized query + const values = users.flatMap(user => [user.name, user.email, user.age]); + + await client.query(insertQuery, values); +} + +async function connect() { + const client = new Client({ + connectionString: databaseUrl!, + ssl: { rejectUnauthorized: false }, + }); + await client.connect().then(() => { + // Define the SQL query to create a table + const createTableQuery = ` + CREATE TABLE IF NOT EXISTS users ( + id SERIAL PRIMARY KEY, + name VARCHAR(100) NOT NULL, + email VARCHAR(100) UNIQUE NOT NULL, + age INTEGER, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP + ); + `; + + // Execute the query + return client.query(createTableQuery); + }); + // check if we need to populate the data + const { rows } = await client.query("SELECT COUNT(*) AS count FROM users"); + const userCount = Number.parseInt(rows[0].count, 10); + if (userCount === 0) await insertUsers(client); + return client; +} + +describe.skipIf(!databaseUrl)("pg", () => { + test("should connect using TLS", async () => { + const pool = new Pool(parse(databaseUrl!)); + try { + const { rows } = await pool.query("SELECT version()", []); + const [{ version }] = rows; + + expect(version).toMatch(/PostgreSQL/); + } finally { + pool.end(); + } + }); + + test("should execute big query and end connection", async () => { + const client = await connect(); + const res = await client.query(`SELECT * FROM users LIMIT 300`); + expect(res.rows.length).toBe(300); + await client.end(); + }, 20_000); +}); diff --git a/test/js/third_party/pq/pq.test.ts b/test/js/third_party/pq/pq.test.ts deleted file mode 100644 index bd2ad8889e..0000000000 --- a/test/js/third_party/pq/pq.test.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { describe, expect, test } from "bun:test"; -import { getSecret } from "harness"; -import { Client, Pool } from "pg"; -import { parse } from "pg-connection-string"; - -const databaseUrl = getSecret("TLS_POSTGRES_DATABASE_URL"); - -describe.skipIf(!databaseUrl)("pg", () => { - test("should connect using TLS", async () => { - const pool = new Pool(parse(databaseUrl!)); - try { - const { rows } = await pool.query("SELECT version()", []); - const [{ version }] = rows; - - expect(version).toMatch(/PostgreSQL/); - } finally { - pool.end(); - } - }); - - test("should execute big query and end connection", async () => { - const client = new Client({ - connectionString: databaseUrl!, - ssl: { rejectUnauthorized: false }, - }); - - await client.connect(); - const res = await client.query(`SELECT * FROM users LIMIT 1000`); - expect(res.rows.length).toBeGreaterThanOrEqual(300); - await client.end(); - }); -});