github.com/ecodeclub/eorm@v0.0.2-0.20231001112437-dae71da914d0/internal/integration/delete_composition_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/go-sql-driver/mysql"
    26  	"github.com/stretchr/testify/assert"
    27  	"github.com/stretchr/testify/require"
    28  	"github.com/stretchr/testify/suite"
    29  )
    30  
    31  type DeleteCompositionTestSuite struct {
    32  	Suite
    33  }
    34  
    35  func (s *DeleteCompositionTestSuite) SetupSuite() {
    36  	s.Suite.SetupSuite()
    37  	data1 := test.NewCombinedModel(1)
    38  	data2 := test.NewCombinedModel(2)
    39  	data3 := test.NewCombinedModel(3)
    40  	res := eorm.NewInserter[test.CombinedModel](s.orm).Values(data1, data2, data3).Exec(context.Background())
    41  	if res.Err() != nil {
    42  		s.T().Fatal(res.Err())
    43  	}
    44  }
    45  
    46  func (i *DeleteCompositionTestSuite) TearDownTest() {
    47  	res := eorm.RawQuery[any](i.orm, "DELETE FROM `combined_model`").Exec(context.Background())
    48  	if res.Err() != nil {
    49  		i.T().Fatal(res.Err())
    50  	}
    51  }
    52  
    53  func (i *DeleteCompositionTestSuite) TestDeleter() {
    54  	testCases := []struct {
    55  		name         string
    56  		i            *eorm.Deleter[test.CombinedModel]
    57  		rowsAffected int64
    58  		wantErr      error
    59  	}{
    60  		{
    61  			name:         "id only",
    62  			i:            eorm.NewDeleter[test.CombinedModel](i.orm).From(&test.CombinedModel{}).Where(eorm.C("Id").EQ("1")),
    63  			rowsAffected: 1,
    64  		},
    65  		{
    66  			name:         "delete all",
    67  			i:            eorm.NewDeleter[test.CombinedModel](i.orm).From(&test.CombinedModel{}),
    68  			rowsAffected: 2,
    69  		},
    70  	}
    71  	for _, tc := range testCases {
    72  		i.T().Run(tc.name, func(t *testing.T) {
    73  			res := tc.i.Exec(context.Background())
    74  			require.Equal(t, tc.wantErr, res.Err())
    75  			affected, err := res.RowsAffected()
    76  			require.Nil(t, err)
    77  			assert.Equal(t, tc.rowsAffected, affected)
    78  		})
    79  	}
    80  }
    81  
    82  func TestMySQL8tDeleteComposition(t *testing.T) {
    83  	suite.Run(t, &DeleteCompositionTestSuite{
    84  		Suite{
    85  			driver: "mysql",
    86  			dsn:    "root:root@tcp(localhost:13306)/integration_test",
    87  		},
    88  	})
    89  }