github.com/goravel/framework@v1.13.9/database/db/dsn_test.go (about)

     1  package db
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/suite"
     8  
     9  	configmock "github.com/goravel/framework/contracts/config/mocks"
    10  	databasecontract "github.com/goravel/framework/contracts/database"
    11  	"github.com/goravel/framework/contracts/database/orm"
    12  )
    13  
    14  const (
    15  	testHost     = "127.0.0.1"
    16  	testPort     = 3306
    17  	testDatabase = "forge"
    18  	testUsername = "root"
    19  	testPassword = "123123"
    20  )
    21  
    22  var testConfig = databasecontract.Config{
    23  	Host:     testHost,
    24  	Port:     testPort,
    25  	Database: testDatabase,
    26  	Username: testUsername,
    27  	Password: testPassword,
    28  }
    29  
    30  type DsnTestSuite struct {
    31  	suite.Suite
    32  	mockConfig *configmock.Config
    33  }
    34  
    35  func TestDsnTestSuite(t *testing.T) {
    36  	suite.Run(t, new(DsnTestSuite))
    37  }
    38  
    39  func (s *DsnTestSuite) SetupTest() {
    40  	s.mockConfig = &configmock.Config{}
    41  }
    42  
    43  func (s *DsnTestSuite) TestMysql() {
    44  	connection := orm.DriverMysql.String()
    45  	dsn := NewDsnImpl(s.mockConfig, connection)
    46  	charset := "utf8mb4"
    47  	loc := "Local"
    48  	s.mockConfig.On("GetString", fmt.Sprintf("database.connections.%s.charset", connection)).Return(charset).Once()
    49  	s.mockConfig.On("GetString", fmt.Sprintf("database.connections.%s.loc", connection)).Return(loc).Once()
    50  
    51  	s.Equal(fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=%t&loc=%s&multiStatements=true",
    52  		testUsername, testPassword, testHost, testPort, testDatabase, charset, true, loc), dsn.Mysql(testConfig))
    53  }
    54  
    55  func (s *DsnTestSuite) TestPostgresql() {
    56  	connection := orm.DriverPostgresql.String()
    57  	dsn := NewDsnImpl(s.mockConfig, connection)
    58  	sslmode := "disable"
    59  	timezone := "UTC"
    60  	s.mockConfig.On("GetString", fmt.Sprintf("database.connections.%s.sslmode", connection)).Return(sslmode).Once()
    61  	s.mockConfig.On("GetString", fmt.Sprintf("database.connections.%s.timezone", connection)).Return(timezone).Once()
    62  
    63  	s.Equal(fmt.Sprintf("postgres://%s:%s@%s:%d/%s?sslmode=%s&timezone=%s",
    64  		testUsername, testPassword, testHost, testPort, testDatabase, sslmode, timezone), dsn.Postgresql(testConfig))
    65  }
    66  
    67  func (s *DsnTestSuite) TestSqlite() {
    68  	dsn := NewDsnImpl(s.mockConfig, "")
    69  	s.Equal(fmt.Sprintf("%s?multi_stmts=true", testDatabase), dsn.Sqlite(testConfig))
    70  }
    71  
    72  func (s *DsnTestSuite) TestSqlserver() {
    73  	connection := orm.DriverSqlserver.String()
    74  	dsn := NewDsnImpl(s.mockConfig, connection)
    75  	charset := "utf8mb4"
    76  	s.mockConfig.On("GetString", fmt.Sprintf("database.connections.%s.charset", connection)).Return(charset).Once()
    77  
    78  	s.Equal(fmt.Sprintf("sqlserver://%s:%s@%s:%d?database=%s&charset=%s&MultipleActiveResultSets=true",
    79  		testUsername, testPassword, testHost, testPort, testDatabase, charset), dsn.Sqlserver(testConfig))
    80  }