2024-03-13 20:29:40 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2024-06-21 19:27:19 +00:00
|
|
|
"fmt"
|
2024-03-13 20:29:40 +00:00
|
|
|
"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},
|
|
|
|
},
|
2024-05-06 22:59:18 +00:00
|
|
|
{
|
|
|
|
name: "Positive Float",
|
|
|
|
req: `{ "keep_alive": 42.5 }`,
|
|
|
|
exp: &Duration{42 * time.Second},
|
|
|
|
},
|
2024-03-13 20:29:40 +00:00
|
|
|
{
|
|
|
|
name: "Positive Integer String",
|
|
|
|
req: `{ "keep_alive": "42m" }`,
|
|
|
|
exp: &Duration{42 * time.Minute},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Negative Integer",
|
|
|
|
req: `{ "keep_alive": -1 }`,
|
|
|
|
exp: &Duration{math.MaxInt64},
|
|
|
|
},
|
2024-05-06 22:59:18 +00:00
|
|
|
{
|
|
|
|
name: "Negative Float",
|
|
|
|
req: `{ "keep_alive": -3.14 }`,
|
|
|
|
exp: &Duration{math.MaxInt64},
|
|
|
|
},
|
2024-03-13 20:29:40 +00:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-05-06 22:59:18 +00:00
|
|
|
|
|
|
|
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",
|
2024-05-22 05:21:04 +00:00
|
|
|
42 * time.Second,
|
|
|
|
42 * time.Second,
|
2024-05-06 22:59:18 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"another positive duration",
|
2024-05-22 05:21:04 +00:00
|
|
|
42 * time.Minute,
|
|
|
|
42 * time.Minute,
|
2024-05-06 22:59:18 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-06-17 19:14:42 +00:00
|
|
|
|
|
|
|
func TestUseMmapParsingFromJSON(t *testing.T) {
|
2024-06-28 16:57:10 +00:00
|
|
|
tr := true
|
|
|
|
fa := false
|
2024-06-17 19:14:42 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
req string
|
2024-06-28 16:57:10 +00:00
|
|
|
exp *bool
|
2024-06-17 19:14:42 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Undefined",
|
|
|
|
req: `{ }`,
|
2024-06-28 16:57:10 +00:00
|
|
|
exp: nil,
|
2024-06-17 19:14:42 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "True",
|
|
|
|
req: `{ "use_mmap": true }`,
|
2024-06-28 16:57:10 +00:00
|
|
|
exp: &tr,
|
2024-06-17 19:14:42 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "False",
|
|
|
|
req: `{ "use_mmap": false }`,
|
2024-06-28 16:57:10 +00:00
|
|
|
exp: &fa,
|
2024-06-17 19:14:42 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-06-21 19:27:19 +00:00
|
|
|
|
|
|
|
func TestUseMmapFormatParams(t *testing.T) {
|
2024-06-28 16:57:10 +00:00
|
|
|
tr := true
|
|
|
|
fa := false
|
2024-06-21 19:27:19 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
req map[string][]string
|
2024-06-28 16:57:10 +00:00
|
|
|
exp *bool
|
2024-06-21 19:27:19 +00:00
|
|
|
err error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "True",
|
|
|
|
req: map[string][]string{
|
2024-06-28 16:57:10 +00:00
|
|
|
"use_mmap": {"true"},
|
2024-06-21 19:27:19 +00:00
|
|
|
},
|
2024-06-28 16:57:10 +00:00
|
|
|
exp: &tr,
|
2024-06-21 19:27:19 +00:00
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "False",
|
|
|
|
req: map[string][]string{
|
2024-06-28 16:57:10 +00:00
|
|
|
"use_mmap": {"false"},
|
2024-06-21 19:27:19 +00:00
|
|
|
},
|
2024-06-28 16:57:10 +00:00
|
|
|
exp: &fa,
|
2024-06-21 19:27:19 +00:00
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Numeric True",
|
|
|
|
req: map[string][]string{
|
2024-06-28 16:57:10 +00:00
|
|
|
"use_mmap": {"1"},
|
2024-06-21 19:27:19 +00:00
|
|
|
},
|
2024-06-28 16:57:10 +00:00
|
|
|
exp: &tr,
|
2024-06-21 19:27:19 +00:00
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Numeric False",
|
|
|
|
req: map[string][]string{
|
2024-06-28 16:57:10 +00:00
|
|
|
"use_mmap": {"0"},
|
2024-06-21 19:27:19 +00:00
|
|
|
},
|
2024-06-28 16:57:10 +00:00
|
|
|
exp: &fa,
|
2024-06-21 19:27:19 +00:00
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid string",
|
|
|
|
req: map[string][]string{
|
2024-06-28 16:57:10 +00:00
|
|
|
"use_mmap": {"foo"},
|
2024-06-21 19:27:19 +00:00
|
|
|
},
|
2024-06-28 16:57:10 +00:00
|
|
|
exp: nil,
|
2024-06-21 19:27:19 +00:00
|
|
|
err: fmt.Errorf("invalid bool value [foo]"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
resp, err := FormatParams(test.req)
|
2024-06-28 16:57:10 +00:00
|
|
|
require.Equal(t, test.err, err)
|
2024-06-21 19:27:19 +00:00
|
|
|
respVal, ok := resp["use_mmap"]
|
2024-06-28 16:57:10 +00:00
|
|
|
if test.exp != nil {
|
2024-06-21 19:27:19 +00:00
|
|
|
assert.True(t, ok, "resp: %v", resp)
|
2024-06-28 16:57:10 +00:00
|
|
|
assert.Equal(t, *test.exp, *respVal.(*bool))
|
2024-06-21 19:27:19 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|