Skip to content

Commit 7336593

Browse files
committed
speed up tests
1 parent 0895126 commit 7336593

File tree

5 files changed

+59
-117
lines changed

5 files changed

+59
-117
lines changed

internal/database/database_test.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,30 @@ package database_test
22

33
import (
44
"context"
5+
"log"
56
"testing"
67
"time"
78

89
"github.com/gitznik/robswebhub/internal/database"
910
"github.com/gitznik/robswebhub/internal/testhelpers"
1011
"github.com/google/uuid"
12+
"github.com/testcontainers/testcontainers-go"
1113
)
1214

13-
func TestDatabase_CreateAndGetMatch(t *testing.T) {
14-
db := testhelpers.SetupTestDB(t)
15-
defer db.Cleanup()
15+
var (
16+
db *testhelpers.TestDB
17+
)
18+
19+
func TestMain(m *testing.M) {
20+
db = testhelpers.SetupTestDB()
21+
db.Pool.Close()
22+
if err := testcontainers.TerminateContainer(db.Container); err != nil {
23+
log.Printf("Failed to terminate container: %v", err)
24+
}
25+
}
1626

27+
func TestDatabase_CreateAndGetMatch(t *testing.T) {
28+
t.Parallel()
1729
ctx := context.Background()
1830
matchID := uuid.New()
1931

@@ -55,8 +67,7 @@ func TestDatabase_CreateAndGetMatch(t *testing.T) {
5567
}
5668

5769
func TestDatabase_CreateAndGetScores(t *testing.T) {
58-
db := testhelpers.SetupTestDB(t)
59-
defer db.Cleanup()
70+
t.Parallel()
6071

6172
ctx := context.Background()
6273

@@ -135,8 +146,7 @@ func TestDatabase_CreateAndGetScores(t *testing.T) {
135146
}
136147

137148
func TestDatabase_BulkCreateScores(t *testing.T) {
138-
db := testhelpers.SetupTestDB(t)
139-
defer db.Cleanup()
149+
t.Parallel()
140150

141151
ctx := context.Background()
142152

@@ -196,8 +206,7 @@ func TestDatabase_BulkCreateScores(t *testing.T) {
196206
}
197207

198208
func TestDatabase_EdgeCases(t *testing.T) {
199-
db := testhelpers.SetupTestDB(t)
200-
defer db.Cleanup()
209+
t.Parallel()
201210

202211
ctx := context.Background()
203212

@@ -267,8 +276,7 @@ func TestDatabase_EdgeCases(t *testing.T) {
267276
}
268277

269278
func TestDatabase_Transactions(t *testing.T) {
270-
db := testhelpers.SetupTestDB(t)
271-
defer db.Cleanup()
279+
t.Parallel()
272280

273281
ctx := context.Background()
274282

internal/handlers/e2e_test.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,41 @@ package handlers_test
33
import (
44
"context"
55
"fmt"
6+
"log"
67
"net/http"
78
"net/http/httptest"
89
"net/url"
910
"strings"
1011
"testing"
1112
"time"
1213

14+
"github.com/gin-gonic/gin"
1315
"github.com/gitznik/robswebhub/internal/database"
1416
"github.com/gitznik/robswebhub/internal/testhelpers"
1517
"github.com/google/uuid"
18+
"github.com/testcontainers/testcontainers-go"
1619
)
1720

21+
var (
22+
db *testhelpers.TestDB
23+
router *gin.Engine
24+
)
25+
26+
func TestMain(m *testing.M) {
27+
db = testhelpers.SetupTestDB()
28+
29+
router = testhelpers.SetupTestRouter(db.Queries)
30+
m.Run()
31+
32+
db.Pool.Close()
33+
if err := testcontainers.TerminateContainer(db.Container); err != nil {
34+
log.Printf("Failed to terminate container: %v", err)
35+
}
36+
}
37+
1838
// TestE2E_CompleteScoreWorkflow tests the complete scorekeeper workflow
1939
func TestE2E_CompleteScoreWorkflow(t *testing.T) {
20-
// Setup
21-
db := testhelpers.SetupTestDB(t)
22-
defer db.Cleanup()
23-
24-
router := testhelpers.SetupTestRouter(db.Queries)
40+
t.Parallel()
2541
ctx := context.Background()
2642

2743
// Step 1: Create a new match
@@ -160,10 +176,6 @@ func TestE2E_CompleteScoreWorkflow(t *testing.T) {
160176
// TestE2E_MultipleMatchesWorkflow tests managing multiple matches
161177
func TestE2E_MultipleMatchesWorkflow(t *testing.T) {
162178
// Setup
163-
db := testhelpers.SetupTestDB(t)
164-
defer db.Cleanup()
165-
166-
router := testhelpers.SetupTestRouter(db.Queries)
167179
ctx := context.Background()
168180

169181
// Create multiple matches
@@ -231,12 +243,6 @@ func TestE2E_MultipleMatchesWorkflow(t *testing.T) {
231243

232244
// TestE2E_ErrorHandlingWorkflow tests error scenarios
233245
func TestE2E_ErrorHandlingWorkflow(t *testing.T) {
234-
// Setup
235-
db := testhelpers.SetupTestDB(t)
236-
defer db.Cleanup()
237-
238-
router := testhelpers.SetupTestRouter(db.Queries)
239-
240246
t.Run("Handle non-existent match gracefully", func(t *testing.T) {
241247
nonExistentID := uuid.New()
242248

@@ -339,12 +345,6 @@ func TestE2E_ErrorHandlingWorkflow(t *testing.T) {
339345

340346
// TestE2E_NavigationWorkflow tests navigation between pages
341347
func TestE2E_NavigationWorkflow(t *testing.T) {
342-
// Setup
343-
db := testhelpers.SetupTestDB(t)
344-
defer db.Cleanup()
345-
346-
router := testhelpers.SetupTestRouter(db.Queries)
347-
348348
navigationPaths := []struct {
349349
name string
350350
path string

internal/handlers/handlers_integration_test.go

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,7 @@ import (
1414
)
1515

1616
func TestIntegration_HomePage(t *testing.T) {
17-
// Setup
18-
db := testhelpers.SetupTestDB(t)
19-
defer db.Cleanup()
20-
21-
router := testhelpers.SetupTestRouter(db.Queries)
17+
t.Parallel()
2218

2319
// Test GET /
2420
t.Run("GET / returns home page", func(t *testing.T) {
@@ -45,11 +41,7 @@ func TestIntegration_HomePage(t *testing.T) {
4541
}
4642

4743
func TestIntegration_AboutPage(t *testing.T) {
48-
// Setup
49-
db := testhelpers.SetupTestDB(t)
50-
defer db.Cleanup()
51-
52-
router := testhelpers.SetupTestRouter(db.Queries)
44+
t.Parallel()
5345

5446
t.Run("GET /about returns about page", func(t *testing.T) {
5547
req, _ := http.NewRequest("GET", "/about", nil)
@@ -63,12 +55,9 @@ func TestIntegration_AboutPage(t *testing.T) {
6355
}
6456

6557
func TestIntegration_ScoresIndex(t *testing.T) {
66-
// Setup
67-
db := testhelpers.SetupTestDB(t)
68-
defer db.Cleanup()
58+
t.Parallel()
6959

7060
testData := db.SeedTestData(t)
71-
router := testhelpers.SetupTestRouter(db.Queries)
7261

7362
t.Run("GET /scores without matchup_id", func(t *testing.T) {
7463
req, _ := http.NewRequest("GET", "/scores", nil)
@@ -112,12 +101,9 @@ func TestIntegration_ScoresIndex(t *testing.T) {
112101
}
113102

114103
func TestIntegration_SingleScoreSubmission(t *testing.T) {
104+
t.Parallel()
115105
// Setup
116-
db := testhelpers.SetupTestDB(t)
117-
defer db.Cleanup()
118-
119106
testData := db.SeedTestData(t)
120-
router := testhelpers.SetupTestRouter(db.Queries)
121107

122108
t.Run("POST /scores/single with valid data", func(t *testing.T) {
123109
formData := url.Values{
@@ -185,12 +171,9 @@ func TestIntegration_SingleScoreSubmission(t *testing.T) {
185171
}
186172

187173
func TestIntegration_BatchScoreSubmission(t *testing.T) {
174+
t.Parallel()
188175
// Setup
189-
db := testhelpers.SetupTestDB(t)
190-
defer db.Cleanup()
191-
192176
testData := db.SeedTestData(t)
193-
router := testhelpers.SetupTestRouter(db.Queries)
194177

195178
t.Run("POST /scores/batch with valid data", func(t *testing.T) {
196179
batchData := fmt.Sprintf(`%s %s 2:1
@@ -243,12 +226,9 @@ invalid line
243226
}
244227

245228
func TestIntegration_ScoreForms(t *testing.T) {
229+
t.Parallel()
246230
// Setup
247-
db := testhelpers.SetupTestDB(t)
248-
defer db.Cleanup()
249-
250231
testData := db.SeedTestData(t)
251-
router := testhelpers.SetupTestRouter(db.Queries)
252232

253233
t.Run("GET /scores/single-form", func(t *testing.T) {
254234
req, _ := http.NewRequest("GET", fmt.Sprintf("/scores/single-form?matchup_id=%s", testData.MatchID), nil)
@@ -272,12 +252,9 @@ func TestIntegration_ScoreForms(t *testing.T) {
272252
}
273253

274254
func TestIntegration_ScoresChart(t *testing.T) {
255+
t.Parallel()
275256
// Setup
276-
db := testhelpers.SetupTestDB(t)
277-
defer db.Cleanup()
278-
279257
testData := db.SeedTestData(t)
280-
router := testhelpers.SetupTestRouter(db.Queries)
281258

282259
t.Run("GET /scores/chart/:id with valid match", func(t *testing.T) {
283260
req, _ := http.NewRequest("GET", fmt.Sprintf("/scores/chart/%s", testData.MatchID), nil)

internal/testhelpers/fixtures.go

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,17 @@
11
package testhelpers
22

33
import (
4-
"os"
54
"path/filepath"
65
"runtime"
76
)
87

9-
// GetProjectRoot returns the project root directory
10-
// This helps tests find migrations and static files regardless of where they're run from
118
func GetProjectRoot() string {
129
_, b, _, _ := runtime.Caller(0)
1310
d := filepath.Dir(b)
14-
// Go up from internal/testhelpers to project root
1511
return filepath.Dir(filepath.Dir(d))
1612
}
1713

18-
// GetMigrationsPath returns the correct path to migrations directory
1914
func GetMigrationsPath() string {
2015
root := GetProjectRoot()
2116
return "file://" + filepath.Join(root, "migrations")
2217
}
23-
24-
// GetStaticPath returns the correct path to static directory
25-
func GetStaticPath() string {
26-
root := GetProjectRoot()
27-
return filepath.Join(root, "static")
28-
}
29-
30-
// TestInProjectRoot changes to project root for test execution
31-
func TestInProjectRoot() func() {
32-
originalWd, _ := os.Getwd()
33-
if err := os.Chdir(GetProjectRoot()); err != nil {
34-
panic("Could not change dir to project root")
35-
}
36-
return func() {
37-
if err := os.Chdir(originalWd); err != nil {
38-
panic("Could not change back to original dir")
39-
}
40-
}
41-
}

0 commit comments

Comments
 (0)