test(sync): fix integration tests - use config for db, fix helper IDs, correct route paths
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bookhoard/internal/config"
|
||||||
"bookhoard/internal/database"
|
"bookhoard/internal/database"
|
||||||
"context"
|
"context"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -19,7 +20,9 @@ import (
|
|||||||
func setupSyncTestDB(t *testing.T) *database.Queries {
|
func setupSyncTestDB(t *testing.T) *database.Queries {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
dbURL := "postgresql://postgres@db:5432/bookhoard?sslmode=disable"
|
cfg := config.LoadConfig()
|
||||||
|
cfg.DatabaseHost = "db"
|
||||||
|
dbURL := cfg.DatabaseURL()
|
||||||
dbConfig, err := pgxpool.ParseConfig(dbURL)
|
dbConfig, err := pgxpool.ParseConfig(dbURL)
|
||||||
require.NoError(t, err, "Failed to parse database URL")
|
require.NoError(t, err, "Failed to parse database URL")
|
||||||
dbConfig.MaxConns = 1
|
dbConfig.MaxConns = 1
|
||||||
@@ -43,11 +46,9 @@ func setupSyncTestDB(t *testing.T) *database.Queries {
|
|||||||
|
|
||||||
func createSyncTestUser(t *testing.T, db *database.Queries) pgtype.UUID {
|
func createSyncTestUser(t *testing.T, db *database.Queries) pgtype.UUID {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
userID := uuid.New()
|
|
||||||
|
|
||||||
hashedPassword := "$2a$10$rKvZHX3lIJ6CpH1lOukQ/xU8j5cH8mYHYP5YGfXllq5hG8y0Ou"
|
hashedPassword := "$2a$10$rKvZHX3lIJ6CpH1lOukQ/xU8j5cH8mYHYP5YGfXllq5hG8y0Ou"
|
||||||
|
|
||||||
_, err := db.CreateUser(ctx, database.CreateUserParams{
|
user, err := db.CreateUser(ctx, database.CreateUserParams{
|
||||||
Email: "test-sync@example.com",
|
Email: "test-sync@example.com",
|
||||||
Username: "testsyncuser",
|
Username: "testsyncuser",
|
||||||
PasswordHash: hashedPassword,
|
PasswordHash: hashedPassword,
|
||||||
@@ -57,14 +58,14 @@ func createSyncTestUser(t *testing.T, db *database.Queries) pgtype.UUID {
|
|||||||
})
|
})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
return pgtype.UUID{Bytes: [16]byte(userID), Valid: true}
|
return user.ID
|
||||||
}
|
}
|
||||||
|
|
||||||
func createSyncTestDevice(t *testing.T, db *database.Queries, userID pgtype.UUID) pgtype.UUID {
|
func createSyncTestDevice(t *testing.T, db *database.Queries, userID pgtype.UUID) pgtype.UUID {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
deviceID := uuid.New()
|
deviceID := uuid.New()
|
||||||
|
|
||||||
_, err := db.CreateDevice(ctx, database.CreateDeviceParams{
|
device, err := db.CreateDevice(ctx, database.CreateDeviceParams{
|
||||||
UserID: userID,
|
UserID: userID,
|
||||||
DeviceName: "Test Sync Device",
|
DeviceName: "Test Sync Device",
|
||||||
DeviceType: "koreader",
|
DeviceType: "koreader",
|
||||||
@@ -77,14 +78,13 @@ func createSyncTestDevice(t *testing.T, db *database.Queries, userID pgtype.UUID
|
|||||||
})
|
})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
return pgtype.UUID{Bytes: [16]byte(deviceID), Valid: true}
|
return device.ID
|
||||||
}
|
}
|
||||||
|
|
||||||
func createSyncTestMedia(t *testing.T, db *database.Queries, libraryID pgtype.UUID) pgtype.UUID {
|
func createSyncTestMedia(t *testing.T, db *database.Queries, libraryID pgtype.UUID) pgtype.UUID {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
mediaID := uuid.New()
|
|
||||||
|
|
||||||
_, err := db.CreateMediaItem(ctx, database.CreateMediaItemParams{
|
media, err := db.CreateMediaItem(ctx, database.CreateMediaItemParams{
|
||||||
LibraryID: libraryID,
|
LibraryID: libraryID,
|
||||||
Title: "Sync Test Book",
|
Title: "Sync Test Book",
|
||||||
Author: pgtype.Text{String: "Test Author", Valid: true},
|
Author: pgtype.Text{String: "Test Author", Valid: true},
|
||||||
@@ -94,7 +94,7 @@ func createSyncTestMedia(t *testing.T, db *database.Queries, libraryID pgtype.UU
|
|||||||
})
|
})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
return pgtype.UUID{Bytes: [16]byte(mediaID), Valid: true}
|
return media.ID
|
||||||
}
|
}
|
||||||
|
|
||||||
func createSyncTestLibrary(t *testing.T, db *database.Queries, userID pgtype.UUID) pgtype.UUID {
|
func createSyncTestLibrary(t *testing.T, db *database.Queries, userID pgtype.UUID) pgtype.UUID {
|
||||||
@@ -176,16 +176,19 @@ func TestSyncQueueProcessor(t *testing.T) {
|
|||||||
SyncType: "progress",
|
SyncType: "progress",
|
||||||
SyncData: []byte(`{"percentage": 50}`),
|
SyncData: []byte(`{"percentage": 50}`),
|
||||||
Priority: pgtype.Int4{Int32: 5, Valid: true},
|
Priority: pgtype.Int4{Int32: 5, Valid: true},
|
||||||
|
MaxAttempts: pgtype.Int4{Int32: 3, Valid: true},
|
||||||
|
Status: pgtype.Text{String: "pending", Valid: true},
|
||||||
})
|
})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// Verify queue item was created
|
// Verify queue item was created
|
||||||
items, err := db.ListPendingSyncQueueItems(ctx, database.ListPendingSyncQueueItemsParams{
|
items, err := db.ListPendingSyncQueueItems(ctx, database.ListPendingSyncQueueItemsParams{
|
||||||
Limit: int32(10),
|
DeviceID: deviceID,
|
||||||
|
Limit: int32(10),
|
||||||
})
|
})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, 1, len(items), "Should have one queue item")
|
assert.Equal(t, 1, len(items), "Should have one queue item")
|
||||||
assert.Equal(t, "pending", items[0].Status)
|
assert.Equal(t, "pending", items[0].Status.String)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestSyncConflictDetection tests conflict detection
|
// TestSyncConflictDetection tests conflict detection
|
||||||
@@ -216,12 +219,15 @@ func TestSyncConflictDetection(t *testing.T) {
|
|||||||
SyncType: "progress",
|
SyncType: "progress",
|
||||||
SyncData: []byte(`{"percentage": 75}`),
|
SyncData: []byte(`{"percentage": 75}`),
|
||||||
Priority: pgtype.Int4{Int32: 5, Valid: true},
|
Priority: pgtype.Int4{Int32: 5, Valid: true},
|
||||||
|
MaxAttempts: pgtype.Int4{Int32: 3, Valid: true},
|
||||||
|
Status: pgtype.Text{String: "pending", Valid: true},
|
||||||
})
|
})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// Verify both exist (conflict detection would happen during processing)
|
// Verify both exist (conflict detection would happen during processing)
|
||||||
items, err := db.ListPendingSyncQueueItems(ctx, database.ListPendingSyncQueueItemsParams{
|
items, err := db.ListPendingSyncQueueItems(ctx, database.ListPendingSyncQueueItemsParams{
|
||||||
Limit: int32(10),
|
DeviceID: deviceID,
|
||||||
|
Limit: int32(10),
|
||||||
})
|
})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, 1, len(items), "Should have one queue item")
|
assert.Equal(t, 1, len(items), "Should have one queue item")
|
||||||
@@ -251,7 +257,7 @@ func TestSyncEndpoint(t *testing.T) {
|
|||||||
device := setup.CreateDevice(t, "Test Sync Device", "koreader", "sync-test-123")
|
device := setup.CreateDevice(t, "Test Sync Device", "koreader", "sync-test-123")
|
||||||
|
|
||||||
// Test sync endpoint with device token
|
// Test sync endpoint with device token
|
||||||
req := httptest.NewRequest("POST", "/api/koreader/sync", nil)
|
req := httptest.NewRequest("POST", "/api/sync/koreader/progress", nil)
|
||||||
req.Header.Set("Authorization", "Bearer "+device.AuthToken)
|
req.Header.Set("Authorization", "Bearer "+device.AuthToken)
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
rec := httptest.NewRecorder()
|
rec := httptest.NewRecorder()
|
||||||
|
|||||||
Reference in New Issue
Block a user