feat(test): 添加测试数据生成和API请求功能

- test.go中添加了生成测试数据的逻辑,并使用gorequest库发起了HTTP POST请求。
- main.go中调整了插入消息的条件,现在包括'message_sent'类型。
- .gitignore中新增了请求数据的忽略项request.json。
- go.mod中新增了gorequest和goconvey库的依赖。

此功能使得测试更加自动化,能够更方便地进行数据的模拟和测试。
This commit is contained in:
liyp 2024-07-02 18:16:26 +08:00
parent fef6cc7c9d
commit 8e4ad23278
6 changed files with 74 additions and 19 deletions

3
.gitignore vendored
View file

@ -1,5 +1,6 @@
data.db
test.json
*.exe
request.json
config.toml
messages.json

4
go.mod
View file

@ -7,11 +7,13 @@ require github.com/BurntSushi/toml v1.3.2
require (
github.com/goccy/go-json v0.10.2
github.com/mattn/go-sqlite3 v1.14.22
github.com/parnurzeal/gorequest v0.3.0
)
require (
github.com/elazarl/goproxy v0.0.0-20240618083138-03be62527ccb // indirect
github.com/moul/http2curl v1.0.0 // indirect
github.com/parnurzeal/gorequest v0.3.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/smartystreets/goconvey v1.8.1 // indirect
golang.org/x/net v0.26.0 // indirect
)

16
go.sum
View file

@ -1,9 +1,14 @@
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/elazarl/goproxy v0.0.0-20240618083138-03be62527ccb h1:2SoxRauy2IqekRMggrQk3yNI5X6omSnk6ugVbFywwXs=
github.com/elazarl/goproxy v0.0.0-20240618083138-03be62527ccb/go.mod h1:Ro8st/ElPeALwNFlcTpWmkr6IoMFfkjXAvTHpevnDsM=
github.com/elazarl/goproxy/ext v0.0.0-20190711103511-473e67f1d7d2/go.mod h1:gNh8nYJoAm43RfaxurUnxr+N1PwuFV3ZMl/efxlIlY8=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
github.com/gopherjs/gopherjs v1.17.2 h1:fQnZVsXk8uxXIStYb0N4bGk7jeyTalG/wsZjQ25dO0g=
github.com/gopherjs/gopherjs v1.17.2/go.mod h1:pRRIvn/QzFLrKfvEz3qUuEhtE/zLCWfreZ6J5gM2i+k=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/moul/http2curl v1.0.0 h1:dRMWoAtb+ePxMlLkrCbAqh4TlPHXvoGUSQ323/9Zahs=
@ -12,7 +17,10 @@ github.com/parnurzeal/gorequest v0.3.0 h1:SoFyqCDC9COr1xuS6VA8fC8RU7XyrJZN2ona1k
github.com/parnurzeal/gorequest v0.3.0/go.mod h1:3Kh2QUMJoqw3icWAecsyzkpY7UzRfDhbRdTjtNwNiUE=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
github.com/rogpeppe/go-charset v0.0.0-20180617210344-2471d30d28b4/go.mod h1:qgYeAmZ5ZIpBWTGllZSQnw97Dj+woV0toclVaRGI8pc=
github.com/smarty/assertions v1.15.0 h1:cR//PqUBUiQRakZWqBiFFQ9wb8emQGDb0HeGdqGByCY=
github.com/smarty/assertions v1.15.0/go.mod h1:yABtdzeQs6l1brC900WlRNwj6ZR55d7B+E8C6HtKdec=
github.com/smartystreets/goconvey v1.8.1 h1:qGjIddxOk4grTu9JPOU31tVfq3cNdBlNa5sSznIX1xY=
github.com/smartystreets/goconvey v1.8.1/go.mod h1:+/u4qLyY6x1jReYOp7GOM2FSt8aP9CzCZL03bI28W60=
golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ=
golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE=

View file

@ -23,7 +23,7 @@ func insertMessage(db *sql.DB, data map[string]interface{}) error {
// if err != nil {
// log.Fatal(err)
// }
if data["post_type"] == "message" {
if data["post_type"] == "message" || data["post_type"] == "message_sent" {
// id := data["message_id"].(float64)
post_type := data["post_type"].(string)
message_type := data["message_type"].(string)
@ -46,7 +46,7 @@ func insertMessage(db *sql.DB, data map[string]interface{}) error {
sender_role := sender["role"].(string)
message_seq := data["message_seq"].(float64)
fmt.Println(post_type, message_time, group_id, message_id, raw_message, sender_user_id, sender_nickname, sender_card, sender_role, message_seq)
fmt.Println(post_type, message_time, int64(group_id), int64(message_id), raw_message, sender_user_id, sender_nickname, sender_card, sender_role, int64(message_seq))
// Insert data into database
_, err = db.Exec("INSERT INTO messages ( post_type, message_type, time, group_id, message_id, raw_message, sender_user_id, sender_nickname, sender_card, sender_role, message_seq) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
post_type, message_type, message_time, group_id, message_id, raw_message, sender_user_id, sender_nickname, sender_card, sender_role, message_seq)

View file

@ -3,29 +3,71 @@ package main
import (
"bufio"
"fmt"
"go-bot/workers"
"go-bot/config"
"os"
"strings"
"time"
"github.com/parnurzeal/gorequest"
)
func main() {
for {
reader := bufio.NewReader(os.Stdin)
// cfg := config.GetConfig()
// config.PrintConfig(cfg, "")
fmt.Print("输入指令(不要带/)")
cfg := config.GetConfig()
fmt.Print("输入指令:")
raw_msg, _ := reader.ReadString('\n')
// 去除末尾的换行符
// raw_msg = strings.TrimRight(raw_msg, "\r\n")
raw_msg = strings.TrimRight(raw_msg, "\r\n")
if raw_msg == "" {
raw_msg = "ping"
raw_msg = "!ping"
}
parms := strings.Fields(raw_msg)
if raw_msg == "!exit" {
break
}
// parms := strings.Fields(raw_msg)
worker := workers.NewWorker(parms, "794508986", "874769998", "111", "222", raw_msg)
fmt.Println("TestPermission:", worker.CheckPermission())
message := worker.GetMsg()
message := map[string]interface{}{
"post_type": "message",
"message_type": "group",
"time": float64(time.Now().Unix()),
"group_id": float64(874769998),
"message_id": float64(time.Now().Unix()),
"raw_message": raw_msg,
"sender": map[string]interface{}{
"user_id": float64(794508986),
"nickname": "111",
"card": "",
"role": "owner",
},
"message_seq": float64(time.Now().Unix()),
"user_id": float64(794508986),
}
fmt.Println("message:", message)
// jsonData, err := json.MarshalIndent(message, "", " ")
// if err != nil {
// log.Fatal(err)
// }
// file, err := os.Create("message.json")
// if err != nil {
// log.Fatal(err)
// }
// defer file.Close()
// _, err = file.Write(jsonData)
// if err != nil {
// log.Fatal(err)
// }
url := "http://" + cfg["APIURL"].(string)
request := gorequest.New()
_, _, err := request.Post(url).Send(message).End()
if err != nil {
fmt.Println("Error:", err)
}
// worker := workers.NewWorker(parms, "794508986", "874769998", "111", "222", raw_msg)
// fmt.Println("TestPermission:", worker.CheckPermission())
// message := worker.GetMsg()
}
}

View file

@ -1,6 +1,7 @@
package workers
import (
"fmt"
"go-bot/config"
"log"
"strings"
@ -154,11 +155,12 @@ func (a *AI) GetMsg() string {
} else {
log.Println("choices为空")
msg = "api解析失败"
}
}
}
}
return msg
return fmt.Sprintf("[CQ:at,qq=%s] %s", a.UID, msg)
}