Skip to content

Commit

Permalink
better test setup
Browse files Browse the repository at this point in the history
  • Loading branch information
0xOlias committed Feb 29, 2024
1 parent 7696f57 commit ed34a24
Show file tree
Hide file tree
Showing 16 changed files with 1,112 additions and 1,309 deletions.
7 changes: 4 additions & 3 deletions packages/core/src/Ponder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ export class Ponder {
common: this.common,
...syncStoreConfig,
});

await this.syncStore.migrateUp();
} else {
const database = new PostgresDatabaseService({
common: this.common,
Expand Down Expand Up @@ -313,6 +315,8 @@ export class Ponder {
common: this.common,
...syncStoreConfig,
});

await this.syncStore.migrateUp();
}

const networksToSync = this.networks.filter((network) => {
Expand Down Expand Up @@ -387,9 +391,6 @@ export class Ponder {
sources: this.sources,
});

// One-time setup for some services.
await this.syncStore.migrateUp();

this.serverService.setup({ registerDevRoutes: isDev });
await this.serverService.start();
this.serverService.reloadGraphqlSchema({
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/_test/e2e/erc20/erc20.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { rmSync } from "node:fs";
import { Ponder } from "@/Ponder.js";
import { ALICE, BOB } from "@/_test/constants.js";
import { setupAnvil } from "@/_test/setup.js";
import { setupAnvil, setupIsolatedDatabase } from "@/_test/setup.js";
import { simulate } from "@/_test/simulate.js";
import { onAllEventsIndexed } from "@/_test/utils.js";
import { buildOptions } from "@/config/options.js";
Expand All @@ -10,7 +10,8 @@ import request from "supertest";
import { zeroAddress } from "viem";
import { afterEach, beforeEach, expect, test } from "vitest";

beforeEach((context) => setupAnvil(context));
beforeEach(setupAnvil);
beforeEach(setupIsolatedDatabase);

const gql = async (ponder: Ponder, query: string) => {
const response = await request(ponder.serverService.app)
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/_test/e2e/factory/factory.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { rmSync } from "node:fs";
import { Ponder } from "@/Ponder.js";
import { setupAnvil } from "@/_test/setup.js";
import { setupAnvil, setupIsolatedDatabase } from "@/_test/setup.js";
import { simulate } from "@/_test/simulate.js";
import { onAllEventsIndexed } from "@/_test/utils.js";
import { buildOptions } from "@/config/options.js";
import { range } from "@/utils/range.js";
import request from "supertest";
import { afterEach, beforeEach, expect, test } from "vitest";

beforeEach((context) => setupAnvil(context));
beforeEach(setupAnvil);
beforeEach(setupIsolatedDatabase);

const gql = async (ponder: Ponder, query: string) => {
const response = await request(ponder.serverService.app)
Expand Down
29 changes: 10 additions & 19 deletions packages/core/src/_test/globalSetup.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createPool } from "@/utils/pg.js";
import { startProxy } from "@viem/anvil";
import dotenv from "dotenv";
import { Pool } from "pg";

export default async function () {
dotenv.config({ path: ".env.local" });
Expand All @@ -15,27 +15,18 @@ export default async function () {
let cleanupDatabase: () => Promise<void>;
if (process.env.DATABASE_URL) {
cleanupDatabase = async () => {
const pool = createPool({ connectionString: process.env.DATABASE_URL! });
const pool = new Pool({ connectionString: process.env.DATABASE_URL });

const schemaRows = await pool.query(`
SELECT nspname FROM pg_catalog.pg_namespace WHERE nspname ~ '^vitest_pool_';
const databaseRows = await pool.query(`
SELECT datname FROM pg_database WHERE datname LIKE 'vitest_%';
`);
const schemas = schemaRows.rows.map((r) => r.nspname) as string[];
const databases = databaseRows.rows.map((r) => r.datname) as string[];

for (const schema of schemas) {
const tableRows = await pool.query(`
SELECT table_name FROM information_schema.tables WHERE table_schema = '${schema}'
`);
const tables = tableRows.rows.map((r) => r.table_name) as string[];

for (const table of tables) {
await pool.query(
`DROP TABLE IF EXISTS "${schema}"."${table}" CASCADE`,
);
}
await pool.query(`DROP SCHEMA IF EXISTS "${schema}" CASCADE`);
console.log(`Dropped ${tables.length} tables from schema "${schema}".`);
}
await Promise.all(
databases.map((databaseName) =>
pool.query(`DROP DATABASE "${databaseName}"`),
),
);

await pool.end();
};
Expand Down
Loading

0 comments on commit ed34a24

Please sign in to comment.