Skip to content

Commit

Permalink
test: add transformer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hum committed Mar 30, 2024
1 parent ee74216 commit ea28ec4
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 2 deletions.
11 changes: 10 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,13 @@ module github.com/hum/vcat

go 1.21.5

require github.com/mitchellh/mapstructure v1.5.0
require (
github.com/mitchellh/mapstructure v1.5.0
github.com/stretchr/testify v1.9.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2 changes: 1 addition & 1 deletion transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func getRawVideoDetailFromInitialHttpResponse(b []byte) (*rawVideoDetail, error)

var (
rawCaptions = parts[0]
rawMetadata = metadataParts[0][1:] // Remove the ":" prefix of the string, so that it is a valid JSON
rawMetadata = strings.Split(metadataParts[0][1:], ",\"annotations\"")[0] // Remove the ":" prefix of the string, so that it is a valid JSON

captionsMap map[string]interface{}
metadataMap map[string]interface{}
Expand Down
60 changes: 60 additions & 0 deletions transformer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package vcat

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestTransformerReturnsRawVideoDetailShouldPass(t *testing.T) {
tests := []struct {
url string
}{
{
url: "https://www.youtube.com/watch?v=uyMtsyzXWd4",
},
{
url: "https://www.youtube.com/watch?v=709z-t7IiFw",
},
}

for _, tt := range tests {
body, err := do(httpclient, tt.url)
require.NoError(t, err)

output, err := getRawVideoDetailFromInitialHttpResponse(body)
require.NoError(t, err)
require.NotEmpty(t, output)
}
}

func TestTransformerGetsTranscriptFromURLShouldPass(t *testing.T) {
tests := []struct {
url string
}{
{
url: "https://www.youtube.com/watch?v=uyMtsyzXWd4",
},
{
url: "https://www.youtube.com/watch?v=709z-t7IiFw",
},
}

for _, tt := range tests {
rawb, err := do(httpclient, tt.url)
require.NoError(t, err)

rawv, err := getRawVideoDetailFromInitialHttpResponse(rawb)
require.NoError(t, err)

var transcriptUrl = rawv.captions.PlayerCaptionsTracklistRenderer.CaptionTracks[0].BaseUrl
body, err := do(httpclient, transcriptUrl)
require.NoError(t, err)

transcript, err := getTranscriptFromXMLResponse(body)
require.NoError(t, err)

require.NotEmpty(t, transcript)
require.NotEmpty(t, transcript.Text)
}
}

0 comments on commit ea28ec4

Please sign in to comment.