2025-02-07 14:15:06 +00:00
|
|
|
package documenttext
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2025-03-05 12:05:46 +00:00
|
|
|
"testing"
|
|
|
|
|
|
2025-02-07 14:15:06 +00:00
|
|
|
"queryorchestration/internal/database"
|
|
|
|
|
"queryorchestration/internal/database/repository"
|
|
|
|
|
"queryorchestration/internal/document"
|
2025-02-12 19:00:25 +00:00
|
|
|
textversion "queryorchestration/internal/document/text/version"
|
2025-02-07 14:15:06 +00:00
|
|
|
|
2025-03-04 15:51:03 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
2025-02-07 14:15:06 +00:00
|
|
|
"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()
|
2025-03-04 15:51:03 +00:00
|
|
|
require.NoError(t, err)
|
2025-02-07 14:15:06 +00:00
|
|
|
|
|
|
|
|
cfg := &DocTextConfig{}
|
|
|
|
|
cfg.DBPool = pool
|
|
|
|
|
cfg.DBQueries = repository.New(pool)
|
|
|
|
|
|
|
|
|
|
svc := Service{
|
|
|
|
|
cfg: cfg,
|
|
|
|
|
svc: &Services{
|
2025-02-12 19:00:25 +00:00
|
|
|
Version: textversion.New(cfg),
|
2025-02-07 14:15:06 +00:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-05 19:49:03 +00:00
|
|
|
t.Run("successful clean", func(t *testing.T) {
|
|
|
|
|
id := uuid.New()
|
|
|
|
|
inloc := document.Location{
|
|
|
|
|
Bucket: "bucket_name",
|
|
|
|
|
Key: "/i/am/here",
|
|
|
|
|
}
|
2025-02-07 14:15:06 +00:00
|
|
|
|
2025-03-05 19:49:03 +00:00
|
|
|
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))
|
2025-02-07 14:15:06 +00:00
|
|
|
|
2025-03-05 19:49:03 +00:00
|
|
|
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)
|
|
|
|
|
})
|
2025-02-07 14:15:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestExecuteExtraction(t *testing.T) {
|
2025-03-05 19:49:03 +00:00
|
|
|
pool, err := pgxmock.NewPool()
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
cfg := &DocTextConfig{}
|
|
|
|
|
cfg.DBPool = pool
|
|
|
|
|
cfg.DBQueries = repository.New(pool)
|
2025-02-07 14:15:06 +00:00
|
|
|
|
2025-03-05 19:49:03 +00:00
|
|
|
svc := Service{
|
|
|
|
|
cfg: cfg,
|
|
|
|
|
svc: &Services{
|
|
|
|
|
Version: textversion.New(cfg),
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx := context.Background()
|
2025-02-07 14:15:06 +00:00
|
|
|
id := uuid.New()
|
2025-03-05 19:49:03 +00:00
|
|
|
location := document.Location{
|
|
|
|
|
Bucket: "buck",
|
|
|
|
|
Key: "key",
|
|
|
|
|
}
|
|
|
|
|
dbmimetype := repository.NullCleanmimetype{
|
|
|
|
|
Valid: true,
|
|
|
|
|
Cleanmimetype: repository.CleanmimetypeApplicationPdf,
|
|
|
|
|
}
|
2025-02-07 14:15:06 +00:00
|
|
|
|
2025-03-05 19:49:03 +00:00
|
|
|
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)
|
2025-02-07 14:15:06 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
assert.EqualExportedValues(t, location, *outloc)
|
|
|
|
|
}
|