Skip to content

Commit

Permalink
Test GetOK
Browse files Browse the repository at this point in the history
This change also removes GetOK in request_test.go. It is somehow duplicated.

Signed-off-by: Kazuyoshi Kato <katokazu@amazon.com>
  • Loading branch information
kzys committed Oct 25, 2021
1 parent 17a5a82 commit 5156d21
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
22 changes: 4 additions & 18 deletions request_test.go
Expand Up @@ -167,27 +167,13 @@ func TestJSONRequest(t *testing.T) {
func TestReadSingle(t *testing.T) {
values := url.Values(make(map[string][]string))
values.Add("something", "the thing")
assert.Equal(t, "the thing", ReadSingleValue(tv(values), "something"))
assert.Empty(t, ReadSingleValue(tv(values), "notthere"))
assert.Equal(t, "the thing", ReadSingleValue(Values(values), "something"))
assert.Empty(t, ReadSingleValue(Values(values), "notthere"))
}

func TestReadCollection(t *testing.T) {
values := url.Values(make(map[string][]string))
values.Add("something", "value1,value2")
assert.Equal(t, []string{"value1", "value2"}, ReadCollectionValue(tv(values), "something", "csv"))
assert.Empty(t, ReadCollectionValue(tv(values), "notthere", ""))
}

type tv map[string][]string

func (v tv) GetOK(key string) (value []string, hasKey bool, hasValue bool) {
value, hasKey = v[key]
if !hasKey {
return
}
if len(value) == 0 {
return
}
hasValue = true
return
assert.Equal(t, []string{"value1", "value2"}, ReadCollectionValue(Values(values), "something", "csv"))
assert.Empty(t, ReadCollectionValue(Values(values), "notthere", ""))
}
29 changes: 29 additions & 0 deletions values_test.go
@@ -0,0 +1,29 @@
package runtime

import (
"testing"

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

func TestGetOK(t *testing.T) {
m := make(map[string][]string)
m["key1"] = []string{"value1"}
m["key2"] = []string{}
values := Values(m)

v, hasKey, hasValue := values.GetOK("key1")
require.Equal(t, []string{"value1"}, v)
require.True(t, hasKey)
require.True(t, hasValue)

v, hasKey, hasValue = values.GetOK("key2")
require.Equal(t, []string{}, v)
require.True(t, hasKey)
require.False(t, hasValue)

v, hasKey, hasValue = values.GetOK("key3")
require.Nil(t, v)
require.False(t, hasKey)
require.False(t, hasValue)
}

0 comments on commit 5156d21

Please sign in to comment.