-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c26232f
commit af54887
Showing
1 changed file
with
79 additions
and
0 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
pkg/component/operator/document/v0/convert_to_images_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package document | ||
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
qt "github.com/frankban/quicktest" | ||
|
||
"github.com/instill-ai/pipeline-backend/pkg/component/base" | ||
"github.com/instill-ai/pipeline-backend/pkg/component/internal/mock" | ||
"github.com/instill-ai/pipeline-backend/pkg/data" | ||
"github.com/instill-ai/pipeline-backend/pkg/data/format" | ||
) | ||
|
||
func Test_ConvertDocumentToImages(t *testing.T) { | ||
c := qt.New(t) | ||
|
||
test := struct { | ||
name string | ||
filepath string | ||
expectedLen int | ||
}{ | ||
name: "Convert PDF to Images", | ||
filepath: "testdata/test.pdf", | ||
expectedLen: 1, | ||
} | ||
|
||
bc := base.Component{} | ||
ctx := context.Background() | ||
|
||
component := Init(bc) | ||
c.Assert(component, qt.IsNotNil) | ||
|
||
execution, err := component.CreateExecution(base.ComponentExecution{ | ||
Component: component, | ||
Task: taskConvertToImages, | ||
}) | ||
c.Assert(err, qt.IsNil) | ||
c.Assert(execution, qt.IsNotNil) | ||
|
||
fileContent, err := os.ReadFile(test.filepath) | ||
c.Assert(err, qt.IsNil) | ||
|
||
base64DataURI := fmt.Sprintf("data:%s;base64,%s", mimeTypeByExtension(test.filepath), base64.StdEncoding.EncodeToString(fileContent)) | ||
|
||
ir, ow, eh, job := mock.GenerateMockJob(c) | ||
|
||
ir.ReadDataMock.Times(1).Set(func(ctx context.Context, input any) error { | ||
switch input := input.(type) { | ||
case *ConvertDocumentToImagesInput: | ||
*input = ConvertDocumentToImagesInput{ | ||
Document: func() format.Document { | ||
doc, err := data.NewDocumentFromURL(base64DataURI) | ||
if err != nil { | ||
return nil | ||
} | ||
return doc | ||
}(), | ||
} | ||
} | ||
return nil | ||
}) | ||
|
||
ow.WriteDataMock.Times(1).Set(func(ctx context.Context, output any) error { | ||
switch output := output.(type) { | ||
case *ConvertDocumentToImagesOutput: | ||
mock.Equal(len(output.Images), test.expectedLen) | ||
} | ||
return nil | ||
}) | ||
|
||
eh.ErrorMock.Optional() | ||
|
||
err = execution.Execute(ctx, []*base.Job{job}) | ||
c.Assert(err, qt.IsNil) | ||
} |