7ce7c9df4d
Feature/ecr * nosave * repo * awscli * unzip * ignore * moreram * 14k * ref * deployment * 12k * uselocal * go * dockercomd * reorder * iamgename * installs * tart * cli * clideps * y * dockerce * nodock * multi * rmecr * dev
126 lines
3.5 KiB
Go
126 lines
3.5 KiB
Go
package documenttext
|
|
|
|
import (
|
|
"database/sql"
|
|
"io"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"queryorchestration/internal/database/repository"
|
|
"queryorchestration/internal/serviceconfig/objectstore"
|
|
objectstoremock "queryorchestration/mocks/objectstore"
|
|
queuemock "queryorchestration/mocks/queue"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/service/s3"
|
|
"github.com/google/uuid"
|
|
"github.com/pashagolub/pgxmock/v3"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/mock"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestStoreExtract(t *testing.T) {
|
|
ctx := t.Context()
|
|
pool, err := pgxmock.NewPool()
|
|
require.NoError(t, err)
|
|
|
|
cfg := &DocTextConfig{}
|
|
cfg.DBPool = pool
|
|
cfg.DBQueries = repository.New(pool)
|
|
cfg.QuerySyncURL = "querysync"
|
|
mockSQS := queuemock.NewMockSQSClient(t)
|
|
cfg.QueueClient = mockSQS
|
|
mockS3 := objectstoremock.NewMockS3Client(t)
|
|
cfg.StoreClient = mockS3
|
|
|
|
svc := Service{
|
|
cfg: cfg,
|
|
}
|
|
|
|
cleanId := uuid.New()
|
|
textId := uuid.New()
|
|
clientId := "hello"
|
|
bucket := "bucket"
|
|
|
|
t.Run("exists", func(t *testing.T) {
|
|
hash := `"5d41402abc4b2a76b9719d911017c592"`
|
|
pool.ExpectBegin()
|
|
pool.ExpectQuery("name: GetDocumentTextExtractionByHash :one").WithArgs(cleanId, hash).WillReturnRows(
|
|
pgxmock.NewRows([]string{"id"}).
|
|
AddRow(textId),
|
|
)
|
|
pool.ExpectExec("name: AddDocumentTextEntry :exec").WithArgs(textId, pgxmock.AnyArg()).
|
|
WillReturnResult(pgxmock.NewResult("", 1))
|
|
pool.ExpectCommit()
|
|
|
|
text := strings.NewReader("hello")
|
|
err = svc.storeProcess(ctx, text, &repository.Currentcleanentry{
|
|
ID: cleanId,
|
|
})
|
|
require.NoError(t, err)
|
|
})
|
|
t.Run("not exists", func(t *testing.T) {
|
|
part := uint16(0)
|
|
key := objectstore.BucketKey{
|
|
ClientID: clientId,
|
|
Location: objectstore.Text,
|
|
CreatedAt: time.Now().UTC(),
|
|
EntityID: cleanId,
|
|
Part: &part,
|
|
}
|
|
keyStr := key.String()
|
|
hash := `"09644372e99020106946045c6fd2d70b"`
|
|
pool.ExpectBegin()
|
|
pool.ExpectQuery("name: GetDocumentTextExtractionByHash :one").WithArgs(cleanId, hash).
|
|
WillReturnError(sql.ErrNoRows)
|
|
pool.ExpectQuery("name: GetTextOutCurrentPart :one").WithArgs(&clientId, pgxmock.AnyArg()).
|
|
WillReturnRows(
|
|
pgxmock.NewRows([]string{"part", "count"}).
|
|
AddRow(0, 2),
|
|
)
|
|
pool.ExpectQuery("name: AddDocumentText :one").WithArgs(cleanId, bucket, key.String(), hash, pgxmock.AnyArg(), uint16(0)).
|
|
WillReturnRows(
|
|
pgxmock.NewRows([]string{"id"}).
|
|
AddRow(textId),
|
|
)
|
|
|
|
mockS3.EXPECT().
|
|
PutObject(
|
|
mock.Anything,
|
|
mock.MatchedBy(func(in *s3.PutObjectInput) bool {
|
|
bod, err := io.ReadAll(in.Body)
|
|
require.NoError(t, err)
|
|
return string(bod) == "byebye" && *in.Key == key.String() && *in.Bucket == bucket
|
|
}),
|
|
mock.Anything,
|
|
).
|
|
Return(&s3.PutObjectOutput{}, nil)
|
|
|
|
pool.ExpectExec("name: AddDocumentTextEntry :exec").WithArgs(textId, pgxmock.AnyArg()).
|
|
WillReturnResult(pgxmock.NewResult("", 1))
|
|
pool.ExpectCommit()
|
|
|
|
text := strings.NewReader("byebye")
|
|
err = svc.storeProcess(ctx, text, &repository.Currentcleanentry{
|
|
ID: cleanId,
|
|
Clientid: clientId,
|
|
Bucket: &bucket,
|
|
Key: &keyStr,
|
|
})
|
|
require.NoError(t, err)
|
|
})
|
|
}
|
|
|
|
func TestDuplicateReader(t *testing.T) {
|
|
original := strings.NewReader("hello")
|
|
readerOne, readerTwo, err := duplicateReader(original)
|
|
require.NoError(t, err)
|
|
contentOne, err := io.ReadAll(readerOne)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, []byte("hello"), contentOne)
|
|
contentTwo, err := io.ReadAll(readerTwo)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, []byte("hello"), contentTwo)
|
|
}
|