123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- // Copyright 2022 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 main
- import (
- "fmt"
- "reflect"
- "testing"
- "github.com/lf-edge/ekuiper/internal/conf"
- "github.com/lf-edge/ekuiper/internal/testx"
- "github.com/lf-edge/ekuiper/internal/topo/context"
- "github.com/lf-edge/ekuiper/internal/topo/state"
- )
- func TestConfig(t *testing.T) {
- tests := []struct {
- conf map[string]interface{}
- expected *taosConfig
- error string
- }{
- { // 0
- conf: map[string]interface{}{
- "host": "e0d9d8089bef",
- "port": 6030,
- "user": "root",
- "password": "taosdata",
- "database": "db",
- "table": "t",
- "tsfieldname": "ts",
- },
- expected: &taosConfig{
- ProvideTs: false,
- Host: "e0d9d8089bef",
- Port: 6030,
- User: "root",
- Password: "taosdata",
- Database: "db",
- Table: "t",
- TsFieldName: "ts",
- Fields: nil,
- },
- },
- { // 1
- conf: map[string]interface{}{
- "ip": "e0d9d8089bef",
- "port": 6030,
- "user": "root1",
- "password": "taosdata1",
- "database": "db",
- "table": "t",
- "provideTs": true,
- "tsfieldname": "ts",
- },
- expected: &taosConfig{
- ProvideTs: true,
- Ip: "e0d9d8089bef",
- Host: "e0d9d8089bef",
- Port: 6030,
- User: "root1",
- Password: "taosdata1",
- Database: "db",
- Table: "t",
- TsFieldName: "ts",
- Fields: nil,
- },
- },
- { // 2
- conf: map[string]interface{}{
- "port": 6030,
- "database": "dab",
- "table": "tt",
- "tsfieldname": "tst",
- "fields": []string{"f1", "f2"},
- "sTable": "s",
- "tagFields": []string{"a", "b"},
- },
- expected: &taosConfig{
- ProvideTs: false,
- Ip: "",
- Host: "localhost",
- Port: 6030,
- User: "root",
- Password: "taosdata",
- Database: "dab",
- Table: "tt",
- TsFieldName: "tst",
- Fields: []string{"f1", "f2"},
- STable: "s",
- TagFields: []string{"a", "b"},
- },
- },
- { // 3
- conf: map[string]interface{}{
- "port": 6030,
- "database": "dab",
- "table": "t",
- "fields": []string{"f1", "f2"},
- },
- error: "property TsFieldName is required",
- },
- { // 4
- conf: map[string]interface{}{
- "port": 6030,
- "database": "dab",
- "table": "tt",
- "tsfieldname": "tst",
- "fields": []string{"f1", "f2"},
- "sTable": "s",
- },
- error: "property tagFields is required when sTable is set",
- },
- }
- fmt.Printf("The test bucket size is %d.\n\n", len(tests))
- for i, test := range tests {
- tdsink := &taosSink{}
- err := tdsink.Configure(test.conf)
- if !reflect.DeepEqual(test.error, testx.Errstring(err)) {
- t.Errorf("%d: error mismatch:\n exp=%s\n got=%s\n\n", i, test.error, err)
- } else if test.error == "" && !reflect.DeepEqual(test.expected, tdsink.conf) {
- t.Errorf("%d\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, test.expected, tdsink.conf)
- }
- }
- }
- func TestBuildSql(t *testing.T) {
- tests := []struct {
- conf *taosConfig
- data map[string]interface{}
- expected string
- error string
- }{
- {
- conf: &taosConfig{
- ProvideTs: false,
- Host: "e0d9d8089bef",
- Port: 6030,
- User: "root",
- Password: "taosdata",
- Database: "db",
- Table: "t",
- TsFieldName: "ts",
- Fields: nil,
- },
- data: map[string]interface{}{
- "f1": "v1",
- },
- expected: `t (ts,f1) values (now,"v1")`,
- },
- {
- conf: &taosConfig{
- ProvideTs: true,
- Ip: "e0d9d8089bef",
- Host: "e0d9d8089bef",
- Port: 6030,
- User: "root1",
- Password: "taosdata1",
- Database: "db",
- Table: "t",
- TsFieldName: "ts",
- Fields: nil,
- },
- data: map[string]interface{}{
- "ts": 1.2345678e+06,
- "f2": 65,
- },
- expected: `t (ts,f2) values (12345678,65)`,
- },
- {
- conf: &taosConfig{
- ProvideTs: true,
- Ip: "e0d9d8089bef",
- Host: "e0d9d8089bef",
- Port: 6030,
- User: "root1",
- Password: "taosdata1",
- Database: "db",
- Table: "t",
- TsFieldName: "ts",
- Fields: nil,
- },
- data: map[string]interface{}{
- "ts": 12345678,
- "f2": 65,
- },
- expected: `t (ts,f2) values (12345678,65)`,
- },
- {
- conf: &taosConfig{
- ProvideTs: false,
- Ip: "",
- Host: "localhost",
- Port: 6030,
- User: "root",
- Password: "taosdata",
- Database: "dab",
- Table: "{{.table}}",
- TsFieldName: "tst",
- Fields: []string{"f1", "f2"},
- STable: "s",
- TagFields: []string{"a", "b"},
- },
- data: map[string]interface{}{
- "table": "t1",
- "ts": 12345678,
- "f2": 65,
- "f1": 12.3,
- "a": "a1",
- "b": 2,
- },
- expected: `t1 (tst,f1,f2) using s tags ("a1",2) values (now,12.3,65)`,
- },
- }
- fmt.Printf("The test bucket size is %d.\n\n", len(tests))
- contextLogger := conf.Log.WithField("rule", "mockRule0")
- ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger).WithMeta("testTD", "op1", &state.MemoryStore{})
- for i, test := range tests {
- sql, err := test.conf.buildSql(ctx, test.data)
- if !reflect.DeepEqual(test.error, testx.Errstring(err)) {
- t.Errorf("%d: error mismatch:\n exp=%s\n got=%s\n\n", i, test.error, err)
- } else if test.error == "" && !reflect.DeepEqual(test.expected, sql) {
- t.Errorf("%d\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, test.expected, sql)
- }
- }
- }
|