package documenttext import ( "context" "testing" "queryorchestration/internal/database" "queryorchestration/internal/database/repository" "queryorchestration/internal/document" "github.com/stretchr/testify/require" "github.com/google/uuid" "github.com/pashagolub/pgxmock/v3" "github.com/stretchr/testify/assert" ) func TestExtract(t *testing.T) { ctx := context.Background() pool, err := pgxmock.NewPool() require.NoError(t, err) cfg := &DocTextConfig{} cfg.DBPool = pool cfg.DBQueries = repository.New(pool) svc := Service{ cfg: cfg, } t.Run("successful clean", func(t *testing.T) { id := uuid.New() inloc := document.Location{ Bucket: "bucket_name", Key: "/i/am/here", } cleanId := uuid.New() dbCleanId := database.MustToDBUUID(cleanId) mimeType := "application/pdf" dbmimetype := repository.NullCleanmimetype{ Valid: true, Cleanmimetype: repository.Cleanmimetype(mimeType), } pool.ExpectQuery("name: GetCleanEntry :one").WithArgs(dbCleanId).WillReturnRows( pgxmock.NewRows([]string{"id", "documentId", "bucket", "key", "version", "mimetype", "fail"}). AddRow(dbCleanId, database.MustToDBUUID(id), &inloc.Bucket, &inloc.Key, int64(1), dbmimetype, repository.NullCleanfailtype{}), ) pool.ExpectExec("name: AddDocumentTextEntry :exec").WithArgs(pgxmock.AnyArg(), inloc.Bucket, inloc.Key, dbCleanId). WillReturnResult(pgxmock.NewResult("", 1)) err = svc.extract(ctx, cleanId) assert.NoError(t, err) }) t.Run("fail clean", func(t *testing.T) { id := uuid.New() cleanId := database.MustToDBUUID(uuid.New()) fail := repository.NullCleanfailtype{ Valid: true, Cleanfailtype: repository.CleanfailtypeInvalidMimetype, } pool.ExpectQuery("name: GetCleanEntry :one").WithArgs(database.MustToDBUUID(id)).WillReturnRows( pgxmock.NewRows([]string{"id", "documentId", "bucket", "key", "version", "mimetype", "fail"}). AddRow(cleanId, database.MustToDBUUID(id), nil, nil, int64(1), nil, fail), ) err = svc.extract(ctx, id) assert.Error(t, err) }) } func TestExecuteExtraction(t *testing.T) { pool, err := pgxmock.NewPool() require.NoError(t, err) cfg := &DocTextConfig{} cfg.DBPool = pool cfg.DBQueries = repository.New(pool) svc := Service{ cfg: cfg, } ctx := context.Background() id := uuid.New() location := document.Location{ Bucket: "buck", Key: "key", } dbmimetype := repository.NullCleanmimetype{ Valid: true, Cleanmimetype: repository.CleanmimetypeApplicationPdf, } pool.ExpectQuery("name: GetCleanEntry :one").WithArgs(database.MustToDBUUID(id)).WillReturnRows( pgxmock.NewRows([]string{"id", "documentId", "bucket", "key", "version", "mimetype", "fail"}). AddRow(database.MustToDBUUID(uuid.New()), database.MustToDBUUID(id), &location.Bucket, &location.Key, int32(1), dbmimetype, repository.NullCleanfailtype{}), ) outloc, err := svc.executeExtraction(ctx, id) assert.NoError(t, err) assert.EqualExportedValues(t, location, *outloc) }