| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- package basic
- import (
- "fmt"
- "testing"
- "time"
- . "git.wecise.com/wecise/odbserver/odb"
- "git.wecise.com/wecise/odbserver/odb/test"
- "gitee.com/wecisecode/util/logger"
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
- )
- type OOTests struct {
- Test *testing.T
- g *Gutil
- }
- func OOTest(t *testing.T) {
- g := test.TestG()
- t.Run("InitG", func(t *testing.T) {
- test := &OOTests{Test: t, g: g}
- test.InitG()
- })
- t.Run("Class", func(t *testing.T) {
- test := &OOTests{Test: t, g: g}
- test.Class()
- })
- t.Run("Data", func(t *testing.T) {
- test := &OOTests{Test: t, g: g}
- test.Data()
- })
- time.Sleep(time.Duration(1) * time.Second)
- t.Run("Valid", func(t *testing.T) {
- test := &OOTests{Test: t, g: g}
- test.Valid()
- })
- t.Run("Search", func(t *testing.T) {
- test := &OOTests{Test: t, g: g}
- test.Search()
- })
- }
- func (t *OOTests) InitG() {
- require.NotNil(t.Test, t.g)
- }
- func (t *OOTests) Class() {
- // clear exists data
- t.g.Query(`
- delete from /test/oo with version;
- `)
- t.g.Query(`
- drop class if exists /test/oo;
- `)
- _, _, err := t.g.Query(`
- create class if not exists /test/oo();
- create class if not exists Dimension : /test/oo (
- llength int,
- wwidth int,
-
- bk2 bucket {
- "type" : "tsdb",
- "param" : ["iparam1","iparam2","fparam1","fparam2","sparam1","vparam1"],
- "ptype" : ["i","i","f","f","s","s"],
- "unit" : ["%","m","ms","ns","",""],
- "precision":[0,0,3,4,0,0],
- "ttl" : 365,
- "crc" : ["iparam1", "sparam1"]
- } 'full',
-
- indexes(llength, wwidth)
- ) with key=manu ;
-
- create class if not exists Chair : /test/oo (
- llength int,
- wwidth int,
- indexes(llength, wwidth)
- ) with key=manu ;
-
- create class if not exists Light : /test/oo (
- lnum int,
- indexes(lnum)
- ) with key=manu ;
-
- create class if not exists Wheel : /test/oo (
-
- dim Dimension,
- mymorph Morph<Chair, Light>[2],
- remark varchar,
-
- indexes(remark)
- ) with key=manu ;
-
- create class if not exists Car : /test/oo (
-
- wheel Wheel[4..4],
- remark varchar,
-
- indexes(remark)
- ) with key=manu ;
-
- create class if not exists Camry : Car () with key=manu ; `)
- if err != nil {
- logger.Errorf("%v", err)
- }
- require.Nil(t.Test, err, fmt.Sprint(err))
- //--------------------------
- // sub children
- //--------------------------
- _, _, err = t.g.Query(`alter class /test/oo/car add column bk2 bucket {
- "type" : "tsdb",
- "param" : ["iparam1","iparam2","fparam1","fparam2","sparam1","vparam1"],
- "ptype" : ["i","i","f","f","s","s"],
- "unit" : ["%","m","ms","ns","",""],
- "precision":[0,0,3,4,0,0],
- "ttl" : 365,
- "crc" : ["iparam1", "sparam1"]
- } 'full',
-
- wwheel Wheel[4..4] "hhh" `)
- if err != nil {
- logger.Errorf("%v", err)
- }
- require.Nil(t.Test, err, fmt.Sprint(err))
- }
- func (t *OOTests) Data() {
- var err error
- _, _, err = t.g.Query(`insert into /test/oo/car (id, wheel, remark ) values ('Car:car1', ['Wheel:wheel1', 'Wheel:wheel2', 'Wheel:wheel3', 'Wheel:wheel4'], 'car1')`)
- require.Nil(t.Test, err, err)
- _, _, err = t.g.Query(`insert into /test/oo/dimension (id, llength, wwidth ) values ('Dimension:dim1', 100, 50)`)
- require.Nil(t.Test, err, fmt.Sprint(err))
- for i := 1234567890; i < 1234567890+10; i++ {
- if i%2 == 0 {
- 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)
- _, _, err := t.g.Query(cql)
- if err != nil {
- logger.Errorf("%v", err)
- }
- } else {
- 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)
- _, _, err := t.g.Query(cql)
- if err != nil {
- logger.Errorf("%v", err)
- }
- }
- }
- _, _, err = t.g.Query(`insert into /test/oo/Wheel (id, dim, mymorph, remark ) values ('Wheel:wheel1', 'Dimension:dim1', ['Chair:chair1', 'Light:light1'], 'wheel1')`)
- require.Nil(t.Test, err, fmt.Sprint(err))
- _, _, err = t.g.Query(`insert into /test/oo/Wheel (id, dim, mymorph, remark ) values ('Wheel:wheel2', 'Dimension:dim1', ['Chair:chair1', 'Light:light1'], 'wheel2')`)
- require.Nil(t.Test, err, fmt.Sprint(err))
- _, _, err = t.g.Query(`insert into /test/oo/Wheel (id, dim, mymorph, remark ) values ('Wheel:wheel3', 'Dimension:dim1', ['Chair:chair1', 'Light:light1'], 'wheel3')`)
- require.Nil(t.Test, err, fmt.Sprint(err))
- _, _, err = t.g.Query(`insert into /test/oo/Wheel (id, dim, mymorph, remark ) values ('Wheel:wheel4', 'Dimension:dim1', ['Chair:chair1', 'Light:light1'], 'wheel4')`)
- require.Nil(t.Test, err, fmt.Sprint(err))
- _, _, err = t.g.Query(`update /test/oo/car set wheel = wheel - ['Wheel:wheel2'] where id='Car:car1'`)
- require.Nil(t.Test, err, fmt.Sprint(err))
- }
- func (t *OOTests) Valid() {
- rtn, _, err := t.g.Query(`select dim['llength', 'wwidth'] from /test/oo/wheel`)
- require.Nil(t.Test, err, fmt.Sprint(err))
- if assert.NotNil(t.Test, rtn) {
- assert.Equal(t.Test, len(rtn), 4, "they should be equal")
- }
- rtn, _, err = t.g.Query(`select id from /test/oo/wheel where dim.llength >= 100`)
- require.Nil(t.Test, err, fmt.Sprint(err))
- logger.Info("===>", len(rtn))
- if assert.NotNil(t.Test, rtn) {
- assert.Equal(t.Test, len(rtn), 4, "they should be equal")
- }
- _, _, err = t.g.Query(`select id from /test/oo/wheel where dim.bk2.time("2020-05-15", "").sum(iparam1).groupby(sparam1).assert($2 > 0)`)
- require.Nil(t.Test, err, fmt.Sprint(err))
- 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)`)
- require.Nil(t.Test, err, fmt.Sprint(err))
- if assert.NotNil(t.Test, rtn) {
- assert.Equal(t.Test, len(rtn), 4, "they should be equal")
- }
- }
- func (t *OOTests) Search() {
- /*var err error
- var rtn []map[string]interface{}
- rtn,_,err = t.g.Query(`select count(*) as count from /test/oo where v_enum = 1`)
- if err !=nil {
- logger.Errorf("%v", err)
- }
- if assert.Nil(t.Test, err) {
- if assert.NotNil(t.Test, rtn) {
- assert.Equal(t.Test, int64(1), rtn[0]["count"].(int64), fmt.Sprintf("not find v_enum = %v", "1"))
- }
- }
- rtn,_,err = t.g.Query(`select count(*) as count from /test/oo where v_enum = 'network'`)
- if err !=nil {
- logger.Errorf("%v", err)
- }
- if assert.Nil(t.Test, err) {
- if assert.NotNil(t.Test, rtn) {
- assert.Equal(t.Test, int64(1), rtn[0]["count"].(int64), fmt.Sprintf("not find v_enum = %v", "network"))
- }
- }
- rtn,_,err = t.g.Query(`select count(*) as count from /test/oo where v_enum = '软件'`)
- if err !=nil {
- logger.Errorf("%v", err)
- }
- if assert.Nil(t.Test, err) {
- if assert.NotNil(t.Test, rtn) {
- assert.Equal(t.Test, int64(1), rtn[0]["count"].(int64), fmt.Sprintf("not find v_enum = %v", "软件"))
- }
- }*/
- }
|