-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Run tests independently of development database
- Loading branch information
Showing
9 changed files
with
139 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,73 @@ | ||
#TOOLDIR = ./.bin | ||
.DEFAULT_GOAL := help | ||
|
||
LINTBIN = ./.bin/golangci-lint | ||
|
||
DB_URL ?=postgres://postgres:postgres@localhost:65432/optrwork?sslmode=disable | ||
APP_URL ?=http://localhost:8080 | ||
|
||
.phony: all | ||
all: | ||
echo "Specify target" | ||
help: # Show this help | ||
@egrep -h '\s#\s' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?# "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}' | ||
|
||
.phony: sqlc | ||
sqlc: | ||
cd pkg/db/sqlc && make sqlc | ||
|
||
.phony: migrate-up | ||
migrate-up: | ||
migrate-up: # Apply database migrations | ||
cd pkg/db/db-migrations && make $@ | ||
|
||
.phony: migrate-drop | ||
migrate-drop: | ||
migrate-drop: # Drop database migrations | ||
cd pkg/db/db-migrations && make $@ | ||
|
||
.phony: docker-compose-build | ||
docker-compose-build: # Build Docker image | ||
cd ops/docker-compose-dev && docker-compose build --no-cache | ||
|
||
.phony: docker-compose-up | ||
docker-compose-up: | ||
cd ops/docker-compose-dev && docker-compose up -d --build | ||
docker-compose-up: docker-compose-build # Start dockerized application with database in background | ||
cd ops/docker-compose-dev && docker-compose up -d | ||
|
||
.phony: docker-compose-down | ||
docker-compose-down: | ||
docker-compose-down: # Stop application | ||
cd ops/docker-compose-dev && docker-compose down | ||
|
||
.phony: run | ||
run: | ||
run: # Run application in development mode | ||
go run . --config ./testdata/dev.yaml run | ||
|
||
.phony: seed | ||
seed: # Populates database with sample data | ||
@env DB_URL=$(DB_URL) go run ./testdata/seed.go | ||
|
||
.phony: run-intest | ||
run-intest: | ||
env DB_URL=postgres://postgres:postgres@localhost:65432/optrwork?sslmode=disable APP_URL=http://localhost:8080 go test -count=1 -v ./intest/ | ||
|
||
$(LINTBIN): | ||
@echo "Getting $@" | ||
@mkdir -p $(@D) | ||
GOBIN=$(abspath $(@D)) go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.45.2 | ||
|
||
.phony: lint | ||
lint: $(LINTBIN) | ||
lint: $(LINTBIN) # Run linter | ||
$(LINTBIN) run ./... | ||
|
||
# NOTE: All lines below used for testing purpose only | ||
TEST_DB_URL ?=postgres://postgres:postgres@localhost:65433/optrwork_test?sslmode=disable | ||
|
||
.phony: test-start-database | ||
test-start-database: # Start dockerized database for testing purpose | ||
cd ops/docker-compose-test && docker-compose up postgres_test | ||
|
||
.phony: test-start-backend | ||
test-start-backend: # Run application in test mode | ||
go run . --config ./testdata/test.yaml run | ||
|
||
.phony: test-integration | ||
test-integration: test-migrate-drop test-migrate-up # Run integration tests | ||
env DB_URL="${TEST_DB_URL}" APP_URL=http://localhost:8081 go test -count=1 -v ./intest/ -test.failfast | ||
|
||
.phony: test-migrate-up | ||
test-migrate-up: # Apply migrations in test database | ||
cd pkg/db/db-migrations && env DB_URL="${TEST_DB_URL}" make migrate-up | ||
|
||
.phony: test-migrate-drop | ||
test-migrate-drop: # Drop migrations in test database | ||
cd pkg/db/db-migrations && env DB_URL="${TEST_DB_URL}" make migrate-drop |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# Optrispace backend | ||
|
||
Prerequisites: | ||
|
||
* docker engine v20 | ||
* docker-compose v1.29 | ||
* GNU Make v4 in PATH location | ||
|
||
## Start development version | ||
|
||
If you need to test development version of the backend just clone repo and | ||
issue commands in the root directory: | ||
|
||
```sh | ||
make docker-compose-up | ||
``` | ||
|
||
This will start REST API on [http://localhost:8080/](http://localhost:8080/). | ||
|
||
After that you may want to populate database with sample data. Just run: | ||
|
||
```sh | ||
make seed | ||
``` | ||
|
||
It's required sometimes to use clean run the application. | ||
For this just use following command: | ||
|
||
```sh | ||
make docker-compose-down | ||
``` | ||
|
||
## Run integration tests | ||
|
||
All test-specific commands provided by `Makefile` use `test-` prefix: | ||
|
||
* `test-start-database` | ||
* `test-start-backend` | ||
* `test-integration` | ||
|
||
Prerequisites: | ||
|
||
* Application with database successfully started | ||
* Go v1.18 installed in PATH location | ||
|
||
All required environment variables for testing purpose are set in `Makefile`. | ||
|
||
In the first terminal run the following command to start testing database: | ||
|
||
```sh | ||
make test-start-database | ||
``` | ||
|
||
It will run PostgreSQL on 65433 port. | ||
|
||
After that in another terminal session start the backend server: | ||
|
||
```sh | ||
make test-start-backend | ||
``` | ||
|
||
It will run applicaton's backend on tcp/8081 port. | ||
|
||
Now you are able to run integration tests: | ||
|
||
```sh | ||
make test-integration | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
COMPOSE_PROJECT_NAME=optrispace-work-dev | ||
|
||
POSTGRES_IMG=postgres:14.2-bullseye | ||
GOLANG_IMG=golang:1.18-bullseye | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,4 +30,4 @@ services: | |
- postgres | ||
|
||
volumes: | ||
pgdata-optrwork: | ||
pgdata-optrwork: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
COMPOSE_PROJECT_NAME=optrispace-work-test | ||
POSTGRES_IMG=postgres:14.2-bullseye |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
version: "3.7" | ||
services: | ||
postgres_test: | ||
image: $POSTGRES_IMG | ||
restart: unless-stopped | ||
environment: | ||
- POSTGRES_USER=postgres | ||
- POSTGRES_PASSWORD=postgres | ||
- POSTGRES_DB=optrwork_test | ||
ports: | ||
- 65433:5432 | ||
|
||
volumes: | ||
pgdata-optrwork-test: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
hide-banner: false | ||
log: | ||
level: trace | ||
caller: false | ||
pprof: | ||
hostport: :5005 | ||
server: | ||
host: :8081 | ||
cors: true | ||
db: | ||
url: postgres://postgres:postgres@localhost:65433/optrwork_test?sslmode=disable |