Skip to content

Commit

Permalink
Merge pull request #82 from filipedeschamps/tests-new-approach
Browse files Browse the repository at this point in the history
Nova abordagem para testes
  • Loading branch information
filipedeschamps authored Jul 24, 2021
2 parents 5ea56cf + 83afe07 commit 5dde95f
Show file tree
Hide file tree
Showing 17 changed files with 199 additions and 341 deletions.
4 changes: 3 additions & 1 deletion .env.development → .env
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ POSTGRES_USER=local
POSTGRES_PASSWORD=local
POSTGRES_DB=tabnews
POSTGRES_HOST=localhost
POSTGRES_PORT=54320
POSTGRES_PORT=54320
WEBSERVER_HOST=localhost
WEBSERVER_PORT=3000
5 changes: 0 additions & 5 deletions .env.test

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/lint-styles.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Lint Styles

on: [push, pull_request]
on: [pull_request]

jobs:
lint-styles:
Expand Down
13 changes: 11 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
name: Tests

on: [push, pull_request]
on: [pull_request]

jobs:
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Start containers
run: docker-compose -f infra/docker-compose.development.yml up -d

- uses: actions/setup-node@v2
with:
node-version: "14"
- run: npm ci
- run: npm test
- run: npm run build
- run: npm start & npm run test:ci

- name: Stop containers
if: always()
run: docker-compose -f infra/docker-compose.development.yml down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ typings/
.yarn-integrity

# dotenv environment variables file
.env
.env*.local

# parcel-bundler cache (https://parceljs.org/)
Expand Down
9 changes: 0 additions & 9 deletions docker-compose.test.yml

This file was deleted.

22 changes: 18 additions & 4 deletions infra/database.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
import { Pool } from "pg";

export default function DatabaseFactory() {
const pool = new Pool({
const poolConfiguration = {
user: process.env.POSTGRES_USER,
host: process.env.POSTGRES_HOST,
database: process.env.POSTGRES_DB,
password: process.env.POSTGRES_PASSWORD,
port: process.env.POSTGRES_PORT,
});
ssl: {
rejectUnauthorized: false,
},
};

// https://github.com/filipedeschamps/tabnews.com.br/issues/84
if (
["test", "development"].includes(process.env.NODE_ENV) ||
process.env.CI
) {
delete poolConfiguration.ssl;
}

const pool = new Pool(poolConfiguration);

async function query(query) {
const results = await pool.query(query);
return results;
}

async function getNewConnectionClient() {
async function getNewConnectedClient() {
// When manually creating a new connection like this,
// you need to make sure to close it afterward
// with the .end() method.
Expand All @@ -23,6 +36,7 @@ export default function DatabaseFactory() {

return {
query,
getNewConnectionClient,
getNewConnectedClient,
pool,
};
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
version: "2.4"
services:
postgres:
container_name: "postgres"
postgres_dev:
container_name: "postgres-dev"
image: "postgres:13.3-alpine"
env_file:
- .env.development
- ../.env
ports:
- "54320:5432"
volumes:
Expand Down
9 changes: 5 additions & 4 deletions infra/migrator.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
const { join, resolve } = require("path");
import databaseFactory from "infra/database.js";
import migrationRunner from "node-pg-migrate";

export default function Migrator() {
const defaultConfigurations = {
dir: "./infra/migrations",
dir: join(resolve("."), "infra", "migrations"),
direction: "up",
migrationsTable: "migrations",
verbose: true,
verbose: false,
};

async function listPendingMigrations() {
const database = databaseFactory();
const databaseClient = await database.getNewConnectionClient();
const databaseClient = await database.getNewConnectedClient();
const pendingMigrations = await migrationRunner({
...defaultConfigurations,
dbClient: databaseClient,
Expand All @@ -25,7 +26,7 @@ export default function Migrator() {

async function runPendingMigrations() {
const database = databaseFactory();
const databaseClient = await database.getNewConnectionClient();
const databaseClient = await database.getNewConnectedClient();

const migratedMigrations = await migrationRunner({
...defaultConfigurations,
Expand Down
Loading

1 comment on commit 5dde95f

@vercel
Copy link

@vercel vercel bot commented on 5dde95f Jul 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.