Merged in feature/batch-status (pull request #215)
Implement and test the batch status feature * working
This commit is contained in:
@@ -2,20 +2,132 @@ package documentsync_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"queryorchestration/internal/client"
|
||||
"queryorchestration/internal/database/repository"
|
||||
"queryorchestration/internal/document"
|
||||
documentsync "queryorchestration/internal/document/sync"
|
||||
"queryorchestration/internal/serviceconfig"
|
||||
"queryorchestration/internal/serviceconfig/objectstore"
|
||||
"queryorchestration/internal/serviceconfig/queue"
|
||||
"queryorchestration/internal/serviceconfig/queue/documentclean"
|
||||
"queryorchestration/internal/test"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
type DocSyncConfig struct {
|
||||
serviceconfig.BaseConfig
|
||||
documentclean.DocCleanConfig
|
||||
queue.QueueConfig
|
||||
objectstore.ObjectStoreConfig
|
||||
}
|
||||
|
||||
func TestNewDocumentSyncService(t *testing.T) {
|
||||
svc := documentsync.New(&DocSyncConfig{}, &documentsync.Services{})
|
||||
assert.NotNil(t, svc)
|
||||
}
|
||||
|
||||
// TestDocumentSyncSync_ReturnsSyncResult verifies that Sync() returns a
|
||||
// *SyncResult with correct Synced and Skipped fields based on the client's
|
||||
// can_sync configuration.
|
||||
func TestDocumentSyncSync_ReturnsSyncResult(t *testing.T) {
|
||||
cfg := &DocSyncConfig{}
|
||||
test.CreateDB(t, cfg)
|
||||
acfg := test.CreateAWSContainer(t, cfg)
|
||||
|
||||
test.SetQueueClient(t, t.Context(), cfg, acfg.ExternalEndpoint)
|
||||
cfg.DocumentCleanURL = test.CreateQueue(t, cfg, test.DocCleanRunnerName)
|
||||
|
||||
ctx := t.Context()
|
||||
|
||||
// Test 1: can_sync=true -> Synced=true, Skipped=false
|
||||
syncClientID := "sync_result_true_" + uuid.New().String()[:8]
|
||||
err := cfg.GetDBQueries().CreateClient(ctx, &repository.CreateClientParams{
|
||||
Clientid: syncClientID,
|
||||
Name: "Test Sync True",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
err = cfg.GetDBQueries().AddClientCanSync(ctx, &repository.AddClientCanSyncParams{
|
||||
Clientid: syncClientID,
|
||||
Cansync: true,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
docID, err := cfg.GetDBQueries().CreateDocument(ctx, &repository.CreateDocumentParams{
|
||||
Clientid: syncClientID,
|
||||
Hash: "hash_sync_true_" + uuid.New().String()[:8],
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
part := uint16(1)
|
||||
filetype := "pdf"
|
||||
key := objectstore.BucketKey{
|
||||
CreatedAt: time.Now().UTC(),
|
||||
ClientID: syncClientID,
|
||||
EntityID: uuid.New(),
|
||||
Location: objectstore.Import,
|
||||
Part: &part,
|
||||
FileType: &filetype,
|
||||
}
|
||||
err = cfg.GetDBQueries().AddDocumentEntry(ctx, &repository.AddDocumentEntryParams{
|
||||
Documentid: docID,
|
||||
Bucket: "bucket",
|
||||
Key: key.String(),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
svc := documentsync.New(cfg, &documentsync.Services{
|
||||
Document: document.New(cfg),
|
||||
Client: client.New(cfg),
|
||||
})
|
||||
|
||||
result, err := svc.Sync(ctx, docID)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
assert.True(t, result.Synced, "should be synced when can_sync=true")
|
||||
assert.False(t, result.Skipped, "should not be skipped when can_sync=true")
|
||||
|
||||
// Test 2: can_sync=false -> Synced=false, Skipped=true
|
||||
skipClientID := "sync_result_false_" + uuid.New().String()[:8]
|
||||
err = cfg.GetDBQueries().CreateClient(ctx, &repository.CreateClientParams{
|
||||
Clientid: skipClientID,
|
||||
Name: "Test Sync False",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
err = cfg.GetDBQueries().AddClientCanSync(ctx, &repository.AddClientCanSyncParams{
|
||||
Clientid: skipClientID,
|
||||
Cansync: false,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
skipDocID, err := cfg.GetDBQueries().CreateDocument(ctx, &repository.CreateDocumentParams{
|
||||
Clientid: skipClientID,
|
||||
Hash: "hash_sync_false_" + uuid.New().String()[:8],
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
key2 := objectstore.BucketKey{
|
||||
CreatedAt: time.Now().UTC(),
|
||||
ClientID: skipClientID,
|
||||
EntityID: uuid.New(),
|
||||
Location: objectstore.Import,
|
||||
Part: &part,
|
||||
FileType: &filetype,
|
||||
}
|
||||
err = cfg.GetDBQueries().AddDocumentEntry(ctx, &repository.AddDocumentEntryParams{
|
||||
Documentid: skipDocID,
|
||||
Bucket: "bucket",
|
||||
Key: key2.String(),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
skipResult, err := svc.Sync(ctx, skipDocID)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, skipResult)
|
||||
assert.False(t, skipResult.Synced, "should not be synced when can_sync=false")
|
||||
assert.True(t, skipResult.Skipped, "should be skipped when can_sync=false")
|
||||
}
|
||||
|
||||
@@ -10,20 +10,40 @@ import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func (s *Service) Sync(ctx context.Context, id uuid.UUID) error {
|
||||
// SyncResult contains the result of the sync check.
|
||||
//
|
||||
// Fields:
|
||||
// - Synced: true if the document was forwarded to the clean queue
|
||||
// - Skipped: true if the client's can_sync flag is false
|
||||
type SyncResult struct {
|
||||
Synced bool
|
||||
Skipped bool
|
||||
}
|
||||
|
||||
// Sync checks if a document is eligible for syncing based on the client's
|
||||
// can_sync configuration, and forwards eligible documents to the clean queue.
|
||||
//
|
||||
// Parameters:
|
||||
// - ctx: request context
|
||||
// - id: the document UUID to sync
|
||||
//
|
||||
// Returns:
|
||||
// - *SyncResult: result indicating whether sync was performed or skipped
|
||||
// - error: if any infrastructure operation (DB, queue) fails
|
||||
func (s *Service) Sync(ctx context.Context, id uuid.UUID) (*SyncResult, error) {
|
||||
doc, err := s.svc.Document.GetSummary(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
j, err := s.svc.Client.Get(ctx, doc.ClientID)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !j.CanSync {
|
||||
slog.Debug("not syncing document", "id", id.String())
|
||||
return nil
|
||||
return &SyncResult{Skipped: true}, nil
|
||||
}
|
||||
|
||||
slog.Debug("syncing document", "id", id.String())
|
||||
@@ -35,8 +55,8 @@ func (s *Service) Sync(ctx context.Context, id uuid.UUID) error {
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return nil
|
||||
return &SyncResult{Synced: true}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user