github.com/matrixorigin/matrixone@v1.2.0/pkg/frontend/resultset_test.go (about)

     1  // Copyright 2022 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package frontend
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	"github.com/smartystreets/goconvey/convey"
    22  
    23  	"github.com/matrixorigin/matrixone/pkg/container/types"
    24  )
    25  
    26  func Test_GetInt64(t *testing.T) {
    27  	var ret int64
    28  	var err error
    29  	var colNum = 16
    30  	convey.Convey("GetInt64 succ", t, func() {
    31  		mrs := &MysqlResultSet{}
    32  		mrs.Data = make([][]interface{}, colNum)
    33  		for i := 0; i < colNum; i++ {
    34  			mrs.Data[i] = make([]interface{}, 1)
    35  		}
    36  		ret, err = mrs.GetInt64(context.TODO(), 1, 0)
    37  		convey.So(ret, convey.ShouldEqual, 0)
    38  		convey.So(err, convey.ShouldNotBeNil)
    39  
    40  		ret, err = mrs.GetInt64(context.TODO(), 0, 0)
    41  		convey.So(ret, convey.ShouldEqual, 0)
    42  		convey.So(err, convey.ShouldNotBeNil)
    43  
    44  		mrs.Data[0][0] = true
    45  		mrs.Columns = make([]Column, colNum)
    46  		ret, err = mrs.GetInt64(context.TODO(), 0, 0)
    47  		convey.So(ret, convey.ShouldEqual, 1)
    48  		convey.So(err, convey.ShouldBeNil)
    49  
    50  		mrs.Data[0][0] = false
    51  		ret, err = mrs.GetInt64(context.TODO(), 0, 0)
    52  		convey.So(ret, convey.ShouldEqual, 0)
    53  		convey.So(err, convey.ShouldBeNil)
    54  
    55  		mrs.Data[1][0] = uint8(1)
    56  		ret, err = mrs.GetInt64(context.TODO(), 1, 0)
    57  		convey.So(ret, convey.ShouldEqual, 1)
    58  		convey.So(err, convey.ShouldBeNil)
    59  
    60  		mrs.Data[2][0] = uint16(2)
    61  		ret, err = mrs.GetInt64(context.TODO(), 2, 0)
    62  		convey.So(ret, convey.ShouldEqual, 2)
    63  		convey.So(err, convey.ShouldBeNil)
    64  
    65  		mrs.Data[3][0] = uint32(3)
    66  		ret, err = mrs.GetInt64(context.TODO(), 3, 0)
    67  		convey.So(ret, convey.ShouldEqual, 3)
    68  		convey.So(err, convey.ShouldBeNil)
    69  
    70  		mrs.Data[4][0] = uint64(4)
    71  		ret, err = mrs.GetInt64(context.TODO(), 4, 0)
    72  		convey.So(ret, convey.ShouldEqual, 4)
    73  		convey.So(err, convey.ShouldBeNil)
    74  
    75  		mrs.Data[5][0] = int8(5)
    76  		ret, err = mrs.GetInt64(context.TODO(), 5, 0)
    77  		convey.So(ret, convey.ShouldEqual, 5)
    78  		convey.So(err, convey.ShouldBeNil)
    79  
    80  		mrs.Data[6][0] = int16(6)
    81  		ret, err = mrs.GetInt64(context.TODO(), 6, 0)
    82  		convey.So(ret, convey.ShouldEqual, 6)
    83  		convey.So(err, convey.ShouldBeNil)
    84  
    85  		mrs.Data[7][0] = int32(7)
    86  		ret, err = mrs.GetInt64(context.TODO(), 7, 0)
    87  		convey.So(ret, convey.ShouldEqual, 7)
    88  		convey.So(err, convey.ShouldBeNil)
    89  
    90  		mrs.Data[8][0] = int64(8)
    91  		ret, err = mrs.GetInt64(context.TODO(), 8, 0)
    92  		convey.So(ret, convey.ShouldEqual, 8)
    93  		convey.So(err, convey.ShouldBeNil)
    94  
    95  		mrs.Data[9][0] = float32(9)
    96  		ret, err = mrs.GetInt64(context.TODO(), 9, 0)
    97  		convey.So(ret, convey.ShouldEqual, 9)
    98  		convey.So(err, convey.ShouldBeNil)
    99  
   100  		mrs.Data[10][0] = float64(10)
   101  		ret, err = mrs.GetInt64(context.TODO(), 10, 0)
   102  		convey.So(ret, convey.ShouldEqual, 10)
   103  		convey.So(err, convey.ShouldBeNil)
   104  
   105  		mrs.Data[11][0] = "11"
   106  		ret, err = mrs.GetInt64(context.TODO(), 11, 0)
   107  		convey.So(ret, convey.ShouldEqual, 11)
   108  		convey.So(err, convey.ShouldBeNil)
   109  
   110  		mrs.Data[12][0] = []byte("12")
   111  		ret, err = mrs.GetInt64(context.TODO(), 12, 0)
   112  		convey.So(ret, convey.ShouldEqual, 12)
   113  		convey.So(err, convey.ShouldBeNil)
   114  
   115  		mrs.Data[13][0] = int(13)
   116  		ret, err = mrs.GetInt64(context.TODO(), 13, 0)
   117  		convey.So(ret, convey.ShouldEqual, 13)
   118  		convey.So(err, convey.ShouldBeNil)
   119  
   120  		mrs.Data[14][0] = uint(14)
   121  		ret, err = mrs.GetInt64(context.TODO(), 14, 0)
   122  		convey.So(ret, convey.ShouldEqual, 14)
   123  		convey.So(err, convey.ShouldBeNil)
   124  
   125  		mrs.Data[15][0] = types.Decimal64(15)
   126  		ret, err = mrs.GetInt64(context.TODO(), 15, 0)
   127  		convey.So(ret, convey.ShouldEqual, 0)
   128  		convey.So(err, convey.ShouldNotBeNil)
   129  	})
   130  }
   131  
   132  func Test_GetUint64(t *testing.T) {
   133  	var ret uint64
   134  	var err error
   135  	var colNum = 16
   136  	convey.Convey("GetUint64 succ", t, func() {
   137  		mrs := &MysqlResultSet{}
   138  		mrs.Data = make([][]interface{}, colNum)
   139  		for i := 0; i < colNum; i++ {
   140  			mrs.Data[i] = make([]interface{}, 1)
   141  		}
   142  		ret, err = mrs.GetUint64(context.TODO(), 1, 0)
   143  		convey.So(ret, convey.ShouldEqual, 0)
   144  		convey.So(err, convey.ShouldNotBeNil)
   145  
   146  		ret, err = mrs.GetUint64(context.TODO(), 0, 0)
   147  		convey.So(ret, convey.ShouldEqual, 0)
   148  		convey.So(err, convey.ShouldNotBeNil)
   149  
   150  		mrs.Data[0][0] = true
   151  		mrs.Columns = make([]Column, colNum)
   152  		ret, err = mrs.GetUint64(context.TODO(), 0, 0)
   153  		convey.So(ret, convey.ShouldEqual, 1)
   154  		convey.So(err, convey.ShouldBeNil)
   155  
   156  		mrs.Data[0][0] = false
   157  		ret, err = mrs.GetUint64(context.TODO(), 0, 0)
   158  		convey.So(ret, convey.ShouldEqual, 0)
   159  		convey.So(err, convey.ShouldBeNil)
   160  
   161  		mrs.Data[1][0] = uint8(1)
   162  		ret, err = mrs.GetUint64(context.TODO(), 1, 0)
   163  		convey.So(ret, convey.ShouldEqual, 1)
   164  		convey.So(err, convey.ShouldBeNil)
   165  
   166  		mrs.Data[2][0] = uint16(2)
   167  		ret, err = mrs.GetUint64(context.TODO(), 2, 0)
   168  		convey.So(ret, convey.ShouldEqual, 2)
   169  		convey.So(err, convey.ShouldBeNil)
   170  
   171  		mrs.Data[3][0] = uint32(3)
   172  		ret, err = mrs.GetUint64(context.TODO(), 3, 0)
   173  		convey.So(ret, convey.ShouldEqual, 3)
   174  		convey.So(err, convey.ShouldBeNil)
   175  
   176  		mrs.Data[4][0] = uint64(4)
   177  		ret, err = mrs.GetUint64(context.TODO(), 4, 0)
   178  		convey.So(ret, convey.ShouldEqual, 4)
   179  		convey.So(err, convey.ShouldBeNil)
   180  
   181  		mrs.Data[5][0] = int8(5)
   182  		ret, err = mrs.GetUint64(context.TODO(), 5, 0)
   183  		convey.So(ret, convey.ShouldEqual, 5)
   184  		convey.So(err, convey.ShouldBeNil)
   185  
   186  		mrs.Data[6][0] = int16(6)
   187  		ret, err = mrs.GetUint64(context.TODO(), 6, 0)
   188  		convey.So(ret, convey.ShouldEqual, 6)
   189  		convey.So(err, convey.ShouldBeNil)
   190  
   191  		mrs.Data[7][0] = int32(7)
   192  		ret, err = mrs.GetUint64(context.TODO(), 7, 0)
   193  		convey.So(ret, convey.ShouldEqual, 7)
   194  		convey.So(err, convey.ShouldBeNil)
   195  
   196  		mrs.Data[8][0] = int64(8)
   197  		ret, err = mrs.GetUint64(context.TODO(), 8, 0)
   198  		convey.So(ret, convey.ShouldEqual, 8)
   199  		convey.So(err, convey.ShouldBeNil)
   200  
   201  		mrs.Data[9][0] = float32(9)
   202  		ret, err = mrs.GetUint64(context.TODO(), 9, 0)
   203  		convey.So(ret, convey.ShouldEqual, 9)
   204  		convey.So(err, convey.ShouldBeNil)
   205  
   206  		mrs.Data[10][0] = float64(10)
   207  		ret, err = mrs.GetUint64(context.TODO(), 10, 0)
   208  		convey.So(ret, convey.ShouldEqual, 10)
   209  		convey.So(err, convey.ShouldBeNil)
   210  
   211  		mrs.Data[11][0] = "11"
   212  		ret, err = mrs.GetUint64(context.TODO(), 11, 0)
   213  		convey.So(ret, convey.ShouldEqual, 11)
   214  		convey.So(err, convey.ShouldBeNil)
   215  
   216  		mrs.Data[12][0] = []byte("12")
   217  		ret, err = mrs.GetUint64(context.TODO(), 12, 0)
   218  		convey.So(ret, convey.ShouldEqual, 12)
   219  		convey.So(err, convey.ShouldBeNil)
   220  
   221  		mrs.Data[13][0] = int(13)
   222  		ret, err = mrs.GetUint64(context.TODO(), 13, 0)
   223  		convey.So(ret, convey.ShouldEqual, 13)
   224  		convey.So(err, convey.ShouldBeNil)
   225  
   226  		mrs.Data[14][0] = uint(14)
   227  		ret, err = mrs.GetUint64(context.TODO(), 14, 0)
   228  		convey.So(ret, convey.ShouldEqual, 14)
   229  		convey.So(err, convey.ShouldBeNil)
   230  
   231  		mrs.Data[15][0] = types.Decimal64(15)
   232  		ret, err = mrs.GetUint64(context.TODO(), 15, 0)
   233  		convey.So(ret, convey.ShouldEqual, 0)
   234  		convey.So(err, convey.ShouldNotBeNil)
   235  	})
   236  }
   237  
   238  func Test_GetFloat64(t *testing.T) {
   239  	var ret float64
   240  	var err error
   241  	var colNum = 16
   242  	convey.Convey("GetFloat64 succ", t, func() {
   243  		mrs := &MysqlResultSet{}
   244  		mrs.Data = make([][]interface{}, colNum)
   245  		for i := 0; i < colNum; i++ {
   246  			mrs.Data[i] = make([]interface{}, 1)
   247  		}
   248  		ret, err = mrs.GetFloat64(context.TODO(), 1, 0)
   249  		convey.So(ret, convey.ShouldEqual, 0)
   250  		convey.So(err, convey.ShouldNotBeNil)
   251  
   252  		ret, err = mrs.GetFloat64(context.TODO(), 0, 0)
   253  		convey.So(ret, convey.ShouldEqual, 0)
   254  		convey.So(err, convey.ShouldNotBeNil)
   255  
   256  		mrs.Data[0][0] = true
   257  		mrs.Columns = make([]Column, colNum)
   258  		ret, err = mrs.GetFloat64(context.TODO(), 0, 0)
   259  		convey.So(ret, convey.ShouldEqual, 1)
   260  		convey.So(err, convey.ShouldBeNil)
   261  
   262  		mrs.Data[0][0] = false
   263  		ret, err = mrs.GetFloat64(context.TODO(), 0, 0)
   264  		convey.So(ret, convey.ShouldEqual, 0)
   265  		convey.So(err, convey.ShouldBeNil)
   266  
   267  		mrs.Data[1][0] = uint8(1)
   268  		ret, err = mrs.GetFloat64(context.TODO(), 1, 0)
   269  		convey.So(ret, convey.ShouldEqual, 1)
   270  		convey.So(err, convey.ShouldBeNil)
   271  
   272  		mrs.Data[2][0] = uint16(2)
   273  		ret, err = mrs.GetFloat64(context.TODO(), 2, 0)
   274  		convey.So(ret, convey.ShouldEqual, 2)
   275  		convey.So(err, convey.ShouldBeNil)
   276  
   277  		mrs.Data[3][0] = uint32(3)
   278  		ret, err = mrs.GetFloat64(context.TODO(), 3, 0)
   279  		convey.So(ret, convey.ShouldEqual, 3)
   280  		convey.So(err, convey.ShouldBeNil)
   281  
   282  		mrs.Data[4][0] = uint64(4)
   283  		ret, err = mrs.GetFloat64(context.TODO(), 4, 0)
   284  		convey.So(ret, convey.ShouldEqual, 4)
   285  		convey.So(err, convey.ShouldBeNil)
   286  
   287  		mrs.Data[5][0] = int8(5)
   288  		ret, err = mrs.GetFloat64(context.TODO(), 5, 0)
   289  		convey.So(ret, convey.ShouldEqual, 5)
   290  		convey.So(err, convey.ShouldBeNil)
   291  
   292  		mrs.Data[6][0] = int16(6)
   293  		ret, err = mrs.GetFloat64(context.TODO(), 6, 0)
   294  		convey.So(ret, convey.ShouldEqual, 6)
   295  		convey.So(err, convey.ShouldBeNil)
   296  
   297  		mrs.Data[7][0] = int32(7)
   298  		ret, err = mrs.GetFloat64(context.TODO(), 7, 0)
   299  		convey.So(ret, convey.ShouldEqual, 7)
   300  		convey.So(err, convey.ShouldBeNil)
   301  
   302  		mrs.Data[8][0] = int64(8)
   303  		ret, err = mrs.GetFloat64(context.TODO(), 8, 0)
   304  		convey.So(ret, convey.ShouldEqual, 8)
   305  		convey.So(err, convey.ShouldBeNil)
   306  
   307  		mrs.Data[9][0] = float32(9)
   308  		ret, err = mrs.GetFloat64(context.TODO(), 9, 0)
   309  		convey.So(ret, convey.ShouldEqual, 9)
   310  		convey.So(err, convey.ShouldBeNil)
   311  
   312  		mrs.Data[10][0] = float64(10)
   313  		ret, err = mrs.GetFloat64(context.TODO(), 10, 0)
   314  		convey.So(ret, convey.ShouldEqual, 10)
   315  		convey.So(err, convey.ShouldBeNil)
   316  
   317  		mrs.Data[11][0] = "11"
   318  		ret, err = mrs.GetFloat64(context.TODO(), 11, 0)
   319  		convey.So(ret, convey.ShouldEqual, 11)
   320  		convey.So(err, convey.ShouldBeNil)
   321  
   322  		mrs.Data[12][0] = []byte("12")
   323  		ret, err = mrs.GetFloat64(context.TODO(), 12, 0)
   324  		convey.So(ret, convey.ShouldEqual, 12)
   325  		convey.So(err, convey.ShouldBeNil)
   326  
   327  		mrs.Data[13][0] = int(13)
   328  		ret, err = mrs.GetFloat64(context.TODO(), 13, 0)
   329  		convey.So(ret, convey.ShouldEqual, 13)
   330  		convey.So(err, convey.ShouldBeNil)
   331  
   332  		mrs.Data[14][0] = uint(14)
   333  		ret, err = mrs.GetFloat64(context.TODO(), 14, 0)
   334  		convey.So(ret, convey.ShouldEqual, 14)
   335  		convey.So(err, convey.ShouldBeNil)
   336  
   337  		mrs.Data[15][0] = types.Decimal64(15)
   338  		ret, err = mrs.GetFloat64(context.TODO(), 15, 0)
   339  		convey.So(ret, convey.ShouldEqual, 0)
   340  		convey.So(err, convey.ShouldNotBeNil)
   341  	})
   342  }
   343  
   344  func Test_GetString(t *testing.T) {
   345  	var ret string
   346  	var err error
   347  	var colNum = 19
   348  	convey.Convey("GetString succ", t, func() {
   349  		mrs := &MysqlResultSet{}
   350  		mrs.Data = make([][]interface{}, colNum)
   351  		for i := 0; i < colNum; i++ {
   352  			mrs.Data[i] = make([]interface{}, 1)
   353  		}
   354  		ret, err = mrs.GetString(context.TODO(), 1, 0)
   355  		convey.So(ret, convey.ShouldEqual, "")
   356  		convey.So(err, convey.ShouldNotBeNil)
   357  
   358  		ret, err = mrs.GetString(context.TODO(), 0, 0)
   359  		convey.So(ret, convey.ShouldEqual, "")
   360  		convey.So(err, convey.ShouldNotBeNil)
   361  
   362  		mrs.Data[0][0] = true
   363  		mrs.Columns = make([]Column, colNum)
   364  		ret, err = mrs.GetString(context.TODO(), 0, 0)
   365  		convey.So(ret, convey.ShouldEqual, "true")
   366  		convey.So(err, convey.ShouldBeNil)
   367  
   368  		mrs.Data[0][0] = false
   369  		ret, err = mrs.GetString(context.TODO(), 0, 0)
   370  		convey.So(ret, convey.ShouldEqual, "false")
   371  		convey.So(err, convey.ShouldBeNil)
   372  
   373  		mrs.Data[1][0] = uint8(1)
   374  		ret, err = mrs.GetString(context.TODO(), 1, 0)
   375  		convey.So(ret, convey.ShouldEqual, "1")
   376  		convey.So(err, convey.ShouldBeNil)
   377  
   378  		mrs.Data[2][0] = uint16(2)
   379  		ret, err = mrs.GetString(context.TODO(), 2, 0)
   380  		convey.So(ret, convey.ShouldEqual, "2")
   381  		convey.So(err, convey.ShouldBeNil)
   382  
   383  		mrs.Data[3][0] = uint32(3)
   384  		ret, err = mrs.GetString(context.TODO(), 3, 0)
   385  		convey.So(ret, convey.ShouldEqual, "3")
   386  		convey.So(err, convey.ShouldBeNil)
   387  
   388  		mrs.Data[4][0] = uint64(4)
   389  		ret, err = mrs.GetString(context.TODO(), 4, 0)
   390  		convey.So(ret, convey.ShouldEqual, "4")
   391  		convey.So(err, convey.ShouldBeNil)
   392  
   393  		mrs.Data[5][0] = int8(5)
   394  		ret, err = mrs.GetString(context.TODO(), 5, 0)
   395  		convey.So(ret, convey.ShouldEqual, "5")
   396  		convey.So(err, convey.ShouldBeNil)
   397  
   398  		mrs.Data[6][0] = int16(6)
   399  		ret, err = mrs.GetString(context.TODO(), 6, 0)
   400  		convey.So(ret, convey.ShouldEqual, "6")
   401  		convey.So(err, convey.ShouldBeNil)
   402  
   403  		mrs.Data[7][0] = int32(7)
   404  		ret, err = mrs.GetString(context.TODO(), 7, 0)
   405  		convey.So(ret, convey.ShouldEqual, "7")
   406  		convey.So(err, convey.ShouldBeNil)
   407  
   408  		mrs.Data[8][0] = int64(8)
   409  		ret, err = mrs.GetString(context.TODO(), 8, 0)
   410  		convey.So(ret, convey.ShouldEqual, "8")
   411  		convey.So(err, convey.ShouldBeNil)
   412  
   413  		mrs.Data[9][0] = float32(9)
   414  		ret, err = mrs.GetString(context.TODO(), 9, 0)
   415  		convey.So(ret, convey.ShouldEqual, "9")
   416  		convey.So(err, convey.ShouldBeNil)
   417  
   418  		mrs.Data[10][0] = float64(10)
   419  		ret, err = mrs.GetString(context.TODO(), 10, 0)
   420  		convey.So(ret, convey.ShouldEqual, "10")
   421  		convey.So(err, convey.ShouldBeNil)
   422  
   423  		mrs.Data[11][0] = "11"
   424  		ret, err = mrs.GetString(context.TODO(), 11, 0)
   425  		convey.So(ret, convey.ShouldEqual, "11")
   426  		convey.So(err, convey.ShouldBeNil)
   427  
   428  		mrs.Data[12][0] = []byte("12")
   429  		ret, err = mrs.GetString(context.TODO(), 12, 0)
   430  		convey.So(ret, convey.ShouldEqual, "12")
   431  		convey.So(err, convey.ShouldBeNil)
   432  
   433  		mrs.Data[13][0] = int(13)
   434  		ret, err = mrs.GetString(context.TODO(), 13, 0)
   435  		convey.So(ret, convey.ShouldEqual, "13")
   436  		convey.So(err, convey.ShouldBeNil)
   437  
   438  		mrs.Data[14][0] = uint(14)
   439  		ret, err = mrs.GetString(context.TODO(), 14, 0)
   440  		convey.So(ret, convey.ShouldEqual, "14")
   441  		convey.So(err, convey.ShouldBeNil)
   442  
   443  		mrs.Data[15][0] = types.Datetime(15)
   444  		ret, err = mrs.GetString(context.TODO(), 15, 0)
   445  		convey.So(ret, convey.ShouldEqual, "0001-01-01 00:00:00")
   446  		convey.So(err, convey.ShouldBeNil)
   447  
   448  		mrs.Data[16][0] = types.Decimal64(15)
   449  		ret, err = mrs.GetString(context.TODO(), 16, 0)
   450  		convey.So(ret, convey.ShouldEqual, "")
   451  		convey.So(err, convey.ShouldNotBeNil)
   452  
   453  		mrs.Data[17][0] = []float32{1, 2, 3}
   454  		ret, err = mrs.GetString(context.TODO(), 17, 0)
   455  		convey.So(ret, convey.ShouldEqual, "[1, 2, 3]")
   456  		convey.So(err, convey.ShouldBeNil)
   457  
   458  		mrs.Data[18][0] = []float64{1, 2, 3}
   459  		ret, err = mrs.GetString(context.TODO(), 18, 0)
   460  		convey.So(ret, convey.ShouldEqual, "[1, 2, 3]")
   461  		convey.So(err, convey.ShouldBeNil)
   462  	})
   463  }