Skip to content

Commit 28c1663

Browse files
author
Your Name
committed
Switch from docker to testcontainers-go for go test
1 parent 8bf2fb0 commit 28c1663

19 files changed

Lines changed: 327 additions & 295 deletions

.claude/settings.local.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
"permissions": {
33
"allow": [
44
"Bash(go test:*)",
5-
"WebFetch(domain:github.com)"
5+
"WebFetch(domain:github.com)",
6+
"Skill(superpowers:brainstorming)",
7+
"Bash(ls:*)",
8+
"Bash(mkdir:*)",
9+
"Bash(go get:*)"
610
]
711
}
812
}

.github/workflows/golangci-lint.yml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@ jobs:
1616
name: lint
1717
runs-on: ubuntu-latest
1818
steps:
19-
- uses: actions/checkout@v3
20-
- uses: actions/setup-go@v4
19+
- uses: actions/checkout@v5
20+
- uses: actions/setup-go@v6
2121
with:
22-
go-version: "1.20"
23-
cache: false
22+
go-version: stable
2423
- name: golangci-lint
25-
uses: golangci/golangci-lint-action@v3
24+
uses: golangci/golangci-lint-action@v9
2625
with:
2726
version: latest
2827

.github/workflows/gosec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: gosec
22

33
env:
4-
GO_VERSION: "1.20"
4+
GO_VERSION: "1.24"
55
on:
66
pull_request:
77
push:

.github/workflows/mod-verify.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: mod-verify
22

33
env:
4-
GO_VERSION: "1.20"
4+
GO_VERSION: "1.24"
55
on:
66
push:
77
branches:

.github/workflows/staticcheck.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: staticcheck
22

33
env:
4-
GO_VERSION: "1.20"
4+
GO_VERSION: "1.24"
55
on:
66
pull_request:
77
push:

.github/workflows/test.yml

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: test
22

33
env:
4-
GO_VERSION: "1.20"
4+
GO_VERSION: "1.24"
55
on:
66
pull_request:
77
push:
@@ -27,10 +27,5 @@ jobs:
2727
run: go mod download
2828

2929
- name: Run tests with coverage
30-
run: go test -v -coverprofile=coverage.out ./...
31-
32-
- name: Upload coverage to Codecov
33-
uses: codecov/codecov-action@v3
34-
with:
35-
token: ${{ secrets.CODECOV_TOKEN }}
36-
file: ./coverage.out
30+
working-directory: ./tests
31+
run: go test -v

.golangci.yml

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
version: "2"
1+
# .golangci.yml
2+
version: '2'
23
run:
3-
go: "1.24"
4+
go: '1.24'
45
issues-exit-code: 1
56
tests: false
67
allow-parallel-runners: true
@@ -49,20 +50,12 @@ linters:
4950
- func
5051
depguard:
5152
rules:
52-
acl:
53-
files:
54-
- '!**/app/acl/**/*.go'
55-
deny:
56-
- pkg: ovya.fr/csite/app/dao/events/acl
57-
desc: acl MUST NOT be called from dao. Use app/acl/... instead
58-
- pkg: ovya.fr/csite/app/dao/personne/acq/alert/acl
59-
desc: acl MUST NOT be called from dao. Use app/acl/... instead
6053
logger:
6154
deny:
6255
- pkg: github.com/sirupsen/logrus
63-
desc: logging is allowed only by github.com/rs/zerolog
56+
desc: logging is allowed only by github.com/uber-go/zap
6457
- pkg: log
65-
desc: logging is allowed only by github.com/rs/zerolog
58+
desc: logging is allowed only by github.com/uber-go/zap
6659
dupl:
6760
threshold: 100
6861
funlen:
@@ -101,7 +94,7 @@ linters:
10194
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Errorf
10295
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Fatalf
10396
lll:
104-
line-length: 140
97+
line-length: 124
10598
mnd:
10699
checks:
107100
- argument
@@ -111,14 +104,21 @@ linters:
111104
- return
112105
- assign
113106
ignored-numbers:
114-
- "0666"
115-
- "0755"
116-
- "42"
117-
- "1"
118-
- "2"
119-
- "3"
120-
- "4"
121-
- "5"
107+
- '0666'
108+
- '0600'
109+
- '0644'
110+
- '0755'
111+
- '0777'
112+
- '0700'
113+
- '42'
114+
- '1'
115+
- '2'
116+
- '3'
117+
- '4'
118+
- '5'
119+
- '8'
120+
- '16'
121+
- '32'
122122
ignored-files:
123123
- magic1_.+\.go$
124124
ignored-functions:

CLAUDE.md

Lines changed: 23 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -31,32 +31,27 @@ The library constrains type parameter `T` to: `bool | int | int16 | int32 | int6
3131

3232
### Running Tests
3333

34-
**Run all tests (including PostgreSQL integration tests):**
34+
**All tests (including PostgreSQL):**
3535
```bash
36-
make test
36+
cd tests
37+
go test -v ./...
3738
```
38-
This builds a Docker container with PostgreSQL and runs the complete test suite.
3939

40-
**Run only unit tests (no database):**
40+
Or from the root:
4141
```bash
42-
cd tests
43-
go test -run TestMarshalUnmarshal -v
44-
go test -run TestNullableEdgeCases -v
42+
make test
4543
```
4644

47-
**Run specific database test:**
45+
**Single test:**
4846
```bash
4947
cd tests
50-
# Requires Docker to be running
5148
go test -run TestAllTypes -v
52-
go test -run TestInsertAndRead -v
5349
```
5450

55-
**Run a single test from the tests directory:**
56-
```bash
57-
cd tests
58-
go test -run TestName -v
59-
```
51+
**Requirements:**
52+
- Docker must be running locally
53+
- testcontainers-go automatically manages PostgreSQL container
54+
- First run downloads PostgreSQL 18 image (~80MB), subsequent runs use cached image
6055

6156
### Code Quality
6257

@@ -73,39 +68,22 @@ go mod tidy
7368
cd tests && go mod tidy
7469
```
7570

76-
### Docker Development
77-
78-
**Build Docker test image:**
79-
```bash
80-
make docker-build
81-
```
82-
83-
**Open shell in test container (for debugging):**
84-
```bash
85-
make docker-shell
86-
```
87-
88-
**Run tests interactively:**
89-
```bash
90-
make docker-run
91-
```
92-
9371
## Test Organization
9472

9573
The test suite is located in `tests/` directory with its own `go.mod` that uses a `replace` directive to reference the parent module.
9674

9775
**Test files:**
98-
- `nullable_test.go` - Unit tests for JSON marshaling/unmarshaling and edge cases
99-
- `postgres_test.go` - Integration tests with PostgreSQL database (requires Docker)
100-
- `setup_test.go` - Test fixtures and database connection helpers
101-
102-
**Test coverage verification:**
103-
```bash
104-
cd tests
105-
# Count test files
106-
find . -name "*_test.go" | wc -l
107-
# Should match the number of test files reported by `go test -v`
108-
```
76+
- `marshal_test.go` - Comprehensive tests for JSON marshaling/unmarshaling with complex nested structures
77+
- `nullable_test.go` - Unit tests for nullable value operations and edge cases
78+
- `postgres_test.go` - Integration tests with PostgreSQL database using testcontainers
79+
- `setup_test.go` - TestMain setup with testcontainers, database helpers, and cleanup utilities
80+
81+
**Test infrastructure:**
82+
- Uses testcontainers-go to automatically manage PostgreSQL 18 container
83+
- TestMain in setup_test.go starts container once for all tests
84+
- Shared database connection stored in package-level `testDB` variable
85+
- `cleanupTables()` helper truncates tables between tests for isolation
86+
- Container automatically terminates after tests complete
10987

11088
## Working with MarshalJSON/UnmarshalJSON
11189

@@ -139,13 +117,13 @@ The library integrates with `database/sql` through two interfaces:
139117
- **Go version:** 1.24.10
140118
- **Dependencies:**
141119
- `github.com/google/uuid` - UUID type support
142-
- Test dependencies: `pgx/v5`, `sqlx`, `testify`
120+
- Test dependencies: `pgx/v5`, `sqlx`, `testify`, `testcontainers-go`
143121

144122
## Common Gotchas
145123

146124
1. **Module structure**: Root module (`github.com/ovya/nullable`) and test module (`github.com/ovya/nullable/tests`) are separate. Always run `go mod tidy` in both directories after dependency changes.
147125

148-
2. **Test execution**: Integration tests require Docker. Use `make test` for full suite, or run unit tests directly with `go test` in the `tests/` directory.
126+
2. **Test execution**: Integration tests require Docker to be running (testcontainers uses it). Run `cd tests && go test -v ./...` or `make test` for the full suite.
149127

150128
3. **Type constraints**: The generic constraint limits supported types. Adding new primitive types requires updating the constraint in both `NullableI` interface and `Of[T]` struct definition.
151129

Makefile

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
1-
.PHONY: help docker-build docker-test docker-run docker-shell test tidy
1+
.PHONY: help test tidy
22

33
help: ## Show this help message
44
@echo 'Usage: make [target]'
55
@echo ''
66
@echo 'Available targets:'
77
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf " %-20s %s\n", $$1, $$2}'
88

9-
docker-build: ## Build the Docker image with PostgreSQL
10-
docker build -t nullable-postgres-test -f tests/Dockerfile .
11-
12-
docker-run: docker-build ## Build and run with interactive output
13-
docker run --rm -it nullable-postgres-test
14-
15-
docker-shell: docker-build ## Open a shell in the container
16-
docker run --rm -it nullable-postgres-test /bin/sh
17-
18-
test: docker-build ## Run all Go tests (including database tests with Docker)
19-
docker run --rm -t nullable-postgres-test
9+
test: ## Run all tests (including PostgreSQL integration tests)
10+
cd tests && go test -v ./...
2011

2112
tidy: ## Tidy Go modules
2213
go mod tidy
14+
cd tests && go mod tidy

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,19 +383,30 @@ err := json.Unmarshal([]byte(`null`), &value)
383383

384384
Run all tests including PostgreSQL integration tests:
385385

386+
```bash
387+
cd tests
388+
go test -v ./...
389+
```
390+
391+
Or from the root:
392+
386393
```bash
387394
make test
388395
```
389396

397+
**Requirements:**
398+
- Docker must be running (testcontainers uses Docker to spin up PostgreSQL)
399+
- No manual database setup needed - testcontainers handles everything
400+
401+
**First run:** Tests will download the PostgreSQL 18 image (~80MB), subsequent runs use cached image.
402+
390403
Run only unit tests (no database required):
391404

392405
```bash
393406
cd tests
394407
go test -run 'TestMarshal|TestUnmarshal|TestNullableEdgeCases' -v
395408
```
396409

397-
See the `tests/` directory for more examples and test cases.
398-
399410
## Comparison with Alternatives
400411

401412
| Feature | `nullable` | `database/sql.Null*` | `gopkg.in/guregu/null.v4` |

0 commit comments

Comments
 (0)