53ea7d34e6
Start adding Textract + UUID changes * base * startclient * ts * short * tests
163 lines
4.4 KiB
Go
163 lines
4.4 KiB
Go
package documenttext
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"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 := 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, id, &inloc.Bucket, &inloc.Key, int64(1), dbmimetype, repository.NullCleanfailtype{}),
|
|
)
|
|
textId := uuid.New()
|
|
pool.ExpectBegin()
|
|
pool.ExpectQuery("name: GetDocumentTextExtractionByHash :one").WithArgs(dbCleanId, "example").WillReturnRows(
|
|
pgxmock.NewRows([]string{"id", "documentId", "bucket", "key", "version", "hash", "cleanEntryId"}),
|
|
)
|
|
pool.ExpectQuery("name: AddDocumentText :one").WithArgs(inloc.Bucket, inloc.Key, dbCleanId, "example").
|
|
WillReturnRows(
|
|
pgxmock.NewRows([]string{"id"}).
|
|
AddRow(textId),
|
|
)
|
|
pool.ExpectExec("name: AddDocumentTextEntry :exec").WithArgs(textId, pgxmock.AnyArg()).
|
|
WillReturnResult(pgxmock.NewResult("", 1))
|
|
pool.ExpectCommit()
|
|
|
|
err = svc.extract(ctx, cleanId)
|
|
require.NoError(t, err)
|
|
})
|
|
t.Run("fail clean", func(t *testing.T) {
|
|
id := uuid.New()
|
|
|
|
cleanId := uuid.New()
|
|
fail := repository.NullCleanfailtype{
|
|
Valid: true,
|
|
Cleanfailtype: repository.CleanfailtypeInvalidMimetype,
|
|
}
|
|
pool.ExpectQuery("name: GetCleanEntry :one").WithArgs(id).WillReturnRows(
|
|
pgxmock.NewRows([]string{"id", "documentId", "bucket", "key", "version", "mimetype", "fail"}).
|
|
AddRow(cleanId, 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",
|
|
}
|
|
extract := extraction{
|
|
TextLocation: location,
|
|
Hash: "example",
|
|
}
|
|
dbmimetype := repository.NullCleanmimetype{
|
|
Valid: true,
|
|
Cleanmimetype: repository.CleanmimetypeApplicationPdf,
|
|
}
|
|
|
|
pool.ExpectQuery("name: GetCleanEntry :one").WithArgs(id).WillReturnRows(
|
|
pgxmock.NewRows([]string{"id", "documentId", "bucket", "key", "version", "mimetype", "fail"}).
|
|
AddRow(uuid.New(), id, &location.Bucket, &location.Key, int32(1), dbmimetype, repository.NullCleanfailtype{}),
|
|
)
|
|
|
|
outloc, err := svc.executeExtraction(ctx, id)
|
|
require.NoError(t, err)
|
|
assert.EqualExportedValues(t, extract, *outloc)
|
|
}
|
|
|
|
func TestStoreExtraction(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()
|
|
|
|
t.Run("not existing", func(t *testing.T) {
|
|
cleanId := uuid.New()
|
|
dbCleanId := cleanId
|
|
textId := uuid.New()
|
|
extract := extraction{
|
|
TextLocation: document.Location{
|
|
Bucket: "buck",
|
|
Key: "key",
|
|
},
|
|
Hash: "example",
|
|
}
|
|
|
|
pool.ExpectBegin()
|
|
pool.ExpectQuery("name: GetDocumentTextExtractionByHash :one").WithArgs(dbCleanId, extract.Hash).WillReturnRows(
|
|
pgxmock.NewRows([]string{"id", "documentId", "bucket", "key", "version", "hash", "cleanEntryId"}),
|
|
)
|
|
pool.ExpectQuery("name: AddDocumentText :one").WithArgs(extract.TextLocation.Bucket, extract.TextLocation.Key, dbCleanId, "example").
|
|
WillReturnRows(
|
|
pgxmock.NewRows([]string{"id"}).
|
|
AddRow(textId),
|
|
)
|
|
pool.ExpectExec("name: AddDocumentTextEntry :exec").WithArgs(textId, pgxmock.AnyArg()).
|
|
WillReturnResult(pgxmock.NewResult("", 1))
|
|
pool.ExpectCommit()
|
|
|
|
err = svc.storeExtraction(ctx, cleanId, &extract)
|
|
require.NoError(t, err)
|
|
})
|
|
}
|