-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: added more code coverage for usecase package
- Loading branch information
1 parent
add720a
commit db81d73
Showing
10 changed files
with
1,323 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.