test(queue): add createTestQueueItem helper and improve queue test assertions
This commit is contained in:
@@ -16,6 +16,61 @@ import (
|
|||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// createTestQueueItem creates a complete test fixture: device, library, media item, and sync queue item
|
||||||
|
func createTestQueueItem(t *testing.T, db *database.Queries, userID uuid.UUID) (database.SyncQueue, database.Devices) {
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
// 1. Create device
|
||||||
|
deviceToken := fmt.Sprintf("dev_%s", uuid.New().String())
|
||||||
|
device, err := db.CreateDevice(ctx, database.CreateDeviceParams{
|
||||||
|
UserID: pgtype.UUID{Bytes: [16]byte(userID), Valid: true},
|
||||||
|
DeviceName: "Test Device",
|
||||||
|
DeviceType: "koreader",
|
||||||
|
DeviceIdentifier: "test-device-" + uuid.New().String(),
|
||||||
|
AuthToken: deviceToken,
|
||||||
|
SyncEnabled: pgtype.Bool{Bool: true, Valid: true},
|
||||||
|
AutoSync: pgtype.Bool{Bool: true, Valid: true},
|
||||||
|
DeviceMetadata: []byte("{}"),
|
||||||
|
})
|
||||||
|
require.NoError(t, err, "Should create device")
|
||||||
|
|
||||||
|
// 2. Get library type
|
||||||
|
libraryType, err := db.GetLibraryTypeByName(ctx, "ebooks")
|
||||||
|
require.NoError(t, err, "Should find library type")
|
||||||
|
|
||||||
|
// 3. Create library
|
||||||
|
library, err := db.CreateLibrary(ctx, database.CreateLibraryParams{
|
||||||
|
Name: "Test Queue Library",
|
||||||
|
Description: pgtype.Text{String: "Test library", Valid: true},
|
||||||
|
LibraryTypeID: libraryType.ID,
|
||||||
|
CreatedByAdminID: pgtype.UUID{Bytes: [16]byte(userID), Valid: true},
|
||||||
|
})
|
||||||
|
require.NoError(t, err, "Should create library")
|
||||||
|
|
||||||
|
// 4. Create media item
|
||||||
|
mediaItem, err := db.CreateMediaItem(ctx, database.CreateMediaItemParams{
|
||||||
|
LibraryID: library.ID,
|
||||||
|
Title: "Test Book for Queue",
|
||||||
|
Author: pgtype.Text{String: "Test Author", Valid: true},
|
||||||
|
FilePath: "/tmp/test.epub",
|
||||||
|
FileSize: pgtype.Int8{Int64: 1024, Valid: true},
|
||||||
|
MimeType: pgtype.Text{String: "application/epub+zip", Valid: true},
|
||||||
|
})
|
||||||
|
require.NoError(t, err, "Should create media item")
|
||||||
|
|
||||||
|
// 5. Create sync queue item
|
||||||
|
queueItem, err := db.CreateSyncQueueItem(ctx, database.CreateSyncQueueItemParams{
|
||||||
|
DeviceID: device.ID,
|
||||||
|
MediaItemID: mediaItem.ID,
|
||||||
|
SyncType: "progress",
|
||||||
|
SyncData: []byte(`{"percentage": 50}`),
|
||||||
|
Priority: pgtype.Int4{Int32: 5, Valid: true},
|
||||||
|
})
|
||||||
|
require.NoError(t, err, "Should create sync queue item")
|
||||||
|
|
||||||
|
return queueItem, device
|
||||||
|
}
|
||||||
|
|
||||||
func TestListAllQueueItems_Admin(t *testing.T) {
|
func TestListAllQueueItems_Admin(t *testing.T) {
|
||||||
setup := setupTestServer(t)
|
setup := setupTestServer(t)
|
||||||
|
|
||||||
@@ -66,7 +121,11 @@ func TestGetDeviceQueueStats(t *testing.T) {
|
|||||||
|
|
||||||
var response map[string]interface{}
|
var response map[string]interface{}
|
||||||
json.Unmarshal(rec.Body.Bytes(), &response)
|
json.Unmarshal(rec.Body.Bytes(), &response)
|
||||||
assert.Equal(t, deviceID.String(), response["device_id"], "Should match device ID")
|
assert.Contains(t, response, "total_count", "Should have total_count")
|
||||||
|
assert.Contains(t, response, "pending_count", "Should have pending_count")
|
||||||
|
assert.Contains(t, response, "processing_count", "Should have processing_count")
|
||||||
|
assert.Contains(t, response, "completed_count", "Should have completed_count")
|
||||||
|
assert.Contains(t, response, "failed_count", "Should have failed_count")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestListDeviceQueueItems(t *testing.T) {
|
func TestListDeviceQueueItems(t *testing.T) {
|
||||||
@@ -100,17 +159,25 @@ func TestListDeviceQueueItems(t *testing.T) {
|
|||||||
|
|
||||||
assert.Equal(t, http.StatusOK, rec.Code, "Should list device queue items")
|
assert.Equal(t, http.StatusOK, rec.Code, "Should list device queue items")
|
||||||
|
|
||||||
var response []interface{}
|
var response map[string]interface{}
|
||||||
json.Unmarshal(rec.Body.Bytes(), &response)
|
json.Unmarshal(rec.Body.Bytes(), &response)
|
||||||
assert.NotNil(t, response, "Should have queue items array")
|
items, ok := response["items"].([]interface{})
|
||||||
|
assert.True(t, ok, "Should have items array")
|
||||||
|
assert.NotNil(t, items, "Should have queue items")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRetryQueueItem(t *testing.T) {
|
func TestRetryQueueItem(t *testing.T) {
|
||||||
setup := setupTestServer(t)
|
setup := setupTestServer(t)
|
||||||
|
|
||||||
token := loginTestUser(t, setup.Server, setup.DB)
|
token := loginTestUser(t, setup.Server, setup.DB)
|
||||||
|
|
||||||
itemID := uuid.New().String()
|
// Get the current test user's ID from database
|
||||||
|
user, err := setup.DB.GetUserByEmail(context.Background(), "testuser@example.com")
|
||||||
|
require.NoError(t, err, "Should get test user")
|
||||||
|
userID, err := uuid.FromBytes(user.ID.Bytes[:])
|
||||||
|
require.NoError(t, err, "Should parse user UUID")
|
||||||
|
|
||||||
|
queueItem, _ := createTestQueueItem(t, setup.DB, userID)
|
||||||
|
itemID := uuid.UUID(queueItem.ID.Bytes).String()
|
||||||
|
|
||||||
req := httptest.NewRequest("POST", fmt.Sprintf("/api/queue/items/%s/retry", itemID), nil)
|
req := httptest.NewRequest("POST", fmt.Sprintf("/api/queue/items/%s/retry", itemID), nil)
|
||||||
req.Header.Set("Authorization", "Bearer "+token)
|
req.Header.Set("Authorization", "Bearer "+token)
|
||||||
@@ -122,17 +189,23 @@ func TestRetryQueueItem(t *testing.T) {
|
|||||||
|
|
||||||
func TestDeleteQueueItem(t *testing.T) {
|
func TestDeleteQueueItem(t *testing.T) {
|
||||||
setup := setupTestServer(t)
|
setup := setupTestServer(t)
|
||||||
|
|
||||||
token := loginTestUser(t, setup.Server, setup.DB)
|
token := loginTestUser(t, setup.Server, setup.DB)
|
||||||
|
|
||||||
itemID := uuid.New().String()
|
// Get the current test user's ID from database
|
||||||
|
user, err := setup.DB.GetUserByEmail(context.Background(), "testuser@example.com")
|
||||||
|
require.NoError(t, err, "Should get test user")
|
||||||
|
userID, err := uuid.FromBytes(user.ID.Bytes[:])
|
||||||
|
require.NoError(t, err, "Should parse user UUID")
|
||||||
|
|
||||||
|
queueItem, _ := createTestQueueItem(t, setup.DB, userID)
|
||||||
|
itemID := uuid.UUID(queueItem.ID.Bytes).String()
|
||||||
|
|
||||||
req := httptest.NewRequest("DELETE", fmt.Sprintf("/api/queue/items/%s", itemID), nil)
|
req := httptest.NewRequest("DELETE", fmt.Sprintf("/api/queue/items/%s", itemID), nil)
|
||||||
req.Header.Set("Authorization", "Bearer "+token)
|
req.Header.Set("Authorization", "Bearer "+token)
|
||||||
rec := httptest.NewRecorder()
|
rec := httptest.NewRecorder()
|
||||||
setup.Server.Config.Handler.ServeHTTP(rec, req)
|
setup.Server.Config.Handler.ServeHTTP(rec, req)
|
||||||
|
|
||||||
assert.Equal(t, http.StatusOK, rec.Code, "Should delete queue item")
|
assert.Equal(t, http.StatusNoContent, rec.Code, "Should delete queue item")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestClearDeviceQueue(t *testing.T) {
|
func TestClearDeviceQueue(t *testing.T) {
|
||||||
|
|||||||
Reference in New Issue
Block a user