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"
|
2021-02-07 09:21:19 +00:00
|
|
|
|
"fmt"
|
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())
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-02-07 09:21:19 +00:00
|
|
|
|
|
2020-04-30 09:33:47 +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
|
|
|
|
|
2020-04-30 09:33:47 +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 {
|
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() {
|
2023-09-26 16:32:29 +00:00
|
|
|
|
logs.Init(config.GetLogConfig().Dir,
|
|
|
|
|
config.GetLogConfig().File,
|
|
|
|
|
config.GetLogConfig().Level,
|
2021-05-22 17:27:55 +00:00
|
|
|
|
config.GetLogConfig().SaveFile)
|
2020-04-30 09:33:47 +00:00
|
|
|
|
}
|
2021-02-08 04:05:38 +00:00
|
|
|
|
|
|
|
|
|
// 从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.
|
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()
|
2023-09-26 16:32:29 +00:00
|
|
|
|
e := model.PortDocumentToElasticsearch("doc_copy1")
|
|
|
|
|
if nil != e {
|
|
|
|
|
log.Print(e.Error())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestPortDocTreeToElastic(t *testing.T) {
|
|
|
|
|
InitConfig()
|
|
|
|
|
InitLogs()
|
|
|
|
|
InitRedisConfig()
|
|
|
|
|
InitMysql()
|
|
|
|
|
db.InitELK()
|
|
|
|
|
e := model.PortDocumentToElasticsearch("doc_copy1")
|
2021-06-11 17:26:37 +00:00
|
|
|
|
if nil != e {
|
|
|
|
|
log.Print(e.Error())
|
|
|
|
|
}
|
2021-02-05 17:46:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
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"`
|
2020-04-30 09:33:47 +00:00
|
|
|
|
}
|
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-08 04:05:38 +00:00
|
|
|
|
// 测试新建索引,查看mapping是否生效
|
2021-07-20 16:14:05 +00:00
|
|
|
|
// // test create new index,and check the mapping works
|
|
|
|
|
// func TestCreateIndex(t *testing.T) {
|
|
|
|
|
// InitConfig()
|
|
|
|
|
// InitLogs()
|
|
|
|
|
// InitRedisConfig()
|
|
|
|
|
// InitMysql()
|
|
|
|
|
// db.InitELK()
|
|
|
|
|
// InitElasticSearch()
|
|
|
|
|
// }
|
2021-02-08 04:05:38 +00:00
|
|
|
|
|
|
|
|
|
// 测试query doc
|
|
|
|
|
// test doc query works
|
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()
|
|
|
|
|
|
2021-02-08 04:05:38 +00:00
|
|
|
|
query := elastic.NewTermQuery("title", "c")
|
2021-02-07 09:21:19 +00:00
|
|
|
|
x := []model.Doc{}
|
2021-02-08 04:05:38 +00:00
|
|
|
|
_, 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-08 04:05:38 +00:00
|
|
|
|
for _, v := range x {
|
2021-05-22 17:27:55 +00:00
|
|
|
|
t.Logf(v.Title, " ", v.ID, " ", v.Type)
|
2021-02-08 04:05:38 +00:00
|
|
|
|
}
|
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
|
|
|
|
}
|
2022-04-11 17:40:39 +00:00
|
|
|
|
|
|
|
|
|
func TestPortDocVersion(t *testing.T) {
|
|
|
|
|
InitConfig()
|
|
|
|
|
InitLogs()
|
|
|
|
|
InitRedisConfig()
|
|
|
|
|
InitMysql()
|
|
|
|
|
|
2022-04-12 17:24:39 +00:00
|
|
|
|
docs := []model.Doc{}
|
2022-04-13 16:38:16 +00:00
|
|
|
|
doc_groups := []model.DocGroup{}
|
2022-04-12 17:24:39 +00:00
|
|
|
|
doc_types := []model.DocType{}
|
2023-09-26 16:32:29 +00:00
|
|
|
|
e := db.GetMysqlClient().Query2("select * from doc_group", &doc_groups)
|
|
|
|
|
|
|
|
|
|
if nil != e {
|
|
|
|
|
log.Print(e.Error())
|
|
|
|
|
}
|
|
|
|
|
// for k,v := range doc_groups{
|
|
|
|
|
// log.Print(k,v)
|
|
|
|
|
// e := db.GetMysqlClient().Query2("select * from doc_group",&doc_groups)
|
|
|
|
|
// if(nil != e){
|
|
|
|
|
// log.Print(e.Error())
|
|
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
// str := fmt.Sprintf("insert into doc_copy1 (title,type,content,author,create_time,version,is_public, deleted,origin_url,es_id,father,level) values ('%s',0,'','admin','%s', 1,1,0,'','',0,0)",
|
|
|
|
|
// v.Name,time.Now().Format("2006-01-02 15:04:05"))
|
|
|
|
|
// log.Print(str)
|
|
|
|
|
// _,e = db.GetMysqlClient().Query(str)
|
|
|
|
|
// if nil != e{
|
|
|
|
|
// log.Print(e.Error())
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// e = db.GetMysqlClient().Query2("select * from doc_type",&doc_types)
|
|
|
|
|
// if(nil != e){
|
|
|
|
|
// log.Print(e.Error())
|
|
|
|
|
// }
|
|
|
|
|
// for k,v := range doc_types{
|
|
|
|
|
// log.Print(k,v)
|
|
|
|
|
// doc_group := []model.DocGroup{}
|
|
|
|
|
// e = db.GetMysqlClient().Query2(fmt.Sprintf("select * from doc_group where doc_group.int = '%d'",v.Group),&doc_group)
|
|
|
|
|
// if nil != e{
|
|
|
|
|
// log.Print(e.Error())
|
|
|
|
|
// }
|
|
|
|
|
// log.Print(doc_group[0])
|
|
|
|
|
// doc := []model.Doc{}
|
|
|
|
|
// e = db.GetMysqlClient().Query2(fmt.Sprintf("select * from doc_copy1 where doc_copy1.title = '%s' and doc_copy1.father=0",doc_group[0].Name),&doc)
|
|
|
|
|
// if nil != e{
|
|
|
|
|
// log.Print(e.Error())
|
|
|
|
|
// }
|
|
|
|
|
// log.Print(doc)
|
|
|
|
|
// str := fmt.Sprintf("insert into doc_copy1 (title,type,content,author,create_time,version,is_public, deleted,origin_url,es_id,father,level) values ('%s',0,'','admin','%s', 1,1,0,'','',%d,1)",
|
|
|
|
|
// v.TypeName,time.Now().Format("2006-01-02 15:04:05"),doc[0].ID)
|
|
|
|
|
// log.Print(str)
|
|
|
|
|
// _,e = db.GetMysqlClient().Query(str)
|
|
|
|
|
// if nil != e{
|
|
|
|
|
// log.Print(e.Error())
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
e = db.GetMysqlClient().Query2("select id,title,type from doc_copy1 where doc_copy1.father is NULL ", &docs)
|
|
|
|
|
if nil != e {
|
|
|
|
|
log.Print(e.Error())
|
|
|
|
|
}
|
|
|
|
|
for k, v := range docs {
|
|
|
|
|
log.Print(k, v)
|
|
|
|
|
|
|
|
|
|
e = db.GetMysqlClient().Query2(fmt.Sprintf("select * from doc_type where doc_type.id = %d ", v.Type), &doc_types)
|
|
|
|
|
if nil != e {
|
|
|
|
|
log.Print(e.Error())
|
|
|
|
|
}
|
|
|
|
|
log.Print(doc_types)
|
|
|
|
|
if len(doc_types) > 0 {
|
|
|
|
|
docsfortype := []model.Doc{}
|
|
|
|
|
e = db.GetMysqlClient().Query2(fmt.Sprintf("select * from doc_copy1 where doc_copy1.title = '%s' and doc_copy1.level = 1",
|
|
|
|
|
doc_types[0].TypeName), &docsfortype)
|
|
|
|
|
if nil != e {
|
|
|
|
|
log.Print(e.Error())
|
|
|
|
|
}
|
|
|
|
|
log.Print(docsfortype)
|
|
|
|
|
if len(docsfortype) > 0 {
|
|
|
|
|
str := fmt.Sprintf("update doc_copy1 set father = %d ,level = %d where doc_copy1.id = %d",
|
|
|
|
|
docsfortype[0].ID, docsfortype[0].Level+1, (v.ID))
|
|
|
|
|
log.Print(str)
|
|
|
|
|
_, e = db.GetMysqlClient().Query(str)
|
|
|
|
|
if nil != e {
|
|
|
|
|
log.Printf(e.Error())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestPortDocTree(t *testing.T) {
|
|
|
|
|
InitConfig()
|
|
|
|
|
InitLogs()
|
|
|
|
|
InitRedisConfig()
|
|
|
|
|
InitMysql()
|
|
|
|
|
|
|
|
|
|
docs := []model.Doc{}
|
|
|
|
|
doc_groups := []model.DocGroup{}
|
|
|
|
|
doc_types := []model.DocType{}
|
|
|
|
|
e := db.GetMysqlClient().Query2("select * from doc_group", &doc_groups)
|
2022-04-12 17:24:39 +00:00
|
|
|
|
|
2023-09-26 16:32:29 +00:00
|
|
|
|
if nil != e {
|
2022-04-13 16:38:16 +00:00
|
|
|
|
log.Print(e.Error())
|
|
|
|
|
}
|
2022-04-12 17:24:39 +00:00
|
|
|
|
// for k,v := range doc_groups{
|
|
|
|
|
// log.Print(k,v)
|
|
|
|
|
// e := db.GetMysqlClient().Query2("select * from doc_group",&doc_groups)
|
|
|
|
|
// if(nil != e){
|
|
|
|
|
// log.Print(e.Error())
|
2023-09-26 16:32:29 +00:00
|
|
|
|
|
2022-04-12 17:24:39 +00:00
|
|
|
|
// }
|
2022-04-13 16:38:16 +00:00
|
|
|
|
// str := fmt.Sprintf("insert into doc_copy1 (title,type,content,author,create_time,version,is_public, deleted,origin_url,es_id,father,level) values ('%s',0,'','admin','%s', 1,1,0,'','',0,0)",
|
|
|
|
|
// v.Name,time.Now().Format("2006-01-02 15:04:05"))
|
|
|
|
|
// log.Print(str)
|
|
|
|
|
// _,e = db.GetMysqlClient().Query(str)
|
|
|
|
|
// if nil != e{
|
|
|
|
|
// log.Print(e.Error())
|
|
|
|
|
// }
|
2022-04-12 17:24:39 +00:00
|
|
|
|
// }
|
|
|
|
|
// e = db.GetMysqlClient().Query2("select * from doc_type",&doc_types)
|
|
|
|
|
// if(nil != e){
|
|
|
|
|
// log.Print(e.Error())
|
|
|
|
|
// }
|
|
|
|
|
// for k,v := range doc_types{
|
|
|
|
|
// log.Print(k,v)
|
|
|
|
|
// doc_group := []model.DocGroup{}
|
|
|
|
|
// e = db.GetMysqlClient().Query2(fmt.Sprintf("select * from doc_group where doc_group.int = '%d'",v.Group),&doc_group)
|
|
|
|
|
// if nil != e{
|
|
|
|
|
// log.Print(e.Error())
|
|
|
|
|
// }
|
|
|
|
|
// log.Print(doc_group[0])
|
|
|
|
|
// doc := []model.Doc{}
|
2022-04-13 16:38:16 +00:00
|
|
|
|
// e = db.GetMysqlClient().Query2(fmt.Sprintf("select * from doc_copy1 where doc_copy1.title = '%s' and doc_copy1.father=0",doc_group[0].Name),&doc)
|
2022-04-12 17:24:39 +00:00
|
|
|
|
// if nil != e{
|
|
|
|
|
// log.Print(e.Error())
|
|
|
|
|
// }
|
|
|
|
|
// log.Print(doc)
|
2022-04-13 16:38:16 +00:00
|
|
|
|
// str := fmt.Sprintf("insert into doc_copy1 (title,type,content,author,create_time,version,is_public, deleted,origin_url,es_id,father,level) values ('%s',0,'','admin','%s', 1,1,0,'','',%d,1)",
|
|
|
|
|
// v.TypeName,time.Now().Format("2006-01-02 15:04:05"),doc[0].ID)
|
|
|
|
|
// log.Print(str)
|
|
|
|
|
// _,e = db.GetMysqlClient().Query(str)
|
|
|
|
|
// if nil != e{
|
|
|
|
|
// log.Print(e.Error())
|
2023-09-26 16:32:29 +00:00
|
|
|
|
// }
|
|
|
|
|
|
2022-04-12 17:24:39 +00:00
|
|
|
|
// }
|
2022-04-11 17:40:39 +00:00
|
|
|
|
|
2023-09-26 16:32:29 +00:00
|
|
|
|
e = db.GetMysqlClient().Query2("select id,title,type from doc_copy1 where doc_copy1.father is NULL ", &docs)
|
|
|
|
|
if nil != e {
|
2022-04-11 17:40:39 +00:00
|
|
|
|
log.Print(e.Error())
|
|
|
|
|
}
|
2023-09-26 16:32:29 +00:00
|
|
|
|
for k, v := range docs {
|
|
|
|
|
log.Print(k, v)
|
2022-04-12 17:24:39 +00:00
|
|
|
|
|
2023-09-26 16:32:29 +00:00
|
|
|
|
e = db.GetMysqlClient().Query2(fmt.Sprintf("select * from doc_type where doc_type.id = %d ", v.Type), &doc_types)
|
|
|
|
|
if nil != e {
|
2022-04-11 17:40:39 +00:00
|
|
|
|
log.Print(e.Error())
|
|
|
|
|
}
|
2022-04-12 17:24:39 +00:00
|
|
|
|
log.Print(doc_types)
|
2023-09-26 16:32:29 +00:00
|
|
|
|
if len(doc_types) > 0 {
|
2022-04-12 17:24:39 +00:00
|
|
|
|
docsfortype := []model.Doc{}
|
2022-04-13 16:38:16 +00:00
|
|
|
|
e = db.GetMysqlClient().Query2(fmt.Sprintf("select * from doc_copy1 where doc_copy1.title = '%s' and doc_copy1.level = 1",
|
2023-09-26 16:32:29 +00:00
|
|
|
|
doc_types[0].TypeName), &docsfortype)
|
|
|
|
|
if nil != e {
|
2022-04-12 17:24:39 +00:00
|
|
|
|
log.Print(e.Error())
|
|
|
|
|
}
|
2023-09-26 16:32:29 +00:00
|
|
|
|
log.Print(docsfortype)
|
|
|
|
|
if len(docsfortype) > 0 {
|
2022-04-13 16:38:16 +00:00
|
|
|
|
str := fmt.Sprintf("update doc_copy1 set father = %d ,level = %d where doc_copy1.id = %d",
|
2023-09-26 16:32:29 +00:00
|
|
|
|
docsfortype[0].ID, docsfortype[0].Level+1, (v.ID))
|
2022-04-12 17:24:39 +00:00
|
|
|
|
log.Print(str)
|
2023-09-26 16:32:29 +00:00
|
|
|
|
_, e = db.GetMysqlClient().Query(str)
|
|
|
|
|
if nil != e {
|
2022-04-13 16:38:16 +00:00
|
|
|
|
log.Printf(e.Error())
|
|
|
|
|
}
|
2022-04-12 17:24:39 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-04-11 17:40:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|