Skip to content

Commit

Permalink
feat: optimize sqlserver test (#652)
Browse files Browse the repository at this point in the history
* feat: cancel sqlserver test

* test

* test

* test

* test

* test

* cancel test

* optimize password
  • Loading branch information
hwbrzzl authored Sep 21, 2024
1 parent 634b3a1 commit 9e9c677
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 45 deletions.
2 changes: 1 addition & 1 deletion support/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
type ContainerType string

const (
password = "Goravel123"
password = "Framework!123"
username = "goravel"
database = "goravel"

Expand Down
8 changes: 4 additions & 4 deletions support/docker/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestPostgresTestSuite(t *testing.T) {

func (s *PostgresTestSuite) SetupTest() {
s.mockConfig = &configmocks.Config{}
s.postgres = NewPostgresImpl("goravel", "goravel", "goravel")
s.postgres = NewPostgresImpl(database, username, password)
}

func (s *PostgresTestSuite) TestBuild() {
Expand All @@ -37,9 +37,9 @@ func (s *PostgresTestSuite) TestBuild() {
s.NotNil(instance)

s.Equal("127.0.0.1", s.postgres.Config().Host)
s.Equal("goravel", s.postgres.Config().Database)
s.Equal("goravel", s.postgres.Config().Username)
s.Equal("goravel", s.postgres.Config().Password)
s.Equal(database, s.postgres.Config().Database)
s.Equal(username, s.postgres.Config().Username)
s.Equal(password, s.postgres.Config().Password)
s.True(s.postgres.Config().Port > 0)

res := instance.Exec(`
Expand Down
2 changes: 1 addition & 1 deletion support/docker/sqlite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (s *SqliteTestSuite) TestBuild() {
s.Nil(err)
s.NotNil(instance)

s.Equal("goravel", s.sqlite.Config().Database)
s.Equal(database, s.sqlite.Config().Database)

res := instance.Exec(`
CREATE TABLE users (
Expand Down
47 changes: 39 additions & 8 deletions support/docker/sqlserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,11 @@ func NewSqlserverImpl(database, username, password string) *SqlserverImpl {
username: username,
password: password,
image: &testing.Image{
Repository: "mcmoe/mssqldocker",
Repository: "mcr.microsoft.com/mssql/server",
Tag: "latest",
Env: []string{
"ACCEPT_EULA=Y",
"MSSQL_DB=" + database,
"MSSQL_USER=" + username,
"MSSQL_PASSWORD=" + password,
"SA_PASSWORD=" + password,
"MSSQL_SA_PASSWORD=" + password,
},
ExposedPorts: []string{"1433"},
},
Expand Down Expand Up @@ -122,13 +119,47 @@ func (receiver *SqlserverImpl) connect() (*gormio.DB, error) {
)

// docker compose need time to start
for i := 0; i < 60; i++ {
for i := 0; i < 100; i++ {
instance, err = gormio.Open(sqlserver.New(sqlserver.Config{
DSN: fmt.Sprintf("sqlserver://%s:%s@%s:%d?database=%s",
receiver.username, receiver.password, receiver.host, receiver.port, receiver.database),
DSN: fmt.Sprintf("sqlserver://%s:%s@%s:%d?database=master",
"sa", receiver.password, receiver.host, receiver.port),
}))

if err == nil {
// Check if database exists
var exists bool
query := fmt.Sprintf("SELECT CASE WHEN EXISTS (SELECT * FROM sys.databases WHERE name = '%s') THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT) END", receiver.database)
if err := instance.Raw(query).Scan(&exists).Error; err != nil {
return nil, err
}

if !exists {
// Create User database
if err := instance.Exec(fmt.Sprintf("CREATE DATABASE %s", receiver.database)).Error; err != nil {
return nil, err
}

// Create User account
if err := instance.Exec(fmt.Sprintf("CREATE LOGIN %s WITH PASSWORD = '%s'", receiver.username, receiver.password)).Error; err != nil {
return nil, err
}

// Create DB account for User
if err := instance.Exec(fmt.Sprintf("USE %s; CREATE USER %s FOR LOGIN %s", receiver.database, receiver.username, receiver.username)).Error; err != nil {
return nil, err
}

// Add permission
if err := instance.Exec(fmt.Sprintf("USE %s; ALTER ROLE db_owner ADD MEMBER %s", receiver.database, receiver.username)).Error; err != nil {
return nil, err
}
}

instance, err = gormio.Open(sqlserver.New(sqlserver.Config{
DSN: fmt.Sprintf("sqlserver://%s:%s@%s:%d?database=%s",
receiver.username, receiver.password, receiver.host, receiver.port, receiver.database),
}))

break
}

Expand Down
6 changes: 3 additions & 3 deletions support/docker/sqlserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ func (s *SqlserverTestSuite) TestBuild() {
s.NotNil(instance)

s.Equal("127.0.0.1", s.sqlserver.Config().Host)
s.Equal("goravel", s.sqlserver.Config().Database)
s.Equal("goravel", s.sqlserver.Config().Username)
s.Equal("Goravel123", s.sqlserver.Config().Password)
s.Equal(database, s.sqlserver.Config().Database)
s.Equal(username, s.sqlserver.Config().Username)
s.Equal(password, s.sqlserver.Config().Password)
s.True(s.sqlserver.Config().Port > 0)

res := instance.Exec(`
Expand Down
13 changes: 12 additions & 1 deletion support/env/env.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package env

import "runtime"
import (
"os"
"runtime"
)

// IsWindows returns whether the current operating system is Windows.
// IsWindows 返回当前操作系统是否为 Windows。
Expand Down Expand Up @@ -37,3 +40,11 @@ func IsX86() bool {
func Is64Bit() bool {
return runtime.GOARCH == "amd64" || runtime.GOARCH == "arm64"
}

// IsGithub returns whether the current environment is github action.
// IsGithub 返回当前系统环境是否为 github action。
func IsGithub() bool {
_, exists := os.LookupEnv("GITHUB_ACTION")

return exists
}
51 changes: 24 additions & 27 deletions testing/docker/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,14 @@ import (
var (
testDatabase = "goravel"
testUsername = "goravel"
testPassword = "Goravel123"
testPassword = "Framework!123"
)

func TestNewDatabase(t *testing.T) {
var (
mockApp *foundationmocks.Application
mockConfig *configmocks.Config
mockGormInitialize *gormmocks.Initialize
database = "goravel"
username = "goravel"
password = "Goravel123"
)

beforeEach := func() {
Expand All @@ -56,16 +53,16 @@ func TestNewDatabase(t *testing.T) {
setup: func() {
mockConfig.On("GetString", "database.default").Return("mysql").Once()
mockConfig.On("GetString", "database.connections.mysql.driver").Return(contractsorm.DriverMysql.String()).Once()
mockConfig.On("GetString", "database.connections.mysql.database").Return(database).Once()
mockConfig.On("GetString", "database.connections.mysql.username").Return(username).Once()
mockConfig.On("GetString", "database.connections.mysql.password").Return(password).Once()
mockConfig.On("GetString", "database.connections.mysql.database").Return(testDatabase).Once()
mockConfig.On("GetString", "database.connections.mysql.username").Return(testUsername).Once()
mockConfig.On("GetString", "database.connections.mysql.password").Return(testPassword).Once()
},
wantDatabase: func() *Database {
return &Database{
app: mockApp,
config: mockConfig,
connection: "mysql",
driver: supportdocker.NewMysqlImpl(database, username, password),
driver: supportdocker.NewMysqlImpl(testDatabase, testUsername, testPassword),
gormInitialize: mockGormInitialize,
}
},
Expand All @@ -75,16 +72,16 @@ func TestNewDatabase(t *testing.T) {
connection: "mysql",
setup: func() {
mockConfig.On("GetString", "database.connections.mysql.driver").Return(contractsorm.DriverMysql.String()).Once()
mockConfig.On("GetString", "database.connections.mysql.database").Return(database).Once()
mockConfig.On("GetString", "database.connections.mysql.username").Return(username).Once()
mockConfig.On("GetString", "database.connections.mysql.password").Return(password).Once()
mockConfig.On("GetString", "database.connections.mysql.database").Return(testDatabase).Once()
mockConfig.On("GetString", "database.connections.mysql.username").Return(testUsername).Once()
mockConfig.On("GetString", "database.connections.mysql.password").Return(testPassword).Once()
},
wantDatabase: func() *Database {
return &Database{
app: mockApp,
config: mockConfig,
connection: "mysql",
driver: supportdocker.NewMysqlImpl(database, username, password),
driver: supportdocker.NewMysqlImpl(testDatabase, testUsername, testPassword),
gormInitialize: mockGormInitialize,
}
},
Expand All @@ -94,16 +91,16 @@ func TestNewDatabase(t *testing.T) {
connection: "postgres",
setup: func() {
mockConfig.On("GetString", "database.connections.postgres.driver").Return(contractsorm.DriverPostgres.String()).Once()
mockConfig.On("GetString", "database.connections.postgres.database").Return(database).Once()
mockConfig.On("GetString", "database.connections.postgres.username").Return(username).Once()
mockConfig.On("GetString", "database.connections.postgres.password").Return(password).Once()
mockConfig.On("GetString", "database.connections.postgres.database").Return(testDatabase).Once()
mockConfig.On("GetString", "database.connections.postgres.username").Return(testUsername).Once()
mockConfig.On("GetString", "database.connections.postgres.password").Return(testPassword).Once()
},
wantDatabase: func() *Database {
return &Database{
app: mockApp,
config: mockConfig,
connection: "postgres",
driver: supportdocker.NewPostgresImpl(database, username, password),
driver: supportdocker.NewPostgresImpl(testDatabase, testUsername, testPassword),
gormInitialize: mockGormInitialize,
}
},
Expand All @@ -113,16 +110,16 @@ func TestNewDatabase(t *testing.T) {
connection: "sqlserver",
setup: func() {
mockConfig.On("GetString", "database.connections.sqlserver.driver").Return(contractsorm.DriverSqlserver.String()).Once()
mockConfig.On("GetString", "database.connections.sqlserver.database").Return(database).Once()
mockConfig.On("GetString", "database.connections.sqlserver.username").Return(username).Once()
mockConfig.On("GetString", "database.connections.sqlserver.password").Return(password).Once()
mockConfig.On("GetString", "database.connections.sqlserver.database").Return(testDatabase).Once()
mockConfig.On("GetString", "database.connections.sqlserver.username").Return(testUsername).Once()
mockConfig.On("GetString", "database.connections.sqlserver.password").Return(testPassword).Once()
},
wantDatabase: func() *Database {
return &Database{
app: mockApp,
config: mockConfig,
connection: "sqlserver",
driver: supportdocker.NewSqlserverImpl(database, username, password),
driver: supportdocker.NewSqlserverImpl(testDatabase, testUsername, testPassword),
gormInitialize: mockGormInitialize,
}
},
Expand All @@ -132,16 +129,16 @@ func TestNewDatabase(t *testing.T) {
connection: "sqlite",
setup: func() {
mockConfig.On("GetString", "database.connections.sqlite.driver").Return(contractsorm.DriverSqlite.String()).Once()
mockConfig.On("GetString", "database.connections.sqlite.database").Return(database).Once()
mockConfig.On("GetString", "database.connections.sqlite.username").Return(username).Once()
mockConfig.On("GetString", "database.connections.sqlite.password").Return(password).Once()
mockConfig.On("GetString", "database.connections.sqlite.database").Return(testDatabase).Once()
mockConfig.On("GetString", "database.connections.sqlite.username").Return(testUsername).Once()
mockConfig.On("GetString", "database.connections.sqlite.password").Return(testPassword).Once()
},
wantDatabase: func() *Database {
return &Database{
app: mockApp,
config: mockConfig,
connection: "sqlite",
driver: supportdocker.NewSqliteImpl(database),
driver: supportdocker.NewSqliteImpl(testDatabase),
gormInitialize: mockGormInitialize,
}
},
Expand All @@ -151,9 +148,9 @@ func TestNewDatabase(t *testing.T) {
connection: "mysql",
setup: func() {
mockConfig.On("GetString", "database.connections.mysql.driver").Return("").Once()
mockConfig.On("GetString", "database.connections.mysql.database").Return(database).Once()
mockConfig.On("GetString", "database.connections.mysql.username").Return(username).Once()
mockConfig.On("GetString", "database.connections.mysql.password").Return(password).Once()
mockConfig.On("GetString", "database.connections.mysql.database").Return(testDatabase).Once()
mockConfig.On("GetString", "database.connections.mysql.username").Return(testUsername).Once()
mockConfig.On("GetString", "database.connections.mysql.password").Return(testPassword).Once()
},
wantErr: fmt.Errorf("not found database connection: %s", "mysql"),
},
Expand Down

0 comments on commit 9e9c677

Please sign in to comment.