Skip to content

Commit

Permalink
Merge pull request #14095 from baude/moreunittests
Browse files Browse the repository at this point in the history
Add more unit tests
  • Loading branch information
openshift-merge-robot authored May 4, 2022
2 parents ad93318 + 245151e commit 0e2a80a
Show file tree
Hide file tree
Showing 2 changed files with 238 additions and 0 deletions.
76 changes: 76 additions & 0 deletions pkg/domain/utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package utils

import (
"net/url"
"sort"
"testing"

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

func TestToLibpodFilters(t *testing.T) {
good := url.Values{}
good.Set("apple", "red")
good.Set("banana", "yellow")
good.Set("pear", "")
goodResult := []string{"apple=red", "banana=yellow", "pear="}
sort.Strings(goodResult)

empty := url.Values{}
type args struct {
f url.Values
}
tests := []struct {
name string
args args
wantFilters []string
}{
{
name: "GoodURLValue",
args: args{
f: good,
},
wantFilters: goodResult,
},
{
name: "Empty",
args: args{
f: empty,
},
wantFilters: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.ElementsMatchf(t, ToLibpodFilters(tt.args.f), tt.wantFilters, "ToLibpodFilters() = %v, want %v", ToLibpodFilters(tt.args.f), tt.wantFilters)
})
}
}

func TestToURLValues(t *testing.T) {
good := url.Values{}
good.Set("apple", "red")
good.Set("banana", "yellow")
good.Set("pear", "")
goodResult := []string{"apple=red", "banana=yellow", "pear="}

type args struct {
f []string
}
tests := []struct {
name string
args args
wantFilters url.Values
}{
{
name: "Good",
args: args{goodResult},
wantFilters: good,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.EqualValuesf(t, ToURLValues(tt.args.f), tt.wantFilters, "ToURLValues() = %v, want %v", ToURLValues(tt.args.f), tt.wantFilters)
})
}
}
162 changes: 162 additions & 0 deletions pkg/env/env_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package env

import (
"testing"

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

func TestSlice(t *testing.T) {
goodMap := make(map[string]string, 0)
goodMap["apple"] = "red"
goodMap["banana"] = "yellow"
goodMap["pear"] = ""
goodResult := []string{"apple=red", "banana=yellow", "pear"}
type args struct {
m map[string]string
}
tests := []struct {
name string
args args
want []string
}{
{
name: "Good",
args: args{
m: goodMap,
},
want: goodResult,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.ElementsMatchf(t, Slice(tt.args.m), tt.want, "Slice() = %v, want %v", Slice(tt.args.m), tt.want)
})
}
}

func TestJoin(t *testing.T) {
firstMap := make(map[string]string, 0)
firstMap["apple"] = "red"
secondMap := make(map[string]string, 0)
secondMap["banana"] = "yellow"
goodResult := make(map[string]string, 0)
goodResult["apple"] = "red"
goodResult["banana"] = "yellow"
overrideResult := make(map[string]string, 0)
overrideResult["apple"] = "green"
overrideResult["banana"] = "yellow"
overrideMap := make(map[string]string, 0)
overrideMap["banana"] = "yellow"
overrideMap["apple"] = "green"
type args struct {
base map[string]string
override map[string]string
}
tests := []struct {
name string
args args
want map[string]string
}{
{
name: "GoodJoin",
args: args{
base: firstMap,
override: secondMap,
},
want: goodResult,
},
{
name: "GoodOverride",
args: args{
base: firstMap,
override: overrideMap,
},
want: overrideResult,
},
{
name: "EmptyOverride",
args: args{
base: firstMap,
override: nil,
},
want: firstMap,
},
{
name: "EmptyBase",
args: args{
base: nil,
override: firstMap,
},
want: firstMap,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := Join(tt.args.base, tt.args.override)
assert.EqualValuesf(t, got, tt.want, "Join() = %v, want %v", got, tt.want)
})
}
}

func Test_parseEnv(t *testing.T) {
good := make(map[string]string)

type args struct {
env map[string]string
line string
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Good",
args: args{
env: good,
line: "apple=red",
},
wantErr: false,
},
{
name: "GoodNoValue",
args: args{
env: good,
line: "apple=",
},
wantErr: false,
},
{
name: "GoodNoKeyNoValue",
args: args{
env: good,
line: "=",
},
wantErr: true,
},
{
name: "BadNoKey",
args: args{
env: good,
line: "=foobar",
},
wantErr: true,
},
{
name: "BadOnlyDelim",
args: args{
env: good,
line: "=",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := parseEnv(tt.args.env, tt.args.line); (err != nil) != tt.wantErr {
t.Errorf("parseEnv() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

0 comments on commit 0e2a80a

Please sign in to comment.