package test import ( "background/config" "background/db" "background/logs" "background/model" "background/utils" "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) } 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) } func TestQueryDoc(t *testing.T) { InitConfig() InitLogs() InitRedisConfig() InitMysql() db.InitELK() query := elastic.NewTermQuery("content", "title") 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) } func TestChangeStructFieldThroughStruct(t *testing.T) { type XX struct { A int16 `json:"bb"` B string `json: "cc" xml:"test"` } }