template_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // Copyright 2023 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package transform
  15. import (
  16. "reflect"
  17. "testing"
  18. )
  19. func Test_SelectMap(t *testing.T) {
  20. type args struct {
  21. input interface{}
  22. fields []string
  23. }
  24. tests := []struct {
  25. name string
  26. args args
  27. want interface{}
  28. }{
  29. {
  30. name: "test1",
  31. args: args{
  32. input: map[string]interface{}{
  33. "a": 1,
  34. "b": 2,
  35. "c": 3,
  36. },
  37. fields: []string{"a", "b"},
  38. },
  39. want: map[string]interface{}{
  40. "a": 1,
  41. "b": 2,
  42. },
  43. },
  44. {
  45. name: "test2",
  46. args: args{
  47. input: []map[string]interface{}{
  48. {
  49. "a": 1,
  50. "b": 2,
  51. "c": 3,
  52. },
  53. },
  54. fields: []string{"a", "b"},
  55. },
  56. want: []map[string]interface{}{
  57. {
  58. "a": 1,
  59. "b": 2,
  60. },
  61. },
  62. },
  63. {
  64. name: "test3",
  65. args: args{
  66. input: []interface{}{
  67. map[string]interface{}{
  68. "a": 1,
  69. "b": 2,
  70. "c": 3,
  71. },
  72. },
  73. fields: []string{"a", "b"},
  74. },
  75. want: []map[string]interface{}{
  76. {
  77. "a": 1,
  78. "b": 2,
  79. },
  80. },
  81. },
  82. {
  83. name: "test4",
  84. args: args{
  85. input: []map[string]interface{}{
  86. {
  87. "a": 1,
  88. "b": 2,
  89. "c": 3,
  90. },
  91. },
  92. fields: nil,
  93. },
  94. want: []map[string]interface{}{
  95. {
  96. "a": 1,
  97. "b": 2,
  98. "c": 3,
  99. },
  100. },
  101. },
  102. {
  103. name: "test5",
  104. args: args{
  105. input: []byte(`{"a": 1, "b": 2, "c": 3}`),
  106. fields: nil,
  107. },
  108. want: []byte(`{"a": 1, "b": 2, "c": 3}`),
  109. },
  110. }
  111. for _, tt := range tests {
  112. t.Run(tt.name, func(t *testing.T) {
  113. if got, _ := selectMap(tt.args.input, tt.args.fields); !reflect.DeepEqual(got, tt.want) {
  114. t.Errorf("selectMap() = %v, want %v", got, tt.want)
  115. }
  116. })
  117. }
  118. }
  119. func TestTransItem(t *testing.T) {
  120. type args struct {
  121. input interface{}
  122. dataField string
  123. fields []string
  124. }
  125. tests := []struct {
  126. name string
  127. args args
  128. want interface{}
  129. wantErr bool
  130. }{
  131. {
  132. name: "test1",
  133. args: args{
  134. input: map[string]interface{}{
  135. "device": map[string]interface{}{
  136. "device_id": 1,
  137. "device_temperature": 31.2,
  138. "device_humidity": 80,
  139. },
  140. "ts": 1625040000,
  141. },
  142. dataField: "device",
  143. fields: []string{"device_temperature", "device_humidity"},
  144. },
  145. want: map[string]interface{}{
  146. "device_temperature": 31.2,
  147. "device_humidity": 80,
  148. },
  149. wantErr: false,
  150. },
  151. {
  152. name: "test2",
  153. args: args{
  154. input: []map[string]interface{}{
  155. {
  156. "device": map[string]interface{}{
  157. "device_id": 1,
  158. "device_temperature": 31.2,
  159. "device_humidity": 80,
  160. },
  161. "ts": 1625040000,
  162. },
  163. },
  164. dataField: "device",
  165. fields: []string{"device_temperature", "device_humidity"},
  166. },
  167. want: map[string]interface{}{
  168. "device_temperature": 31.2,
  169. "device_humidity": 80,
  170. },
  171. wantErr: false,
  172. },
  173. {
  174. name: "test3",
  175. args: args{
  176. input: map[string]interface{}{
  177. "telemetry": []map[string]interface{}{
  178. {
  179. "temperature": 32.32,
  180. "humidity": 80.8,
  181. "f3": "f3tagValue",
  182. "f4": "f4tagValue",
  183. "ts": 1388082430,
  184. },
  185. {
  186. "temperature": 34.32,
  187. "humidity": 81.8,
  188. "f3": "f3tagValue",
  189. "f4": "f4tagValue",
  190. "ts": 1388082440,
  191. },
  192. },
  193. "device": map[string]interface{}{
  194. "device_id": 1,
  195. "device_temperature": 31.2,
  196. "device_humidity": 80,
  197. },
  198. },
  199. dataField: "telemetry",
  200. fields: []string{"temperature", "humidity"},
  201. },
  202. want: []map[string]interface{}{
  203. {
  204. "temperature": 32.32,
  205. "humidity": 80.8,
  206. },
  207. {
  208. "temperature": 34.32,
  209. "humidity": 81.8,
  210. },
  211. },
  212. wantErr: false,
  213. },
  214. {
  215. name: "test4",
  216. args: args{
  217. input: []interface{}{
  218. map[string]interface{}{
  219. "telemetry": []map[string]interface{}{
  220. {
  221. "temperature": 32.32,
  222. "humidity": 80.8,
  223. "ts": 1388082430,
  224. },
  225. {
  226. "temperature": 34.32,
  227. "humidity": 81.8,
  228. "ts": 1388082440,
  229. },
  230. },
  231. },
  232. },
  233. dataField: "telemetry",
  234. fields: []string{"temperature", "humidity"},
  235. },
  236. want: []map[string]interface{}{
  237. {
  238. "temperature": 32.32,
  239. "humidity": 80.8,
  240. },
  241. {
  242. "temperature": 34.32,
  243. "humidity": 81.8,
  244. },
  245. },
  246. wantErr: false,
  247. },
  248. }
  249. for _, tt := range tests {
  250. t.Run(tt.name, func(t *testing.T) {
  251. got, _, err := TransItem(tt.args.input, tt.args.dataField, tt.args.fields)
  252. if (err != nil) != tt.wantErr {
  253. t.Errorf("TransItem() error = %v, wantErr %v", err, tt.wantErr)
  254. return
  255. }
  256. if !reflect.DeepEqual(got, tt.want) {
  257. t.Errorf("TransItem() got = %v, want %v", got, tt.want)
  258. }
  259. })
  260. }
  261. }