123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- // Copyright 2021-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 io
- import (
- "errors"
- "fmt"
- "testing"
- "github.com/lf-edge/ekuiper/internal/binder"
- "github.com/lf-edge/ekuiper/internal/binder/mock"
- "github.com/lf-edge/ekuiper/pkg/errorx"
- )
- func TestBindings(t *testing.T) {
- m := mock.NewMockFactory()
- e := binder.FactoryEntry{
- Name: "mock",
- Factory: m,
- }
- err := Initialize([]binder.FactoryEntry{e})
- if err != nil {
- t.Error(err)
- return
- }
- tests := []struct {
- name string
- isSource bool
- isLookupSource bool
- isSink bool
- }{
- {
- name: "unknown",
- isSource: false,
- isLookupSource: false,
- isSink: false,
- }, {
- name: "mqtt",
- isSource: true,
- isLookupSource: false,
- isSink: true,
- }, {
- name: "mock1",
- isSource: true,
- isLookupSource: false,
- isSink: true,
- }, {
- name: "rest",
- isSource: false,
- isLookupSource: false,
- isSink: true,
- }, {
- name: "redis",
- isSource: false,
- isLookupSource: true,
- isSink: true,
- },
- }
- fmt.Printf("The test bucket size is %d.\n\n", len(tests))
- for _, tt := range tests {
- _, err := Source(tt.name)
- isSource := err == nil
- if tt.isSource != isSource {
- t.Errorf("%s is source: expect %v but got %v", tt.name, tt.isSource, isSource)
- }
- _, err = LookupSource(tt.name)
- if tt.isLookupSource != (err == nil) {
- t.Errorf("%s is lookup source: expect %v but got %v", tt.name, tt.isLookupSource, err == nil)
- }
- _, err = Sink(tt.name)
- isSink := err == nil
- if tt.isSink != isSink {
- t.Errorf("%s is sink: expect %v but got %v", tt.name, tt.isSink, isSink)
- }
- }
- }
- func TestSource(t *testing.T) {
- m1 := mock.NewMockFactory()
- m2 := mock.NewMockFactory()
- e1 := binder.FactoryEntry{
- Name: "mock1",
- Factory: m1,
- }
- e2 := binder.FactoryEntry{
- Name: "mock2",
- Factory: m2,
- }
- err := Initialize([]binder.FactoryEntry{e1, e2})
- if err != nil {
- t.Error(err)
- return
- }
- type args struct {
- name string
- }
- tests := []struct {
- name string
- args args
- isSrc bool
- wantErr bool
- errs error
- }{
- {
- name: "mockFunc1",
- args: args{
- name: "mock",
- },
- isSrc: true,
- wantErr: false,
- errs: nil,
- },
- {
- name: "mockFunc2",
- args: args{
- name: "echo",
- },
- isSrc: false,
- wantErr: true,
- errs: errors.Join(fmt.Errorf("mock1: %v", errorx.NotFoundErr), fmt.Errorf("mock2: %v", errorx.NotFoundErr)),
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- src, err := Source(tt.args.name)
- if (src != nil) != tt.isSrc {
- t.Errorf("Source() src = %v, isSrc = %v", src, tt.isSrc)
- }
- if (err != nil) != tt.wantErr {
- t.Errorf("Source() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- if err != nil {
- if errors.Is(err, tt.errs) {
- t.Errorf("Source() error = %v, wantErr %v", err.Error(), tt.errs)
- }
- }
- })
- }
- }
- func TestSink(t *testing.T) {
- m1 := mock.NewMockFactory()
- m2 := mock.NewMockFactory()
- e1 := binder.FactoryEntry{
- Name: "mock1",
- Factory: m1,
- }
- e2 := binder.FactoryEntry{
- Name: "mock2",
- Factory: m2,
- }
- err := Initialize([]binder.FactoryEntry{e1, e2})
- if err != nil {
- t.Error(err)
- return
- }
- type args struct {
- name string
- }
- tests := []struct {
- name string
- args args
- isSink bool
- wantErr bool
- errs error
- }{
- {
- name: "mockFunc1",
- args: args{
- name: "mock",
- },
- isSink: true,
- wantErr: false,
- errs: nil,
- },
- {
- name: "mockFunc2",
- args: args{
- name: "echo",
- },
- isSink: false,
- wantErr: true,
- errs: errors.Join(fmt.Errorf("mock1: %v", errorx.NotFoundErr), fmt.Errorf("mock2: %v", errorx.NotFoundErr)),
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- sink, err := Sink(tt.args.name)
- if (sink != nil) != tt.isSink {
- t.Errorf("Sink() sink = %v, isSink = %v", sink, tt.isSink)
- }
- if (err != nil) != tt.wantErr {
- t.Errorf("Sink() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- if err != nil {
- if errors.Is(err, tt.errs) {
- t.Errorf("Sink() error = %v, wantErr %v", err.Error(), tt.errs)
- }
- }
- })
- }
- }
- func TestLookupSource(t *testing.T) {
- m := mock.NewMockFactory()
- e := binder.FactoryEntry{
- Name: "mock",
- Factory: m,
- }
- err := Initialize([]binder.FactoryEntry{e})
- if err != nil {
- t.Error(err)
- return
- }
- type args struct {
- name string
- }
- tests := []struct {
- name string
- args args
- isSrc bool
- wantErr bool
- errs error
- }{
- {
- name: "mockFunc1",
- args: args{
- name: "mock",
- },
- isSrc: false,
- wantErr: true,
- errs: fmt.Errorf("lookup source type mock not found"),
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- src, err := LookupSource(tt.args.name)
- if (src != nil) != tt.isSrc {
- t.Errorf("LookupSource() src = %v, isSrc = %v", src, tt.isSrc)
- }
- if (err != nil) != tt.wantErr {
- t.Errorf("LookupSource() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- if err != nil {
- if errors.Is(err, tt.errs) {
- t.Errorf("LookupSource() error = %v, wantErr %v", err.Error(), tt.errs)
- }
- }
- })
- }
- }
|