Skip to content

Commit

Permalink
add: some test in url_service
Browse files Browse the repository at this point in the history
  • Loading branch information
FantasyRL committed Dec 3, 2024
1 parent 0e41c34 commit a932e8f
Show file tree
Hide file tree
Showing 2 changed files with 188 additions and 0 deletions.
94 changes: 94 additions & 0 deletions internal/url/service/get_dump_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
Copyright 2024 The west2-online Authors.
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 service

import (
"fmt"
"testing"

"github.com/bytedance/mockey"
"github.com/stretchr/testify/assert"

"github.com/west2-online/fzuhelper-server/pkg/upyun"
)

func TestGetDump(t *testing.T) {
type testCase struct {
name string // 测试用例名称
mockJsonBytes *[]byte // mock返回的JSON数据
mockError error // mock返回的错误
expectedResult *string // 期望返回的结果
expectingError bool // 是否期望抛出错误
expectedErrorInfo string // 期望的错误信息
}
mockJsonBytes := []byte(`{"key": "value"}`)

// 测试用例
testCases := []testCase{
{
name: "SuccessCase",
mockJsonBytes: &mockJsonBytes,
mockError: nil,
expectedResult: toPointer(`{"key": "value"}`),
expectingError: false,
},
{
name: "FileNotFound",
mockJsonBytes: nil,
mockError: fmt.Errorf("file not found"),
expectedResult: nil,
expectingError: true,
expectedErrorInfo: "UrlService.GetDump error:file not found",
},
}

defer mockey.UnPatchAll() // 清理所有mock

for _, tc := range testCases {
mockey.PatchConvey(tc.name, t, func() {
// Mock upyun.URlGetFile 方法
mockey.Mock(upyun.URlGetFile).To(func(filename string) (*[]byte, error) {
return tc.mockJsonBytes, tc.mockError
}).Build()
mockey.Mock(upyun.JoinFileName).To(func(filename string) string {
return filename
}).Build()

// 初始化 UrlService 实例
urlService := &UrlService{}

// 调用方法
result, err := urlService.GetDump()

if tc.expectingError {
// 如果期望抛错,检查错误信息
assert.NotNil(t, err)
assert.EqualError(t, err, tc.expectedErrorInfo)
assert.Nil(t, result)
} else {
// 如果不期望抛错,验证结果
assert.NoError(t, err)
assert.Equal(t, tc.expectedResult, result)
}
})
}
}

// 工具方法,将字符串转换为指针
func toPointer(value string) *string {
return &value
}
94 changes: 94 additions & 0 deletions internal/url/service/get_version_release_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
Copyright 2024 The west2-online Authors.
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 service

import (
"encoding/json"
"fmt"
"testing"

"github.com/bytedance/mockey"
"github.com/stretchr/testify/assert"

"github.com/west2-online/fzuhelper-server/internal/url/pack"
"github.com/west2-online/fzuhelper-server/pkg/upyun"
)

func TestGetReleaseVersion(t *testing.T) {
type testCase struct {
name string // 测试用例名称
mockJsonBytes *[]byte // mock返回的JSON数据
mockError error // mock返回的错误
expectedResult *pack.Version // 期望返回的结果
expectingError bool // 是否期望抛出错误
expectedErrorInfo string // 期望的错误信息
}

// 模拟数据
mockVersion := &pack.Version{Url: "http://example.com/release.apk", Version: "1.0.0"}
mockVersionBytes, _ := json.Marshal(mockVersion)

testCases := []testCase{
{
name: "SuccessCase",
mockJsonBytes: &mockVersionBytes,
mockError: nil,
expectedResult: mockVersion,
expectingError: false,
expectedErrorInfo: "",
},
{
name: "FileNotFound",
mockJsonBytes: nil,
mockError: fmt.Errorf("file not found"),
expectedResult: nil,
expectingError: true,
expectedErrorInfo: "UrlService.GetReleaseVersion error:file not found",
},
}

defer mockey.UnPatchAll() // 清理所有mock

for _, tc := range testCases {
mockey.PatchConvey(tc.name, t, func() {
// Mock upyun.URlGetFile 方法
mockey.Mock(upyun.URlGetFile).To(func(filename string) (*[]byte, error) {
return tc.mockJsonBytes, tc.mockError
}).Build()
mockey.Mock(upyun.JoinFileName).To(func(filename string) string {
return filename
}).Build()

// 初始化 UrlService 实例
urlService := &UrlService{}

// 调用方法
result, err := urlService.GetReleaseVersion()

if tc.expectingError {
// 如果期望抛错,检查错误信息
assert.NotNil(t, err)
assert.EqualError(t, err, tc.expectedErrorInfo)
assert.Nil(t, result)
} else {
// 如果不期望抛错,验证结果
assert.Nil(t, err)
assert.Equal(t, tc.expectedResult, result)
}
})
}
}

0 comments on commit a932e8f

Please sign in to comment.