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