types_test.go 945 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package api
  2. import (
  3. "encoding/json"
  4. "math"
  5. "testing"
  6. "time"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. )
  10. func TestKeepAliveParsingFromJSON(t *testing.T) {
  11. tests := []struct {
  12. name string
  13. req string
  14. exp *Duration
  15. }{
  16. {
  17. name: "Positive Integer",
  18. req: `{ "keep_alive": 42 }`,
  19. exp: &Duration{42 * time.Second},
  20. },
  21. {
  22. name: "Positive Integer String",
  23. req: `{ "keep_alive": "42m" }`,
  24. exp: &Duration{42 * time.Minute},
  25. },
  26. {
  27. name: "Negative Integer",
  28. req: `{ "keep_alive": -1 }`,
  29. exp: &Duration{math.MaxInt64},
  30. },
  31. {
  32. name: "Negative Integer String",
  33. req: `{ "keep_alive": "-1m" }`,
  34. exp: &Duration{math.MaxInt64},
  35. },
  36. }
  37. for _, test := range tests {
  38. t.Run(test.name, func(t *testing.T) {
  39. var dec ChatRequest
  40. err := json.Unmarshal([]byte(test.req), &dec)
  41. require.NoError(t, err)
  42. assert.Equal(t, test.exp, dec.KeepAlive)
  43. })
  44. }
  45. }