package documenttext import ( "context" "testing" "queryorchestration/internal/database/repository" "queryorchestration/internal/document" "queryorchestration/internal/serviceconfig/objectstore" objectstoremock "queryorchestration/mocks/objectstore" textractmock "queryorchestration/mocks/textract" "github.com/aws/aws-sdk-go-v2/service/textract" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" "github.com/google/uuid" "github.com/pashagolub/pgxmock/v3" ) func TestTriggerExtract(t *testing.T) { ctx := context.Background() pool, err := pgxmock.NewPool() require.NoError(t, err) cfg := &DocTextConfig{} cfg.DBPool = pool cfg.DBQueries = repository.New(pool) mockS3 := objectstoremock.NewMockS3Client(t) cfg.StoreClient = mockS3 mockTextract := textractmock.NewMockTextractClient(t) cfg.TextractClient = mockTextract svc := Service{ cfg: cfg, } t.Run("successful clean", func(t *testing.T) { cleanId := uuid.New() inloc := document.Location{ Bucket: "bucket_name", Key: "/i/am/here", } hash := "hhasshhh" jobId := "hello" triggerId := uuid.New() pool.ExpectBegin() pool.ExpectQuery("name: AddDocumentTextTrigger :one").WithArgs(cleanId, pgxmock.AnyArg()).WillReturnRows( pgxmock.NewRows([]string{"id"}).AddRow(triggerId), ) mockTextract.EXPECT(). StartDocumentTextDetection( mock.Anything, mock.MatchedBy(func(in *textract.StartDocumentTextDetectionInput) bool { return true }), mock.Anything, ). Return(&textract.StartDocumentTextDetectionOutput{ JobId: &jobId, }, nil) pool.ExpectExec("name: AddDocumentTextTriggerJobId :exec").WithArgs(&jobId, triggerId). WillReturnResult(pgxmock.NewResult("", 1)) pool.ExpectCommit() err = svc.triggerExtract(ctx, &repository.Currentcleanentry{ ID: cleanId, Clientid: "AA", Key: &inloc.Key, Bucket: &inloc.Bucket, Hash: &hash, Mimetype: repository.NullCleanmimetype{ Valid: true, Cleanmimetype: repository.CleanmimetypeApplicationPdf, }, }) require.NoError(t, err) }) } func TestGetOutputFilename(t *testing.T) { svc := Service{} _, err := svc.getOutputFilename(uuid.Nil) assert.EqualError(t, err, "trigger id required") triggerId := uuid.New() key, err := svc.getOutputFilename(triggerId) assert.NoError(t, err) assert.Equal(t, triggerId.String(), key) } func TestGetTriggerIdFromKey(t *testing.T) { svc := Service{} _, err := svc.getTriggerIdFromKey(objectstore.BucketKey{}) assert.EqualError(t, err, "invalid UUID length: 0") _, err = svc.getTriggerIdFromKey(objectstore.BucketKey{ Filename: "aa", }) assert.EqualError(t, err, "invalid UUID length: 2") triggerId, err := svc.getTriggerIdFromKey(objectstore.BucketKey{ Filename: "6ac21b2b-f36c-4ae7-a815-307d4a9bfe4d", }) assert.NoError(t, err) assert.Equal(t, "6ac21b2b-f36c-4ae7-a815-307d4a9bfe4d", triggerId.String()) }