package docinitrunner_test import ( "context" "fmt" "testing" docinitrunner "queryorchestration/api/docInitRunner" "queryorchestration/internal/database/repository" "queryorchestration/internal/document" documentinit "queryorchestration/internal/document/init" "queryorchestration/internal/server/runner" "queryorchestration/internal/serviceconfig/objectstore" "queryorchestration/internal/serviceconfig/queue/documentsync" queuemock "queryorchestration/mocks/queue" "github.com/aws/aws-sdk-go-v2/service/sqs" "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" "github.com/stretchr/testify/require" ) type DocInitConfig struct { runner.BaseConfig[docinitrunner.Body] documentsync.DocSyncConfig } 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 runner := docinitrunner.New(validator.New(), &docinitrunner.Services{ Document: documentinit.New(cfg), }) assert.NotNil(t, runner) t.Run("valid", func(t *testing.T) { clientId := "clientid" bucketName := "bucketName" location := objectstore.BucketKey{ Location: objectstore.Import, ClientID: clientId, Filename: "aaa", } docinfo := document.DocumentSummary{ ID: uuid.New(), ClientID: "hello", Hash: "example_hash", } doc := docinitrunner.Body{ Bucket: bucketName, Key: location.String(), Hash: docinfo.Hash, ClientID: clientId, } 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.DocumentSyncURL && *in.MessageBody == fmt.Sprintf("{\"id\":\"%s\"}", docinfo.ID.String()) }), mock.Anything, ). Return(&sqs.SendMessageOutput{}, nil) assert.True(t, runner.Process(ctx, doc)) }) }