github.com/ecodeclub/eorm@v0.0.2-0.20231001112437-dae71da914d0/internal/integration/sharding_update_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  //go:build e2e
    16  
    17  package integration
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	"github.com/ecodeclub/eorm"
    24  	"github.com/ecodeclub/eorm/internal/datasource/masterslave"
    25  	"github.com/ecodeclub/eorm/internal/model"
    26  	"github.com/ecodeclub/eorm/internal/sharding"
    27  	"github.com/ecodeclub/eorm/internal/test"
    28  	"github.com/stretchr/testify/assert"
    29  	"github.com/stretchr/testify/require"
    30  	"github.com/stretchr/testify/suite"
    31  )
    32  
    33  type ShardingUpdateTestSuite struct {
    34  	ShardingSelectUpdateInsertSuite
    35  }
    36  
    37  func (s *ShardingUpdateTestSuite) TestShardingUpdater_Exec() {
    38  	t := s.T()
    39  	r := model.NewMetaRegistry()
    40  	_, err := r.Register(&test.OrderDetail{},
    41  		model.WithTableShardingAlgorithm(s.algorithm))
    42  	require.NoError(t, err)
    43  	eorm.DBWithMetaRegistry(r)(s.shardingDB)
    44  	testCases := []struct {
    45  		name             string
    46  		wantAffectedRows int64
    47  		wantErr          error
    48  		exec             sharding.Executor
    49  		updatedSelector  *eorm.ShardingSelector[test.OrderDetail]
    50  		selector         *eorm.ShardingSelector[test.OrderDetail]
    51  		updatedQuerySet  []*test.OrderDetail
    52  		querySet         []*test.OrderDetail
    53  	}{
    54  		{
    55  			name: "where eq",
    56  			exec: eorm.NewShardingUpdater[test.OrderDetail](s.shardingDB).Update(&test.OrderDetail{
    57  				ItemId: 111, UsingCol1: "Jack", UsingCol2: "Jerry",
    58  			}).Where(eorm.C("OrderId").EQ(123)),
    59  			wantAffectedRows: 1,
    60  			updatedSelector:  eorm.NewShardingSelector[test.OrderDetail](s.shardingDB).Where(eorm.C("OrderId").EQ(123)),
    61  			selector:         eorm.NewShardingSelector[test.OrderDetail](s.shardingDB).Where(eorm.C("OrderId").NEQ(123)),
    62  			updatedQuerySet: []*test.OrderDetail{
    63  				{OrderId: 123, ItemId: 111, UsingCol1: "Jack", UsingCol2: "Jerry"},
    64  			},
    65  			querySet: []*test.OrderDetail{
    66  				{8, 6, "Kobe", "Bryant"},
    67  				{11, 8, "James", "Harden"},
    68  				{234, 12, "Kevin", "Durant"},
    69  				{253, 8, "Stephen", "Curry"},
    70  				{181, 11, "Kawhi", "Leonard"},
    71  			},
    72  		},
    73  		{
    74  			name: "where or broadcast",
    75  			exec: eorm.NewShardingUpdater[test.OrderDetail](s.shardingDB).Update(&test.OrderDetail{
    76  				ItemId: 112, UsingCol1: "King", UsingCol2: "James",
    77  			}).Set(eorm.Columns("ItemId", "UsingCol1", "UsingCol2")).
    78  				Where(eorm.C("OrderId").EQ(123).Or(eorm.C("ItemId").EQ(11))),
    79  			wantAffectedRows: 2,
    80  			updatedSelector: eorm.NewShardingSelector[test.OrderDetail](s.shardingDB).
    81  				Where(eorm.C("OrderId").In(123, 181)),
    82  			selector: eorm.NewShardingSelector[test.OrderDetail](s.shardingDB).
    83  				Where(eorm.C("OrderId").NotIn(123, 181)),
    84  			updatedQuerySet: []*test.OrderDetail{
    85  				{OrderId: 123, ItemId: 112, UsingCol1: "King", UsingCol2: "James"},
    86  				{OrderId: 181, ItemId: 112, UsingCol1: "King", UsingCol2: "James"},
    87  			},
    88  			querySet: []*test.OrderDetail{
    89  				{8, 6, "Kobe", "Bryant"},
    90  				{11, 8, "James", "Harden"},
    91  				{234, 12, "Kevin", "Durant"},
    92  				{253, 8, "Stephen", "Curry"},
    93  			},
    94  		},
    95  	}
    96  	for _, tc := range testCases {
    97  		t.Run(tc.name, func(t *testing.T) {
    98  			res := tc.exec.Exec(context.Background())
    99  			require.Equal(t, tc.wantErr, res.Err())
   100  			if res.Err() != nil {
   101  				return
   102  			}
   103  
   104  			affectRows, err := res.RowsAffected()
   105  			require.NoError(t, err)
   106  			assert.Equal(t, tc.wantAffectedRows, affectRows)
   107  
   108  			// TODO 从库测试目前有查不到数据的bug
   109  			ctx := masterslave.UseMaster(context.Background())
   110  			updatedQuerySet, err := tc.updatedSelector.GetMulti(ctx)
   111  			require.NoError(t, err)
   112  			assert.ElementsMatch(t, tc.updatedQuerySet, updatedQuerySet)
   113  
   114  			querySet, err := tc.selector.GetMulti(ctx)
   115  			require.NoError(t, err)
   116  			assert.ElementsMatch(t, tc.querySet, querySet)
   117  
   118  		})
   119  	}
   120  }
   121  
   122  func TestMySQL8ShardingUpdate(t *testing.T) {
   123  	suite.Run(t, &ShardingUpdateTestSuite{
   124  		ShardingSelectUpdateInsertSuite: newShardingSelectUpdateInsertSuite(),
   125  	})
   126  }