order_intent_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package sentio
  2. import (
  3. "encoding/json"
  4. "reflect"
  5. "testing"
  6. )
  7. func TestOrderIntent_MarshalJSON(t *testing.T) {
  8. type Obj struct {
  9. Intent OrderIntent `json:"intent"`
  10. }
  11. tests := []struct {
  12. obj Obj
  13. want []byte
  14. }{
  15. {
  16. obj: Obj{Intent: BuyToOpen},
  17. want: []byte(`{"intent":"buy_to_open"}`),
  18. },
  19. {
  20. obj: Obj{Intent: BuyToClose},
  21. want: []byte(`{"intent":"buy_to_close"}`),
  22. },
  23. {
  24. obj: Obj{Intent: SellToOpen},
  25. want: []byte(`{"intent":"sell_to_open"}`),
  26. },
  27. {
  28. obj: Obj{Intent: SellToClose},
  29. want: []byte(`{"intent":"sell_to_close"}`),
  30. },
  31. {
  32. obj: Obj{Intent: -1},
  33. want: []byte(`{"intent":"undefined"}`),
  34. },
  35. {
  36. obj: Obj{Intent: 100},
  37. want: []byte(`{"intent":"undefined"}`),
  38. },
  39. }
  40. for _, tt := range tests {
  41. t.Run("TestOrderIntent", func(t *testing.T) {
  42. if got, _ := json.Marshal(tt.obj); !reflect.DeepEqual(got, tt.want) {
  43. t.Errorf("MarshalJSON() got = %v, want %v", got, tt.want)
  44. }
  45. })
  46. }
  47. }
  48. func TestOrderIntent_UnmarshalJSON(t *testing.T) {
  49. type Obj struct {
  50. Intent OrderIntent `json:"intent"`
  51. }
  52. tests := []struct {
  53. obj Obj
  54. data []byte
  55. wantErr bool
  56. }{
  57. {
  58. obj: Obj{
  59. Intent: BuyToOpen,
  60. },
  61. data: []byte(`{"intent":"buy_to_open"}`),
  62. wantErr: false,
  63. },
  64. {
  65. obj: Obj{
  66. Intent: BuyToClose,
  67. },
  68. data: []byte(`{"intent":"buy_to_close"}`),
  69. wantErr: false,
  70. },
  71. {
  72. obj: Obj{
  73. Intent: SellToOpen,
  74. },
  75. data: []byte(`{"intent":"sell_to_open"}`),
  76. wantErr: false,
  77. },
  78. {
  79. obj: Obj{
  80. Intent: SellToClose,
  81. },
  82. data: []byte(`{"intent":"sell_to_close"}`),
  83. wantErr: false,
  84. },
  85. {
  86. obj: Obj{
  87. Intent: SellToClose,
  88. },
  89. data: []byte(`{"intent":3}`),
  90. wantErr: false,
  91. },
  92. {
  93. obj: Obj{
  94. Intent: BuyToOpen,
  95. },
  96. data: []byte(`{"intent":19}`),
  97. wantErr: true,
  98. },
  99. {
  100. obj: Obj{
  101. Intent: BuyToOpen,
  102. },
  103. data: []byte(`{"intent":null}`),
  104. wantErr: false,
  105. },
  106. }
  107. for _, tt := range tests {
  108. t.Run("TestOrderIntent", func(t *testing.T) {
  109. var v Obj
  110. if err := json.Unmarshal(tt.data, &v); (err != nil) != tt.wantErr {
  111. t.Errorf("UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
  112. return
  113. }
  114. if !reflect.DeepEqual(v, tt.obj) {
  115. t.Errorf("MarshalJSON() got = %v, want %v", v, tt.obj)
  116. }
  117. })
  118. }
  119. }