Skip to content

Commit

Permalink
Add CI (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
hwbrzzl authored Feb 10, 2023
1 parent 62df799 commit 6c56e0d
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 39 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Test
on:
push:
branches:
- master
pull_request:
jobs:
build:
strategy:
matrix:
go: [ '1.20', '1.19', '1.18' ]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go }}
- name: Tests
run: go mod tidy && go test -covermode=atomic -coverprofile="coverage.out" ./...
8 changes: 4 additions & 4 deletions database/gorm/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func MysqlDocker() (*dockertest.Pool, *dockertest.Resource, ormcontract.DB, erro
return nil, nil, nil, err
}

_ = resource.Expire(60)
_ = resource.Expire(600)

if err := pool.Retry(func() error {
return initDatabase(ormcontract.DriverMysql, resource.GetPort("3306/tcp"))
Expand Down Expand Up @@ -71,7 +71,7 @@ func PostgresqlDocker() (*dockertest.Pool, *dockertest.Resource, ormcontract.DB,
return nil, nil, nil, err
}

_ = resource.Expire(60)
_ = resource.Expire(600)

if err := pool.Retry(func() error {
return initDatabase(ormcontract.DriverPostgresql, resource.GetPort("5432/tcp"))
Expand Down Expand Up @@ -105,7 +105,7 @@ func SqliteDocker() (*dockertest.Pool, *dockertest.Resource, ormcontract.DB, err
return nil, nil, nil, err
}

_ = resource.Expire(60)
_ = resource.Expire(600)

var db ormcontract.DB
if err := pool.Retry(func() error {
Expand Down Expand Up @@ -141,7 +141,7 @@ func SqlserverDocker() (*dockertest.Pool, *dockertest.Resource, ormcontract.DB,
return nil, nil, nil, err
}

_ = resource.Expire(60)
_ = resource.Expire(600)

if err := pool.Retry(func() error {
return initDatabase(ormcontract.DriverSqlserver, resource.GetPort("1433/tcp"))
Expand Down
64 changes: 30 additions & 34 deletions event/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (

"github.com/stretchr/testify/suite"

"github.com/goravel/framework/config"
"github.com/goravel/framework/contracts/event"
eventcontract "github.com/goravel/framework/contracts/event"
"github.com/goravel/framework/facades"
"github.com/goravel/framework/queue"
testingdocker "github.com/goravel/framework/testing/docker"
"github.com/goravel/framework/testing/mock"
)

var (
Expand All @@ -26,6 +26,7 @@ var (

type EventTestSuite struct {
suite.Suite
redisPort string
}

func TestEventTestSuite(t *testing.T) {
Expand All @@ -34,11 +35,12 @@ func TestEventTestSuite(t *testing.T) {
log.Fatalf("Get redis error: %s", err)
}

initConfig(redisResource.GetPort("6379/tcp"))
facades.Queue = queue.NewApplication()
facades.Event = NewApplication()

suite.Run(t, new(EventTestSuite))
suite.Run(t, &EventTestSuite{
redisPort: redisResource.GetPort("6379/tcp"),
})

if err := redisPool.Purge(redisResource); err != nil {
log.Fatalf("Could not purge resource: %s", err)
Expand All @@ -50,6 +52,17 @@ func (s *EventTestSuite) SetupTest() {
}

func (s *EventTestSuite) TestEvent() {
mockConfig := mock.Config()
mockConfig.On("GetString", "app.name").Return("goravel").Times(3)
mockConfig.On("GetString", "queue.default").Return("redis").Twice()
mockConfig.On("GetString", "queue.connections.redis.queue", "default").Return("default").Times(3)
mockConfig.On("GetString", "queue.connections.redis.driver").Return("redis").Times(3)
mockConfig.On("GetString", "queue.connections.redis.connection").Return("default").Twice()
mockConfig.On("GetString", "database.redis.default.host").Return("localhost").Twice()
mockConfig.On("GetString", "database.redis.default.password").Return("").Twice()
mockConfig.On("GetString", "database.redis.default.port").Return(s.redisPort).Twice()
mockConfig.On("GetInt", "database.redis.default.database").Return(0).Twice()

facades.Event.Register(map[event.Event][]event.Listener{
&TestEvent{}: {
&TestSyncListener{},
Expand Down Expand Up @@ -78,9 +91,22 @@ func (s *EventTestSuite) TestEvent() {
time.Sleep(1 * time.Second)
s.Equal(1, testSyncListener)
s.Equal(1, testAsyncListener)

mockConfig.AssertExpectations(s.T())
}

func (s *EventTestSuite) TestCancelEvent() {
mockConfig := mock.Config()
mockConfig.On("GetString", "app.name").Return("goravel").Twice()
mockConfig.On("GetString", "queue.default").Return("redis").Once()
mockConfig.On("GetString", "queue.connections.redis.queue", "default").Return("default").Twice()
mockConfig.On("GetString", "queue.connections.redis.driver").Return("redis").Once()
mockConfig.On("GetString", "queue.connections.redis.connection").Return("default").Once()
mockConfig.On("GetString", "database.redis.default.host").Return("localhost").Once()
mockConfig.On("GetString", "database.redis.default.password").Return("").Once()
mockConfig.On("GetString", "database.redis.default.port").Return(s.redisPort).Once()
mockConfig.On("GetInt", "database.redis.default.database").Return(0).Once()

facades.Event.Register(map[event.Event][]event.Listener{
&TestCancelEvent{}: {
&TestCancelListener{},
Expand Down Expand Up @@ -109,38 +135,8 @@ func (s *EventTestSuite) TestCancelEvent() {
time.Sleep(1 * time.Second)
s.Equal(1, testCancelListener)
s.Equal(0, testCancelAfterListener)
}

func initConfig(redisPort string) {
application := config.NewApplication("../.env")
application.Add("app", map[string]any{
"name": "goravel",
})
application.Add("queue", map[string]any{
"default": "redis",
"connections": map[string]any{
"sync": map[string]any{
"driver": "sync",
},
"redis": map[string]any{
"driver": "redis",
"connection": "default",
"queue": "default",
},
},
})
application.Add("database", map[string]any{
"redis": map[string]any{
"default": map[string]any{
"host": "localhost",
"password": "",
"port": redisPort,
"database": 0,
},
},
})

facades.Config = application
mockConfig.AssertExpectations(s.T())
}

type TestEvent struct {
Expand Down
2 changes: 1 addition & 1 deletion testing/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func Redis() (*dockertest.Pool, *dockertest.Resource, error) {
if err != nil {
return nil, nil, err
}
_ = resource.Expire(60)
_ = resource.Expire(600)

return pool, resource, nil
}

0 comments on commit 6c56e0d

Please sign in to comment.