schema_test.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. { // 6
  96. method: "RestEncodedJson",
  97. params: []interface{}{
  98. []byte("{\"name\":\"encoded json\",\"size\":1}"),
  99. },
  100. iresult: []interface{}{
  101. "{\"name\":\"encoded json\",\"size\":1}",
  102. },
  103. jresult: []byte("{\"name\":\"encoded json\",\"size\":1}"),
  104. },
  105. }
  106. for i, descriptor := range descriptors {
  107. for j, tt := range tests {
  108. r, err := descriptor.(interfaceDescriptor).ConvertParams(tt.method, tt.params)
  109. if !reflect.DeepEqual(tt.err, common.Errstring(err)) {
  110. t.Errorf("%d.%d : interface error mismatch:\n exp=%s\n got=%s\n\n", i, j, tt.err, err)
  111. } else if tt.err == "" && !reflect.DeepEqual(tt.iresult, r) {
  112. t.Errorf("%d.%d \n\ninterface result mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, j, tt.iresult, r)
  113. }
  114. rj, err := descriptor.(jsonDescriptor).ConvertParamsToJson(tt.method, tt.params)
  115. if !reflect.DeepEqual(tt.err, common.Errstring(err)) {
  116. t.Errorf("%d.%d : json error mismatch:\n exp=%s\n got=%s\n\n", i, j, tt.err, err)
  117. } else if tt.err == "" && !reflect.DeepEqual(tt.jresult, rj) {
  118. t.Errorf("%d.%d \n\njson result mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, j, tt.jresult, rj)
  119. }
  120. }
  121. }
  122. }
  123. func TestConvertReturns(t *testing.T) {
  124. tests := []struct {
  125. method string
  126. ireturn interface{}
  127. iresult interface{}
  128. ierr string
  129. jreturn []byte
  130. jresult interface{}
  131. jerr string
  132. }{
  133. { // 0
  134. method: "SayHello",
  135. ireturn: map[string]interface{}{"message": "world"},
  136. iresult: map[string]interface{}{"message": "world"},
  137. jreturn: []byte(`{"message":"world"}`),
  138. jresult: map[string]interface{}{"message": "world"},
  139. },
  140. { // 1
  141. method: "SayHello",
  142. ireturn: map[string]interface{}{"message": 65},
  143. ierr: "invalid type of return value for 'message': cannot convert int(65) to string",
  144. jreturn: []byte(`{"message":65}`),
  145. jerr: "invalid type of return value for 'message': cannot convert float64(65) to string",
  146. },
  147. //{
  148. // method: "SayHello",
  149. // ireturn: map[string]interface{}{
  150. // "mess":"world",
  151. // },
  152. // jreturn: []byte(`{"mess":"world"}`),
  153. //err: "invalid type for field 'message', expect string but got int)",
  154. //},
  155. { // 2
  156. method: "Compute",
  157. ireturn: map[string]interface{}{
  158. "code": int64(200),
  159. "msg": "success",
  160. },
  161. iresult: map[string]interface{}{
  162. "code": int64(200),
  163. "msg": "success",
  164. },
  165. jreturn: []byte(`{"code":200,"msg":"success"}`),
  166. jresult: map[string]interface{}{
  167. "code": int64(200),
  168. "msg": "success",
  169. },
  170. },
  171. {
  172. method: "get_feature",
  173. ireturn: map[string]interface{}{"feature": []interface{}{ //TODO check msgpack result
  174. map[string]interface{}{
  175. "box": map[string]interface{}{"x": int32(55), "y": int32(65), "w": int32(33), "h": int32(69)},
  176. "features": []float32{0.031646, -0.800592, -1.101858, -0.354359, 0.656587},
  177. },
  178. map[string]interface{}{
  179. "box": map[string]interface{}{"x": int32(987), "y": int32(66), "w": int32(66), "h": int32(55)},
  180. "features": []float32{0.354359, 0.656587, -0.327047, 0.198284, -2.142494, 0.760160, 1.680131},
  181. },
  182. }},
  183. iresult: map[string]interface{}{
  184. "feature": []map[string]interface{}{
  185. {
  186. "box": map[string]interface{}{"x": int64(55), "y": int64(65), "w": int64(33), "h": int64(69)},
  187. "features": []float64{float64(float32(0.031646)), float64(float32(-0.800592)), float64(float32(-1.101858)), float64(float32(-0.354359)), float64(float32(0.656587))},
  188. },
  189. {
  190. "box": map[string]interface{}{"x": int64(987), "y": int64(66), "w": int64(66), "h": int64(55)},
  191. "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))},
  192. },
  193. },
  194. },
  195. 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]}]}`),
  196. jresult: map[string]interface{}{
  197. "feature": []map[string]interface{}{
  198. {
  199. "box": map[string]interface{}{"x": int64(55), "y": int64(65), "w": int64(33), "h": int64(69)},
  200. "features": []float64{0.031646, -0.800592, -1.101858, -0.354359, 0.656587},
  201. },
  202. {
  203. "box": map[string]interface{}{"x": int64(987), "y": int64(66), "w": int64(66), "h": int64(55)},
  204. "features": []float64{0.354359, 0.656587, -0.327047, 0.198284, -2.142494, 0.760160, 1.680131},
  205. },
  206. },
  207. },
  208. },
  209. //{
  210. // method: "get_similarity",
  211. // ireturn: float32(0.987),
  212. // iresult: float64(float32(0.987)),
  213. // jreturn: []byte(`{"response":0.987}`),
  214. // jresult: map[string]interface{}{
  215. // "response": 0.987,
  216. // },
  217. //},
  218. }
  219. for i, descriptor := range descriptors {
  220. for j, tt := range tests {
  221. r, err := descriptor.(interfaceDescriptor).ConvertReturn(tt.method, tt.ireturn)
  222. if !reflect.DeepEqual(tt.ierr, common.Errstring(err)) {
  223. t.Errorf("%d.%d : interface error mismatch:\n exp=%s\n got=%s\n\n", i, j, tt.ierr, err)
  224. } else if tt.ierr == "" && !reflect.DeepEqual(tt.iresult, r) {
  225. t.Errorf("%d.%d \n\ninterface result mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, j, tt.iresult, r)
  226. }
  227. rj, err := descriptor.(jsonDescriptor).ConvertReturnJson(tt.method, tt.jreturn)
  228. if !reflect.DeepEqual(tt.jerr, common.Errstring(err)) {
  229. t.Errorf("%d.%d : json error mismatch:\n exp=%s\n got=%s\n\n", i, j, tt.jerr, err)
  230. } else if tt.jerr == "" && !reflect.DeepEqual(tt.jresult, rj) {
  231. t.Errorf("%d.%d \n\njson result mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, j, tt.jresult, rj)
  232. }
  233. }
  234. }
  235. }