Skip to content

Commit eb98cff

Browse files
authored
backend chores and refactors (#22)
* refactor(tests): move test db setup to testutils package - Move db setup to testutils package for reuse * refactor(log): rework logger to have a factory pattern * chore: use OS-appropriate cache directory
2 parents fde1121 + 95794c1 commit eb98cff

File tree

8 files changed

+235
-479
lines changed

8 files changed

+235
-479
lines changed

internal/collections/manager_test.go

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,15 @@ package collections
22

33
import (
44
"context"
5-
"database/sql"
65
"fmt"
76
"testing"
87

98
"github.com/maniac-en/req/internal/crud"
10-
"github.com/maniac-en/req/internal/database"
11-
_ "github.com/mattn/go-sqlite3"
9+
"github.com/maniac-en/req/internal/testutils"
1210
)
1311

14-
func setupTestDB(t *testing.T) *database.Queries {
15-
db, err := sql.Open("sqlite3", ":memory:")
16-
if err != nil {
17-
t.Fatalf("Failed to open test database: %v", err)
18-
}
19-
20-
schema := `
21-
CREATE TABLE collections (
22-
id INTEGER PRIMARY KEY AUTOINCREMENT,
23-
name TEXT NOT NULL,
24-
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
25-
updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
26-
);`
27-
28-
if _, err := db.Exec(schema); err != nil {
29-
t.Fatalf("Failed to create test schema: %v", err)
30-
}
31-
32-
return database.New(db)
33-
}
34-
3512
func TestCollectionsManagerCRUD(t *testing.T) {
36-
db := setupTestDB(t)
13+
db := testutils.SetupTestDB(t, "collections")
3714
manager := NewCollectionsManager(db)
3815
ctx := context.Background()
3916

@@ -131,7 +108,7 @@ func TestCollectionsManagerCRUD(t *testing.T) {
131108
}
132109

133110
func TestCollectionsManagerValidation(t *testing.T) {
134-
db := setupTestDB(t)
111+
db := testutils.SetupTestDB(t, "collections")
135112
manager := NewCollectionsManager(db)
136113
ctx := context.Background()
137114

internal/endpoints/manager_test.go

Lines changed: 12 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,62 +2,17 @@ package endpoints
22

33
import (
44
"context"
5-
"database/sql"
65
"testing"
76

87
"github.com/maniac-en/req/internal/crud"
9-
"github.com/maniac-en/req/internal/database"
10-
_ "github.com/mattn/go-sqlite3"
8+
"github.com/maniac-en/req/internal/testutils"
119
)
1210

13-
func setupTestDB(t *testing.T) *database.Queries {
14-
db, err := sql.Open("sqlite3", ":memory:")
15-
if err != nil {
16-
t.Fatalf("Failed to open test database: %v", err)
17-
}
18-
19-
schema := `
20-
CREATE TABLE collections (
21-
id INTEGER PRIMARY KEY AUTOINCREMENT,
22-
name TEXT NOT NULL,
23-
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
24-
updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
25-
);
26-
27-
CREATE TABLE endpoints (
28-
id INTEGER PRIMARY KEY AUTOINCREMENT,
29-
collection_id INTEGER NOT NULL,
30-
name TEXT NOT NULL,
31-
method TEXT NOT NULL,
32-
url TEXT NOT NULL,
33-
headers TEXT DEFAULT '{}' NOT NULL,
34-
query_params TEXT DEFAULT '{}' NOT NULL,
35-
request_body TEXT DEFAULT '' NOT NULL,
36-
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
37-
updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
38-
FOREIGN KEY (collection_id) REFERENCES collections(id) ON DELETE CASCADE
39-
);`
40-
41-
if _, err := db.Exec(schema); err != nil {
42-
t.Fatalf("Failed to create test schema: %v", err)
43-
}
44-
45-
return database.New(db)
46-
}
47-
48-
func createTestCollection(t *testing.T, db *database.Queries) int64 {
49-
collection, err := db.CreateCollection(context.Background(), "Test Collection")
50-
if err != nil {
51-
t.Fatalf("Failed to create test collection: %v", err)
52-
}
53-
return collection.ID
54-
}
55-
5611
func TestEndpointsManagerCRUD(t *testing.T) {
57-
db := setupTestDB(t)
12+
db := testutils.SetupTestDB(t, "collections", "endpoints")
5813
manager := NewEndpointsManager(db)
5914
ctx := context.Background()
60-
collectionID := createTestCollection(t, db)
15+
collectionID := testutils.CreateTestCollection(t, db, "Test Collection")
6116

6217
t.Run("Create returns error", func(t *testing.T) {
6318
_, err := manager.Create(ctx, "Test Endpoint")
@@ -137,10 +92,10 @@ func TestEndpointsManagerCRUD(t *testing.T) {
13792
}
13893

13994
func TestCreateEndpoint(t *testing.T) {
140-
db := setupTestDB(t)
95+
db := testutils.SetupTestDB(t, "collections", "endpoints")
14196
manager := NewEndpointsManager(db)
14297
ctx := context.Background()
143-
collectionID := createTestCollection(t, db)
98+
collectionID := testutils.CreateTestCollection(t, db, "Test Collection")
14499

145100
t.Run("Valid endpoint creation", func(t *testing.T) {
146101
data := EndpointData{
@@ -230,10 +185,10 @@ func TestCreateEndpoint(t *testing.T) {
230185
}
231186

232187
func TestUpdateEndpoint(t *testing.T) {
233-
db := setupTestDB(t)
188+
db := testutils.SetupTestDB(t, "collections", "endpoints")
234189
manager := NewEndpointsManager(db)
235190
ctx := context.Background()
236-
collectionID := createTestCollection(t, db)
191+
collectionID := testutils.CreateTestCollection(t, db, "Test Collection")
237192

238193
t.Run("Valid endpoint update", func(t *testing.T) {
239194
// Create endpoint first
@@ -305,10 +260,10 @@ func TestUpdateEndpoint(t *testing.T) {
305260
}
306261

307262
func TestListByCollection(t *testing.T) {
308-
db := setupTestDB(t)
263+
db := testutils.SetupTestDB(t, "collections", "endpoints")
309264
manager := NewEndpointsManager(db)
310265
ctx := context.Background()
311-
collectionID := createTestCollection(t, db)
266+
collectionID := testutils.CreateTestCollection(t, db, "Test Collection")
312267

313268
// Create test endpoints
314269
endpoints := []EndpointData{
@@ -400,7 +355,7 @@ func TestListByCollection(t *testing.T) {
400355
})
401356

402357
t.Run("Empty collection", func(t *testing.T) {
403-
emptyCollectionID := createTestCollection(t, db)
358+
emptyCollectionID := testutils.CreateTestCollection(t, db, "Empty Collection")
404359
result, err := manager.ListByCollection(ctx, emptyCollectionID, 10, 0)
405360
if err != nil {
406361
t.Fatalf("ListByCollection failed: %v", err)
@@ -422,7 +377,7 @@ func TestListByCollection(t *testing.T) {
422377
}
423378

424379
func TestEndpointsManagerValidation(t *testing.T) {
425-
db := setupTestDB(t)
380+
db := testutils.SetupTestDB(t, "collections", "endpoints")
426381
manager := NewEndpointsManager(db)
427382
ctx := context.Background()
428383

@@ -446,4 +401,4 @@ func TestEndpointsManagerValidation(t *testing.T) {
446401
t.Errorf("Expected ErrInvalidInput, got %v", err)
447402
}
448403
})
449-
}
404+
}

internal/history/manager_test.go

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,15 @@ package history
22

33
import (
44
"context"
5-
"database/sql"
65
"testing"
76
"time"
87

9-
"github.com/maniac-en/req/internal/database"
10-
_ "github.com/mattn/go-sqlite3"
8+
"github.com/maniac-en/req/internal/testutils"
119
)
1210

13-
func setupTestDB(t *testing.T) *database.Queries {
14-
db, err := sql.Open("sqlite3", ":memory:")
15-
if err != nil {
16-
t.Fatalf("failed to open in-memory database: %v", err)
17-
}
18-
19-
// Use same schema as migration
20-
schema := `
21-
CREATE TABLE history (
22-
id INTEGER PRIMARY KEY AUTOINCREMENT,
23-
collection_id INTEGER,
24-
collection_name TEXT,
25-
endpoint_name TEXT,
26-
method TEXT NOT NULL,
27-
url TEXT NOT NULL,
28-
status_code INTEGER NOT NULL,
29-
duration INTEGER NOT NULL,
30-
response_size INTEGER DEFAULT 0,
31-
request_headers TEXT DEFAULT '{}',
32-
query_params TEXT DEFAULT '{}',
33-
request_body TEXT DEFAULT '',
34-
response_body TEXT DEFAULT '',
35-
response_headers TEXT DEFAULT '{}',
36-
executed_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
37-
);`
38-
39-
if _, err := db.Exec(schema); err != nil {
40-
t.Fatalf("failed to create schema: %v", err)
41-
}
42-
43-
return database.New(db)
44-
}
45-
4611
func TestHistoryManagerCRUD(t *testing.T) {
4712
ctx := context.Background()
48-
db := setupTestDB(t)
13+
db := testutils.SetupTestDB(t, "history")
4914
manager := NewHistoryManager(db)
5015

5116
t.Run("Create returns error", func(t *testing.T) {
@@ -86,7 +51,7 @@ func TestHistoryManagerCRUD(t *testing.T) {
8651

8752
func TestRecordExecution(t *testing.T) {
8853
ctx := context.Background()
89-
db := setupTestDB(t)
54+
db := testutils.SetupTestDB(t, "history")
9055
manager := NewHistoryManager(db)
9156

9257
t.Run("valid execution", func(t *testing.T) {
@@ -162,7 +127,7 @@ func TestRecordExecution(t *testing.T) {
162127

163128
func TestListByCollection(t *testing.T) {
164129
ctx := context.Background()
165-
db := setupTestDB(t)
130+
db := testutils.SetupTestDB(t, "history")
166131
manager := NewHistoryManager(db)
167132

168133
// Create test data
@@ -259,7 +224,7 @@ func TestListByCollection(t *testing.T) {
259224

260225
func TestDelete(t *testing.T) {
261226
ctx := context.Background()
262-
db := setupTestDB(t)
227+
db := testutils.SetupTestDB(t, "history")
263228
manager := NewHistoryManager(db)
264229

265230
// Create test entry

0 commit comments

Comments
 (0)