| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326 |
- package odb_test
- import (
- "fmt"
- "strconv"
- "testing"
- "time"
- "git.wecise.com/wecise/odbserver/odb"
- "git.wecise.com/wecise/odbserver/odb/facet"
- "git.wecise.com/wecise/odbserver/odb/notify"
- "git.wecise.com/wecise/odbserver/odb/stmt"
- "git.wecise.com/wecise/odbserver/odb/test"
- )
- func getTestGutil() (*odb.Gutil, error) {
- return test.TestG(), nil
- }
- func TestGutil_CreateObjectsFromjson(t *testing.T) {
- //g, e := getTestGutil()
- //if e != nil {
- // t.Fatal(e)
- //}
- //defer g.Close()
- //if e = initTestClass(g);e != nil {
- // t.Fatal(e)
- //}
- //
- //var testList = `[
- // {"test":"Tom", "class":"/matrix/test"},
- // {"test":"Jack", "class":"/matrix/test"},
- // {"test":"Alice", "class":"/matrix/test"},
- // {"test":"Billy", "class":"/matrix/test"},
- // {"test":"Bill", "class":"/matrix/test"},
- // {"test":"Jobs", "class":"/matrix/test"},
- // {"test":"T", "class":"/matrix/test"},
- // {"test":"CT", "class":"/matrix/test"},
- // {"test":"Allen", "class":"/matrix/test"},
- // {"test":"Coco", "class":"/matrix/test"}
- //]`
- /*if e = g.createObjectsFromJson(testList);e != nil {
- t.Fatal(e)
- } else {
- t.Log("Add object list by json success.")
- }
- if e = g.createObjectsFromJson(`{"test":"Test", "class":"/matrix/test"}`);e != nil {
- t.Fatal(e)
- } else {
- t.Log("Add object by json success.")
- }
- if e = g.createObjectsFromJson(`{"test":"Test"}`);e != nil {
- t.Log("No class.")
- }*/
- }
- /*
- func TestGutil_CreateObjectFromMap(t *testing.T) {
- g, e := getTestGutil()
- if e != nil {
- t.Fatal(e)
- }
- defer g.Close()
- // wait for trigger finish
- time.Sleep(1*time.Second)
- obj := map[string]interface{}{
- "class":"/matrix/devops/event",
- "inst":"system.log",
- "app":"cassandra",
- "host":"mxsvr221",
- "biz":"matrix",
- "src":"matrix",
- "msg":"test",
- "severity":"4",
- "status":0,
- }
- if e = g.createObjectFromMap(obj);e != nil {
- t.Fatal(e)
- }
- t.Log("Create object success.")
- // wait for trigger finish
- time.Sleep(30*time.Second)
- }*/
- /*func TestGutil_AddObjectRels(t *testing.T) {
- gutil, _ := getTestGutil()
- if err := gutil.addObjectRels("greg", "/matrix/entity/test", 0, "contain", []string{"dani"}, []string{"label option"}, true);err != nil {
- t.Fatal(err.Error())
- } else {
- t.Log("Add rels success.")
- }
- }*/
- func TestGutil_Search(t *testing.T) {
- gutil, err := getTestGutil()
- if err != nil {
- t.Fatal(err)
- }
- for i := 0; i < 1000; i++ {
- st := time.Now()
- ret, meta, err := gutil.Search(`#/matrix/portal/tools: | sort by seat asc`)
- if err != nil {
- t.Fatal(err)
- }
- et := time.Now()
- fmt.Println(i, len(ret), len(meta), et.Sub(st).Seconds())
- }
- }
- //func TestMergeMap(t *testing.T) {
- // oragin := map[string]interface{}{
- // "Name":"Tom",
- // "hobbies":[]string{"football", "runing"},
- // "footprint":[]string{"Beijing", "Shanghai"},
- // "other": map[string][]string{
- // "home":{"mother", "father"},
- // "company":{"Jack", "Lily"},
- // },
- // "score":map[string]float64{"math":96.5, "chinese":85.2},
- // "infos":map[string]string{"city":"Beijing", "age":"22"},
- // }
- // new := map[string]interface{}{
- // "Name":"Tom",
- // "hobbies":[]string{"football", "Fitness"},
- // "footprint":[]string{"Beijing", "Shanghai"},
- // "other": map[string][]string{
- // "home":{"mother", "father"},
- // "company":{"Jack", "Lily"},
- // },
- // "score":map[string]float64{"math":96.5, "chinese":85.2},
- // "infos":map[string]string{"city":"Beijing", "age":"22"},
- // }
- //}
- func TestGutil_Facets(t *testing.T) {
- // g, err := getTestGutil()
- // if err != nil {
- // t.Fatal(err)
- // }
- // Create data
- num := 10000
- data := make([]map[string]interface{}, num)
- for i := 0; i < num; i++ {
- id := "ID_" + strconv.FormatInt(int64(i+1), 10)
- n := i % 5
- //fmt.Printf("DocID: %s %d\n", id, n)
- d := map[string]interface{}{
- "id": id,
- "from": "marty.schoch@gmail.com 哈哈 中国" + strconv.Itoa(n),
- "body": "bleve indexing is easy",
- "dtype": n,
- "hobbies": []string{"football" + strconv.Itoa(n), "running" + strconv.Itoa(n)},
- "scores": map[string]string{"math" + strconv.Itoa(n): "100", "english": "99"},
- "birthday": time.Now().Add(-time.Hour * time.Duration(n) * 24),
- "age": (n + 1) * 10,
- }
- data[i] = d
- }
- var (
- childrenMin float64 = 0
- childrenMax float64 = 11
- youngMin float64 = 12
- youngMax float64 = 40
- )
- fields := []*stmt.FacetField{
- {
- Name: "from",
- Vtype: "text",
- Ftype: facet.FacetTypeString,
- Segment: false,
- Size: 6,
- },
- {
- Name: "dtype",
- Vtype: "int",
- Ftype: facet.FacetTypeString,
- Segment: false,
- Size: 3,
- },
- {
- Name: "hobbies",
- Vtype: "list",
- Ftype: facet.FacetTypeString,
- Segment: false,
- Size: 3,
- },
- {
- Name: "scores",
- Vtype: "map",
- Ftype: facet.FacetTypeString,
- Segment: false,
- Size: 3,
- },
- {
- Name: "age",
- Vtype: "int",
- Ftype: facet.FacetTypeNumber,
- Segment: false,
- Size: 3,
- NumberRanges: []*stmt.NumberRange{{Name: "children", Min: &childrenMin, Max: &childrenMax}, {Name: "young", Min: &youngMin, Max: &youngMax}},
- },
- {
- Name: "birthday",
- Vtype: "date",
- Ftype: facet.FacetTypeDate,
- Segment: false,
- Size: 3,
- DateRanges: []*stmt.DateRange{
- {
- Name: "2days",
- Start: time.Now().Add(-time.Hour * time.Duration(1) * 24),
- End: time.Now().Add(-time.Hour * time.Duration(0) * 24),
- },
- {
- Name: "3days",
- Start: time.Now().Add(-time.Hour * time.Duration(2) * 24),
- End: time.Now().Add(-time.Hour * time.Duration(1) * 24),
- },
- {
- Name: "5days",
- Start: time.Now().Add(-time.Hour * time.Duration(6) * 24),
- End: time.Now().Add(-time.Hour * time.Duration(5) * 24),
- },
- },
- },
- }
- result, err := facet.Facets(fields, data)
- if err != nil {
- t.Fatal(err)
- }
- for field, a := range result {
- t.Log(field + ":")
- for _, v := range a {
- t.Log(v)
- }
- }
- }
- func TestGutil_Facets2(t *testing.T) {
- g, err := getTestGutil()
- if err != nil {
- t.Fatal(err)
- }
- _, meta, err := g.Query(`select * from /matrix/devops/event/ facet vtime{"old":[,'-1 day'],"new":['-1 day',]}`)
- if err != nil {
- t.Fatal(err)
- }
- t.Log(meta)
- }
- func TestGutil_SchemaChangeNotify(t *testing.T) {
- g, err := getTestGutil()
- if err != nil {
- t.Fatal(err)
- }
- t.Log("Reset class")
- if _, _, err = g.Query(`drop class if exists /test_schema_notify`); err != nil {
- t.Fatal(err)
- }
- time.Sleep(time.Second)
- count := 10
- for i := 0; i < count; i++ {
- t.Logf("Test %d", i+1)
- class := `create class /test_schema_notify(
- testkey varchar,
- testcol varchar,
- keys(testkey),
- indexes(testkey)
- )`
- newClass := `create class /test_schema_notify(
- testkey varchar,
- testcol varchar,
- keys(testkey),
- indexes(testkey, testcol)
- )`
- // Class no index
- t.Log("Create class")
- if _, _, err = g.Query(class); err != nil {
- t.Fatal(err)
- }
- time.Sleep(time.Second)
- if _, _, err = g.Query(`select id from /test_schema_notify where testcol = 'v1'`); err == nil {
- t.Fatal("Should be query error, but query ok.")
- }
- t.Log("Drop class")
- if _, _, err = g.Query(`drop class /test_schema_notify`); err != nil {
- t.Fatal(err)
- }
- time.Sleep(time.Second)
- // Class index
- t.Log("Create new class")
- if _, _, err = g.Query(newClass); err != nil {
- t.Fatal(err)
- }
- time.Sleep(time.Second)
- if _, _, err = g.Query(`select id from /test_schema_notify where testcol = 'v1'`); err != nil {
- t.Fatal("Should be query ok, but query error.")
- }
- t.Log("Drop new class")
- if _, _, err = g.Query(`drop class /test_schema_notify`); err != nil {
- t.Fatal(err)
- }
- time.Sleep(time.Second)
- }
- }
- func TestGutil_GetCacheTypeName(t *testing.T) {
- Name := odb.GetCacheTypeName(notify.CacheAll)
- if Name != "all" {
- t.Errorf("Should be Name 'all', bug get '%s'", Name)
- }
- }
|