@@ -2,62 +2,17 @@ package endpoints
22
33import (
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-
5611func 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
13994func 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
232187func 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
307262func 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
424379func 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+ }
0 commit comments