blog_backend_api/test/portData_test.go

128 lines
2.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package test
import (
"background/config"
"background/db"
"background/logs"
"background/model"
"background/utils"
"fmt"
"log"
"reflect"
"testing"
"github.com/olivere/elastic"
)
func InitConfig() {
e := config.Init("../user.yaml")
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
}
}
func InitElasticSearch() {
e := db.GetElastic().CreateIndex("hardware", model.HardwareTypeMapping())
if nil != e {
}
}
func InitLogs() {
logs.Init(config.GetLogConfig().Dir, config.GetLogConfig().File, config.GetLogConfig().Level, config.GetLogConfig().SaveFile)
}
// 从mysql数据自动导出数据到es从describe 表新建索引和mapping然后全表导出到es
// port data from mysql to es, it can describe table and create index with mapping ,then port all data to es.
func TestPortDocToElastic(t *testing.T) {
InitConfig()
InitLogs()
InitRedisConfig()
InitMysql()
db.InitELK()
e := model.PortDocumentToElasticsearch("doc")
if nil != e {
log.Print(e.Error())
}
}
func TestReflect(t *testing.T) {
type XX struct {
A int16 `json:"bb"`
B string `json: "cc" xml:"test"`
}
unmar := map[string]interface{}{
"bb": 2,
"cc": "hello",
}
xx := XX{}
utils.UnmarshalJson2Struct(&xx, unmar)
log.Print(xx)
}
// 测试新建索引,查看mapping是否生效
// test create new index,and check the mapping works
func TestCreateIndex(t *testing.T) {
InitConfig()
InitLogs()
InitRedisConfig()
InitMysql()
db.InitELK()
InitElasticSearch()
}
// 测试query doc
// test doc query works
func TestQueryDoc(t *testing.T) {
InitConfig()
InitLogs()
InitRedisConfig()
InitMysql()
db.InitELK()
query := elastic.NewTermQuery("title", "c")
x := []model.Doc{}
_, e := db.GetElastic().Query("doc", query, &x, 10, 0)
if nil != e {
log.Print(e.Error())
}
for _, v := range x {
log.Print(v.Title, " ", v.ID, " ", v.Type)
}
}
func TestChangeStructFieldThroughStruct(t *testing.T) {
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...)
a0.Set(val_arr1)
fmt.Println("a0 is ", a0)
fmt.Println("arr1 is ", arr1)
fmt.Println(cap(e0))
fmt.Println(cap(arr1))
}