types_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. tests := []struct {
  102. name string
  103. req string
  104. exp TriState
  105. }{
  106. {
  107. name: "Undefined",
  108. req: `{ }`,
  109. exp: TriStateUndefined,
  110. },
  111. {
  112. name: "True",
  113. req: `{ "use_mmap": true }`,
  114. exp: TriStateTrue,
  115. },
  116. {
  117. name: "False",
  118. req: `{ "use_mmap": false }`,
  119. exp: TriStateFalse,
  120. },
  121. }
  122. for _, test := range tests {
  123. t.Run(test.name, func(t *testing.T) {
  124. var oMap map[string]interface{}
  125. err := json.Unmarshal([]byte(test.req), &oMap)
  126. require.NoError(t, err)
  127. opts := DefaultOptions()
  128. err = opts.FromMap(oMap)
  129. require.NoError(t, err)
  130. assert.Equal(t, test.exp, opts.UseMMap)
  131. })
  132. }
  133. }
  134. func TestUseMmapFormatParams(t *testing.T) {
  135. tests := []struct {
  136. name string
  137. req map[string][]string
  138. exp TriState
  139. err error
  140. }{
  141. {
  142. name: "True",
  143. req: map[string][]string{
  144. "use_mmap": []string{"true"},
  145. },
  146. exp: TriStateTrue,
  147. err: nil,
  148. },
  149. {
  150. name: "False",
  151. req: map[string][]string{
  152. "use_mmap": []string{"false"},
  153. },
  154. exp: TriStateFalse,
  155. err: nil,
  156. },
  157. {
  158. name: "Numeric True",
  159. req: map[string][]string{
  160. "use_mmap": []string{"1"},
  161. },
  162. exp: TriStateTrue,
  163. err: nil,
  164. },
  165. {
  166. name: "Numeric False",
  167. req: map[string][]string{
  168. "use_mmap": []string{"0"},
  169. },
  170. exp: TriStateFalse,
  171. err: nil,
  172. },
  173. {
  174. name: "invalid string",
  175. req: map[string][]string{
  176. "use_mmap": []string{"foo"},
  177. },
  178. exp: TriStateUndefined,
  179. err: fmt.Errorf("invalid bool value [foo]"),
  180. },
  181. }
  182. for _, test := range tests {
  183. t.Run(test.name, func(t *testing.T) {
  184. resp, err := FormatParams(test.req)
  185. require.Equal(t, err, test.err)
  186. respVal, ok := resp["use_mmap"]
  187. if test.exp != TriStateUndefined {
  188. assert.True(t, ok, "resp: %v", resp)
  189. assert.Equal(t, test.exp, respVal)
  190. }
  191. })
  192. }
  193. }