fix(tests) pq -> pg + populate before (#14748)

This commit is contained in:
Ciro Spaciari
2024-10-23 18:01:06 -07:00
committed by GitHub
parent 93d115f9b7
commit 29bf8a505d
3 changed files with 76 additions and 33 deletions

View File

@@ -1,5 +1,5 @@
{
"name": "pq",
"name": "pg",
"dependencies": {
"pg": "8.11.1"
}

75
test/js/third_party/pg/pg.test.ts vendored Normal file
View File

@@ -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);
});

View File

@@ -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();
});
});