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

Refactoring #32

Merged
merged 4 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions pages/api/v1/migrations/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import database from "infra/database.js";
import migrationRunner from "node-pg-migrate";
import { join } from "node:path";
import { resolve } from "node:path";

export default async function migrations(request, response) {
const allowedMethods = ["GET", "POST"];
if (!allowedMethods.includes(request.method)) {
Expand All @@ -14,7 +15,7 @@ export default async function migrations(request, response) {
const defaultMigrationsOptions = {
dbClient: dbClient,
dryRun: true,
dir: join("infra", "migrations"),
dir: resolve("infra", "migrations"),
direction: "up",
verbose: true,
migrationsTable: "pgmigrations",
Expand Down
20 changes: 12 additions & 8 deletions tests/integration/api/v1/migrations/get.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import database from "infra/database";
import orchestrator from "tests/orchestrator.js";
beforeAll(async () => {
await orchestrator.waitForAllServices();
await database.query("drop schema public cascade; create schema public;");
await orchestrator.clearDatabase();
});

test("GET to /api/v1/migrations should return 200", async () => {
const response = await fetch("http://localhost:3000/api/v1/migrations");
expect(response.status).toBe(200);
describe("GET /api/v1/migrations", () => {
describe("Anonymous user", () => {
test("Retrieving pending migrations", async () => {
const response = await fetch("http://localhost:3000/api/v1/migrations");
expect(response.status).toBe(200);

const responseBody = await response.json();
expect(Array.isArray(responseBody)).toBe(true);
expect(responseBody.length).toBeGreaterThan(0);
const responseBody = await response.json();

expect(Array.isArray(responseBody)).toBe(true);
expect(responseBody.length).toBeGreaterThan(0);
});
});
});
48 changes: 31 additions & 17 deletions tests/integration/api/v1/migrations/post.test.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,40 @@
import database from "infra/database";
import orchestrator from "tests/orchestrator.js";
beforeAll(async () => {
await orchestrator.waitForAllServices();
await database.query("drop schema public cascade; create schema public;");
await orchestrator.clearDatabase();
});

test("POST to /api/v1/migrations should return 200", async () => {
const response1 = await fetch("http://localhost:3000/api/v1/migrations", {
method: "POST",
});
expect(response1.status).toBe(201);
describe("POST /api/v1/migrations", () => {
describe("Anonymous user", () => {
describe("Retrieving pending migrations", () => {
test("For the first time", async () => {
const response1 = await fetch(
"http://localhost:3000/api/v1/migrations",
{
method: "POST",
},
);
expect(response1.status).toBe(201);

const response1Body = await response1.json();
expect(Array.isArray(response1Body)).toBe(true);
expect(response1Body.length).toBeGreaterThan(0);
const response1Body = await response1.json();

const response2 = await fetch("http://localhost:3000/api/v1/migrations", {
method: "POST",
});
expect(response2.status).toBe(200);
expect(Array.isArray(response1Body)).toBe(true);
expect(response1Body.length).toBeGreaterThan(0);
});
test("For the second time", async () => {
const response2 = await fetch(
"http://localhost:3000/api/v1/migrations",
{
method: "POST",
},
);
expect(response2.status).toBe(200);

const response2Body = await response2.json();
expect(Array.isArray(response2Body)).toBe(true);
expect(response2Body.length).toBe(0);
const response2Body = await response2.json();

expect(Array.isArray(response2Body)).toBe(true);
expect(response2Body.length).toBe(0);
});
});
});
});
23 changes: 13 additions & 10 deletions tests/integration/api/v1/status/get.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ import orchestrator from "tests/orchestrator.js";
beforeAll(async () => {
await orchestrator.waitForAllServices();
});
test("GET to /api/v1/status should return 200", async () => {
const response = await fetch("http://localhost:3000/api/v1/status");
expect(response.status).toBe(200);
describe("GET /api/v1/status", () => {
describe("Anonymous user", () => {
test("Retrieving current system status", async () => {
const response = await fetch("http://localhost:3000/api/v1/status");
expect(response.status).toBe(200);

const responseBody = await response.json();
expect(responseBody.updated_at).toBeDefined();
const responseBody = await response.json();

const parsedUpdatedAt = new Date(responseBody.updated_at).toISOString();
expect(responseBody.updated_at).toEqual(parsedUpdatedAt);
const parsedUpdatedAt = new Date(responseBody.updated_at).toISOString();
expect(responseBody.updated_at).toEqual(parsedUpdatedAt);

expect(responseBody.dependencies.database.version).toEqual("16.0");
expect(responseBody.dependencies.database.max_connections).toEqual(100);
expect(responseBody.dependencies.database.opened_connections).toEqual(1);
expect(responseBody.dependencies.database.version).toEqual("16.0");
expect(responseBody.dependencies.database.max_connections).toEqual(100);
expect(responseBody.dependencies.database.opened_connections).toEqual(1);
});
});
});
8 changes: 8 additions & 0 deletions tests/orchestrator.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import retry from "async-retry";
import database from "infra/database";

async function waitForAllServices() {
await waitForWebServer();

Expand All @@ -19,7 +21,13 @@ async function waitForAllServices() {
}
}
}

async function clearDatabase() {
await database.query("drop schema public cascade; create schema public;");
}

const orchestrator = {
waitForAllServices,
clearDatabase,
};
export default orchestrator;