Skip to content

Commit 8923de8

Browse files
committed
add test and fix bugs
1 parent 64a64c4 commit 8923de8

File tree

22 files changed

+2504
-34
lines changed

22 files changed

+2504
-34
lines changed

.env.test

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
APP_ENVIRONMENT=test
2+
APP_APPLICATION__HOST=127.0.0.1
3+
APP_APPLICATION__PORT=8081
4+
DATABASE_URL=postgres://testuser:testpass@localhost:5432/testdb?sslmode=disable
5+
TELEMETRY_SENTRY_DSN=

.github/workflows/go.yml

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232
run: go build -v ./...
3333

3434
test:
35-
name: Test Suite
35+
name: Run Tests
3636
runs-on: ubuntu-latest
3737
services:
3838
postgres:
@@ -48,6 +48,7 @@ jobs:
4848
--health-retries 5
4949
ports:
5050
- 5432:5432
51+
5152
steps:
5253
- name: Checkout sources
5354
uses: actions/checkout@v4
@@ -62,31 +63,66 @@ jobs:
6263
go install github.com/a-h/templ/cmd/templ@latest
6364
go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest
6465
go install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@latest
66+
go install gotest.tools/gotestsum@latest
6567
6668
- name: Generate code
6769
run: |
6870
templ generate
6971
sqlc generate
7072
71-
- name: Run migrations
73+
- name: Download dependencies
74+
run: go mod download
75+
76+
- name: Run Unit Tests
77+
run: |
78+
gotestsum --format testname -- -v -race -coverprofile=coverage.out ./...
79+
80+
- name: Run Integration Tests
7281
env:
7382
DATABASE_URL: postgres://postgres:password@localhost:5432/robswebhub_test?sslmode=disable
7483
run: |
7584
migrate -path migrations -database "$DATABASE_URL" up
85+
gotestsum --format testname -- -v -race -tags=integration ./...
7686
77-
- name: Run tests
87+
- name: Run E2E Tests
7888
env:
7989
DATABASE_URL: postgres://postgres:password@localhost:5432/robswebhub_test?sslmode=disable
80-
run: go test -v -race -coverprofile=coverage.out ./...
90+
run: |
91+
gotestsum --format testname -- -v -race -tags=e2e ./...
8192
82-
- name: Upload coverage reports
93+
- name: Generate Test Report
94+
if: always()
95+
run: |
96+
gotestsum --junitfile test-results.xml --format standard-verbose -- -v ./...
97+
98+
- name: Upload Test Results
99+
if: always()
100+
uses: actions/upload-artifact@v4
101+
with:
102+
name: test-results
103+
path: test-results.xml
104+
105+
- name: Upload Coverage Reports
83106
uses: codecov/codecov-action@v4
84107
with:
85108
file: ./coverage.out
86109
flags: unittests
87110
name: codecov-umbrella
88111
continue-on-error: true
89112

113+
- name: Display Test Summary
114+
if: always()
115+
run: |
116+
echo "## Test Summary" >> $GITHUB_STEP_SUMMARY
117+
echo "Test results have been generated with gotestsum" >> $GITHUB_STEP_SUMMARY
118+
if [ -f test-results.xml ]; then
119+
echo "✅ Test report available as artifact" >> $GITHUB_STEP_SUMMARY
120+
fi
121+
if [ -f coverage.out ]; then
122+
echo "📊 Coverage report uploaded to Codecov" >> $GITHUB_STEP_SUMMARY
123+
go tool cover -func=coverage.out | tail -n 1 >> $GITHUB_STEP_SUMMARY
124+
fi
125+
90126
lints:
91127
name: Lints
92128
runs-on: ubuntu-latest

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ tmp/
1313
# Output of the go coverage tool
1414
*.out
1515
coverage.html
16+
coverage.out
1617

1718
# Dependency directories
1819
vendor/

Dockerfile.test

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
FROM golang:1.23-alpine AS test
2+
3+
RUN apk add --no-cache git make gcc musl-dev
4+
5+
WORKDIR /app
6+
7+
# Install tools
8+
RUN go install github.com/a-h/templ/cmd/templ@latest
9+
RUN go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest
10+
RUN go install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@latest
11+
12+
# Copy go mod files
13+
COPY go.mod go.sum ./
14+
RUN go mod download
15+
16+
# Copy source code
17+
COPY . .
18+
19+
# Generate code
20+
RUN templ generate
21+
RUN sqlc generate
22+
23+
# Run tests
24+
CMD ["go", "test", "-v", "./..."]

go.mod

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,52 +16,103 @@ require (
1616
github.com/joho/godotenv v1.5.1
1717
github.com/lib/pq v1.10.9
1818
github.com/spf13/viper v1.18.2
19+
github.com/testcontainers/testcontainers-go v0.38.0
20+
github.com/testcontainers/testcontainers-go/modules/postgres v0.38.0
1921
)
2022

2123
require (
24+
dario.cat/mergo v1.0.1 // indirect
25+
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
26+
github.com/Microsoft/go-winio v0.6.2 // indirect
2227
github.com/bytedance/sonic v1.10.2 // indirect
28+
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
2329
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
2430
github.com/chenzhuoyu/iasm v0.9.1 // indirect
31+
github.com/containerd/errdefs v1.0.0 // indirect
32+
github.com/containerd/errdefs/pkg v0.3.0 // indirect
33+
github.com/containerd/log v0.1.0 // indirect
34+
github.com/containerd/platforms v0.2.1 // indirect
35+
github.com/cpuguy83/dockercfg v0.3.2 // indirect
36+
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
37+
github.com/distribution/reference v0.6.0 // indirect
38+
github.com/docker/docker v28.3.3+incompatible // indirect
39+
github.com/docker/go-connections v0.5.0 // indirect
40+
github.com/docker/go-units v0.5.0 // indirect
41+
github.com/ebitengine/purego v0.8.4 // indirect
42+
github.com/felixge/httpsnoop v1.0.4 // indirect
2543
github.com/fsnotify/fsnotify v1.7.0 // indirect
2644
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
2745
github.com/gin-contrib/sse v0.1.0 // indirect
46+
github.com/go-logr/logr v1.4.3 // indirect
47+
github.com/go-logr/stdr v1.2.2 // indirect
48+
github.com/go-ole/go-ole v1.2.6 // indirect
2849
github.com/go-playground/locales v0.14.1 // indirect
2950
github.com/go-playground/universal-translator v0.18.1 // indirect
3051
github.com/go-playground/validator/v10 v10.20.0 // indirect
3152
github.com/goccy/go-json v0.10.2 // indirect
53+
github.com/gogo/protobuf v1.3.2 // indirect
54+
github.com/grpc-ecosystem/grpc-gateway/v2 v2.27.2 // indirect
3255
github.com/hashicorp/errwrap v1.1.0 // indirect
3356
github.com/hashicorp/go-multierror v1.1.1 // indirect
3457
github.com/hashicorp/hcl v1.0.0 // indirect
3558
github.com/jackc/pgpassfile v1.0.0 // indirect
3659
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect
3760
github.com/jackc/puddle/v2 v2.2.1 // indirect
3861
github.com/json-iterator/go v1.1.12 // indirect
62+
github.com/klauspost/compress v1.18.0 // indirect
3963
github.com/klauspost/cpuid/v2 v2.2.6 // indirect
4064
github.com/leodido/go-urn v1.4.0 // indirect
41-
github.com/magiconair/properties v1.8.7 // indirect
65+
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
66+
github.com/magiconair/properties v1.8.10 // indirect
4267
github.com/mattn/go-isatty v0.0.20 // indirect
4368
github.com/mitchellh/mapstructure v1.5.0 // indirect
69+
github.com/moby/docker-image-spec v1.3.1 // indirect
70+
github.com/moby/go-archive v0.1.0 // indirect
71+
github.com/moby/patternmatcher v0.6.0 // indirect
72+
github.com/moby/sys/sequential v0.6.0 // indirect
73+
github.com/moby/sys/user v0.4.0 // indirect
74+
github.com/moby/sys/userns v0.1.0 // indirect
75+
github.com/moby/term v0.5.0 // indirect
4476
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
4577
github.com/modern-go/reflect2 v1.0.2 // indirect
78+
github.com/morikuni/aec v1.0.0 // indirect
79+
github.com/opencontainers/go-digest v1.0.0 // indirect
80+
github.com/opencontainers/image-spec v1.1.1 // indirect
4681
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
82+
github.com/pkg/errors v0.9.1 // indirect
83+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
84+
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
4785
github.com/sagikazarmark/locafero v0.4.0 // indirect
4886
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
87+
github.com/shirou/gopsutil/v4 v4.25.5 // indirect
88+
github.com/sirupsen/logrus v1.9.3 // indirect
4989
github.com/sourcegraph/conc v0.3.0 // indirect
5090
github.com/spf13/afero v1.11.0 // indirect
5191
github.com/spf13/cast v1.6.0 // indirect
5292
github.com/spf13/pflag v1.0.5 // indirect
93+
github.com/stretchr/testify v1.11.1 // indirect
5394
github.com/subosito/gotenv v1.6.0 // indirect
95+
github.com/tklauser/go-sysconf v0.3.12 // indirect
96+
github.com/tklauser/numcpus v0.6.1 // indirect
5497
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
5598
github.com/ugorji/go/codec v1.2.12 // indirect
99+
github.com/yusufpapurcu/wmi v1.2.4 // indirect
100+
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
101+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.54.0 // indirect
102+
go.opentelemetry.io/otel v1.37.0 // indirect
103+
go.opentelemetry.io/otel/metric v1.37.0 // indirect
104+
go.opentelemetry.io/otel/sdk v1.37.0 // indirect
105+
go.opentelemetry.io/otel/trace v1.37.0 // indirect
56106
go.uber.org/multierr v1.11.0 // indirect
57107
golang.org/x/arch v0.6.0 // indirect
58108
golang.org/x/crypto v0.40.0 // indirect
59109
golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc // indirect
60110
golang.org/x/net v0.42.0 // indirect
61111
golang.org/x/sync v0.16.0 // indirect
62112
golang.org/x/sys v0.34.0 // indirect
63-
golang.org/x/text v0.27.0 // indirect
64-
google.golang.org/protobuf v1.34.2 // indirect
113+
golang.org/x/text v0.28.0 // indirect
114+
google.golang.org/grpc v1.75.0 // indirect
115+
google.golang.org/protobuf v1.36.7 // indirect
65116
gopkg.in/ini.v1 v1.67.0 // indirect
66117
gopkg.in/yaml.v3 v3.0.1 // indirect
67118
)

0 commit comments

Comments
 (0)