54 lines
2.2 KiB
Go
54 lines
2.2 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"git.5iot.com/iotlcm-public/iotlcm-app/internal/model/do"
|
|
"git.5iot.com/iotlcm-public/iotlcm-app/internal/model/entity"
|
|
|
|
_ "github.com/gogf/gf/contrib/drivers/sqlite/v2"
|
|
"github.com/gogf/gf/v2/database/gdb"
|
|
)
|
|
|
|
func TestAppWarningNotifySummaryScopesConfiguration(t *testing.T) {
|
|
ctx := context.Background()
|
|
db, err := gdb.New(gdb.ConfigNode{Type: "sqlite", Name: filepath.Join(t.TempDir(), "warning.db")})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = db.Close(ctx) })
|
|
_, err = db.Exec(ctx, "CREATE TABLE t_warn_config (id INTEGER PRIMARY KEY, tenant_id TEXT, created_by INTEGER, item_type INTEGER, warn_unit INTEGER, is_enabled INTEGER, app_notify_enabled INTEGER)")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
configs := []do.TWarnConfig{
|
|
{TenantId: "tenant-a", CreatedBy: 11, ItemType: 1, WarnUnit: 1, IsEnabled: 1, AppNotifyEnabled: 1},
|
|
{TenantId: "tenant-a", CreatedBy: 11, ItemType: 1, WarnUnit: 1, IsEnabled: 1, AppNotifyEnabled: 0},
|
|
{TenantId: "tenant-b", CreatedBy: 11, ItemType: 1, WarnUnit: 1, IsEnabled: 1, AppNotifyEnabled: 1},
|
|
{TenantId: "tenant-a", CreatedBy: 12, ItemType: 1, WarnUnit: 1, IsEnabled: 1, AppNotifyEnabled: 1},
|
|
{TenantId: "tenant-a", CreatedBy: 11, ItemType: 1, WarnUnit: 1, IsEnabled: 0, AppNotifyEnabled: 1},
|
|
{TenantId: "tenant-a", CreatedBy: 11, ItemType: 1, WarnUnit: 3, IsEnabled: 1, AppNotifyEnabled: 1},
|
|
{TenantId: "tenant-a", CreatedBy: 11, ItemType: 2, WarnUnit: 1, IsEnabled: 1, AppNotifyEnabled: 1},
|
|
}
|
|
if _, err = db.Model("t_warn_config").Ctx(ctx).Data(configs).Insert(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var summary appWarningNotifySummary
|
|
user := &entity.SysUser{TenantId: "tenant-a", UserId: 11}
|
|
if err = appWarningNotifySummaryModel(db.Model("t_warn_config").Ctx(ctx), user).Scan(&summary); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if summary.ActiveConfigCount != 2 || summary.NotifyConfigCount != 1 {
|
|
t.Fatalf("summary = %+v, expected active=2, notify=1", summary)
|
|
}
|
|
user.UserId = 99
|
|
if err = appWarningNotifySummaryModel(db.Model("t_warn_config").Ctx(ctx), user).Scan(&summary); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if summary.ActiveConfigCount != 0 || summary.NotifyConfigCount != 0 {
|
|
t.Fatalf("empty summary = %+v, expected zero counts", summary)
|
|
}
|
|
}
|