f11f4def43
Clean Set Up * fail * starteddb * constraint * cleantest * fixqueries * fixtests * storemimetype * buffer * tests * setup * passingtests * pdfHElloWorld * rm * test * notodos * tests * clean * testpdf
215 lines
4.3 KiB
Go
215 lines
4.3 KiB
Go
package documentclean
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"queryorchestration/internal/database"
|
|
"queryorchestration/internal/database/repository"
|
|
"queryorchestration/internal/document"
|
|
cleanversion "queryorchestration/internal/document/clean/version"
|
|
objectstoremock "queryorchestration/mocks/objectstore"
|
|
"strings"
|
|
"testing"
|
|
|
|
"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"
|
|
)
|
|
|
|
func TestClean(t *testing.T) {
|
|
ctx := context.Background()
|
|
pool, err := pgxmock.NewPool()
|
|
if err != nil {
|
|
t.Fatalf("failed to open pgxmock database: %v", err)
|
|
}
|
|
|
|
cfg := &DocCleanConfig{}
|
|
cfg.DBPool = pool
|
|
cfg.DBQueries = repository.New(pool)
|
|
mockS3 := objectstoremock.NewMockS3Client(t)
|
|
cfg.StoreClient = mockS3
|
|
|
|
svc := Service{
|
|
cfg: cfg,
|
|
svc: &Services{
|
|
Version: cleanversion.New(cfg),
|
|
},
|
|
}
|
|
|
|
doc := document.Document{
|
|
ID: uuid.New(),
|
|
JobID: uuid.New(),
|
|
Hash: "example_hash",
|
|
}
|
|
inloc := document.Location{
|
|
Bucket: "bucket_name",
|
|
Key: "/i/am/here",
|
|
}
|
|
dbid := database.MustToDBUUID(doc.ID)
|
|
|
|
pool.ExpectQuery("name: GetDocument :one").WithArgs(dbid).
|
|
WillReturnRows(
|
|
pgxmock.NewRows([]string{"id", "jobId", "hash"}).
|
|
AddRow(dbid, database.MustToDBUUID(doc.JobID), doc.Hash),
|
|
)
|
|
pool.ExpectQuery("name: GetDocumentEntry :one").WithArgs(dbid).WillReturnRows(
|
|
pgxmock.NewRows([]string{"documentId", "bucket", "key"}).
|
|
AddRow(dbid, inloc.Bucket, inloc.Key),
|
|
)
|
|
mimeType := "application/pdf"
|
|
dbmimetype := repository.NullCleanmimetypes{
|
|
Valid: true,
|
|
Cleanmimetypes: repository.Cleanmimetypes(mimeType),
|
|
}
|
|
pool.ExpectExec("name: AddDocumentCleanEntry :exec").WithArgs(dbid, int32(1), &inloc.Bucket, &inloc.Key, dbmimetype, repository.NullCleanfailtype{}).
|
|
WillReturnResult(pgxmock.NewResult("", 1))
|
|
|
|
var length int64 = 10
|
|
mockS3.EXPECT().
|
|
HeadObject(
|
|
mock.Anything,
|
|
mock.MatchedBy(func(in *s3.HeadObjectInput) bool {
|
|
return *in.Bucket == inloc.Bucket && *in.Key == inloc.Key
|
|
}),
|
|
mock.Anything,
|
|
).
|
|
Return(&s3.HeadObjectOutput{
|
|
ContentType: &mimeType,
|
|
ContentLength: &length,
|
|
}, nil)
|
|
|
|
body := io.NopCloser(strings.NewReader(pdfHelloWorld))
|
|
mockS3.EXPECT().
|
|
GetObject(
|
|
mock.Anything,
|
|
mock.MatchedBy(func(in *s3.GetObjectInput) bool {
|
|
return *in.Bucket == inloc.Bucket && *in.Key == inloc.Key
|
|
}),
|
|
mock.Anything,
|
|
).
|
|
Return(&s3.GetObjectOutput{
|
|
Body: body,
|
|
}, nil)
|
|
|
|
err = svc.clean(ctx, doc.ID)
|
|
assert.NoError(t, err)
|
|
}
|
|
|
|
func TestExecuteCleanTasks(t *testing.T) {
|
|
ctx := context.Background()
|
|
|
|
cfg := &DocCleanConfig{}
|
|
mockS3 := objectstoremock.NewMockS3Client(t)
|
|
cfg.StoreClient = mockS3
|
|
svc := Service{
|
|
cfg: cfg,
|
|
}
|
|
|
|
id := uuid.New()
|
|
hash := "example_hash"
|
|
inloc := document.Location{
|
|
Bucket: "bucket_name",
|
|
Key: "/i/am/here",
|
|
}
|
|
|
|
mimeType := "application/pdf"
|
|
mockS3.EXPECT().
|
|
HeadObject(
|
|
mock.Anything,
|
|
mock.MatchedBy(func(in *s3.HeadObjectInput) bool {
|
|
return *in.Bucket == inloc.Bucket && *in.Key == inloc.Key
|
|
}),
|
|
mock.Anything,
|
|
).
|
|
Return(&s3.HeadObjectOutput{
|
|
ContentType: &mimeType,
|
|
}, nil)
|
|
|
|
body := io.NopCloser(strings.NewReader(pdfHelloWorld))
|
|
mockS3.EXPECT().
|
|
GetObject(
|
|
mock.Anything,
|
|
mock.MatchedBy(func(in *s3.GetObjectInput) bool {
|
|
return *in.Bucket == inloc.Bucket && *in.Key == inloc.Key
|
|
}),
|
|
mock.Anything,
|
|
).
|
|
Return(&s3.GetObjectOutput{
|
|
Body: body,
|
|
}, nil)
|
|
|
|
outloc, err := svc.executeCleanTasks(ctx, &CleanParams{
|
|
ID: id,
|
|
Hash: hash,
|
|
Location: inloc,
|
|
})
|
|
assert.NoError(t, err)
|
|
assert.EqualExportedValues(t, ExecuteCleanResponse{
|
|
location: &inloc,
|
|
}, *outloc)
|
|
}
|
|
|
|
const pdfHelloWorld = `%PDF-1.4
|
|
%����
|
|
1 0 obj
|
|
<<
|
|
/Type /Catalog
|
|
/Pages 2 0 R
|
|
/Version /1.4
|
|
>>
|
|
endobj
|
|
2 0 obj
|
|
<<
|
|
/Type /Pages
|
|
/Kids [3 0 R]
|
|
/Count 1
|
|
>>
|
|
endobj
|
|
3 0 obj
|
|
<<
|
|
/Type /Page
|
|
/Parent 2 0 R
|
|
/MediaBox [0 0 612 792]
|
|
/Resources <<
|
|
/Font <<
|
|
/F1 <<
|
|
/Type /Font
|
|
/Subtype /Type1
|
|
/BaseFont /Helvetica
|
|
>>
|
|
>>
|
|
>>
|
|
/Contents 4 0 R
|
|
>>
|
|
endobj
|
|
4 0 obj
|
|
<<
|
|
/Length
|
|
44
|
|
>>
|
|
stream
|
|
BT
|
|
/F1 24 Tf
|
|
100 700 Td
|
|
(Hello World!) Tj
|
|
ET
|
|
endstream
|
|
endobj
|
|
xref
|
|
0 5
|
|
0000000000 65535 f
|
|
0000000015 00000 n
|
|
0000000086 00000 n
|
|
0000000151 00000 n
|
|
0000000376 00000 n
|
|
trailer
|
|
<<
|
|
/Size 5
|
|
/Root 1 0 R
|
|
>>
|
|
startxref
|
|
472
|
|
%%EOF`
|