Skip to content

Commit

Permalink
test: added more code coverage for usecase package
Browse files Browse the repository at this point in the history
  • Loading branch information
emeeker2002 committed Sep 12, 2024
1 parent add720a commit db81d73
Show file tree
Hide file tree
Showing 10 changed files with 1,323 additions and 56 deletions.
98 changes: 98 additions & 0 deletions internal/app/app_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package app_test

import (
"crypto/rand"
"fmt"
"math/big"
"os"
"testing"

"go.uber.org/mock/gomock"

"github.com/open-amt-cloud-toolkit/console/config"
"github.com/open-amt-cloud-toolkit/console/internal/app"
)

func getFreePort() (string, error) {
port, err := rand.Int(rand.Reader, big.NewInt(1000))
if err != nil {
return "", err
}

return fmt.Sprintf(":%d", 8000+port.Int64()), nil
}

func teardown() {}

func TestRun(t *testing.T) {
t.Parallel()

ctrl := gomock.NewController(t)
t.Cleanup(ctrl.Finish)
defer ctrl.Finish()

mockDB := NewMockDB(ctrl)
mockHTTPServer := NewMockHTTPServer(ctrl)

port, err := getFreePort()
if err != nil {
t.Fatalf("Failed to get a free port: %v", err)
}

cfg := &config.Config{
Log: config.Log{
Level: "info",
},
DB: config.DB{
URL: "postgres://testuser:testpass@localhost/testdb",
PoolMax: 10,
},
HTTP: config.HTTP{
Port: port,
AllowedOrigins: []string{"*"},
AllowedHeaders: []string{"Content-Type"},
},
App: config.App{
Version: "DEVELOPMENT",
},
}

tests := []struct {
name string
setupMocks func()
setupEnv func()
cfg *config.Config
expectFunc func(t *testing.T)
}{
{
name: "Successful run and shutdown",
setupMocks: func() {
mockDB.EXPECT().Close().Return(nil).Times(1)
mockHTTPServer.EXPECT().Notify().Return(make(chan error)).Times(1)
mockHTTPServer.EXPECT().Shutdown().Return(nil).Times(1)
},
setupEnv: func() {
os.Setenv("GIN_MODE", "release")
},
cfg: cfg,
expectFunc: func(_ *testing.T) {
go func() {
defer teardown()
app.Run(cfg)
}()
},
},
}

for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

tc.setupEnv()
tc.setupMocks()

tc.expectFunc(t)
})
}
}
28 changes: 28 additions & 0 deletions internal/app/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package app

import (
"context"
"database/sql"
"net/http"

"github.com/gorilla/websocket"
)

// DB is an interface for database operations.
type DB interface {
Close() error
QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
}

// HTTPServer is an interface for the HTTP server.
type HTTPServer interface {
Notify() <-chan error
Shutdown() error
Start() error
}

// WebSocketUpgrader is an interface for WebSocket upgrading.
type WebSocketUpgrader interface {
Upgrade(w http.ResponseWriter, r *http.Request, responseHeader http.Header) (*websocket.Conn, error)
}
200 changes: 200 additions & 0 deletions internal/app/mocks_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit db81d73

Please sign in to comment.