github.com/ecodeclub/eorm@v0.0.2-0.20231001112437-dae71da914d0/internal/rows/data_rows_test.go (about)

     1  // Copyright 2021 ecodeclub
     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 rows
    16  
    17  import (
    18  	"errors"
    19  	"testing"
    20  
    21  	"github.com/ecodeclub/ekit"
    22  	"github.com/ecodeclub/eorm/internal/errs"
    23  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  func TestDataRows_Close(t *testing.T) {
    27  	rows := NewDataRows(nil, nil, nil)
    28  	assert.Nil(t, rows.Close())
    29  }
    30  
    31  func TestDataRows_Columns(t *testing.T) {
    32  	testCases := []struct {
    33  		name    string
    34  		columns []string
    35  	}{
    36  		{
    37  			name: "nil",
    38  		},
    39  		{
    40  			name:    "columns",
    41  			columns: []string{"column1"},
    42  		},
    43  	}
    44  	for _, tc := range testCases {
    45  		t.Run(tc.name, func(t *testing.T) {
    46  			rows := NewDataRows(nil, tc.columns, nil)
    47  			columns, err := rows.Columns()
    48  			assert.NoError(t, err)
    49  			assert.Equal(t, tc.columns, columns)
    50  		})
    51  	}
    52  }
    53  
    54  func TestDataRows_Err(t *testing.T) {
    55  	rows := NewDataRows(nil, nil, nil)
    56  	assert.NoError(t, rows.Err())
    57  }
    58  
    59  func TestDataRows_Next(t *testing.T) {
    60  	testCases := []struct {
    61  		name      string
    62  		data      [][]any
    63  		beforeIdx int
    64  
    65  		wantNext bool
    66  		afterIdx int
    67  	}{
    68  		{
    69  			name:      "nil",
    70  			wantNext:  false,
    71  			beforeIdx: -1,
    72  			afterIdx:  -1,
    73  		},
    74  		{
    75  			name:      "第一个",
    76  			data:      [][]any{{1, 2, 3}},
    77  			wantNext:  true,
    78  			beforeIdx: -1,
    79  			afterIdx:  0,
    80  		},
    81  		{
    82  			name:      "还有一个",
    83  			data:      [][]any{{1}, {2}},
    84  			beforeIdx: 0,
    85  			wantNext:  true,
    86  			afterIdx:  1,
    87  		},
    88  		{
    89  			name:      "到了最后一个",
    90  			data:      [][]any{{1}, {2}},
    91  			beforeIdx: 1,
    92  			wantNext:  false,
    93  			afterIdx:  1,
    94  		},
    95  	}
    96  	for _, tc := range testCases {
    97  		t.Run(tc.name, func(t *testing.T) {
    98  			rows := NewDataRows(tc.data, nil, nil)
    99  			rows.idx = tc.beforeIdx
   100  			assert.Equal(t, tc.wantNext, rows.Next())
   101  			assert.Equal(t, tc.afterIdx, rows.idx)
   102  		})
   103  	}
   104  }
   105  
   106  func TestDataRows_Scan(t *testing.T) {
   107  	testCases := []struct {
   108  		name string
   109  		data [][]any
   110  		idx  int
   111  
   112  		input   []any
   113  		wantRes []any
   114  		wantErr error
   115  	}{
   116  		{
   117  			name:  "获得了数据",
   118  			data:  [][]any{{1, 2, 3}},
   119  			input: []any{new(int), new(int32), new(int64)},
   120  			wantRes: []any{ekit.ToPtr[int](1),
   121  				ekit.ToPtr[int32](2), ekit.ToPtr[int64](3)},
   122  			wantErr: nil,
   123  		},
   124  		{
   125  			name:    "dst 过长",
   126  			data:    [][]any{{1, 2, 3}},
   127  			input:   []any{new(int), new(int32), new(int64), new(int64)},
   128  			wantErr: errs.NewErrScanWrongDestinationArguments(3, 4),
   129  		},
   130  		{
   131  			name:    "dst 过短",
   132  			data:    [][]any{{1, 2, 3}},
   133  			input:   []any{new(int), new(int32)},
   134  			wantErr: errs.NewErrScanWrongDestinationArguments(3, 2),
   135  		},
   136  		{
   137  			name:    "ConvertAndAssign错误",
   138  			data:    [][]any{{1, "abc", 3}},
   139  			input:   []any{new(int), new(int64), new(int64)},
   140  			wantErr: errors.New(`converting driver.Value type string ("abc") to a int64: invalid syntax`),
   141  		},
   142  	}
   143  
   144  	for _, tc := range testCases {
   145  		t.Run(tc.name, func(t *testing.T) {
   146  			rows := NewDataRows(tc.data, nil, nil)
   147  			rows.idx = tc.idx
   148  			err := rows.Scan(tc.input...)
   149  			assert.Equal(t, tc.wantErr, err)
   150  			if err != nil {
   151  				return
   152  			}
   153  			assert.Equal(t, tc.wantRes, tc.input)
   154  		})
   155  	}
   156  }
   157  
   158  func TestDataRows_NextResultSet(t *testing.T) {
   159  	// 固化行为,防止不小心改了
   160  	rows := NewDataRows(nil, nil, nil)
   161  	assert.False(t, rows.NextResultSet())
   162  }