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

Maintenance: dependencies + refactoring #24

Merged
merged 10 commits into from
Sep 16, 2024
4,554 changes: 2,895 additions & 1,659 deletions package-lock.json

Large diffs are not rendered by default.

49 changes: 25 additions & 24 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,47 @@
"description": "copy of tabnews repo for development training",
"main": "index.js",
"scripts": {
"dev": "npm run services:up && npm run wait-for-postgres && npm run migration:up && next dev",
"dev": "npm run services:up && npm run services:wait:database && npm run migrations:up && next dev",
"test": "npm run services:up && concurrently --names next,jest --hide next --kill-others --success command-jest \"next dev\" \"jest --runInBand --verbose\"",
"posttest": "npm run services:stop",
"test:watch": "jest --watchAll --runInBand --verbose",
"services:up": "docker compose -f infra/compose.yaml up -d",
"services:stop": "docker compose -f infra/compose.yaml stop",
"services:down": "docker compose -f infra/compose.yaml down",
"services:wait:database": "node infra/scripts/wait-for-postgres.js",
"migrations:create": "node-pg-migrate --migrations-dir infra/migrations create",
"migrations:up": "node-pg-migrate --migrations-dir infra/migrations --envPath .env.development up",
"lint:prettier:check": "prettier --check .",
"lint:prettier:fix": "prettier --write .",
"lint:eslint:check": "next lint --dir .",
"test": "npm run services:up && concurrently --names next,jest --hide next --kill-others --success command-jest \"next dev\" \"jest --runInBand --verbose\"",
"test:watch": "jest --watchAll --runInBand",
"migration:create": "node-pg-migrate --migrations-dir infra/migrations create",
"migration:up": "node-pg-migrate --migrations-dir infra/migrations --envPath .env.development up",
"wait-for-postgres": "node infra/scripts/wait-for-postgres.js",
"prepare": "husky",
"commit": "cz"
},
"author": "",
"license": "MIT",
"dependencies": {
"async-retry": "^1.3.3",
"dotenv": "^16.4.4",
"dotenv-expand": "^11.0.6",
"next": "^13.1.6",
"node-pg-migrate": "^6.2.2",
"pg": "^8.11.3",
"react": "^18.2.0",
"react-dom": "^18.2.0"
"async-retry": "1.3.3",
"dotenv": "16.4.5",
"dotenv-expand": "11.0.6",
"next": "14.2.5",
"node-pg-migrate": "7.6.1",
"pg": "8.12.0",
"react": "18.3.1",
"react-dom": "18.3.1"
},
"devDependencies": {
"@commitlint/cli": "^19.3.0",
"@commitlint/config-conventional": "^19.2.2",
"commitizen": "^4.3.0",
"concurrently": "^8.2.2",
"cz-conventional-changelog": "^3.3.0",
"@commitlint/cli": "19.4.0",
"@commitlint/config-conventional": "19.2.2",
"commitizen": "4.3.0",
"concurrently": "8.2.2",
"cz-conventional-changelog": "3.3.0",
"eslint": "8.57.0",
"eslint-config-next": "14.2.5",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-jest": "^28.6.0",
"husky": "^9.1.4",
"jest": "^29.6.2",
"prettier": "^3.2.5"
"eslint-config-prettier": "9.1.0",
"eslint-plugin-jest": "28.8.0",
"husky": "9.1.4",
"jest": "29.7.0",
"prettier": "3.3.3"
},
"config": {
"commitizen": {
Expand Down
4 changes: 2 additions & 2 deletions pages/api/v1/migrations/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import migrationRunner from "node-pg-migrate";
import { join } from "node:path";
import { resolve } from "node:path";
import database from "infra/database.js";

export default async function migrations(request, response) {
Expand All @@ -20,7 +20,7 @@ export default async function migrations(request, response) {
const defaultMigrationOptions = {
dbClient: dbClient,
dryRun: true,
dir: join("infra", "migrations"),
dir: resolve("infra", "migrations"),
direction: "up",
verbose: true,
migrationsTable: "pgmigrations",
Expand Down
3 changes: 1 addition & 2 deletions tests/integration/api/v1/migrations/get.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import database from "infra/database.js";
import orchestrator from "../orchestrator.js";

beforeAll(async () => {
await orchestrator.waitForAllServices();
await database.query("drop schema public cascade; create schema public;"); // de um lado da query um schema é derrubado e no outro é criado e todas as tabelas ficam associadas a um schema e por padrão é usado o public e derrubar o schema principal em forma de cascata
await orchestrator.clearDatabase();
});

//beforeAll(cleanDatabase); pra rodar a função de forma controlada o jest fornece essa função e antes de rodar a bateria de testes o jest vai rodar essa função
Expand Down
60 changes: 33 additions & 27 deletions tests/integration/api/v1/migrations/post.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import database from "infra/database.js";
import orchestrator from "../orchestrator.js";

beforeAll(async () => {
await orchestrator.waitForAllServices();
await database.query("drop schema public cascade; create schema public;"); // de um lado da query um schema é derrubado e no outro é criado e todas as tabelas ficam associadas a um schema e por padrão é usado o public e derrubar o schema principal em forma de cascata
await orchestrator.clearDatabase();
});

//beforeAll(cleanDatabase); // pra rodar a função de forma controlada o jest fornece essa função e antes de rodar a bateria de testes o jest vai rodar essa função
Expand All @@ -12,30 +11,37 @@ beforeAll(async () => {
// await database.query("drop schema public cascade; create schema public;"); // de um lado da query um schema é derrubado e no outro é criado e todas as tabelas ficam associadas a um schema e por padrão é usado o public e derrubar o schema principal em forma de cascata
//}

test("POST to /api/v1/migrations should return 200", async () => {
const response1 = await fetch("http:localhost:3000/api/v1/migrations", {
method: "POST",
describe("POST /api/v1/migrations", () => {
describe("Anonymous user", () => {
describe("Running 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();
//console.log(response1Body);

//expect(response1Body).toBe(Array) em teoria o que precisaria é isso mas o jest não aceita isso simples assim
expect(Array.isArray(response1Body)).toBe(true); // assim é a forma certa
expect(response1Body.length).toBeGreaterThan(0); // o tamanho do array precisa ser maior do que zero e momentâneamente isso causa erro e é preciso fazer o banco fazer o erro do zero
});
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();
//console.log(response2Body);

//expect(response2Body).toBe(Array) em teoria o que precisaria é isso mas o jest não aceita isso simples assim
expect(Array.isArray(response2Body)).toBe(true); // assim é a forma certa
expect(response2Body.length).toBe(0); // simulando requisições http por dentro dos testes
});
});
});

expect(response1.status).toBe(201);

const response1Body = await response1.json();
//console.log(response1Body);

//expect(response1Body).toBe(Array) em teoria o que precisaria é isso mas o jest não aceita isso simples assim
expect(Array.isArray(response1Body)).toBe(true); // assim é a forma certa
expect(response1Body.length).toBeGreaterThan(0); // o tamanho do array precisa ser maior do que zero e momentâneamente isso causa erro e é preciso fazer o banco fazer o erro do zero

const response2 = await fetch("http:localhost:3000/api/v1/migrations", {
method: "POST",
});

expect(response2.status).toBe(200);

const response2Body = await response2.json();
//console.log(response2Body);

//expect(response2Body).toBe(Array) em teoria o que precisaria é isso mas o jest não aceita isso simples assim
expect(Array.isArray(response2Body)).toBe(true); // assim é a forma certa
expect(response2Body.length).toBe(0); // simulando requisições http por dentro dos testes
});
6 changes: 6 additions & 0 deletions tests/integration/api/v1/orchestrator.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import retry from "async-retry";
import database from "infra/database.js";

async function waitForAllServices() {
await waitForWebServer();
Expand All @@ -18,8 +19,13 @@ async function waitForAllServices() {
}
}

async function clearDatabase() {
await database.query("drop schema public cascade; create schema public;"); // de um lado da query um schema é derrubado e no outro é criado e todas as tabelas ficam associadas a um schema e por padrão é usado o public e derrubar o schema principal em forma de cascata
}

const orchestrator = {
waitForAllServices,
clearDatabase,
};

export default orchestrator;
32 changes: 18 additions & 14 deletions tests/integration/api/v1/status/get.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,25 @@ 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();
//console.log(responseby);
//expect(responseBody.updated_at).toBeDefined(); // espero o response by vindo da propriedade updated_at esteja definido
const responseBody = await response.json();
//console.log(responseby);
//expect(responseBody.updated_at).toBeDefined(); // espero o response by vindo da propriedade updated_at esteja definido

//new Date(responseBody.updated_at).toISOString();
const parsedUpdatedAt = new Date(responseBody.updated_at).toISOString();
expect(responseBody.updated_at).toEqual(parsedUpdatedAt); // se na propriedade updated_at chegar o valor null não vai ser igual ao unix timestamp
//console.log(parsedUpdatedAt);
//new Date(responseBody.updated_at).toISOString();
const parsedUpdatedAt = new Date(responseBody.updated_at).toISOString();
expect(responseBody.updated_at).toEqual(parsedUpdatedAt); // se na propriedade updated_at chegar o valor null não vai ser igual ao unix timestamp
//console.log(parsedUpdatedAt);

expect(responseBody.dependencies.database.version).toEqual("16.0");
//console.log(responseBody);
expect(responseBody.dependencies.database.max_connections).toEqual(100);
expect(responseBody.dependencies.database.opened_connections).toEqual(1); // 1 porque se tiver mais de uma pode ter uma conexão vazando
expect(responseBody.dependencies.database.version).toEqual("16.0");
//console.log(responseBody);
expect(responseBody.dependencies.database.max_connections).toEqual(100);
expect(responseBody.dependencies.database.opened_connections).toEqual(1); // 1 porque se tiver mais de uma pode ter uma conexão vazando
});
});
});