prepare_insert_batch.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package prepare_batch
  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 PrepareInsertBatchTests struct {
  13. Test *testing.T
  14. g *odb.Gutil
  15. }
  16. func PrepareInsertBatchTest(t *testing.T) {
  17. g := test.TestG()
  18. t.Run("InitG", func(t *testing.T) {
  19. test := &PrepareInsertBatchTests{Test: t, g: g}
  20. test.InitG()
  21. })
  22. t.Run("Class", func(t *testing.T) {
  23. test := &PrepareInsertBatchTests{Test: t, g: g}
  24. test.Class()
  25. })
  26. t.Run("Data", func(t *testing.T) {
  27. test := &PrepareInsertBatchTests{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 := &PrepareInsertBatchTests{Test: t, g: g}
  33. test.Valid()
  34. })
  35. }
  36. func (t *PrepareInsertBatchTests) InitG() {
  37. require.NotNil(t.Test, t.g)
  38. }
  39. func (t *PrepareInsertBatchTests) Class() {
  40. _, _, err := t.g.Query(`
  41. create class if not exists /test/batch (
  42. v_varchar varchar,
  43. v_text text,
  44. v_int int,
  45. v_bigint bigint,
  46. v_smalldouble double,
  47. v_double double,
  48. v_float float,
  49. v_bool bool,
  50. indexes(v_varchar, v_text, v_int, v_bigint, v_double, v_float, v_smalldouble, v_bool),
  51. keys(v_varchar , v_text)
  52. ) with ckeys=v_bool`)
  53. if err != nil {
  54. logger.Errorf("%v", err)
  55. }
  56. require.Nil(t.Test, err, fmt.Sprint(err))
  57. }
  58. func (t *PrepareInsertBatchTests) Data() {
  59. var err error
  60. stat, err := t.g.Prepare(`
  61. BEGIN BATCH
  62. insert into /test/batch (v_varchar, v_text, v_int, v_bigint, v_float, v_double, v_smalldouble, v_bool) values (?, ?, ?, ?, ?, ?, ?, ? );
  63. insert into /test/batch (v_varchar, v_text, v_int, v_bigint, v_float, v_double, v_smalldouble, v_bool) values (?, ?, ?, ?, ?, ?, ?, ? );
  64. insert into /test/batch (v_varchar, v_text, v_int, v_bigint, v_float, v_double, v_smalldouble, v_bool) values (?, ?, ?, ?, ?, ?, ?, ? );
  65. insert into /test/batch (v_varchar, v_text, v_int, v_bigint, v_float, v_double, v_smalldouble, v_bool) values (?, ?, ?, ?, ?, ?, ?, ? );
  66. insert into /test/batch (v_varchar, v_text, v_int, v_bigint, v_float, v_double, v_smalldouble, v_bool) values (?, ?, ?, ?, ?, ?, ?, ? );
  67. insert into /test/batch (v_varchar, v_text, v_int, v_bigint, v_float, v_double, v_smalldouble, v_bool) values (?, ?, ?, ?, ?, ?, ?, ? );
  68. insert into /test/batch (v_varchar, v_text, v_int, v_bigint, v_float, v_double, v_smalldouble, v_bool) values (?, ?, ?, ?, ?, ?, ?, ? );
  69. insert into /test/batch (v_varchar, v_text, v_int, v_bigint, v_float, v_double, v_smalldouble, v_bool) values (?, ?, ?, ?, ?, ?, ?, ? );
  70. insert into /test/batch (v_varchar, v_text, v_int, v_bigint, v_float, v_double, v_smalldouble, v_bool) values (?, ?, ?, ?, ?, ?, ?, ? );
  71. insert into /test/batch (v_varchar, v_text, v_int, v_bigint, v_float, v_double, v_smalldouble, v_bool) values (?, ?, ?, ?, ?, ?, ?, ? )
  72. END
  73. `)
  74. if err != nil {
  75. logger.Errorf("%v", err)
  76. }
  77. _, _, err = stat.Exec(
  78. "4006", "hello word 2003", 4006, 4006, 1.1, 1.1234567890123456, 1.123456, true,
  79. "40", "hello word 2003", 40, 40, 1.1, 1.1234567890123456, 1.123456, false,
  80. "400", "hello word 2003", 400, 400, 1.1, 1.1234567890123456, 1.123456, true,
  81. "74006", "hello word 2003", -4006, 4006, 1.1, 1.1234567890123456, 1.123456, false,
  82. "409", "hello word 2003", -40, 40, 1.1, 1.1234567890123456, 1.123456, true,
  83. "500", "hello word 2003", -400, 400, 1.1, 1.1234567890123456, 1.123456, false,
  84. "600", "hello word 2003", 370, 370, 1.1, 1.1234567890123456, 1.123456, true,
  85. "700", "hello word 2003", 580, 580, 1.1, 1.1234567890123456, 1.123456, true,
  86. "602", "hello word 2003", -370, -370, 1.1, 1.1234567890123456, 1.123456, true,
  87. "708", "hello word 2003", -580, -580, 1.1, 1.1234567890123456, 1.123456, true)
  88. require.Nil(t.Test, err, fmt.Sprint(err))
  89. }
  90. func (t *PrepareInsertBatchTests) Valid() {
  91. rtn, _, err := t.g.Query(`select * from /test/batch`)
  92. if err != nil {
  93. logger.Errorf("%v", err)
  94. }
  95. if assert.Nil(t.Test, err) {
  96. if assert.NotNil(t.Test, rtn) {
  97. assert.Equal(t.Test, 10, len(rtn), "they should be equal")
  98. }
  99. }
  100. }