Skip to content

Commit

Permalink
update tests to test each GetTemplate types
Browse files Browse the repository at this point in the history
  • Loading branch information
bthaile committed Nov 30, 2023
1 parent 24f6286 commit 1230302
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
10 changes: 8 additions & 2 deletions flixkit.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/fs"
"log"
"net/http"
"net/url"
Expand Down Expand Up @@ -51,9 +50,13 @@ type flixServiceImpl struct {

var _ FlixService = (*flixServiceImpl)(nil)

type FileReader interface {
ReadFile(path string) ([]byte, error)
}

type Config struct {
FlixServerURL string
FileReader fs.ReadFileFS
FileReader FileReader
}

func NewFlixService(config *Config) FlixService {
Expand Down Expand Up @@ -230,6 +233,9 @@ func (s *flixServiceImpl) GetTemplate(ctx context.Context, flixQuery string) (st
}

case flixPath:
if s.config.FileReader == nil {
return "", fmt.Errorf("file reader not provided")
}
file, err := s.config.FileReader.ReadFile(flixQuery)
if err != nil {
return "", fmt.Errorf("could not read flix file %s: %w", flixQuery, err)
Expand Down
19 changes: 10 additions & 9 deletions flixkit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package flixkit

import (
"context"
"io/fs"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -252,6 +251,12 @@ func TestFetchFlix(t *testing.T) {
assert.Equal("Hello World", body, "GetFlix should return the correct body")
}

type DefaultReader struct{}

func (d DefaultReader) ReadFile(path string) ([]byte, error) {
return []byte(flix_template), nil
}

func TestGetFlixRaw(t *testing.T) {
assert := assert.New(t)

Expand All @@ -261,24 +266,24 @@ func TestGetFlixRaw(t *testing.T) {
}))
defer server.Close()

flixService := NewFlixService(&Config{FlixServerURL: server.URL})
flixService := NewFlixService(&Config{FlixServerURL: server.URL, FileReader: DefaultReader{}})
ctx := context.Background()
body, err := flixService.GetTemplate(ctx, "templateName")
assert.NoError(err, "GetFlixByName should not return an error")
assert.Equal("Hello World", body, "GetFlixByName should return the correct body")
}

func TestGetFlix(t *testing.T) {
func TestGetFlixFilename(t *testing.T) {
assert := assert.New(t)

server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.Write([]byte(flix_template))
}))
defer server.Close()

flixService := NewFlixService(&Config{FlixServerURL: server.URL})
flixService := NewFlixService(&Config{FlixServerURL: server.URL, FileReader: DefaultReader{}})
ctx := context.Background()
flix, err := flixService.GetTemplate(ctx, "templateName")
flix, err := flixService.GetTemplate(ctx, "./templateFileName")
assert.NoError(err, "GetParsedFlixByName should not return an error")
assert.NotNil(flix, "GetParsedFlixByName should not return a nil Flix")
assert.Equal(flix_template, flix, "GetParsedFlixByName should return the correct Flix")
Expand Down Expand Up @@ -316,10 +321,6 @@ func TestGetFlixByID(t *testing.T) {
assert.Equal(flix_template, flix, "GetParsedFlixByID should return the correct Flix")
}

type MapFsReader struct {
FS fs.FS
}

func TestTemplateVersion(t *testing.T) {
assert := assert.New(t)

Expand Down
Empty file added templateFileName
Empty file.
1 change: 0 additions & 1 deletion types.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,6 @@ func ShaHex(value interface{}, debugKey string) string {
// Convert the hash to a hexadecimal string
hashHex := hex.EncodeToString(hash[:])

//fmt.Println(value, hashHex)
return hashHex
}

Expand Down

0 comments on commit 1230302

Please sign in to comment.