123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- // Copyright 2023 EMQ Technologies Co., Ltd.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package transform
- import (
- "reflect"
- "testing"
- )
- func Test_SelectMap(t *testing.T) {
- type args struct {
- input interface{}
- fields []string
- }
- tests := []struct {
- name string
- args args
- want interface{}
- }{
- {
- name: "test1",
- args: args{
- input: map[string]interface{}{
- "a": 1,
- "b": 2,
- "c": 3,
- },
- fields: []string{"a", "b"},
- },
- want: map[string]interface{}{
- "a": 1,
- "b": 2,
- },
- },
- {
- name: "test2",
- args: args{
- input: []map[string]interface{}{
- {
- "a": 1,
- "b": 2,
- "c": 3,
- },
- },
- fields: []string{"a", "b"},
- },
- want: []map[string]interface{}{
- {
- "a": 1,
- "b": 2,
- },
- },
- },
- {
- name: "test3",
- args: args{
- input: []interface{}{
- map[string]interface{}{
- "a": 1,
- "b": 2,
- "c": 3,
- },
- },
- fields: []string{"a", "b"},
- },
- want: []map[string]interface{}{
- {
- "a": 1,
- "b": 2,
- },
- },
- },
- {
- name: "test4",
- args: args{
- input: []map[string]interface{}{
- {
- "a": 1,
- "b": 2,
- "c": 3,
- },
- },
- fields: nil,
- },
- want: []map[string]interface{}{
- {
- "a": 1,
- "b": 2,
- "c": 3,
- },
- },
- },
- {
- name: "test5",
- args: args{
- input: []byte(`{"a": 1, "b": 2, "c": 3}`),
- fields: nil,
- },
- want: []byte(`{"a": 1, "b": 2, "c": 3}`),
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- if got, _ := selectMap(tt.args.input, tt.args.fields); !reflect.DeepEqual(got, tt.want) {
- t.Errorf("selectMap() = %v, want %v", got, tt.want)
- }
- })
- }
- }
- func TestTransItem(t *testing.T) {
- type args struct {
- input interface{}
- dataField string
- fields []string
- }
- tests := []struct {
- name string
- args args
- want interface{}
- wantErr bool
- }{
- {
- name: "test1",
- args: args{
- input: map[string]interface{}{
- "device": map[string]interface{}{
- "device_id": 1,
- "device_temperature": 31.2,
- "device_humidity": 80,
- },
- "ts": 1625040000,
- },
- dataField: "device",
- fields: []string{"device_temperature", "device_humidity"},
- },
- want: map[string]interface{}{
- "device_temperature": 31.2,
- "device_humidity": 80,
- },
- wantErr: false,
- },
- {
- name: "test2",
- args: args{
- input: []map[string]interface{}{
- {
- "device": map[string]interface{}{
- "device_id": 1,
- "device_temperature": 31.2,
- "device_humidity": 80,
- },
- "ts": 1625040000,
- },
- },
- dataField: "device",
- fields: []string{"device_temperature", "device_humidity"},
- },
- want: map[string]interface{}{
- "device_temperature": 31.2,
- "device_humidity": 80,
- },
- wantErr: false,
- },
- {
- name: "test3",
- args: args{
- input: map[string]interface{}{
- "telemetry": []map[string]interface{}{
- {
- "temperature": 32.32,
- "humidity": 80.8,
- "f3": "f3tagValue",
- "f4": "f4tagValue",
- "ts": 1388082430,
- },
- {
- "temperature": 34.32,
- "humidity": 81.8,
- "f3": "f3tagValue",
- "f4": "f4tagValue",
- "ts": 1388082440,
- },
- },
- "device": map[string]interface{}{
- "device_id": 1,
- "device_temperature": 31.2,
- "device_humidity": 80,
- },
- },
- dataField: "telemetry",
- fields: []string{"temperature", "humidity"},
- },
- want: []map[string]interface{}{
- {
- "temperature": 32.32,
- "humidity": 80.8,
- },
- {
- "temperature": 34.32,
- "humidity": 81.8,
- },
- },
- wantErr: false,
- },
- {
- name: "test4",
- args: args{
- input: []interface{}{
- map[string]interface{}{
- "telemetry": []map[string]interface{}{
- {
- "temperature": 32.32,
- "humidity": 80.8,
- "ts": 1388082430,
- },
- {
- "temperature": 34.32,
- "humidity": 81.8,
- "ts": 1388082440,
- },
- },
- },
- },
- dataField: "telemetry",
- fields: []string{"temperature", "humidity"},
- },
- want: []map[string]interface{}{
- {
- "temperature": 32.32,
- "humidity": 80.8,
- },
- {
- "temperature": 34.32,
- "humidity": 81.8,
- },
- },
- wantErr: false,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- got, _, err := TransItem(tt.args.input, tt.args.dataField, tt.args.fields)
- if (err != nil) != tt.wantErr {
- t.Errorf("TransItem() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- if !reflect.DeepEqual(got, tt.want) {
- t.Errorf("TransItem() got = %v, want %v", got, tt.want)
- }
- })
- }
- }
|