util_test.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. package odb_test
  2. import (
  3. "fmt"
  4. "strconv"
  5. "testing"
  6. "time"
  7. "git.wecise.com/wecise/odbserver/odb"
  8. "git.wecise.com/wecise/odbserver/odb/facet"
  9. "git.wecise.com/wecise/odbserver/odb/notify"
  10. "git.wecise.com/wecise/odbserver/odb/stmt"
  11. "git.wecise.com/wecise/odbserver/odb/test"
  12. )
  13. func getTestGutil() (*odb.Gutil, error) {
  14. return test.TestG(), nil
  15. }
  16. func TestGutil_CreateObjectsFromjson(t *testing.T) {
  17. //g, e := getTestGutil()
  18. //if e != nil {
  19. // t.Fatal(e)
  20. //}
  21. //defer g.Close()
  22. //if e = initTestClass(g);e != nil {
  23. // t.Fatal(e)
  24. //}
  25. //
  26. //var testList = `[
  27. // {"test":"Tom", "class":"/matrix/test"},
  28. // {"test":"Jack", "class":"/matrix/test"},
  29. // {"test":"Alice", "class":"/matrix/test"},
  30. // {"test":"Billy", "class":"/matrix/test"},
  31. // {"test":"Bill", "class":"/matrix/test"},
  32. // {"test":"Jobs", "class":"/matrix/test"},
  33. // {"test":"T", "class":"/matrix/test"},
  34. // {"test":"CT", "class":"/matrix/test"},
  35. // {"test":"Allen", "class":"/matrix/test"},
  36. // {"test":"Coco", "class":"/matrix/test"}
  37. //]`
  38. /*if e = g.createObjectsFromJson(testList);e != nil {
  39. t.Fatal(e)
  40. } else {
  41. t.Log("Add object list by json success.")
  42. }
  43. if e = g.createObjectsFromJson(`{"test":"Test", "class":"/matrix/test"}`);e != nil {
  44. t.Fatal(e)
  45. } else {
  46. t.Log("Add object by json success.")
  47. }
  48. if e = g.createObjectsFromJson(`{"test":"Test"}`);e != nil {
  49. t.Log("No class.")
  50. }*/
  51. }
  52. /*
  53. func TestGutil_CreateObjectFromMap(t *testing.T) {
  54. g, e := getTestGutil()
  55. if e != nil {
  56. t.Fatal(e)
  57. }
  58. defer g.Close()
  59. // wait for trigger finish
  60. time.Sleep(1*time.Second)
  61. obj := map[string]interface{}{
  62. "class":"/matrix/devops/event",
  63. "inst":"system.log",
  64. "app":"cassandra",
  65. "host":"mxsvr221",
  66. "biz":"matrix",
  67. "src":"matrix",
  68. "msg":"test",
  69. "severity":"4",
  70. "status":0,
  71. }
  72. if e = g.createObjectFromMap(obj);e != nil {
  73. t.Fatal(e)
  74. }
  75. t.Log("Create object success.")
  76. // wait for trigger finish
  77. time.Sleep(30*time.Second)
  78. }*/
  79. /*func TestGutil_AddObjectRels(t *testing.T) {
  80. gutil, _ := getTestGutil()
  81. if err := gutil.addObjectRels("greg", "/matrix/entity/test", 0, "contain", []string{"dani"}, []string{"label option"}, true);err != nil {
  82. t.Fatal(err.Error())
  83. } else {
  84. t.Log("Add rels success.")
  85. }
  86. }*/
  87. func TestGutil_Search(t *testing.T) {
  88. gutil, err := getTestGutil()
  89. if err != nil {
  90. t.Fatal(err)
  91. }
  92. for i := 0; i < 1000; i++ {
  93. st := time.Now()
  94. ret, meta, err := gutil.Search(`#/matrix/portal/tools: | sort by seat asc`)
  95. if err != nil {
  96. t.Fatal(err)
  97. }
  98. et := time.Now()
  99. fmt.Println(i, len(ret), len(meta), et.Sub(st).Seconds())
  100. }
  101. }
  102. //func TestMergeMap(t *testing.T) {
  103. // oragin := map[string]interface{}{
  104. // "Name":"Tom",
  105. // "hobbies":[]string{"football", "runing"},
  106. // "footprint":[]string{"Beijing", "Shanghai"},
  107. // "other": map[string][]string{
  108. // "home":{"mother", "father"},
  109. // "company":{"Jack", "Lily"},
  110. // },
  111. // "score":map[string]float64{"math":96.5, "chinese":85.2},
  112. // "infos":map[string]string{"city":"Beijing", "age":"22"},
  113. // }
  114. // new := map[string]interface{}{
  115. // "Name":"Tom",
  116. // "hobbies":[]string{"football", "Fitness"},
  117. // "footprint":[]string{"Beijing", "Shanghai"},
  118. // "other": map[string][]string{
  119. // "home":{"mother", "father"},
  120. // "company":{"Jack", "Lily"},
  121. // },
  122. // "score":map[string]float64{"math":96.5, "chinese":85.2},
  123. // "infos":map[string]string{"city":"Beijing", "age":"22"},
  124. // }
  125. //}
  126. func TestGutil_Facets(t *testing.T) {
  127. // g, err := getTestGutil()
  128. // if err != nil {
  129. // t.Fatal(err)
  130. // }
  131. // Create data
  132. num := 10000
  133. data := make([]map[string]interface{}, num)
  134. for i := 0; i < num; i++ {
  135. id := "ID_" + strconv.FormatInt(int64(i+1), 10)
  136. n := i % 5
  137. //fmt.Printf("DocID: %s %d\n", id, n)
  138. d := map[string]interface{}{
  139. "id": id,
  140. "from": "marty.schoch@gmail.com 哈哈 中国" + strconv.Itoa(n),
  141. "body": "bleve indexing is easy",
  142. "dtype": n,
  143. "hobbies": []string{"football" + strconv.Itoa(n), "running" + strconv.Itoa(n)},
  144. "scores": map[string]string{"math" + strconv.Itoa(n): "100", "english": "99"},
  145. "birthday": time.Now().Add(-time.Hour * time.Duration(n) * 24),
  146. "age": (n + 1) * 10,
  147. }
  148. data[i] = d
  149. }
  150. var (
  151. childrenMin float64 = 0
  152. childrenMax float64 = 11
  153. youngMin float64 = 12
  154. youngMax float64 = 40
  155. )
  156. fields := []*stmt.FacetField{
  157. {
  158. Name: "from",
  159. Vtype: "text",
  160. Ftype: facet.FacetTypeString,
  161. Segment: false,
  162. Size: 6,
  163. },
  164. {
  165. Name: "dtype",
  166. Vtype: "int",
  167. Ftype: facet.FacetTypeString,
  168. Segment: false,
  169. Size: 3,
  170. },
  171. {
  172. Name: "hobbies",
  173. Vtype: "list",
  174. Ftype: facet.FacetTypeString,
  175. Segment: false,
  176. Size: 3,
  177. },
  178. {
  179. Name: "scores",
  180. Vtype: "map",
  181. Ftype: facet.FacetTypeString,
  182. Segment: false,
  183. Size: 3,
  184. },
  185. {
  186. Name: "age",
  187. Vtype: "int",
  188. Ftype: facet.FacetTypeNumber,
  189. Segment: false,
  190. Size: 3,
  191. NumberRanges: []*stmt.NumberRange{{Name: "children", Min: &childrenMin, Max: &childrenMax}, {Name: "young", Min: &youngMin, Max: &youngMax}},
  192. },
  193. {
  194. Name: "birthday",
  195. Vtype: "date",
  196. Ftype: facet.FacetTypeDate,
  197. Segment: false,
  198. Size: 3,
  199. DateRanges: []*stmt.DateRange{
  200. {
  201. Name: "2days",
  202. Start: time.Now().Add(-time.Hour * time.Duration(1) * 24),
  203. End: time.Now().Add(-time.Hour * time.Duration(0) * 24),
  204. },
  205. {
  206. Name: "3days",
  207. Start: time.Now().Add(-time.Hour * time.Duration(2) * 24),
  208. End: time.Now().Add(-time.Hour * time.Duration(1) * 24),
  209. },
  210. {
  211. Name: "5days",
  212. Start: time.Now().Add(-time.Hour * time.Duration(6) * 24),
  213. End: time.Now().Add(-time.Hour * time.Duration(5) * 24),
  214. },
  215. },
  216. },
  217. }
  218. result, err := facet.Facets(fields, data)
  219. if err != nil {
  220. t.Fatal(err)
  221. }
  222. for field, a := range result {
  223. t.Log(field + ":")
  224. for _, v := range a {
  225. t.Log(v)
  226. }
  227. }
  228. }
  229. func TestGutil_Facets2(t *testing.T) {
  230. g, err := getTestGutil()
  231. if err != nil {
  232. t.Fatal(err)
  233. }
  234. _, meta, err := g.Query(`select * from /matrix/devops/event/ facet vtime{"old":[,'-1 day'],"new":['-1 day',]}`)
  235. if err != nil {
  236. t.Fatal(err)
  237. }
  238. t.Log(meta)
  239. }
  240. func TestGutil_SchemaChangeNotify(t *testing.T) {
  241. g, err := getTestGutil()
  242. if err != nil {
  243. t.Fatal(err)
  244. }
  245. t.Log("Reset class")
  246. if _, _, err = g.Query(`drop class if exists /test_schema_notify`); err != nil {
  247. t.Fatal(err)
  248. }
  249. time.Sleep(time.Second)
  250. count := 10
  251. for i := 0; i < count; i++ {
  252. t.Logf("Test %d", i+1)
  253. class := `create class /test_schema_notify(
  254. testkey varchar,
  255. testcol varchar,
  256. keys(testkey),
  257. indexes(testkey)
  258. )`
  259. newClass := `create class /test_schema_notify(
  260. testkey varchar,
  261. testcol varchar,
  262. keys(testkey),
  263. indexes(testkey, testcol)
  264. )`
  265. // Class no index
  266. t.Log("Create class")
  267. if _, _, err = g.Query(class); err != nil {
  268. t.Fatal(err)
  269. }
  270. time.Sleep(time.Second)
  271. if _, _, err = g.Query(`select id from /test_schema_notify where testcol = 'v1'`); err == nil {
  272. t.Fatal("Should be query error, but query ok.")
  273. }
  274. t.Log("Drop class")
  275. if _, _, err = g.Query(`drop class /test_schema_notify`); err != nil {
  276. t.Fatal(err)
  277. }
  278. time.Sleep(time.Second)
  279. // Class index
  280. t.Log("Create new class")
  281. if _, _, err = g.Query(newClass); err != nil {
  282. t.Fatal(err)
  283. }
  284. time.Sleep(time.Second)
  285. if _, _, err = g.Query(`select id from /test_schema_notify where testcol = 'v1'`); err != nil {
  286. t.Fatal("Should be query ok, but query error.")
  287. }
  288. t.Log("Drop new class")
  289. if _, _, err = g.Query(`drop class /test_schema_notify`); err != nil {
  290. t.Fatal(err)
  291. }
  292. time.Sleep(time.Second)
  293. }
  294. }
  295. func TestGutil_GetCacheTypeName(t *testing.T) {
  296. Name := odb.GetCacheTypeName(notify.CacheAll)
  297. if Name != "all" {
  298. t.Errorf("Should be Name 'all', bug get '%s'", Name)
  299. }
  300. }