refactor platform logic modules
This commit is contained in:
22
backend/api/internal/logic/platform/dashboard/dashboard.go
Normal file
22
backend/api/internal/logic/platform/dashboard/dashboard.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package dashboard
|
||||
|
||||
import (
|
||||
"git.apinb.com/bsm-sdk/core/infra"
|
||||
"git.apinb.com/heqiapp/platforms/backend/api/internal/models"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// PingHello 返回匿名健康状态。
|
||||
func PingHello(ctx *gin.Context) {
|
||||
infra.Response.Success(ctx, gin.H{"service": "platform-api", "status": "ok"})
|
||||
}
|
||||
|
||||
// DashboardOverview 返回平台总后台的运营概览数据。
|
||||
func DashboardOverview(ctx *gin.Context) {
|
||||
overview, err := models.GetDashboardOverview()
|
||||
if err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
infra.Response.Success(ctx, overview)
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package dashboard
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"testing"
|
||||
|
||||
"git.apinb.com/heqiapp/platforms/backend/api/internal/models"
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
)
|
||||
|
||||
func TestDashboardOverviewReturnsZeroValuesForAnEmptyDatabase(t *testing.T) {
|
||||
_, mock := setupDashboardDatabase(t)
|
||||
for _, query := range []string{
|
||||
`SELECT count\(\*\) FROM "gas_basic" WHERE status = \$1`,
|
||||
`SELECT count\(\*\) FROM "delivery_basic" WHERE status = \$1`,
|
||||
`SELECT count\(\*\) FROM "staff_account" WHERE work_status = \$1`,
|
||||
`SELECT count\(\*\) FROM "user_account" WHERE status = \$1`,
|
||||
} {
|
||||
mock.ExpectQuery(regexp.MustCompile(query).String()).WithArgs(sqlmock.AnyArg()).WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(0))
|
||||
}
|
||||
|
||||
overview, err := models.GetDashboardOverview()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if overview != (models.DashboardOverview{}) {
|
||||
t.Fatalf("empty dashboard = %#v, want zero values", overview)
|
||||
}
|
||||
assertDashboardMockExpectations(t, mock)
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package dashboard
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.apinb.com/heqiapp/platforms/backend/api/internal/impl"
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func setupDashboardDatabase(t *testing.T) (*gorm.DB, sqlmock.Sqlmock) {
|
||||
t.Helper()
|
||||
previous := impl.DBService
|
||||
sqlDatabase, mock, err := sqlmock.New()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
database, err := gorm.Open(postgres.New(postgres.Config{Conn: sqlDatabase}), &gorm.Config{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
impl.DBService = database
|
||||
t.Cleanup(func() {
|
||||
impl.DBService = previous
|
||||
_ = sqlDatabase.Close()
|
||||
})
|
||||
return database, mock
|
||||
}
|
||||
|
||||
func assertDashboardMockExpectations(t *testing.T, mock sqlmock.Sqlmock) {
|
||||
t.Helper()
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user