Add test coverage for getVideoFromFileStore flow
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Rob Watson 2022-01-03 20:59:59 +01:00
parent 12e6e73976
commit 932648a44b
3 changed files with 77 additions and 4 deletions

View File

@ -115,10 +115,10 @@ func (s *videoGetterState) Next() (GetVideoProgress, error) {
}
}
type videoGetterDownloaded string
type videoGetterFromFileStore string
// Next() implements GetVideoProgressReader.
func (s *videoGetterDownloaded) Next() (GetVideoProgress, error) {
func (s *videoGetterFromFileStore) Next() (GetVideoProgress, error) {
return GetVideoProgress{
PercentComplete: 100,
URL: string(*s),

View File

@ -0,0 +1,72 @@
package media_test
import (
"context"
"database/sql"
"errors"
"io"
"testing"
"time"
"git.netflux.io/rob/clipper/config"
"git.netflux.io/rob/clipper/generated/mocks"
"git.netflux.io/rob/clipper/generated/store"
"git.netflux.io/rob/clipper/media"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
)
func TestGetVideoFromFileStore(t *testing.T) {
ctx := context.Background()
logger := zap.NewNop().Sugar()
videoID := "video001"
mediaSetID := uuid.New()
mediaSet := store.MediaSet{
ID: mediaSetID,
YoutubeID: videoID,
VideoS3UploadedAt: sql.NullTime{Time: time.Now(), Valid: true},
VideoS3Key: sql.NullString{String: "videos/myvideo", Valid: true},
}
t.Run("NOK,ErrorFetchingMediaSet", func(t *testing.T) {
var mockStore mocks.Store
mockStore.On("GetMediaSet", ctx, mediaSetID).Return(store.MediaSet{}, errors.New("database fail"))
service := media.NewMediaSetService(&mockStore, nil, nil, nil, config.Config{}, logger)
_, err := service.GetVideo(ctx, mediaSetID)
require.EqualError(t, err, "error getting media set: database fail")
})
t.Run("NOK,ErrorGettingObjectURL", func(t *testing.T) {
var mockStore mocks.Store
mockStore.On("GetMediaSet", ctx, mediaSetID).Return(mediaSet, nil)
var fileStore mocks.FileStore
fileStore.On("GetURL", ctx, "videos/myvideo").Return("", errors.New("key missing"))
service := media.NewMediaSetService(&mockStore, nil, &fileStore, nil, config.Config{}, logger)
_, err := service.GetVideo(ctx, mediaSetID)
require.EqualError(t, err, "error generating presigned URL: key missing")
})
t.Run("OK", func(t *testing.T) {
var mockStore mocks.Store
mockStore.On("GetMediaSet", ctx, mediaSetID).Return(mediaSet, nil)
const url = "https://www.example.com/audio"
var fileStore mocks.FileStore
fileStore.On("GetURL", ctx, "videos/myvideo").Return(url, nil)
service := media.NewMediaSetService(&mockStore, nil, &fileStore, nil, config.Config{}, logger)
stream, err := service.GetVideo(ctx, mediaSetID)
require.NoError(t, err)
progress, err := stream.Next()
assert.Equal(t, float32(100), progress.PercentComplete)
assert.Equal(t, url, progress.URL)
assert.Equal(t, io.EOF, err)
})
}

View File

@ -214,11 +214,12 @@ func (s *MediaSetService) GetVideo(ctx context.Context, id uuid.UUID) (GetVideoP
}
if mediaSet.VideoS3UploadedAt.Valid {
url, err := s.fileStore.GetURL(ctx, mediaSet.VideoS3Key.String)
var url string
url, err = s.fileStore.GetURL(ctx, mediaSet.VideoS3Key.String)
if err != nil {
return nil, fmt.Errorf("error generating presigned URL: %v", err)
}
videoGetter := videoGetterDownloaded(url)
videoGetter := videoGetterFromFileStore(url)
return &videoGetter, nil
}