github.com/ecodeclub/eorm@v0.0.2-0.20231001112437-dae71da914d0/internal/integration/select_combination_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/suite"
    27  )
    28  
    29  type SelectCombinationTestSuite struct {
    30  	Suite
    31  	data *test.CombinedModel
    32  }
    33  
    34  func (s *SelectCombinationTestSuite) SetupSuite() {
    35  	s.Suite.SetupSuite()
    36  	s.data = test.NewCombinedModel(1)
    37  	res := eorm.NewInserter[test.CombinedModel](s.orm).Values(s.data).Exec(context.Background())
    38  	if res.Err() != nil {
    39  		s.T().Fatal(res.Err())
    40  	}
    41  }
    42  
    43  func (s *SelectCombinationTestSuite) TearDownSuite() {
    44  	res := eorm.RawQuery[any](s.orm, "DELETE FROM `combined_model`").Exec(context.Background())
    45  	if res.Err() != nil {
    46  		s.T().Fatal(res.Err())
    47  	}
    48  }
    49  
    50  func (s *SelectCombinationTestSuite) TestGet() {
    51  	testCases := []struct {
    52  		name    string
    53  		s       *eorm.Selector[test.CombinedModel]
    54  		wantErr error
    55  		wantRes *test.CombinedModel
    56  	}{
    57  		{
    58  			name:    "not found",
    59  			s:       eorm.NewSelector[test.CombinedModel](s.orm).Where(eorm.C("Id").EQ(9)),
    60  			wantErr: eorm.ErrNoRows,
    61  		},
    62  		{
    63  			name:    "found",
    64  			s:       eorm.NewSelector[test.CombinedModel](s.orm).Where(eorm.C("Id").EQ(1)),
    65  			wantRes: s.data,
    66  		},
    67  	}
    68  
    69  	for _, tc := range testCases {
    70  		s.T().Run(tc.name, func(t *testing.T) {
    71  			res, err := tc.s.Get(context.Background())
    72  			assert.Equal(t, tc.wantErr, err)
    73  			if err != nil {
    74  				return
    75  			}
    76  			assert.Equal(t, tc.wantRes, res)
    77  		})
    78  	}
    79  }
    80  
    81  func TestMySQL8SelectCombination(t *testing.T) {
    82  	suite.Run(t, &SelectCombinationTestSuite{
    83  		Suite: Suite{
    84  			driver: "mysql",
    85  			dsn:    "root:root@tcp(localhost:13306)/integration_test",
    86  		},
    87  	})
    88  }