123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- package api
- import (
- "encoding/json"
- "fmt"
- "math"
- "testing"
- "time"
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
- )
- func TestKeepAliveParsingFromJSON(t *testing.T) {
- tests := []struct {
- name string
- req string
- exp *Duration
- }{
- {
- name: "Positive Integer",
- req: `{ "keep_alive": 42 }`,
- exp: &Duration{42 * time.Second},
- },
- {
- name: "Positive Float",
- req: `{ "keep_alive": 42.5 }`,
- exp: &Duration{42 * time.Second},
- },
- {
- name: "Positive Integer String",
- req: `{ "keep_alive": "42m" }`,
- exp: &Duration{42 * time.Minute},
- },
- {
- name: "Negative Integer",
- req: `{ "keep_alive": -1 }`,
- exp: &Duration{math.MaxInt64},
- },
- {
- name: "Negative Float",
- req: `{ "keep_alive": -3.14 }`,
- exp: &Duration{math.MaxInt64},
- },
- {
- name: "Negative Integer String",
- req: `{ "keep_alive": "-1m" }`,
- exp: &Duration{math.MaxInt64},
- },
- }
- for _, test := range tests {
- t.Run(test.name, func(t *testing.T) {
- var dec ChatRequest
- err := json.Unmarshal([]byte(test.req), &dec)
- require.NoError(t, err)
- assert.Equal(t, test.exp, dec.KeepAlive)
- })
- }
- }
- func TestDurationMarshalUnmarshal(t *testing.T) {
- tests := []struct {
- name string
- input time.Duration
- expected time.Duration
- }{
- {
- "negative duration",
- time.Duration(-1),
- time.Duration(math.MaxInt64),
- },
- {
- "positive duration",
- 42 * time.Second,
- 42 * time.Second,
- },
- {
- "another positive duration",
- 42 * time.Minute,
- 42 * time.Minute,
- },
- {
- "zero duration",
- time.Duration(0),
- time.Duration(0),
- },
- {
- "max duration",
- time.Duration(math.MaxInt64),
- time.Duration(math.MaxInt64),
- },
- }
- for _, test := range tests {
- t.Run(test.name, func(t *testing.T) {
- b, err := json.Marshal(Duration{test.input})
- require.NoError(t, err)
- var d Duration
- err = json.Unmarshal(b, &d)
- require.NoError(t, err)
- assert.Equal(t, test.expected, d.Duration, "input %v, marshalled %v, got %v", test.input, string(b), d.Duration)
- })
- }
- }
- func TestUseMmapParsingFromJSON(t *testing.T) {
- tr := true
- fa := false
- tests := []struct {
- name string
- req string
- exp *bool
- }{
- {
- name: "Undefined",
- req: `{ }`,
- exp: nil,
- },
- {
- name: "True",
- req: `{ "use_mmap": true }`,
- exp: &tr,
- },
- {
- name: "False",
- req: `{ "use_mmap": false }`,
- exp: &fa,
- },
- }
- for _, test := range tests {
- t.Run(test.name, func(t *testing.T) {
- var oMap map[string]interface{}
- err := json.Unmarshal([]byte(test.req), &oMap)
- require.NoError(t, err)
- opts := DefaultOptions()
- err = opts.FromMap(oMap)
- require.NoError(t, err)
- assert.Equal(t, test.exp, opts.UseMMap)
- })
- }
- }
- func TestUseMmapFormatParams(t *testing.T) {
- tr := true
- fa := false
- tests := []struct {
- name string
- req map[string][]string
- exp *bool
- err error
- }{
- {
- name: "True",
- req: map[string][]string{
- "use_mmap": {"true"},
- },
- exp: &tr,
- err: nil,
- },
- {
- name: "False",
- req: map[string][]string{
- "use_mmap": {"false"},
- },
- exp: &fa,
- err: nil,
- },
- {
- name: "Numeric True",
- req: map[string][]string{
- "use_mmap": {"1"},
- },
- exp: &tr,
- err: nil,
- },
- {
- name: "Numeric False",
- req: map[string][]string{
- "use_mmap": {"0"},
- },
- exp: &fa,
- err: nil,
- },
- {
- name: "invalid string",
- req: map[string][]string{
- "use_mmap": {"foo"},
- },
- exp: nil,
- err: fmt.Errorf("invalid bool value [foo]"),
- },
- }
- for _, test := range tests {
- t.Run(test.name, func(t *testing.T) {
- resp, err := FormatParams(test.req)
- require.Equal(t, test.err, err)
- respVal, ok := resp["use_mmap"]
- if test.exp != nil {
- assert.True(t, ok, "resp: %v", resp)
- assert.Equal(t, *test.exp, *respVal.(*bool))
- }
- })
- }
- }
|