refactor: add cache tests and remove cache merge task (#14)

This commit is contained in:
l3uddz
2021-02-19 19:15:38 +00:00
committed by GitHub
parent 54cfd68810
commit e1945cf714
18 changed files with 537 additions and 61 deletions

83
util/default_test.go Normal file
View File

@@ -0,0 +1,83 @@
package util
import "testing"
func TestAtof64(t *testing.T) {
type args struct {
val string
defaultVal float64
}
tests := []struct {
name string
args args
want float64
}{
{
name: "expect whole value",
args: args{
val: "1",
defaultVal: 0,
},
want: 1.0,
},
{
name: "expect non whole value",
args: args{
val: "1.5",
defaultVal: 0,
},
want: 1.5,
},
{
name: "expect default",
args: args{
val: "invalid",
defaultVal: 0,
},
want: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Atof64(tt.args.val, tt.args.defaultVal); got != tt.want {
t.Errorf("Atof64() = %v, want %v", got, tt.want)
}
})
}
}
func TestAtoi(t *testing.T) {
type args struct {
val string
defaultVal int
}
tests := []struct {
name string
args args
want int
}{
{
name: "expect value",
args: args{
val: "5",
defaultVal: 0,
},
want: 5,
},
{
name: "expect default",
args: args{
val: "invalid",
defaultVal: 2,
},
want: 2,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Atoi(tt.args.val, tt.args.defaultVal); got != tt.want {
t.Errorf("Atoi() = %v, want %v", got, tt.want)
}
})
}
}

37
util/hash_test.go Normal file
View File

@@ -0,0 +1,37 @@
package util
import "testing"
func TestAsSHA256(t *testing.T) {
type args struct {
o interface{}
}
tests := []struct {
name string
args args
want string
}{
{
name: "hash struct",
args: args{
o: struct {
Name string
Surname string
Age int
}{
Name: "John",
Surname: "Smith",
Age: 18,
},
},
want: "f20fe06d96e179073fc3eebac62d7a2edf3164f0c50524d82c0c6390013bbc4a",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := AsSHA256(tt.args.o); got != tt.want {
t.Errorf("AsSHA256() = %v, want %v", got, tt.want)
}
})
}
}

29
util/string_test.go Normal file
View File

@@ -0,0 +1,29 @@
package util
import "testing"
func TestStripNonAlphaNumeric(t *testing.T) {
type args struct {
value string
}
tests := []struct {
name string
args args
want string
}{
{
name: "remove trailing slash",
args: args{
value: "tt1234567/",
},
want: "tt1234567",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := StripNonAlphaNumeric(tt.args.value); got != tt.want {
t.Errorf("StripNonAlphaNumeric() = %v, want %v", got, tt.want)
}
})
}
}

97
util/url_test.go Normal file
View File

@@ -0,0 +1,97 @@
package util
import (
"net/url"
"testing"
)
func TestJoinURL(t *testing.T) {
type args struct {
base string
paths []string
}
tests := []struct {
name string
args args
want string
}{
{
name: "single path",
args: args{
base: "https://www.google.co.uk/",
paths: []string{"search"},
},
want: "https://www.google.co.uk/search",
},
{
name: "multiple path",
args: args{
base: "https://www.google.co.uk",
paths: []string{"search", "string"},
},
want: "https://www.google.co.uk/search/string",
},
{
name: "multiple path with slashes",
args: args{
base: "https://www.google.co.uk/",
paths: []string{"/search/", "/string/"},
},
want: "https://www.google.co.uk/search/string",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := JoinURL(tt.args.base, tt.args.paths...); got != tt.want {
t.Errorf("JoinURL() = %v, want %v", got, tt.want)
}
})
}
}
func TestURLWithQuery(t *testing.T) {
type args struct {
base string
q url.Values
}
tests := []struct {
name string
args args
want string
wantErr bool
}{
{
name: "url with values",
args: args{
base: JoinURL("https://api.trakt.tv", "search", "tvdb", "12345"),
q: url.Values{
"extended": []string{"full"},
"type": []string{"show"},
},
},
want: "https://api.trakt.tv/search/tvdb/12345?extended=full&type=show",
wantErr: false,
},
{
name: "url without values",
args: args{
base: JoinURL("https://api.trakt.tv", "search", "tvdb", "12345"),
q: nil,
},
want: "https://api.trakt.tv/search/tvdb/12345",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := URLWithQuery(tt.args.base, tt.args.q)
if (err != nil) != tt.wantErr {
t.Errorf("URLWithQuery() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("URLWithQuery() got = %v, want %v", got, tt.want)
}
})
}
}