types_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package api
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "math"
  6. "testing"
  7. "time"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. )
  11. func TestKeepAliveParsingFromJSON(t *testing.T) {
  12. tests := []struct {
  13. name string
  14. req string
  15. exp *Duration
  16. }{
  17. {
  18. name: "Positive Integer",
  19. req: `{ "keep_alive": 42 }`,
  20. exp: &Duration{42 * time.Second},
  21. },
  22. {
  23. name: "Positive Float",
  24. req: `{ "keep_alive": 42.5 }`,
  25. exp: &Duration{42 * time.Second},
  26. },
  27. {
  28. name: "Positive Integer String",
  29. req: `{ "keep_alive": "42m" }`,
  30. exp: &Duration{42 * time.Minute},
  31. },
  32. {
  33. name: "Negative Integer",
  34. req: `{ "keep_alive": -1 }`,
  35. exp: &Duration{math.MaxInt64},
  36. },
  37. {
  38. name: "Negative Float",
  39. req: `{ "keep_alive": -3.14 }`,
  40. exp: &Duration{math.MaxInt64},
  41. },
  42. {
  43. name: "Negative Integer String",
  44. req: `{ "keep_alive": "-1m" }`,
  45. exp: &Duration{math.MaxInt64},
  46. },
  47. }
  48. for _, test := range tests {
  49. t.Run(test.name, func(t *testing.T) {
  50. var dec ChatRequest
  51. err := json.Unmarshal([]byte(test.req), &dec)
  52. require.NoError(t, err)
  53. assert.Equal(t, test.exp, dec.KeepAlive)
  54. })
  55. }
  56. }
  57. func TestDurationMarshalUnmarshal(t *testing.T) {
  58. tests := []struct {
  59. name string
  60. input time.Duration
  61. expected time.Duration
  62. }{
  63. {
  64. "negative duration",
  65. time.Duration(-1),
  66. time.Duration(math.MaxInt64),
  67. },
  68. {
  69. "positive duration",
  70. 42 * time.Second,
  71. 42 * time.Second,
  72. },
  73. {
  74. "another positive duration",
  75. 42 * time.Minute,
  76. 42 * time.Minute,
  77. },
  78. {
  79. "zero duration",
  80. time.Duration(0),
  81. time.Duration(0),
  82. },
  83. {
  84. "max duration",
  85. time.Duration(math.MaxInt64),
  86. time.Duration(math.MaxInt64),
  87. },
  88. }
  89. for _, test := range tests {
  90. t.Run(test.name, func(t *testing.T) {
  91. b, err := json.Marshal(Duration{test.input})
  92. require.NoError(t, err)
  93. var d Duration
  94. err = json.Unmarshal(b, &d)
  95. require.NoError(t, err)
  96. assert.Equal(t, test.expected, d.Duration, "input %v, marshalled %v, got %v", test.input, string(b), d.Duration)
  97. })
  98. }
  99. }
  100. func TestUseMmapParsingFromJSON(t *testing.T) {
  101. tr := true
  102. fa := false
  103. tests := []struct {
  104. name string
  105. req string
  106. exp *bool
  107. }{
  108. {
  109. name: "Undefined",
  110. req: `{ }`,
  111. exp: nil,
  112. },
  113. {
  114. name: "True",
  115. req: `{ "use_mmap": true }`,
  116. exp: &tr,
  117. },
  118. {
  119. name: "False",
  120. req: `{ "use_mmap": false }`,
  121. exp: &fa,
  122. },
  123. }
  124. for _, test := range tests {
  125. t.Run(test.name, func(t *testing.T) {
  126. var oMap map[string]interface{}
  127. err := json.Unmarshal([]byte(test.req), &oMap)
  128. require.NoError(t, err)
  129. opts := DefaultOptions()
  130. err = opts.FromMap(oMap)
  131. require.NoError(t, err)
  132. assert.Equal(t, test.exp, opts.UseMMap)
  133. })
  134. }
  135. }
  136. func TestUseMmapFormatParams(t *testing.T) {
  137. tr := true
  138. fa := false
  139. tests := []struct {
  140. name string
  141. req map[string][]string
  142. exp *bool
  143. err error
  144. }{
  145. {
  146. name: "True",
  147. req: map[string][]string{
  148. "use_mmap": {"true"},
  149. },
  150. exp: &tr,
  151. err: nil,
  152. },
  153. {
  154. name: "False",
  155. req: map[string][]string{
  156. "use_mmap": {"false"},
  157. },
  158. exp: &fa,
  159. err: nil,
  160. },
  161. {
  162. name: "Numeric True",
  163. req: map[string][]string{
  164. "use_mmap": {"1"},
  165. },
  166. exp: &tr,
  167. err: nil,
  168. },
  169. {
  170. name: "Numeric False",
  171. req: map[string][]string{
  172. "use_mmap": {"0"},
  173. },
  174. exp: &fa,
  175. err: nil,
  176. },
  177. {
  178. name: "invalid string",
  179. req: map[string][]string{
  180. "use_mmap": {"foo"},
  181. },
  182. exp: nil,
  183. err: fmt.Errorf("invalid bool value [foo]"),
  184. },
  185. }
  186. for _, test := range tests {
  187. t.Run(test.name, func(t *testing.T) {
  188. resp, err := FormatParams(test.req)
  189. require.Equal(t, test.err, err)
  190. respVal, ok := resp["use_mmap"]
  191. if test.exp != nil {
  192. assert.True(t, ok, "resp: %v", resp)
  193. assert.Equal(t, *test.exp, *respVal.(*bool))
  194. }
  195. })
  196. }
  197. }