package storeeventrunner import ( "context" "testing" "time" "queryorchestration/internal/database/repository" "queryorchestration/internal/document" documentstore "queryorchestration/internal/document/store" "queryorchestration/internal/server/runner" "queryorchestration/internal/serviceconfig/objectstore" "queryorchestration/internal/serviceconfig/queue/documentinit" queuemock "queryorchestration/mocks/queue" "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" "github.com/stretchr/testify/require" ) type DocInitConfig struct { runner.BaseConfig[S3EventNotification] documentinit.DocInitConfig } func TestDocInitRunner(t *testing.T) { ctx := context.Background() pool, err := pgxmock.NewPool() require.NoError(t, err) cfg := &DocInitConfig{} cfg.DBPool = pool cfg.DBQueries = repository.New(pool) mockSQS := queuemock.NewMockSQSClient(t) cfg.QueueClient = mockSQS cfg.DocInitURL = "hi" runner := New(&Services{ documentstore.New(cfg), }) t.Run("valid", func(t *testing.T) { clientId := "hi" bucketName := "bucketName" location := objectstore.BucketKey{ Location: objectstore.Import, ClientID: clientId, CreatedAt: time.Now().UTC(), } docinfo := document.DocumentSummary{ ID: uuid.New(), ClientID: clientId, Hash: "example_hash", } doc := S3EventNotification{ Records: []S3EventRecord{ { EventName: EventS3ObjectCreatedPut, S3: S3EventRecordDetails{ Bucket: S3Bucket{ Name: bucketName, }, Object: S3Object{ Key: location.String(), ETag: docinfo.Hash, }, }, }, }, } pool.ExpectQuery("name: GetDocumentIDByHash :one").WithArgs(docinfo.Hash, clientId).WillReturnRows( pgxmock.NewRows([]string{"id"}), ) pool.ExpectBegin() pool.ExpectQuery("name: CreateDocument :one").WithArgs(clientId, docinfo.Hash). WillReturnRows( pgxmock.NewRows([]string{"id"}). AddRow(docinfo.ID), ) pool.ExpectExec("name: AddDocumentEntry :exec").WithArgs(docinfo.ID, bucketName, location.String()). WillReturnResult(pgxmock.NewResult("", 1)) pool.ExpectCommit() pool.ExpectQuery("name: GetDocumentSummary :one").WithArgs(docinfo.ID). WillReturnRows( pgxmock.NewRows([]string{"id", "clientId", "hash"}). AddRow(docinfo.ID, docinfo.ClientID, "example"), ) mockSQS.EXPECT(). SendMessage( mock.Anything, mock.MatchedBy(func(in *sqs.SendMessageInput) bool { return *in.QueueUrl == cfg.DocInitURL }), mock.Anything, ). Return(&sqs.SendMessageOutput{}, nil) assert.True(t, runner.Process(ctx, doc)) }) }