Files
query-orchestration/internal/document/text/extract_test.go
T
Michael McGuinness 8f409e60a8 Merged in bugfix/errhandling (pull request #89)
Error handling

* basic
2025-03-05 19:49:03 +00:00

112 lines
3.1 KiB
Go

package documenttext
import (
"context"
"testing"
"queryorchestration/internal/database"
"queryorchestration/internal/database/repository"
"queryorchestration/internal/document"
textversion "queryorchestration/internal/document/text/version"
"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,
svc: &Services{
Version: textversion.New(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, int32(1), dbmimetype, repository.NullCleanfailtype{}),
)
pool.ExpectExec("name: AddDocumentTextEntry :exec").WithArgs(int32(1), 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, int32(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,
svc: &Services{
Version: textversion.New(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)
}