-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(integration-tests): adding boilerplate for integration tests (#15)
* feat(integration-tests): adding boilerplate for integration tests * feat(integration-tests): ensure database is reset between tests * feat(integration-tests): add compose absed test to CI * feat(integration-tests): move to go 1.22.5 * feat(integration-tests): add test env file for fake creds
- Loading branch information
1 parent
141f7bc
commit 75349a3
Showing
9 changed files
with
173 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
SERVER_PORT="8080" | ||
PG_CONN="postgres://url_short:password@db-test:5432/url_short?sslmode=disable" | ||
JWT_SECRET="mY+gjoSSg9qeEE0J3mDIlEkp3cEMk0sRReoNkOnLmnGYiWj0/2K0zl9cj1e8QJ34 | ||
LHc0sHPkhqATCfB9ENHxNQ==" | ||
RDB_CONN="redis://redis:6379/0" |
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,20 +30,18 @@ jobs: | |
uses: golangci/[email protected] | ||
with: | ||
version: latest | ||
# TODO: Move to use compose based tests | ||
unit-test: | ||
test: | ||
needs: [fmt, lint] | ||
name: unit-test | ||
name: test | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: Setup Go environment | ||
uses: actions/[email protected] | ||
with: | ||
go-version: '1.22.3' | ||
- run: go test ./... | ||
- name: Build test container | ||
run: make build/test | ||
- name: Run Tests | ||
run: make test | ||
build: | ||
needs: unit-test | ||
needs: test | ||
name: build | ||
runs-on: ubuntu-latest | ||
steps: | ||
|
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
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,10 +1,29 @@ | ||
version: '3.1' | ||
|
||
services: | ||
api: | ||
api-test: | ||
image: url-short:test | ||
env_file: | ||
- .env | ||
- .envtest | ||
volumes: | ||
- ./:/opt/url-short | ||
|
||
db-test: | ||
image: postgres:16-alpine | ||
restart: always | ||
environment: | ||
PGDATA: /var/lib/postgresql/data/pgdata | ||
POSTGRES_USER: postgres | ||
POSTGRES_PASSWORD: password | ||
user: postgres | ||
volumes: | ||
- pgdatatest:/var/lib/postgresql/data | ||
- ./bin/local-init.sql:/docker-entrypoint-initdb.d/local-init.sql | ||
ports: | ||
- 5002:5432 | ||
healthcheck: | ||
test: ["CMD-SHELL", "pg_isready"] | ||
interval: 2s | ||
timeout: 5s | ||
retries: 10 | ||
volumes: | ||
pgdatatest: |
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
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,12 +1,47 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"database/sql" | ||
"encoding/json" | ||
"errors" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"testing" | ||
|
||
"github.com/pressly/goose/v3" | ||
|
||
"url-short/internal/database" | ||
) | ||
|
||
func resetDB(db *sql.DB) error { | ||
provider, err := goose.NewProvider( | ||
goose.DialectPostgres, | ||
db, | ||
os.DirFS("./sql/schema/"), | ||
) | ||
|
||
if err != nil { | ||
return errors.New("can not create goose provider") | ||
} | ||
|
||
_, err = provider.DownTo(context.Background(), 0) | ||
|
||
if err != nil { | ||
return errors.New("can not reset database") | ||
} | ||
|
||
_, err = provider.Up(context.Background()) | ||
|
||
if err != nil { | ||
return errors.New("could not run migrations") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func TestHealthEndpoint(t *testing.T) { | ||
t.Run("test healthz endpoint", func(t *testing.T) { | ||
request, _ := http.NewRequest(http.MethodGet, "/api/v1/healthz", nil) | ||
|
@@ -16,6 +51,9 @@ func TestHealthEndpoint(t *testing.T) { | |
|
||
apiCfg.healthz(response, request) | ||
|
||
// its not a good idea to use a stuct we already define in our code | ||
// this could introduce a subtle bug where a test could pass because | ||
// we incorrectly altered this struct | ||
got := HealthResponse{} | ||
err := json.NewDecoder(response.Body).Decode(&got) | ||
|
||
|
@@ -32,3 +70,45 @@ func TestHealthEndpoint(t *testing.T) { | |
} | ||
}) | ||
} | ||
|
||
func TestPostUser(t *testing.T) { | ||
dbURL := os.Getenv("PG_CONN") | ||
db, err := sql.Open("postgres", dbURL) | ||
|
||
if err != nil { | ||
t.Errorf("can not open database connection") | ||
} | ||
|
||
err = resetDB(db) | ||
|
||
if err != nil { | ||
t.Errorf("could not resetDB %q", err) | ||
} | ||
|
||
dbQueries := database.New(db) | ||
|
||
t.Run("test user creation", func(t *testing.T) { | ||
requestJSON := []byte(`{"email": "[email protected]", "password": "test"}`) | ||
request, _ := http.NewRequest(http.MethodPost, "/api/v1/users", bytes.NewBuffer(requestJSON)) | ||
request.Header.Set("Content-Type", "application/json") | ||
|
||
response := httptest.NewRecorder() | ||
|
||
apiCfg := apiConfig{ | ||
DB: dbQueries, | ||
} | ||
|
||
apiCfg.postAPIUsers(response, request) | ||
|
||
got := APIUsersResponse{} | ||
err := json.NewDecoder(response.Body).Decode(&got) | ||
|
||
if err != nil { | ||
t.Errorf("unable to parse response %q into %q", response.Body, got) | ||
} | ||
|
||
if got.Email != "[email protected]" { | ||
t.Errorf("unexpected email in response, got %q, wanted %q", got.Email, "[email protected]") | ||
} | ||
}) | ||
} |
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