2020-04-30 09:33:47 +00:00
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"background/config"
|
|
|
|
"background/db"
|
|
|
|
"background/logs"
|
|
|
|
"background/model"
|
2021-02-05 17:46:59 +00:00
|
|
|
"background/utils"
|
2020-04-30 09:33:47 +00:00
|
|
|
"log"
|
2021-02-05 17:46:59 +00:00
|
|
|
"reflect"
|
2020-04-30 09:33:47 +00:00
|
|
|
"testing"
|
2021-02-05 17:46:59 +00:00
|
|
|
|
|
|
|
"github.com/olivere/elastic"
|
2020-04-30 09:33:47 +00:00
|
|
|
)
|
2020-11-10 16:05:17 +00:00
|
|
|
|
2020-04-30 09:33:47 +00:00
|
|
|
func InitConfig() {
|
2021-02-05 17:46:59 +00:00
|
|
|
e := config.Init("../user.yaml")
|
2020-04-30 09:33:47 +00:00
|
|
|
if nil != e {
|
|
|
|
log.Println(e.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func InitMysql() {
|
|
|
|
c := config.GetMysqlConfig()
|
|
|
|
if c == nil {
|
|
|
|
logs.Error("cannnot connect mysql server")
|
|
|
|
} else {
|
|
|
|
db.Init()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func InitRedisConfig() {
|
|
|
|
e := config.InitRedis()
|
|
|
|
if nil != e {
|
|
|
|
logs.Error(e.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-11-10 16:05:17 +00:00
|
|
|
func InitElasticSearch() {
|
|
|
|
e := db.GetElastic().CreateIndex("hardware", model.HardwareTypeMapping())
|
|
|
|
if nil != e {
|
2020-04-30 09:33:47 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-05 17:46:59 +00:00
|
|
|
|
2020-04-30 09:33:47 +00:00
|
|
|
func InitLogs() {
|
|
|
|
logs.Init(config.GetLogConfig().Dir, config.GetLogConfig().File, config.GetLogConfig().Level, config.GetLogConfig().SaveFile)
|
|
|
|
}
|
2020-11-10 16:05:17 +00:00
|
|
|
func TestPortDocToElastic(t *testing.T) {
|
2020-04-30 09:33:47 +00:00
|
|
|
InitConfig()
|
|
|
|
InitLogs()
|
|
|
|
InitRedisConfig()
|
|
|
|
InitMysql()
|
|
|
|
db.InitELK()
|
|
|
|
e := model.PortDocumentToElasticsearch("doc")
|
2020-11-10 16:05:17 +00:00
|
|
|
if nil != e {
|
2021-02-05 17:46:59 +00:00
|
|
|
log.Print(e.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestReflect(t *testing.T){
|
|
|
|
type XX struct{
|
|
|
|
a int16 `json:"bb"`
|
|
|
|
b string `json: "cc"`
|
2020-04-30 09:33:47 +00:00
|
|
|
}
|
2021-02-05 17:46:59 +00:00
|
|
|
x := utils.ReflectMakeNew(reflect.TypeOf(1212))
|
|
|
|
y := utils.ReflectMakeNew(reflect.TypeOf(XX{}))
|
|
|
|
|
|
|
|
log.Print(x)
|
|
|
|
log.Print(y)
|
|
|
|
remap := utils.ReflectTagMap(reflect.TypeOf(XX{}))
|
|
|
|
log.Print(remap)
|
|
|
|
t.Log(x)
|
2020-11-10 16:05:17 +00:00
|
|
|
}
|
2021-02-05 17:46:59 +00:00
|
|
|
|
|
|
|
func TestQueryDoc(t *testing.T){
|
|
|
|
InitConfig()
|
|
|
|
InitLogs()
|
|
|
|
InitRedisConfig()
|
|
|
|
InitMysql()
|
|
|
|
db.InitELK()
|
|
|
|
|
|
|
|
query := elastic.NewTermQuery("content", "title")
|
|
|
|
//_ = elastic.NewQueryStringQuery(fieldValue) //关键字查询
|
|
|
|
searchResult,titles,e := db.GetElastic().Query("doc",query,reflect.TypeOf(model.Doc{}),10,0)
|
|
|
|
if nil != e{
|
|
|
|
log.Print(e.Error())
|
|
|
|
}
|
|
|
|
log.Print(searchResult)
|
|
|
|
log.Print(titles)
|
|
|
|
}
|