package documentsync_test import ( "context" "fmt" "queryorchestration/internal/client" "queryorchestration/internal/database" "queryorchestration/internal/database/repository" "queryorchestration/internal/document" documentsync "queryorchestration/internal/document/sync" "queryorchestration/internal/job" queuemock "queryorchestration/mocks/queue" "testing" "github.com/aws/aws-sdk-go-v2/service/sqs" "github.com/google/uuid" "github.com/pashagolub/pgxmock/v3" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" ) func TestSync(t *testing.T) { ctx := context.Background() pool, err := pgxmock.NewPool() if err != nil { t.Fatalf("failed to open pgxmock database: %v", err) } cfg := &DocSyncConfig{} cfg.DBPool = pool cfg.DBQueries = repository.New(pool) mockSQS := queuemock.NewMockSQSClient(t) cfg.QueueClient = mockSQS cfg.DocumentCleanURL = "/i/am/here" svc := documentsync.New(cfg, &documentsync.Services{ Document: document.New(cfg), Job: job.New(cfg, &job.Services{ Client: client.New(cfg), }), }) j := job.Job{ ID: uuid.New(), ClientID: uuid.New(), CanSync: true, } doc := document.Document{ ID: uuid.New(), JobID: j.ID, Hash: "example_hash", } pool.ExpectQuery("name: GetDocument :one").WithArgs(database.MustToDBUUID(doc.ID)). WillReturnRows( pgxmock.NewRows([]string{"id", "jobId", "hash"}). AddRow(database.MustToDBUUID(doc.ID), database.MustToDBUUID(doc.JobID), doc.Hash), ) pool.ExpectQuery("name: GetJob :one").WithArgs(database.MustToDBUUID(j.ID)).WillReturnRows( pgxmock.NewRows([]string{"id", "clientId", "canSync"}). AddRow(database.MustToDBUUID(j.ID), database.MustToDBUUID(j.ClientID), j.CanSync), ) pool.ExpectQuery("name: GetClient :one").WithArgs(database.MustToDBUUID(j.ClientID)).WillReturnRows( pgxmock.NewRows([]string{"id", "name", "canSync"}). AddRow(database.MustToDBUUID(j.ClientID), "client_name", true), ) mockSQS.EXPECT(). SendMessage( mock.Anything, mock.MatchedBy(func(in *sqs.SendMessageInput) bool { return *in.QueueUrl == cfg.DocumentCleanURL && *in.MessageBody == fmt.Sprintf("{\"id\":\"%s\"}", doc.ID) }), mock.Anything, ). Return(&sqs.SendMessageOutput{}, nil) err = svc.Sync(ctx, doc.ID) assert.NoError(t, err) }