使用相对于项目地址的配置文件路径以便于单元测试
This commit is contained in:
2
.vscode/settings.json
vendored
2
.vscode/settings.json
vendored
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"editor.inlineSuggest.showToolbar": "always"
|
||||
"editor.inlineSuggest.showToolbar": "onHover"
|
||||
}
|
13
bin/main.go
13
bin/main.go
@@ -304,6 +304,10 @@ func main() {
|
||||
var text_ids []int
|
||||
if text := QueryConditions("text"); len(text) > 0 {
|
||||
rest := models.ElasticsearchSearch(strings.Join(text, " "))
|
||||
if rest == nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
for _, hit := range rest["hits"].(map[string]interface{})["hits"].([]interface{}) {
|
||||
id, err := strconv.Atoi(hit.(map[string]interface{})["_id"].(string))
|
||||
if err != nil {
|
||||
@@ -421,7 +425,7 @@ func main() {
|
||||
image_list = image_list_sorted
|
||||
}
|
||||
|
||||
// 用户ID, 图集ID
|
||||
// 用户ID, 文章ID
|
||||
var user_ids []int
|
||||
var article_ids []int
|
||||
for _, image := range image_list {
|
||||
@@ -429,7 +433,7 @@ func main() {
|
||||
article_ids = append(article_ids, image.Article.Id)
|
||||
}
|
||||
|
||||
// 附加用户信息(第三步: 将用户信息附加到图片信息中)
|
||||
// 附加用户信息
|
||||
users := models.QueryUserList(user_ids)
|
||||
for i, image := range image_list {
|
||||
for _, user := range users {
|
||||
@@ -439,7 +443,7 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
// 附加图片集信息(第三步: 将图片集信息附加到图片信息中)
|
||||
// 附加文章信息
|
||||
articles := models.QueryArticleList(article_ids)
|
||||
for i, image := range image_list {
|
||||
for _, article := range articles {
|
||||
@@ -455,8 +459,9 @@ func main() {
|
||||
images.List[i] = v
|
||||
}
|
||||
|
||||
// 如果不是获取相似图像固定数量, 则从mysql获取总数
|
||||
if similar := QueryConditions("similar"); len(similar) > 0 {
|
||||
// 总数不变
|
||||
// 固定数量
|
||||
} else {
|
||||
// 获取总数
|
||||
err = mysqlConnection.Database.QueryRow("SELECT COUNT(*) FROM web_images" + conditions.String()).Scan(&images.Total)
|
||||
|
3
go.mod
3
go.mod
@@ -12,6 +12,7 @@ require (
|
||||
github.com/jmoiron/sqlx v1.3.5
|
||||
github.com/milvus-io/milvus-sdk-go/v2 v2.2.1
|
||||
github.com/spf13/viper v1.15.0
|
||||
github.com/stretchr/testify v1.8.1
|
||||
)
|
||||
|
||||
require (
|
||||
@@ -23,6 +24,7 @@ require (
|
||||
github.com/alibabacloud-go/tea-xml v1.1.2 // indirect
|
||||
github.com/aliyun/credentials-go v1.1.2 // indirect
|
||||
github.com/clbanning/mxj/v2 v2.5.6 // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/elastic/elastic-transport-go/v8 v8.3.0 // indirect
|
||||
github.com/fsnotify/fsnotify v1.6.0 // indirect
|
||||
github.com/golang/protobuf v1.5.2 // indirect
|
||||
@@ -35,6 +37,7 @@ require (
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/sizeofint/webp-animation v0.0.0-20190207194838-b631dc900de9 // indirect
|
||||
github.com/spf13/afero v1.9.3 // indirect
|
||||
github.com/spf13/cast v1.5.0 // indirect
|
||||
|
@@ -1,21 +1,25 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var Viper *viper.Viper
|
||||
var (
|
||||
_, b, _, _ = runtime.Caller(0)
|
||||
Root = filepath.Join(filepath.Dir(b), "..")
|
||||
Viper *viper.Viper
|
||||
)
|
||||
|
||||
func init() {
|
||||
//如果命令行参数中有test,则使用测试环境的配置
|
||||
var config_file string = "./data/config.yaml"
|
||||
if len(os.Args) > 1 && os.Args[1] == "test" {
|
||||
log.Println("使用测试环境配置")
|
||||
config_file = "./data/config_test.yaml"
|
||||
}
|
||||
config_file := filepath.Join(Root, "data", "config.yaml")
|
||||
fmt.Println(config_file)
|
||||
|
||||
viper.SetConfigFile(config_file)
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
log.Println("读取配置文件失败", err)
|
||||
|
@@ -29,6 +29,10 @@ func elasticsearch_init() (es *elasticsearch.Client) {
|
||||
return es
|
||||
}
|
||||
|
||||
type SearchData struct {
|
||||
Total int64 `json:"total"`
|
||||
}
|
||||
|
||||
func ElasticsearchSearch(text string) map[string]interface{} {
|
||||
var (
|
||||
r map[string]interface{}
|
||||
|
16
models/elasticsearch_test.go
Normal file
16
models/elasticsearch_test.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestMyFunction(t *testing.T) {
|
||||
// 创建一个测试用例
|
||||
expected := 10
|
||||
actual := ElasticsearchSearch("豌豆")
|
||||
|
||||
// 使用 assert 包中的函数来验证函数的输出
|
||||
assert.Equal(t, expected, actual)
|
||||
}
|
Reference in New Issue
Block a user