github.com/ecodeclub/eorm@v0.0.2-0.20231001112437-dae71da914d0/internal/integration/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/test"
    25  	"github.com/stretchr/testify/assert"
    26  	"github.com/stretchr/testify/require"
    27  	"github.com/stretchr/testify/suite"
    28  )
    29  
    30  type UpdateTestSuite struct {
    31  	Suite
    32  }
    33  
    34  func (u *UpdateTestSuite) SetupSuite() {
    35  	u.Suite.SetupSuite()
    36  	data1 := test.NewSimpleStruct(1)
    37  	res := eorm.NewInserter[test.SimpleStruct](u.orm).Values(data1).Exec(context.Background())
    38  	if res.Err() != nil {
    39  		u.T().Fatal(res.Err())
    40  	}
    41  }
    42  
    43  func (u *UpdateTestSuite) TearDownTest() {
    44  	res := eorm.RawQuery[any](u.orm, "DELETE FROM `simple_struct`").Exec(context.Background())
    45  	if res.Err() != nil {
    46  		u.T().Fatal(res.Err())
    47  	}
    48  }
    49  
    50  func (u *UpdateTestSuite) TestUpdate() {
    51  	testCases := []struct {
    52  		name         string
    53  		u            *eorm.Updater[test.SimpleStruct]
    54  		rowsAffected int64
    55  		wantErr      error
    56  	}{
    57  		{
    58  			name: "update columns",
    59  			u: eorm.NewUpdater[test.SimpleStruct](u.orm).Update(&test.SimpleStruct{Int: 18}).
    60  				Set(eorm.Columns("Int")).Where(eorm.C("Id").EQ(1)),
    61  			rowsAffected: 1,
    62  		},
    63  	}
    64  	for _, tc := range testCases {
    65  		u.T().Run(tc.name, func(t *testing.T) {
    66  			res := tc.u.Exec(context.Background())
    67  			assert.Equal(t, tc.wantErr, res.Err())
    68  			if res.Err() != nil {
    69  				return
    70  			}
    71  			affected, err := res.RowsAffected()
    72  			require.NoError(t, err)
    73  			assert.Equal(t, tc.rowsAffected, affected)
    74  		})
    75  	}
    76  }
    77  
    78  func TestMySQL8Update(t *testing.T) {
    79  	suite.Run(t, &UpdateTestSuite{
    80  		Suite{
    81  			driver: "mysql",
    82  			dsn:    "root:root@tcp(localhost:13306)/integration_test",
    83  		},
    84  	})
    85  }