Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
memoryonrepeat committed Dec 17, 2024
1 parent ab1a3b3 commit 5440c94
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
117 changes: 117 additions & 0 deletions pkg/component/operator/video/v0/task_embed_audio_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package video

import (
"context"
"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"
)

func TestEmbedAudio(t *testing.T) {
c := qt.New(t)

testCases := []struct {
name string
videoFile string
audioFile string
wantVideo string
expectedError string
}{
{
name: "ok - extract audio from video",
videoFile: "testdata/video-sample-bunny.mp4",
audioFile: "testdata/audio.ogg",
wantVideo: "testdata/embed-video.mp4",
},
{
name: "nok - invalid video file",
videoFile: "invalid_video_data",
expectedError: "reading input data: open invalid_video_data: no such file or directory",
},
}

for _, tc := range testCases {
c.Run(tc.name, func(c *qt.C) {
component := Init(base.Component{})
c.Assert(component, qt.IsNotNil)

execution, err := component.CreateExecution(base.ComponentExecution{
Component: component,
Task: "TASK_EMBED_AUDIO",
})
c.Assert(err, qt.IsNil)
c.Assert(execution, qt.IsNotNil)

ir, ow, eh, job := mock.GenerateMockJob(c)

ir.ReadDataMock.Set(func(ctx context.Context, input any) error {
switch input := input.(type) {
case *embedAudioInput:
videoBytes, err := os.ReadFile(tc.videoFile)
if err != nil {
return err
}
video, err := data.NewVideoFromBytes(videoBytes, "video/mp4", tc.videoFile)
if err != nil {
return err
}
audioBytes, err := os.ReadFile(tc.audioFile)
if err != nil {
return err
}
audio, err := data.NewAudioFromBytes(audioBytes, "audio/ogg", tc.audioFile)
if err != nil {
return err
}
*input = embedAudioInput{
Video: video,
Audio: audio,
}
}
return nil
})

var capturedOutput any
ow.WriteDataMock.Set(func(ctx context.Context, output any) error {
capturedOutput = output
actualOutput := output.(embedAudioOutput)

expectedVideoBytes, err := os.ReadFile(tc.wantVideo)
if err != nil {
return err
}
expectedVideo, err := data.NewVideoFromBytes(expectedVideoBytes, "video/mp4", tc.wantVideo)
if err != nil {
return err
}

compareVideo(c, actualOutput.Video, expectedVideo)
return nil
})

eh.ErrorMock.Set(func(ctx context.Context, err error) {
c.Assert(err, qt.ErrorMatches, tc.expectedError)
})

if tc.expectedError != "" {
ow.WriteDataMock.Optional()
} else {
eh.ErrorMock.Optional()
}

err = execution.Execute(context.Background(), []*base.Job{job})

if tc.expectedError == "" {
c.Assert(err, qt.IsNil)
output, ok := capturedOutput.(embedAudioOutput)
c.Assert(ok, qt.IsTrue)
c.Assert(output.Video, qt.Not(qt.IsNil))
}
})
}
}
Binary file not shown.
Binary file not shown.

0 comments on commit 5440c94

Please sign in to comment.