Skip to content

Commit

Permalink
flesh out unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
jmillerv committed Aug 6, 2023
1 parent 0119591 commit 51472ab
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
run: go build -v ./...

- name: Test
run: go test -v ./...
run: export CI=true && go test -v ./...
# configuration: https://github.com/golangci/golangci-lint-action
golangci:
name: lint
Expand Down
65 changes: 60 additions & 5 deletions content/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"os"
"path/filepath"
"strconv"
"strings"
"time"

Expand All @@ -20,11 +21,16 @@ import (
)

const (
wavFile = "wav"
mp3File = "mp3"
oggFile = "oggs"
flacFile = "flac"
sampleRateTime = 10
wavFile string = "wav"
mp3File string = "mp3"
oggFile string = "oggs"
flacFile string = "flac"
sampleRateTime = 10
wavFileBitRate int64 = 1441
mp3FileBitRate int64 = 128
oggFileBitRate int64 = 192
flacFileBitRate int64 = 320
durationBase = 10
)

type LocalFile struct {
Expand Down Expand Up @@ -164,3 +170,52 @@ func (l *LocalFile) getFileType(buf []byte) string {

return ""
}

// getEstimatedFileDuration uses oft use bitrates for given filtypes and provides and estimated duration in seconds
func (l *LocalFile) getEstimatedFileDuration() string {
// assume a constant bitrate for the file types
switch l.fileType {
case wavFile:
stats, err := l.Content.Stat()
if err != nil {
log.WithError(err).Error("failed to get wav file stats")
return ""
}
duration := stats.Size() / wavFileBitRate

return strconv.FormatInt(duration, durationBase)

case mp3File:
stats, err := l.Content.Stat()
if err != nil {
log.WithError(err).Error("failed to get mp3 file stats")
return ""
}
duration := stats.Size() / mp3FileBitRate

return strconv.FormatInt(duration, durationBase)

case oggFile:
stats, err := l.Content.Stat()
if err != nil {
log.WithError(err).Error("failed to get mp3 file stats")
return ""
}
duration := stats.Size() / oggFileBitRate

return strconv.FormatInt(duration, durationBase)

case flacFile:
stats, err := l.Content.Stat()
if err != nil {
log.WithError(err).Error("failed to get FLAC file stats")
return ""
}
duration := stats.Size() / flacFileBitRate

return strconv.FormatInt(duration, durationBase)

default:
return "unknown file type: can't determine duration"
}
}
109 changes: 104 additions & 5 deletions content/file_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
// nolint:TODO https://github.com/jmillerv/go-dj/issues/16
package content_test
package content

import (
"io"
"os"
"testing"

"github.com/faiface/beep"
"github.com/jmillerv/go-dj/content"
"github.com/stretchr/testify/assert"
)

const (
envCI = "CI"
)

func TestLocalFile_Get(t *testing.T) {
Expand All @@ -28,7 +32,7 @@ func TestLocalFile_Get(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
l := &content.LocalFile{
l := &LocalFile{
Name: tt.fields.Name,
Content: tt.fields.Content,
Path: tt.fields.Path,
Expand Down Expand Up @@ -58,7 +62,7 @@ func TestLocalFile_Play(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
l := &content.LocalFile{
l := &LocalFile{
Name: tt.fields.Name,
Content: tt.fields.Content,
Path: tt.fields.Path,
Expand Down Expand Up @@ -88,7 +92,7 @@ func TestLocalFile_Stop(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
l := &content.LocalFile{
l := &LocalFile{
Name: tt.fields.Name,
Content: tt.fields.Content,
Path: tt.fields.Path,
Expand All @@ -99,3 +103,98 @@ func TestLocalFile_Stop(t *testing.T) {
})
}
}

func TestLocalFile_getEstimatedFileDuration(t *testing.T) {
if os.Getenv(envCI) == "true" {
t.Skipf("CI detected, skipping")
}
type fields struct {
Name string
Content *os.File
Path string
decodeReader func(r io.Reader) (s beep.StreamSeekCloser, format beep.Format, err error)
decodeReadCloser func(rc io.ReadCloser) (s beep.StreamSeekCloser, format beep.Format, err error)
fileType string
}
mp3File := &LocalFile{
Name: "piano_six_seconds.mp3",
Path: "../static/piano_six_seconds.mp3",
Content: nil,
}
wavFile := &LocalFile{
Name: "CantinaBand3.wav",
Path: "../static/CantinaBand3.wav",
Content: nil,
}
flacFile := &LocalFile{
Name: "JosefSuk-Meditation.flac",
Path: "../static/JosefSuk-Meditation.flac",
Content: nil,
}
oggFile := &LocalFile{
Name: "",
Content: nil,
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "wav file",
fields: fields{
Name: wavFile.Name,
Content: wavFile.Content,
Path: wavFile.Path,
fileType: wavFile.fileType,
},
want: "3",
},
{
name: "mp3 file",
fields: fields{
Name: mp3File.Name,
Content: mp3File.Content,
Path: mp3File.Path,
fileType: mp3File.fileType,
},
want: "",
},
{
name: "ogg file",
fields: fields{
Name: oggFile.Name,
Content: oggFile.Content,
Path: oggFile.Path,
fileType: oggFile.fileType},
want: "",
},
{
name: "FLAC file",
fields: fields{
Name: flacFile.Name,
Content: flacFile.Content,
Path: flacFile.Path,
fileType: flacFile.fileType,
},
want: "",
},
{
name: "default",
fields: fields{
fileType: "mp4",
},
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
l := &LocalFile{
Name: tt.fields.Name,
Content: tt.fields.Content,
Path: tt.fields.Path,
}
assert.Equalf(t, tt.want, l.getEstimatedFileDuration(), "getEstimatedFileDuration()")
})
}
}

0 comments on commit 51472ab

Please sign in to comment.