package doccleanrunner_test import ( "context" "encoding/json" "fmt" doccleanrunner "queryorchestration/api/docCleanRunner" "queryorchestration/internal/database" "queryorchestration/internal/database/repository" "queryorchestration/internal/document" documentclean "queryorchestration/internal/document/clean" cleanversion "queryorchestration/internal/document/clean/version" "queryorchestration/internal/serviceconfig" "queryorchestration/internal/serviceconfig/objectstore" "queryorchestration/internal/serviceconfig/queue/documenttext" objectstoremock "queryorchestration/mocks/objectstore" queuemock "queryorchestration/mocks/queue" "testing" "github.com/aws/aws-sdk-go-v2/service/sqs" "github.com/aws/aws-sdk-go-v2/service/sqs/types" "github.com/go-playground/validator/v10" "github.com/google/uuid" "github.com/pashagolub/pgxmock/v3" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" ) type DocCleanConfig struct { serviceconfig.BaseConfig documenttext.DocTextConfig objectstore.ObjectStoreConfig } func TestDocCleanRunner(t *testing.T) { ctx := context.Background() pool, err := pgxmock.NewPool() if err != nil { t.Fatalf("failed to open pgxmock database: %v", err) } cfg := &DocCleanConfig{} cfg.DBPool = pool cfg.DBQueries = repository.New(pool) mockStore := objectstoremock.NewMockS3Client(t) cfg.StoreClient = mockStore mockSQS := queuemock.NewMockSQSClient(t) cfg.QueueClient = mockSQS cfg.DocumentTextURL = "/i/am/here" runner := doccleanrunner.New(validator.New(), &doccleanrunner.Services{ Clean: documentclean.New(cfg, &documentclean.Services{ Version: cleanversion.New(cfg), }), }) assert.NotNil(t, runner) doc := doccleanrunner.Body{ ID: uuid.New(), } bodyBytes, err := json.Marshal(doc) assert.NoError(t, err) body := string(bodyBytes) msg := &types.Message{ Body: &body, } inloc := document.Location{ Bucket: "bucket_name", Key: "/i/am/here", } pool.ExpectQuery("name: IsDocumentClean :one").WithArgs(database.MustToDBUUID(doc.ID)).WillReturnRows( pgxmock.NewRows([]string{"isclean"}). AddRow(false), ) pool.ExpectQuery("name: GetDocumentEntry :one").WithArgs(database.MustToDBUUID(doc.ID)).WillReturnRows( pgxmock.NewRows([]string{"documentId", "bucket", "key"}). AddRow(database.MustToDBUUID(doc.ID), inloc.Bucket, inloc.Key), ) pool.ExpectExec("name: AddDocumentCleanEntry :exec").WithArgs(database.MustToDBUUID(doc.ID), int32(1), inloc.Bucket, inloc.Key). WillReturnResult(pgxmock.NewResult("", 1)) mockSQS.EXPECT(). SendMessage( mock.Anything, mock.MatchedBy(func(in *sqs.SendMessageInput) bool { return *in.QueueUrl == cfg.DocumentTextURL && *in.MessageBody == fmt.Sprintf("{\"id\":\"%s\"}", doc.ID.String()) }), mock.Anything, ). Return(&sqs.SendMessageOutput{}, nil) err = runner.Process(ctx, msg) assert.NoError(t, err) }