oo.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. package basic
  2. import (
  3. "fmt"
  4. "testing"
  5. "time"
  6. . "git.wecise.com/wecise/odbserver/odb"
  7. "git.wecise.com/wecise/odbserver/odb/test"
  8. "gitee.com/wecisecode/util/logger"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/require"
  11. )
  12. type OOTests struct {
  13. Test *testing.T
  14. g *Gutil
  15. }
  16. func OOTest(t *testing.T) {
  17. g := test.TestG()
  18. t.Run("InitG", func(t *testing.T) {
  19. test := &OOTests{Test: t, g: g}
  20. test.InitG()
  21. })
  22. t.Run("Class", func(t *testing.T) {
  23. test := &OOTests{Test: t, g: g}
  24. test.Class()
  25. })
  26. t.Run("Data", func(t *testing.T) {
  27. test := &OOTests{Test: t, g: g}
  28. test.Data()
  29. })
  30. time.Sleep(time.Duration(1) * time.Second)
  31. t.Run("Valid", func(t *testing.T) {
  32. test := &OOTests{Test: t, g: g}
  33. test.Valid()
  34. })
  35. t.Run("Search", func(t *testing.T) {
  36. test := &OOTests{Test: t, g: g}
  37. test.Search()
  38. })
  39. }
  40. func (t *OOTests) InitG() {
  41. require.NotNil(t.Test, t.g)
  42. }
  43. func (t *OOTests) Class() {
  44. // clear exists data
  45. t.g.Query(`
  46. delete from /test/oo with version;
  47. `)
  48. t.g.Query(`
  49. drop class if exists /test/oo;
  50. `)
  51. _, _, err := t.g.Query(`
  52. create class if not exists /test/oo();
  53. create class if not exists Dimension : /test/oo (
  54. llength int,
  55. wwidth int,
  56. bk2 bucket {
  57. "type" : "tsdb",
  58. "param" : ["iparam1","iparam2","fparam1","fparam2","sparam1","vparam1"],
  59. "ptype" : ["i","i","f","f","s","s"],
  60. "unit" : ["%","m","ms","ns","",""],
  61. "precision":[0,0,3,4,0,0],
  62. "ttl" : 365,
  63. "crc" : ["iparam1", "sparam1"]
  64. } 'full',
  65. indexes(llength, wwidth)
  66. ) with key=manu ;
  67. create class if not exists Chair : /test/oo (
  68. llength int,
  69. wwidth int,
  70. indexes(llength, wwidth)
  71. ) with key=manu ;
  72. create class if not exists Light : /test/oo (
  73. lnum int,
  74. indexes(lnum)
  75. ) with key=manu ;
  76. create class if not exists Wheel : /test/oo (
  77. dim Dimension,
  78. mymorph Morph<Chair, Light>[2],
  79. remark varchar,
  80. indexes(remark)
  81. ) with key=manu ;
  82. create class if not exists Car : /test/oo (
  83. wheel Wheel[4..4],
  84. remark varchar,
  85. indexes(remark)
  86. ) with key=manu ;
  87. create class if not exists Camry : Car () with key=manu ; `)
  88. if err != nil {
  89. logger.Errorf("%v", err)
  90. }
  91. require.Nil(t.Test, err, fmt.Sprint(err))
  92. //--------------------------
  93. // sub children
  94. //--------------------------
  95. _, _, err = t.g.Query(`alter class /test/oo/car add column bk2 bucket {
  96. "type" : "tsdb",
  97. "param" : ["iparam1","iparam2","fparam1","fparam2","sparam1","vparam1"],
  98. "ptype" : ["i","i","f","f","s","s"],
  99. "unit" : ["%","m","ms","ns","",""],
  100. "precision":[0,0,3,4,0,0],
  101. "ttl" : 365,
  102. "crc" : ["iparam1", "sparam1"]
  103. } 'full',
  104. wwheel Wheel[4..4] "hhh" `)
  105. if err != nil {
  106. logger.Errorf("%v", err)
  107. }
  108. require.Nil(t.Test, err, fmt.Sprint(err))
  109. }
  110. func (t *OOTests) Data() {
  111. var err error
  112. _, _, err = t.g.Query(`insert into /test/oo/car (id, wheel, remark ) values ('Car:car1', ['Wheel:wheel1', 'Wheel:wheel2', 'Wheel:wheel3', 'Wheel:wheel4'], 'car1')`)
  113. require.Nil(t.Test, err, err)
  114. _, _, err = t.g.Query(`insert into /test/oo/dimension (id, llength, wwidth ) values ('Dimension:dim1', 100, 50)`)
  115. require.Nil(t.Test, err, fmt.Sprint(err))
  116. for i := 1234567890; i < 1234567890+10; i++ {
  117. if i%2 == 0 {
  118. cql := fmt.Sprintf(`insert into /test/oo/dimension (id, bk2[ 'iparam1', 'iparam2', 'fparam1', 'fparam2', 'sparam1', 'vparam1']) values ('Dimension:dim1', [1, nil, 3.3, 6.5, '%d', 'abcdefghhhhh'] ) at '2020-07-24 10:24:17'`, i)
  119. _, _, err := t.g.Query(cql)
  120. if err != nil {
  121. logger.Errorf("%v", err)
  122. }
  123. } else {
  124. cql := fmt.Sprintf(`insert into /test/oo/dimension (id, bk2[ 'iparam1', 'iparam2', 'fparam1', 'fparam2', 'sparam1', 'vparam1']) values ('Dimension:dim1', [nil, nil, 3.3, 6.5, '%d', 'abcdefghhhhh'] ) at '2020-07-24 10:24:17'`, i)
  125. _, _, err := t.g.Query(cql)
  126. if err != nil {
  127. logger.Errorf("%v", err)
  128. }
  129. }
  130. }
  131. _, _, err = t.g.Query(`insert into /test/oo/Wheel (id, dim, mymorph, remark ) values ('Wheel:wheel1', 'Dimension:dim1', ['Chair:chair1', 'Light:light1'], 'wheel1')`)
  132. require.Nil(t.Test, err, fmt.Sprint(err))
  133. _, _, err = t.g.Query(`insert into /test/oo/Wheel (id, dim, mymorph, remark ) values ('Wheel:wheel2', 'Dimension:dim1', ['Chair:chair1', 'Light:light1'], 'wheel2')`)
  134. require.Nil(t.Test, err, fmt.Sprint(err))
  135. _, _, err = t.g.Query(`insert into /test/oo/Wheel (id, dim, mymorph, remark ) values ('Wheel:wheel3', 'Dimension:dim1', ['Chair:chair1', 'Light:light1'], 'wheel3')`)
  136. require.Nil(t.Test, err, fmt.Sprint(err))
  137. _, _, err = t.g.Query(`insert into /test/oo/Wheel (id, dim, mymorph, remark ) values ('Wheel:wheel4', 'Dimension:dim1', ['Chair:chair1', 'Light:light1'], 'wheel4')`)
  138. require.Nil(t.Test, err, fmt.Sprint(err))
  139. _, _, err = t.g.Query(`update /test/oo/car set wheel = wheel - ['Wheel:wheel2'] where id='Car:car1'`)
  140. require.Nil(t.Test, err, fmt.Sprint(err))
  141. }
  142. func (t *OOTests) Valid() {
  143. rtn, _, err := t.g.Query(`select dim['llength', 'wwidth'] from /test/oo/wheel`)
  144. require.Nil(t.Test, err, fmt.Sprint(err))
  145. if assert.NotNil(t.Test, rtn) {
  146. assert.Equal(t.Test, len(rtn), 4, "they should be equal")
  147. }
  148. rtn, _, err = t.g.Query(`select id from /test/oo/wheel where dim.llength >= 100`)
  149. require.Nil(t.Test, err, fmt.Sprint(err))
  150. logger.Info("===>", len(rtn))
  151. if assert.NotNil(t.Test, rtn) {
  152. assert.Equal(t.Test, len(rtn), 4, "they should be equal")
  153. }
  154. _, _, err = t.g.Query(`select id from /test/oo/wheel where dim.bk2.time("2020-05-15", "").sum(iparam1).groupby(sparam1).assert($2 > 0)`)
  155. require.Nil(t.Test, err, fmt.Sprint(err))
  156. rtn, _, err = t.g.Query(`select id from /test/oo/wheel where dim.bk2.time("2020-05-15", "").sum(iparam1).groupby(sparam1).assert($2 > 0)`)
  157. require.Nil(t.Test, err, fmt.Sprint(err))
  158. if assert.NotNil(t.Test, rtn) {
  159. assert.Equal(t.Test, len(rtn), 4, "they should be equal")
  160. }
  161. }
  162. func (t *OOTests) Search() {
  163. /*var err error
  164. var rtn []map[string]interface{}
  165. rtn,_,err = t.g.Query(`select count(*) as count from /test/oo where v_enum = 1`)
  166. if err !=nil {
  167. logger.Errorf("%v", err)
  168. }
  169. if assert.Nil(t.Test, err) {
  170. if assert.NotNil(t.Test, rtn) {
  171. assert.Equal(t.Test, int64(1), rtn[0]["count"].(int64), fmt.Sprintf("not find v_enum = %v", "1"))
  172. }
  173. }
  174. rtn,_,err = t.g.Query(`select count(*) as count from /test/oo where v_enum = 'network'`)
  175. if err !=nil {
  176. logger.Errorf("%v", err)
  177. }
  178. if assert.Nil(t.Test, err) {
  179. if assert.NotNil(t.Test, rtn) {
  180. assert.Equal(t.Test, int64(1), rtn[0]["count"].(int64), fmt.Sprintf("not find v_enum = %v", "network"))
  181. }
  182. }
  183. rtn,_,err = t.g.Query(`select count(*) as count from /test/oo where v_enum = '软件'`)
  184. if err !=nil {
  185. logger.Errorf("%v", err)
  186. }
  187. if assert.Nil(t.Test, err) {
  188. if assert.NotNil(t.Test, rtn) {
  189. assert.Equal(t.Test, int64(1), rtn[0]["count"].(int64), fmt.Sprintf("not find v_enum = %v", "软件"))
  190. }
  191. }*/
  192. }