Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: server test with docker CI #36

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
c91bcf5
fix: refactor and fix server tests
kartikp36 Aug 3, 2021
fe7d6c4
chore: initialize docker in server
kartikp36 Aug 11, 2021
6cad33f
fix: minor fixes
kartikp36 Aug 13, 2021
36bbc83
fix: integrate postgres & docker and copy types.d.ts
kartikp36 Aug 15, 2021
321d9c8
feat: add docker-compose ci
kartikp36 Aug 16, 2021
8abb27a
fix: path of docker-compose
kartikp36 Aug 16, 2021
1fa702b
fix: env vars for postgres and command fix
kartikp36 Aug 16, 2021
3fca112
Merge branch 'KB-139-fix-server-test' into KB-147-server-test-with-do…
kartikp36 Aug 16, 2021
58153e5
fix: docker-postgres fixed and initialize pool in db.ts
kartikp36 Aug 20, 2021
cd7f2e6
fix: update docker-compose.yml
kartikp36 Aug 20, 2021
f438fed
fix: make test exit
kartikp36 Aug 20, 2021
138c520
fix: initialise notes table in jest test
kartikp36 Aug 21, 2021
97a2e42
fix: timeout and fixs
kartikp36 Aug 22, 2021
6126be3
fix: jest test waits for 10 seconds for postgres
kartikp36 Aug 22, 2021
38ad451
fix: initialise beforeall and drop afterall in Jest and timeout 5 mins
kartikp36 Aug 24, 2021
5bc7771
fix: add jest.config.js and minor fixes
kartikp36 Aug 24, 2021
0b6e1a8
fix: run test file only once
kartikp36 Aug 24, 2021
db491ca
refactor: delete server/types, move docker-compose.yml and minor fixes
kartikp36 Aug 25, 2021
dcab86f
fix: ./docker-compose and timeout 3 mins
kartikp36 Aug 25, 2021
8386daf
fix: db exits with app container
kartikp36 Aug 25, 2021
4d49c4c
Merge branch 'KB-147-server-test-with-docker-ci' of https://github.co…
kartikp36 Aug 25, 2021
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
28 changes: 28 additions & 0 deletions .github/workflows/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Docker Test

on: [pull_request, workflow_dispatch]
jobs:
test:
name: Run test suite
runs-on: ubuntu-latest
timeout-minutes: 3
env:
POSTGRES_USER: "postgres"
POSTGRES_PASSWORD: "postgres"
POSTGRES_DB: "notes_data"
COMPOSE_FILE: ./docker-compose.yml
DOCKER_USER: ${{ secrets.DOCKER_USER }}
DOCKER_PASS: ${{ secrets.DOCKER_PASS }}

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Login to DockerHub
run: docker login -u $DOCKER_USER -p $DOCKER_PASS
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are we logging to DockerHub again? Can we not use the Dockerfile from this repo?

Copy link
Member Author

Choose a reason for hiding this comment

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

We need to login for Dockerfile to PULL images.


- name: Build docker images
run: docker-compose build

- name: Run tests
run: docker-compose up --abort-on-container-exit
31 changes: 31 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
version: "3"
services:
db:
image: postgres:10
networks:
- webnet
environment:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
POSTGRES_DB: notes_data
ports:
- "5432:5432"
app:
image: server_app
build:
context: ./
dockerfile: ./server/Dockerfile
depends_on:
- db
ports:
- "5000:5000"
environment:
DATABASE_URL: "postgres://postgres:postgres@db:5432/notes_data?sslmode=disable"
networks:
- webnet
volumes:
- .:/app/
- /app/node_modules
command: npm test
networks:
webnet:
7 changes: 7 additions & 0 deletions server/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM node
WORKDIR /usr/app
COPY types.d.ts ../
COPY ./server .
RUN npm install typescript -g && npm install
EXPOSE 3000
CMD npm test
11 changes: 11 additions & 0 deletions server/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as pg from "pg";
import * as env from "./env";

const pool = new pg.Pool({
connectionString: env.DATABASE_URL,
ssl: {
rejectUnauthorized: false,
},
});

export default pool;
4 changes: 4 additions & 0 deletions server/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
};
7 changes: 4 additions & 3 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"dev": "tsnd --respawn --transpile-only server.ts",
"lint": "eslint . --ext .ts",
"lint:fix": "eslint . --ext .ts --fix",
"test": "jest"
"test": "jest --detectOpenHandles -- ./test/index.test.ts"
},
"repository": {
"type": "git",
Expand All @@ -34,7 +34,7 @@
},
"devDependencies": {
"@types/express": "^4.17.11",
"@types/jest": "^26.0.22",
"@types/jest": "^27.0.1",
"@types/lodash": "^4.14.168",
"@types/node": "^14.14.41",
"@types/pg": "^7.14.11",
Expand All @@ -43,10 +43,11 @@
"eslint": "^7.24.0",
"eslint-config-prettier": "^8.2.0",
"eslint-plugin-prettier": "^3.4.0",
"jest": "^26.6.3",
"jest": "^27.0.3",
"nodemon": "^2.0.7",
"prettier": "^2.2.1",
"supertest": "^6.0.1",
"ts-jest": "^27.0.2",
"ts-node": "^9.1.1",
"ts-node-dev": "^1.1.6",
"typescript": "^4.2.4"
Expand Down
8 changes: 0 additions & 8 deletions server/server.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
import app from "./index";
import * as env from "./env";
import * as pg from "pg";
import { initialiseSQLTable } from "./service";

export const pool = new pg.Pool({
connectionString: env.DATABASE_URL,
ssl: {
rejectUnauthorized: false,
},
});

initialiseSQLTable();
const port = env.SERVER_PORT;
app.listen(port, () => console.log(`Listening at port ${port} ...`));
3 changes: 1 addition & 2 deletions server/service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import _ from "lodash";
import { v4 as uuidv4 } from "uuid";

import { pool } from "./server";
import pool from "./db";

const handleSQLQuery = async (sqlQuery, values?) => {
const client = await pool.connect();
Expand Down
33 changes: 27 additions & 6 deletions server/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import request from "supertest";

import app from "../index";
import pool from "../db";

describe("http requests testing", () => {
beforeAll(async () => {
const client = await pool.connect();
await client.query(
`CREATE TABLE IF NOT EXISTS "notes"(id UUID PRIMARY KEY, title VARCHAR(32) NOT NULL, "noteContent" TEXT NOT NULL, created TIMESTAMPTZ, "lastUpdated" TIMESTAMPTZ)`
);
client.release(true);
});

let secondId;
test("Initial GET request", async () => {
const response = await request(app).get("/notes");
expect(response.statusCode).toBe(200);
Expand Down Expand Up @@ -32,6 +42,7 @@ describe("http requests testing", () => {
title: "2nd",
noteContent: "This is Second note",
});
secondId = response.text;
expect(response.statusCode).toBe(200);
});

Expand All @@ -45,28 +56,38 @@ describe("http requests testing", () => {
});

test("GET request of second note by id", async () => {
const response = await request(app).get("/notes/2");
const response = await request(app).get(`/notes/${secondId}`);
expect(response.statusCode).toStrictEqual(200);
const noteJson: Note = response.body;
expect(noteJson.title).toBe("2nd");
expect(noteJson.noteContent).toBe("This is Second note");
});

test("GET request by id to get 404 response", async () => {
const response = await request(app).get("/notes/99");
test("GET request by a random id to get 404 response", async () => {
const response = await request(app).get(
"/notes/c61f1fc9-e0d2-43ce-a6c1-b5436cdebe7d"
Copy link
Contributor

Choose a reason for hiding this comment

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

Where is c61f1fc9-e0d2-43ce-a6c1-b5436cdebe7d coming from

Copy link
Member Author

Choose a reason for hiding this comment

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

It's just an example in uuid format as 99 is invalid now.

);
expect(response.statusCode).toStrictEqual(404);
});

test("DELETE request of second note by id", async () => {
const response = await request(app).delete("/notes/2");
const response = await request(app).delete(`/notes/${secondId}`);
expect(response.statusCode).toStrictEqual(200);
});
test("GET request of deleted second note", async () => {
const response = await request(app).get("/notes/2");
const response = await request(app).get(`/notes/${secondId}`);
expect(response.statusCode).toStrictEqual(404);
});
test("DELETE request to get bad request response", async () => {
const response = await request(app).delete("/notes/2");
const response = await request(app).delete(
"/notes/c61f1fc9-e0d2-43ce-a6c1-b5436cdebe7d"
);
expect(response.statusCode).toStrictEqual(400);
});

afterAll(async () => {
const client = await pool.connect();
await client.query(`DROP TABLE notes`);
client.release(true);
});
});
2 changes: 1 addition & 1 deletion server/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
"noImplicitAny": false,
"typeRoots": ["./node_modules/@types", "../types.d.ts"]
},
"exclude": ["build", "node_modules", "test"],
"exclude": ["build", "node_modules", "jest.config.js"],
"include": ["./", "../types.d.ts"]
}
Loading