schema_test.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package services
  2. import (
  3. "github.com/emqx/kuiper/common"
  4. "reflect"
  5. "testing"
  6. )
  7. var descriptors []descriptor
  8. func init() {
  9. schemas := []*schemaInfo{
  10. {
  11. SchemaType: PROTOBUFF,
  12. SchemaFile: "hw.proto",
  13. },
  14. }
  15. descriptors = make([]descriptor, len(schemas))
  16. for i, sch := range schemas {
  17. d, err := parse(sch.SchemaType, sch.SchemaFile)
  18. if err != nil {
  19. panic(err)
  20. }
  21. descriptors[i] = d
  22. }
  23. }
  24. func TestConvertParams(t *testing.T) {
  25. tests := []struct {
  26. method string
  27. params []interface{}
  28. iresult []interface{}
  29. jresult []byte
  30. err string
  31. }{
  32. { //0
  33. method: "SayHello",
  34. params: []interface{}{
  35. "world",
  36. },
  37. iresult: []interface{}{
  38. "world",
  39. },
  40. jresult: []byte(`{"name":"world"}`),
  41. },
  42. { //1
  43. method: "SayHello",
  44. params: []interface{}{
  45. map[string]interface{}{
  46. "name": "world",
  47. },
  48. },
  49. iresult: []interface{}{
  50. "world",
  51. },
  52. jresult: []byte(`{"name":"world"}`),
  53. },
  54. { //2
  55. method: "SayHello",
  56. params: []interface{}{
  57. map[string]interface{}{
  58. "arbitrary": "world",
  59. },
  60. },
  61. err: "invalid type for string type field 'name': cannot convert map[string]interface {}(map[arbitrary:world]) to string",
  62. },
  63. { //3
  64. method: "Compute",
  65. params: []interface{}{
  66. "rid", "uuid", "outlet", "path", []byte("data"), "extra",
  67. },
  68. iresult: []interface{}{
  69. "rid", "uuid", "outlet", "path", []byte("data"), "extra",
  70. },
  71. jresult: []byte(`{"rid":"rid","uuid":"uuid","outlet":"outlet","path":"path","data":"ZGF0YQ==","extra":"extra"}`),
  72. },
  73. { //4
  74. method: "get_feature",
  75. params: []interface{}{
  76. []byte("golang"),
  77. },
  78. iresult: []interface{}{
  79. []byte("golang"),
  80. },
  81. jresult: []byte(`"Z29sYW5n"`),
  82. },
  83. //{ //5
  84. // method: "get_similarity",
  85. // params: []interface{}{
  86. // []float64{0.031646, -0.800592, -1.101858, -0.354359, 0.656587},
  87. // []float64{0.354359, 0.656587, -0.327047, 0.198284, -2.142494, 0.760160, 1.680131},
  88. // },
  89. // iresult: []interface{}{
  90. // []float32{0.031646, -0.800592, -1.101858, -0.354359, 0.656587},
  91. // []float32{0.354359, 0.656587, -0.327047, 0.198284, -2.142494, 0.760160, 1.680131},
  92. // },
  93. // jresult: []byte(`{"featureA":[0.031646,-0.800592,-1.101858,-0.354359,0.656587],"featureB":[0.354359,0.656587,-0.327047,0.198284,-2.142494,0.76016,1.680131]}`),
  94. //},
  95. }
  96. for i, descriptor := range descriptors {
  97. for j, tt := range tests {
  98. r, err := descriptor.(interfaceDescriptor).ConvertParams(tt.method, tt.params)
  99. if !reflect.DeepEqual(tt.err, common.Errstring(err)) {
  100. t.Errorf("%d.%d : interface error mismatch:\n exp=%s\n got=%s\n\n", i, j, tt.err, err)
  101. } else if tt.err == "" && !reflect.DeepEqual(tt.iresult, r) {
  102. t.Errorf("%d.%d \n\ninterface result mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, j, tt.iresult, r)
  103. }
  104. rj, err := descriptor.(jsonDescriptor).ConvertParamsToJson(tt.method, tt.params)
  105. if !reflect.DeepEqual(tt.err, common.Errstring(err)) {
  106. t.Errorf("%d.%d : json error mismatch:\n exp=%s\n got=%s\n\n", i, j, tt.err, err)
  107. } else if tt.err == "" && !reflect.DeepEqual(tt.jresult, rj) {
  108. t.Errorf("%d.%d \n\njson result mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, j, tt.jresult, rj)
  109. }
  110. }
  111. }
  112. }
  113. func TestConvertReturns(t *testing.T) {
  114. tests := []struct {
  115. method string
  116. ireturn interface{}
  117. iresult interface{}
  118. ierr string
  119. jreturn []byte
  120. jresult interface{}
  121. jerr string
  122. }{
  123. { // 0
  124. method: "SayHello",
  125. ireturn: map[string]interface{}{"message": "world"},
  126. iresult: map[string]interface{}{"message": "world"},
  127. jreturn: []byte(`{"message":"world"}`),
  128. jresult: map[string]interface{}{"message": "world"},
  129. },
  130. { // 1
  131. method: "SayHello",
  132. ireturn: map[string]interface{}{"message": 65},
  133. ierr: "invalid type of return value for 'message': cannot convert int(65) to string",
  134. jreturn: []byte(`{"message":65}`),
  135. jerr: "invalid type of return value for 'message': cannot convert float64(65) to string",
  136. },
  137. //{
  138. // method: "SayHello",
  139. // ireturn: map[string]interface{}{
  140. // "mess":"world",
  141. // },
  142. // jreturn: []byte(`{"mess":"world"}`),
  143. //err: "invalid type for field 'message', expect string but got int)",
  144. //},
  145. { // 2
  146. method: "Compute",
  147. ireturn: map[string]interface{}{
  148. "code": int64(200),
  149. "msg": "success",
  150. },
  151. iresult: map[string]interface{}{
  152. "code": int64(200),
  153. "msg": "success",
  154. },
  155. jreturn: []byte(`{"code":200,"msg":"success"}`),
  156. jresult: map[string]interface{}{
  157. "code": int64(200),
  158. "msg": "success",
  159. },
  160. },
  161. {
  162. method: "get_feature",
  163. ireturn: map[string]interface{}{"feature": []interface{}{ //TODO check msgpack result
  164. map[string]interface{}{
  165. "box": map[string]interface{}{"x": int32(55), "y": int32(65), "w": int32(33), "h": int32(69)},
  166. "features": []float32{0.031646, -0.800592, -1.101858, -0.354359, 0.656587},
  167. },
  168. map[string]interface{}{
  169. "box": map[string]interface{}{"x": int32(987), "y": int32(66), "w": int32(66), "h": int32(55)},
  170. "features": []float32{0.354359, 0.656587, -0.327047, 0.198284, -2.142494, 0.760160, 1.680131},
  171. },
  172. }},
  173. iresult: map[string]interface{}{
  174. "feature": []map[string]interface{}{
  175. {
  176. "box": map[string]interface{}{"x": int64(55), "y": int64(65), "w": int64(33), "h": int64(69)},
  177. "features": []float64{float64(float32(0.031646)), float64(float32(-0.800592)), float64(float32(-1.101858)), float64(float32(-0.354359)), float64(float32(0.656587))},
  178. },
  179. {
  180. "box": map[string]interface{}{"x": int64(987), "y": int64(66), "w": int64(66), "h": int64(55)},
  181. "features": []float64{float64(float32(0.354359)), float64(float32(0.656587)), float64(float32(-0.327047)), float64(float32(0.198284)), float64(float32(-2.142494)), float64(float32(0.760160)), float64(float32(1.680131))},
  182. },
  183. },
  184. },
  185. jreturn: []byte(`{"feature":[{"box":{"x":55,"y":65,"w":33,"h":69},"features":[0.031646, -0.800592, -1.101858, -0.354359, 0.656587]},{"box":{"x":987,"y":66,"w":66,"h":55},"features":[0.354359, 0.656587, -0.327047, 0.198284, -2.142494, 0.760160, 1.680131]}]}`),
  186. jresult: map[string]interface{}{
  187. "feature": []map[string]interface{}{
  188. {
  189. "box": map[string]interface{}{"x": int64(55), "y": int64(65), "w": int64(33), "h": int64(69)},
  190. "features": []float64{0.031646, -0.800592, -1.101858, -0.354359, 0.656587},
  191. },
  192. {
  193. "box": map[string]interface{}{"x": int64(987), "y": int64(66), "w": int64(66), "h": int64(55)},
  194. "features": []float64{0.354359, 0.656587, -0.327047, 0.198284, -2.142494, 0.760160, 1.680131},
  195. },
  196. },
  197. },
  198. },
  199. //{
  200. // method: "get_similarity",
  201. // ireturn: float32(0.987),
  202. // iresult: float64(float32(0.987)),
  203. // jreturn: []byte(`{"response":0.987}`),
  204. // jresult: map[string]interface{}{
  205. // "response": 0.987,
  206. // },
  207. //},
  208. }
  209. for i, descriptor := range descriptors {
  210. for j, tt := range tests[1:2] {
  211. r, err := descriptor.(interfaceDescriptor).ConvertReturn(tt.method, tt.ireturn)
  212. if !reflect.DeepEqual(tt.ierr, common.Errstring(err)) {
  213. t.Errorf("%d.%d : interface error mismatch:\n exp=%s\n got=%s\n\n", i, j, tt.ierr, err)
  214. } else if tt.ierr == "" && !reflect.DeepEqual(tt.iresult, r) {
  215. t.Errorf("%d.%d \n\ninterface result mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, j, tt.iresult, r)
  216. }
  217. rj, err := descriptor.(jsonDescriptor).ConvertReturnJson(tt.method, tt.jreturn)
  218. if !reflect.DeepEqual(tt.jerr, common.Errstring(err)) {
  219. t.Errorf("%d.%d : json error mismatch:\n exp=%s\n got=%s\n\n", i, j, tt.jerr, err)
  220. } else if tt.jerr == "" && !reflect.DeepEqual(tt.jresult, rj) {
  221. t.Errorf("%d.%d \n\njson result mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, j, tt.jresult, rj)
  222. }
  223. }
  224. }
  225. }