Skip to content

Commit

Permalink
feat: add test code
Browse files Browse the repository at this point in the history
  • Loading branch information
chuang8511 committed Nov 6, 2024
1 parent c26232f commit af54887
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions pkg/component/operator/document/v0/convert_to_images_test.go
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)
}

0 comments on commit af54887

Please sign in to comment.