batch_insert.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package batch
  2. import (
  3. "fmt"
  4. "testing"
  5. "time"
  6. //"fmt"
  7. . "git.wecise.com/wecise/odbserver/odb"
  8. "git.wecise.com/wecise/odbserver/odb/test"
  9. "gitee.com/wecisecode/util/logger"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/stretchr/testify/require"
  12. )
  13. type BatchInsertTests struct {
  14. Test *testing.T
  15. g *Gutil
  16. }
  17. func BatchInsertTest(t *testing.T) {
  18. g := test.TestG()
  19. t.Run("InitG", func(t *testing.T) {
  20. test := &BatchInsertTests{Test: t, g: g}
  21. test.InitG()
  22. })
  23. t.Run("Class", func(t *testing.T) {
  24. test := &BatchInsertTests{Test: t, g: g}
  25. test.Class()
  26. })
  27. t.Run("Data", func(t *testing.T) {
  28. test := &BatchInsertTests{Test: t, g: g}
  29. test.Data()
  30. })
  31. time.Sleep(time.Duration(2) * time.Second)
  32. t.Run("Valid", func(t *testing.T) {
  33. test := &BatchInsertTests{Test: t, g: g}
  34. test.Valid()
  35. })
  36. }
  37. func (t *BatchInsertTests) InitG() {
  38. require.NotNil(t.Test, t.g)
  39. }
  40. func (t *BatchInsertTests) Class() {
  41. _, _, err := t.g.Query(`
  42. create class if not exists /test/batch (
  43. v_varchar varchar,
  44. v_text text,
  45. v_int int,
  46. v_bigint bigint,
  47. v_smalldouble double,
  48. v_double double,
  49. v_float float,
  50. v_bool bool,
  51. indexes(v_varchar, v_text, v_int, v_bigint, v_double, v_float, v_smalldouble, v_bool),
  52. keys(v_varchar , v_text)
  53. ) with ckeys=v_bool`)
  54. if err != nil {
  55. logger.Errorf("%v", err)
  56. }
  57. require.Nil(t.Test, err, fmt.Sprint(err))
  58. }
  59. func (t *BatchInsertTests) Data() {
  60. var err error
  61. _, _, err = t.g.Query(`
  62. BEGIN BATCH
  63. insert into /test/batch (v_varchar, v_text, v_int, v_bigint, v_float, v_double, v_smalldouble, v_bool) values ('4006', 'hello word 2003', 4006, 4006, 1.1, 1.1234567890123456, 1.123456, true );
  64. insert into /test/batch (v_varchar, v_text, v_int, v_bigint, v_float, v_double, v_smalldouble, v_bool) values ('40', 'hello word 2003', 40, 40, 1.1, 1.1234567890123456, 1.123456, false );
  65. insert into /test/batch (v_varchar, v_text, v_int, v_bigint, v_float, v_double, v_smalldouble, v_bool) values ('400', 'hello word 2003', 400, 400, 1.1, 1.1234567890123456, 1.123456, true );
  66. insert into /test/batch (v_varchar, v_text, v_int, v_bigint, v_float, v_double, v_smalldouble, v_bool) values ('74006', 'hello word 2003', -4006, 4006, 1.1, 1.1234567890123456, 1.123456, false );
  67. insert into /test/batch (v_varchar, v_text, v_int, v_bigint, v_float, v_double, v_smalldouble, v_bool) values ('409', 'hello word 2003', -40, 40, 1.1, 1.1234567890123456, 1.123456, true );
  68. insert into /test/batch (v_varchar, v_text, v_int, v_bigint, v_float, v_double, v_smalldouble, v_bool) values ('500', 'hello word 2003', -400, 400, 1.1, 1.1234567890123456, 1.123456, false );
  69. insert into /test/batch (v_varchar, v_text, v_int, v_bigint, v_float, v_double, v_smalldouble, v_bool) values ('600', 'hello word 2003', 370, 370, 1.1, 1.1234567890123456, 1.123456, true );
  70. insert into /test/batch (v_varchar, v_text, v_int, v_bigint, v_float, v_double, v_smalldouble, v_bool) values ('700', 'hello word 2003', 580, 580, 1.1, 1.1234567890123456, 1.123456, true );
  71. insert into /test/batch (v_varchar, v_text, v_int, v_bigint, v_float, v_double, v_smalldouble, v_bool) values ('602', 'hello word 2003', -370, -370, 1.1, 1.1234567890123456, 1.123456, true );
  72. insert into /test/batch (v_varchar, v_text, v_int, v_bigint, v_float, v_double, v_smalldouble, v_bool) values ('708', 'hello word 2003', -580, -580, 1.1, 1.1234567890123456, 1.123456, true );
  73. END
  74. `)
  75. if err != nil {
  76. logger.Errorf("%v", err)
  77. }
  78. require.Nil(t.Test, err, fmt.Sprint(err))
  79. }
  80. func (t *BatchInsertTests) Valid() {
  81. rtn, _, err := t.g.Query(`select * from /test/batch`)
  82. if err != nil {
  83. logger.Errorf("%v", err)
  84. }
  85. if assert.Nil(t.Test, err) {
  86. if assert.NotNil(t.Test, rtn) {
  87. assert.Equal(t.Test, 10, len(rtn), "they should be equal")
  88. }
  89. }
  90. }