Skip to content

Commit

Permalink
create container directly
Browse files Browse the repository at this point in the history
  • Loading branch information
hwbrzzl committed Nov 15, 2024
1 parent 16cb054 commit d4532b4
Show file tree
Hide file tree
Showing 34 changed files with 122 additions and 79 deletions.
2 changes: 2 additions & 0 deletions contracts/testing/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ type DatabaseDriver interface {
Fresh() error
// Image gets the database image.
Image(image Image)
// Ready checks if the database is ready, the Build method needs to be called first.
Ready() error
// Stop the database.
Stop() error
}
Expand Down
5 changes: 4 additions & 1 deletion database/factory/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@ func TestFactoryTestSuite(t *testing.T) {
}

func (s *FactoryTestSuite) SetupSuite() {
postgresQuery := gorm.NewTestQuery(docker.Postgres())
postgresDocker := docker.Postgres()
s.Require().NoError(postgresDocker.Ready())

postgresQuery := gorm.NewTestQuery(postgresDocker)
postgresQuery.CreateTable(gorm.TestTableHouses, gorm.TestTableUsers)

s.query = postgresQuery.Query()
Expand Down
Empty file added database/gorm/goravel_PZ769e
Empty file.
Empty file added database/gorm/goravel_YoBjp8
Empty file.
Empty file added database/gorm/goravel_xBFvVH
Empty file.
2 changes: 2 additions & 0 deletions database/gorm/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3599,6 +3599,8 @@ func TestCustomConnection(t *testing.T) {
}

postgresDocker := supportdocker.Postgres()
require.NoError(t, postgresDocker.Ready())

postgresQuery := NewTestQuery(postgresDocker)
postgresQuery.CreateTable(TestTableReviews, TestTableProducts)

Expand Down
21 changes: 18 additions & 3 deletions database/gorm/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,32 @@ type TestQueries struct {
}

func NewTestQueries() *TestQueries {
postgresDockers := supportdocker.Postgreses(2)
if err := supportdocker.Ready(postgresDockers...); err != nil {
panic(err)
}

testQueries := &TestQueries{
sqliteDockers: supportdocker.Sqlites(2),
postgresDockers: supportdocker.Postgreses(2),
postgresDockers: postgresDockers,
}

if supportdocker.TestModel == supportdocker.TestModelMinimum {
return testQueries
}

testQueries.mysqlDockers = supportdocker.Mysqls(2)
testQueries.sqlserverDockers = supportdocker.Sqlservers(2)
// Create all containers first, containers will be returned directly, then check the container status, the speed will be faster.
mysqlDockers := supportdocker.Mysqls(2)
sqlserverDockers := supportdocker.Sqlservers(2)
if err := supportdocker.Ready(mysqlDockers...); err != nil {
panic(err)
}
if err := supportdocker.Ready(sqlserverDockers...); err != nil {
panic(err)
}

testQueries.mysqlDockers = mysqlDockers
testQueries.sqlserverDockers = sqlserverDockers

return testQueries
}
Expand Down
5 changes: 4 additions & 1 deletion database/gorm/to_sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ func TestToSqlTestSuite(t *testing.T) {
}

func (s *ToSqlTestSuite) SetupSuite() {
postgresQuery := NewTestQuery(docker.Postgres())
postgresDocker := docker.Postgres()
s.Require().NoError(postgresDocker.Ready())

postgresQuery := NewTestQuery(postgresDocker)
postgresQuery.CreateTable(TestTableUsers)

s.query = postgresQuery.Query()
Expand Down
2 changes: 2 additions & 0 deletions database/migration/default_migrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ func TestDefaultMigratorWithDBSuite(t *testing.T) {
func (s *DefaultMigratorWithDBSuite) SetupTest() {
// TODO Add other drivers
postgresDocker := docker.Postgres()
s.NoError(postgresDocker.Ready())

postgresQuery := gorm.NewTestQuery(postgresDocker, true)
s.driverToTestQuery = map[contractsdatabase.Driver]*gorm.TestQuery{
contractsdatabase.DriverPostgres: postgresQuery,
Expand Down
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
2 changes: 2 additions & 0 deletions database/migration/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ func TestRepositoryTestSuite(t *testing.T) {

func (s *RepositoryTestSuite) SetupTest() {
postgresDocker := docker.Postgres()
s.Require().NoError(postgresDocker.Ready())

postgresQuery := gorm.NewTestQuery(postgresDocker, true)
s.driverToTestQuery = map[database.Driver]*gorm.TestQuery{
database.DriverPostgres: postgresQuery,
Expand Down
Empty file added database/orm/goravel_jUDDNo
Empty file.
2 changes: 2 additions & 0 deletions database/schema/common_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ func TestCommonSchemaSuite(t *testing.T) {

func (s *CommonSchemaSuite) SetupTest() {
postgresDocker := docker.Postgres()
s.Require().NoError(postgresDocker.Ready())

postgresQuery := gorm.NewTestQuery(postgresDocker, true)
s.driverToTestQuery = map[database.Driver]*gorm.TestQuery{
database.DriverPostgres: postgresQuery,
Expand Down
2 changes: 2 additions & 0 deletions database/schema/postgres_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ func TestPostgresSchemaSuite(t *testing.T) {

func (s *PostgresSchemaSuite) SetupTest() {
postgresDocker := docker.Postgres()
s.Require().NoError(postgresDocker.Ready())

s.testQuery = gorm.NewTestQuery(postgresDocker, true)
s.mockOrm = mocksorm.NewOrm(s.T())
s.postgresSchema = NewPostgresSchema(grammars.NewPostgres("goravel_"), s.mockOrm, "goravel", "goravel_")
Expand Down
2 changes: 2 additions & 0 deletions database/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ func TestSchemaSuite(t *testing.T) {
func (s *SchemaSuite) SetupTest() {
// TODO Add other drivers
postgresDocker := docker.Postgres()
s.Require().NoError(postgresDocker.Ready())

postgresQuery := gorm.NewTestQuery(postgresDocker, true)

sqliteDocker := docker.Sqlite()
Expand Down
2 changes: 2 additions & 0 deletions foundation/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ func (s *ApplicationTestSuite) TestMakeOrm() {
}

postgresDocker := supportdocker.Postgres()
s.Require().NoError(postgresDocker.Ready())

config := postgresDocker.Config()
mockConfig := mocksconfig.NewConfig(s.T())
mockConfig.EXPECT().GetString("database.default").Return("postgres").Once()
Expand Down
12 changes: 11 additions & 1 deletion support/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const (
TestModelNormal

// Switch this value to control the test model.
TestModel = TestModelNormal
TestModel = TestModelMinimum
)

func Mysql() testing.DatabaseDriver {
Expand Down Expand Up @@ -52,6 +52,16 @@ func Sqlites(num int) []testing.DatabaseDriver {
return Database(ContainerTypeSqlite, num)
}

func Ready(drivers ...testing.DatabaseDriver) error {
for _, driver := range drivers {
if err := driver.Ready(); err != nil {
return err
}
}

return nil
}

func Database(containerType ContainerType, num int) []testing.DatabaseDriver {
if num <= 0 {
panic(errors.DockerDatabaseContainerCountZero)
Expand Down
48 changes: 27 additions & 21 deletions support/docker/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/goravel/framework/contracts/database"
"github.com/goravel/framework/contracts/testing"
"github.com/goravel/framework/errors"
"github.com/goravel/framework/support/color"
)

type MysqlImpl struct {
Expand Down Expand Up @@ -59,10 +60,6 @@ func (r *MysqlImpl) Build() error {
r.containerID = containerID
r.port = getExposedPort(exposedPorts, 3306)

if _, err := r.connect(); err != nil {
return fmt.Errorf("connect Mysql docker error: %v", err)
}

return nil
}

Expand All @@ -78,30 +75,33 @@ func (r *MysqlImpl) Config() testing.DatabaseConfig {
}

func (r *MysqlImpl) Database(name string) (testing.DatabaseDriver, error) {
instance, err := r.connect("root")
if err != nil {
return nil, fmt.Errorf("connect Mysql error: %v", err)
}
// We want to return the DatabaseDriver instance directly, to avoid blocking test cases in different packages,
// because the test process will be clocked when creating a new container, each package should call the Ready method
// to check if the container is ready. Returning the DatabaseDriver instance directly will allow to initialize multiple
// container simultaneously.
go func() {
instance, err := r.connect("root")
if err != nil {
color.Errorf("connect Mysql error: %v", err)
return
}

res := instance.Exec(fmt.Sprintf(`CREATE DATABASE %s;`, name))
if res.Error != nil {
return nil, fmt.Errorf("create Mysql database error: %v", res.Error)
}
res := instance.Exec(fmt.Sprintf(`CREATE DATABASE %s;`, name))
if res.Error != nil {
color.Errorf("create Mysql database error: %v", res.Error)
return
}

res = instance.Exec(fmt.Sprintf("GRANT ALL PRIVILEGES ON %s.* TO `%s`@`%%`;", name, r.username))
if res.Error != nil {
return nil, fmt.Errorf("grant privileges in Mysql database error: %v", res.Error)
}
res = instance.Exec(fmt.Sprintf("GRANT ALL PRIVILEGES ON %s.* TO `%s`@`%%`;", name, r.username))
if res.Error != nil {
color.Errorf("grant privileges in Mysql database error: %v", res.Error)
}
}()

mysqlImpl := NewMysqlImpl(name, r.username, r.password)
mysqlImpl.containerID = r.containerID
mysqlImpl.port = r.port

_, err = mysqlImpl.connect()
if err != nil {
return nil, fmt.Errorf("connect Mysql error: %v", err)
}

return mysqlImpl, nil
}

Expand Down Expand Up @@ -140,6 +140,12 @@ func (r *MysqlImpl) Image(image testing.Image) {
r.image = &image
}

func (r *MysqlImpl) Ready() error {
_, err := r.connect()

return err
}

func (r *MysqlImpl) Stop() error {
if _, err := run(fmt.Sprintf("docker stop %s", r.containerID)); err != nil {
return fmt.Errorf("stop Mysql error: %v", err)
Expand Down
8 changes: 2 additions & 6 deletions support/docker/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,12 @@ INSERT INTO users (name) VALUES ('goravel');
res = instance.Raw(fmt.Sprintf("SELECT count(*) FROM information_schema.tables WHERE table_schema = '%s' and table_name = 'users';", s.mysql.Config().Database)).Scan(&count)
s.Nil(res.Error)
s.Equal(int64(0), count)
s.Nil(s.mysql.Stop())
}

func (s *MysqlTestSuite) TestDatabase() {
s.NoError(s.mysql.Build())

databaseDriver, err := s.mysql.Database("another")
s.NoError(err)
s.NotNil(databaseDriver)
s.NoError(databaseDriver.Stop())

s.Nil(s.mysql.Stop())
}

func (s *MysqlTestSuite) TestDriver() {
Expand Down
35 changes: 18 additions & 17 deletions support/docker/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/goravel/framework/contracts/database"
"github.com/goravel/framework/contracts/testing"
"github.com/goravel/framework/support/color"
)

type PostgresImpl struct {
Expand Down Expand Up @@ -53,10 +54,6 @@ func (r *PostgresImpl) Build() error {
r.containerID = containerID
r.port = getExposedPort(exposedPorts, 5432)

if _, err := r.connect(); err != nil {
return fmt.Errorf("connect Postgres error: %v", err)
}

return nil
}

Expand All @@ -72,25 +69,23 @@ func (r *PostgresImpl) Config() testing.DatabaseConfig {
}

func (r *PostgresImpl) Database(name string) (testing.DatabaseDriver, error) {
instance, err := r.connect()
if err != nil {
return nil, fmt.Errorf("connect Postgres error: %v", err)
}
go func() {
instance, err := r.connect()
if err != nil {
color.Errorf("connect Postgres error: %v", err)
return
}

res := instance.Exec(fmt.Sprintf(`CREATE DATABASE "%s";`, name))
if res.Error != nil {
return nil, fmt.Errorf("create Postgres database error: %v", res.Error)
}
res := instance.Exec(fmt.Sprintf(`CREATE DATABASE "%s";`, name))
if res.Error != nil {
color.Errorf("create Postgres database error: %v", res.Error)
}
}()

postgresImpl := NewPostgresImpl(name, r.username, r.password)
postgresImpl.containerID = r.containerID
postgresImpl.port = r.port

_, err = postgresImpl.connect()
if err != nil {
return nil, fmt.Errorf("connect Postgres error: %v", err)
}

return postgresImpl, nil
}

Expand Down Expand Up @@ -119,6 +114,12 @@ func (r *PostgresImpl) Image(image testing.Image) {
r.image = &image
}

func (r *PostgresImpl) Ready() error {
_, err := r.connect()

return err
}

func (r *PostgresImpl) Stop() error {
if _, err := run(fmt.Sprintf("docker stop %s", r.containerID)); err != nil {
return fmt.Errorf("stop Postgres error: %v", err)
Expand Down
8 changes: 2 additions & 6 deletions support/docker/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,12 @@ func (s *PostgresTestSuite) TestBuild() {
`).Scan(&count)
s.Nil(res.Error)
s.Equal(int64(0), count)
s.Nil(s.postgres.Stop())
}

func (s *PostgresTestSuite) TestDatabase() {
s.NoError(s.postgres.Build())

databaseDriver, err := s.postgres.Database("another")
s.NoError(err)
s.NotNil(databaseDriver)
s.NoError(databaseDriver.Stop())

s.Nil(s.postgres.Stop())
}

func (s *PostgresTestSuite) TestDriver() {
Expand Down
6 changes: 6 additions & 0 deletions support/docker/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ func (r *SqliteImpl) Image(image testing.Image) {
r.image = &image
}

func (r *SqliteImpl) Ready() error {
_, err := r.connect()

return err
}

func (r *SqliteImpl) Stop() error {
if err := file.Remove(r.database); err != nil {
return fmt.Errorf("stop Sqlite error: %v", err)
Expand Down
9 changes: 2 additions & 7 deletions support/docker/sqlite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,12 @@ INSERT INTO users (name) VALUES ('goravel');
s.Nil(res.Error)
s.Equal(int64(0), count)

s.Nil(s.sqlite.Stop())
}

func (s *SqliteTestSuite) TestDatabase() {
s.NoError(s.sqlite.Build())

databaseDriver, err := s.sqlite.Database("another")
s.NoError(err)
s.NotNil(databaseDriver)
s.NoError(databaseDriver.Stop())
s.NoError(s.sqlite.Stop())

s.Nil(s.sqlite.Stop())
}

func (s *SqliteTestSuite) TestDriver() {
Expand Down
Loading

0 comments on commit d4532b4

Please sign in to comment.