background/test/portData_test.go

111 lines
2.1 KiB
Go
Raw Normal View History

package test
import (
"background/config"
"background/db"
"background/logs"
"background/model"
2021-02-05 17:46:59 +00:00
"background/utils"
2021-02-07 09:21:19 +00:00
"fmt"
"log"
2021-02-05 17:46:59 +00:00
"reflect"
"testing"
2021-02-05 17:46:59 +00:00
"github.com/olivere/elastic"
)
2020-11-10 16:05:17 +00:00
func InitConfig() {
2021-02-05 17:46:59 +00:00
e := config.Init("../user.yaml")
if nil != e {
log.Println(e.Error())
}
}
2021-02-07 09:21:19 +00:00
func InitMysql() {
c := config.GetMysqlConfig()
if c == nil {
logs.Error("cannnot connect mysql server")
} else {
db.Init()
}
}
2021-02-07 09:21:19 +00:00
func InitRedisConfig() {
e := config.InitRedis()
if nil != e {
logs.Error(e.Error())
return
}
}
2021-02-07 09:21:19 +00:00
2020-11-10 16:05:17 +00:00
func InitElasticSearch() {
e := db.GetElastic().CreateIndex("hardware", model.HardwareTypeMapping())
if nil != e {
}
}
2021-02-05 17:46:59 +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) {
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())
}
}
2021-02-07 05:14:01 +00:00
func TestReflect(t *testing.T) {
type XX struct {
A int16 `json:"bb"`
2021-02-06 16:45:59 +00:00
B string `json: "cc" xml:"test"`
}
2021-02-07 05:14:01 +00:00
unmar := map[string]interface{}{
"bb": 2,
2021-02-06 16:45:59 +00:00
"cc": "hello",
}
xx := XX{}
2021-02-07 05:14:01 +00:00
utils.UnmarshalJson2Struct(&xx, unmar)
2021-02-06 16:45:59 +00:00
log.Print(xx)
2020-11-10 16:05:17 +00:00
}
2021-02-05 17:46:59 +00:00
2021-02-07 05:14:01 +00:00
func TestQueryDoc(t *testing.T) {
2021-02-05 17:46:59 +00:00
InitConfig()
InitLogs()
InitRedisConfig()
InitMysql()
db.InitELK()
query := elastic.NewTermQuery("content", "title")
2021-02-07 09:21:19 +00:00
x := []model.Doc{}
titles, e := db.GetElastic().Query("doc", query, &x, 10, 0)
2021-02-07 05:14:01 +00:00
if nil != e {
2021-02-05 17:46:59 +00:00
log.Print(e.Error())
}
2021-02-07 09:21:19 +00:00
log.Print(x)
2021-02-05 17:46:59 +00:00
log.Print(titles)
2021-02-07 05:14:01 +00:00
}
func TestChangeStructFieldThroughStruct(t *testing.T) {
2021-02-07 09:21:19 +00:00
var arr1 = []int{1, 2, 3}
log.Print(reflect.ValueOf(&arr1).Kind())
a0 := reflect.ValueOf(&arr1).Elem()
log.Print(a0.Kind())
e0 := make([]reflect.Value, 0)
e0 = append(e0, reflect.ValueOf(100))
e0 = append(e0, reflect.ValueOf(200))
e0 = append(e0, reflect.ValueOf(300))
e0 = append(e0, reflect.ValueOf(400))
val_arr1 := reflect.Append(a0, e0...)
2021-02-07 05:14:01 +00:00
2021-02-07 09:21:19 +00:00
a0.Set(val_arr1)
fmt.Println("a0 is ", a0)
fmt.Println("arr1 is ", arr1)
fmt.Println(cap(e0))
fmt.Println(cap(arr1))
2021-02-07 05:14:01 +00:00
}