From 1b3c69d709eb3abe83fada66d358e4ce6cb259b2 Mon Sep 17 00:00:00 2001 From: "Xinwei Xiong(cubxxw)" <3293172751nss@gmail.com> Date: Thu, 9 Nov 2023 18:14:59 +0800 Subject: [PATCH] feat: add openim info test --- go.mod | 3 +- go.sum | 1 + pkg/common/cmd/api_test.go | 100 --------------------------- pkg/common/config/parse.go | 6 +- pkg/common/config/parse_test.go | 117 ++++++++++++++++++++++++++++++++ 5 files changed, 124 insertions(+), 103 deletions(-) delete mode 100644 pkg/common/cmd/api_test.go create mode 100644 pkg/common/config/parse_test.go diff --git a/go.mod b/go.mod index c754d45ca..3fdaad892 100644 --- a/go.mod +++ b/go.mod @@ -45,6 +45,7 @@ require ( github.com/redis/go-redis/v9 v9.2.1 github.com/tencentyun/cos-go-sdk-v5 v0.7.45 gopkg.in/src-d/go-git.v4 v4.13.1 + gotest.tools v2.2.0+incompatible ) require ( @@ -117,6 +118,7 @@ require ( github.com/sergi/go-diff v1.0.0 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/src-d/gcfg v1.4.0 // indirect + github.com/stretchr/objx v0.5.0 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/xanzy/ssh-agent v0.2.1 // indirect github.com/xdg-go/pbkdf2 v1.0.0 // indirect @@ -140,7 +142,6 @@ require ( google.golang.org/genproto/googleapis/rpc v0.0.0-20231012201019-e917dd12ba7a // indirect gopkg.in/src-d/go-billy.v4 v4.3.2 // indirect gopkg.in/warnings.v0 v0.1.2 // indirect - gotest.tools v2.2.0+incompatible // indirect ) require ( diff --git a/go.sum b/go.sum index 99d272692..342656813 100644 --- a/go.sum +++ b/go.sum @@ -312,6 +312,7 @@ github.com/src-d/gcfg v1.4.0/go.mod h1:p/UMsR43ujA89BJY9duynAwIpvqEujIH/jFlfL7jW github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= diff --git a/pkg/common/cmd/api_test.go b/pkg/common/cmd/api_test.go deleted file mode 100644 index 74847ba2b..000000000 --- a/pkg/common/cmd/api_test.go +++ /dev/null @@ -1,100 +0,0 @@ -// Copyright © 2023 OpenIM. All rights reserved. -// -// 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 cmd - -import ( - "testing" - - "github.com/OpenIMSDK/protocol/constant" -) - -func TestApiCmd_AddApi(t *testing.T) { - type fields struct { - RootCmd *RootCmd - } - type args struct { - f func(port int) error - } - tests := []struct { - name string - fields fields - args args - // No 'want' field here since we're testing side effects, not return values. - }{ - { - name: "adds API with valid function", - fields: fields{ - RootCmd: &RootCmd{ - // setup RootCmd properties as needed for the test - }, - }, - args: args{ - f: func(port int) error { - // implement a mock function or check side effects - return nil - }, - }, - }, - // Add more test cases as needed. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - a := &ApiCmd{ - RootCmd: tt.fields.RootCmd, - } - a.AddApi(tt.args.f) - // Test the side effects or behavior of AddApi here. - }) - } -} - -func TestApiCmd_GetPortFromConfig(t *testing.T) { - type fields struct { - RootCmd *RootCmd - } - type args struct { - portType string - } - tests := []struct { - name string - fields fields - args args - want int - }{ - { - name: "gets API port from config", - fields: fields{ - RootCmd: &RootCmd{ - // setup RootCmd properties as needed for the test - }, - }, - args: args{ - portType: constant.FlagPort, - }, - want: 8080, // This should be the expected port number according to your configuration - }, - // Add more test cases for different portTypes and expected results. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - a := &ApiCmd{ - RootCmd: tt.fields.RootCmd, - } - if got := a.GetPortFromConfig(tt.args.portType); got != tt.want { - t.Errorf("ApiCmd.GetPortFromConfig() = %v, want %v", got, tt.want) - } - }) - } -} diff --git a/pkg/common/config/parse.go b/pkg/common/config/parse.go index 7bd77d92b..f2ea962ee 100644 --- a/pkg/common/config/parse.go +++ b/pkg/common/config/parse.go @@ -49,7 +49,7 @@ func GetDefaultConfigPath() string { func GetProjectRoot() string { b, _ := filepath.Abs(os.Args[0]) - return filepath.Join(filepath.Dir(b), "../../..") + return filepath.Join(filepath.Dir(b), "../../../../..") } func GetOptionsByNotification(cfg NotificationConf) msgprocessor.Options { @@ -67,6 +67,7 @@ func GetOptionsByNotification(cfg NotificationConf) msgprocessor.Options { opts = msgprocessor.WithOptions(opts, msgprocessor.WithHistory(true), msgprocessor.WithPersistent()) } opts = msgprocessor.WithOptions(opts, msgprocessor.WithSendMsg(cfg.IsSendMsg)) + return opts } @@ -89,6 +90,7 @@ func initConfig(config interface{}, configName, configFolderPath string) error { return fmt.Errorf("unmarshal yaml error: %w", err) } fmt.Println("use config", configFolderPath) + return nil } @@ -107,4 +109,4 @@ func InitConfig(configFolderPath string) error { } return initConfig(&Config.Notification, NotificationFileName, configFolderPath) -} \ No newline at end of file +} diff --git a/pkg/common/config/parse_test.go b/pkg/common/config/parse_test.go new file mode 100644 index 000000000..e34aa5b7f --- /dev/null +++ b/pkg/common/config/parse_test.go @@ -0,0 +1,117 @@ +// Copyright © 2023 OpenIM. All rights reserved. +// +// 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 config + +import ( + _ "embed" + "reflect" + "testing" + + "github.com/openimsdk/open-im-server/v3/pkg/msgprocessor" +) + +func TestGetDefaultConfigPath(t *testing.T) { + tests := []struct { + name string + want string + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := GetDefaultConfigPath(); got != tt.want { + t.Errorf("GetDefaultConfigPath() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestGetProjectRoot(t *testing.T) { + tests := []struct { + name string + want string + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := GetProjectRoot(); got != tt.want { + t.Errorf("GetProjectRoot() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestGetOptionsByNotification(t *testing.T) { + type args struct { + cfg NotificationConf + } + tests := []struct { + name string + args args + want msgprocessor.Options + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := GetOptionsByNotification(tt.args.cfg); !reflect.DeepEqual(got, tt.want) { + t.Errorf("GetOptionsByNotification() = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_initConfig(t *testing.T) { + type args struct { + config interface{} + configName string + configFolderPath string + } + tests := []struct { + name string + args args + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := initConfig(tt.args.config, tt.args.configName, tt.args.configFolderPath); (err != nil) != tt.wantErr { + t.Errorf("initConfig() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestInitConfig(t *testing.T) { + type args struct { + configFolderPath string + } + tests := []struct { + name string + args args + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := InitConfig(tt.args.configFolderPath); (err != nil) != tt.wantErr { + t.Errorf("InitConfig() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +}