190 lines
6.1 KiB
Go
190 lines
6.1 KiB
Go
package test
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
jsontime "github.com/liamylian/jsontime/v2/v2"
|
|
"github.com/stretchr/testify/assert"
|
|
"ifms/internal/mods/rbac/schema"
|
|
trace_schema "ifms/internal/mods/trace/schema"
|
|
"ifms/pkg/convert"
|
|
"ifms/pkg/crypto/hash"
|
|
"ifms/pkg/util"
|
|
"net/http"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestUser(t *testing.T) {
|
|
e := tester(t)
|
|
|
|
menuFormItem := schema.MenuForm{
|
|
Code: "user",
|
|
Name: "User management",
|
|
Description: "User management",
|
|
Sequence: 7,
|
|
Type: "page",
|
|
Path: "/system/user",
|
|
Properties: `{"icon":"user"}`,
|
|
Status: schema.MenuStatusEnabled,
|
|
}
|
|
|
|
var menu schema.Menu
|
|
e.POST(baseAPI + "/menus").WithJSON(menuFormItem).
|
|
Expect().Status(http.StatusOK).JSON().Decode(&util.ResponseResult{Data: &menu})
|
|
|
|
assert := assert.New(t)
|
|
assert.NotEmpty(menu.ID)
|
|
assert.Equal(menuFormItem.Code, menu.Code)
|
|
assert.Equal(menuFormItem.Name, menu.Name)
|
|
assert.Equal(menuFormItem.Description, menu.Description)
|
|
assert.Equal(menuFormItem.Sequence, menu.Sequence)
|
|
assert.Equal(menuFormItem.Type, menu.Type)
|
|
assert.Equal(menuFormItem.Path, menu.Path)
|
|
assert.Equal(menuFormItem.Properties, menu.Properties)
|
|
assert.Equal(menuFormItem.Status, menu.Status)
|
|
|
|
roleFormItem := schema.RoleForm{
|
|
Code: "user",
|
|
Name: "Normal",
|
|
//Menus: schema.RoleMenus{
|
|
// {MenuID: menu.ID},
|
|
//},
|
|
Description: "Normal",
|
|
Sequence: 8,
|
|
}
|
|
|
|
var role schema.Role
|
|
e.POST(baseAPI + "/roles").WithJSON(roleFormItem).Expect().Status(http.StatusOK).JSON().Decode(&util.ResponseResult{Data: &role})
|
|
assert.NotEmpty(role.ID)
|
|
assert.Equal(roleFormItem.Code, role.Code)
|
|
assert.Equal(roleFormItem.Name, role.Name)
|
|
assert.Equal(roleFormItem.Description, role.Description)
|
|
assert.Equal(roleFormItem.Sequence, role.Sequence)
|
|
//assert.Equal(len(roleFormItem.Menus), len(role.Menus))
|
|
|
|
userFormItem := schema.UserForm{
|
|
Username: "test",
|
|
Name: "Test",
|
|
Password: hash.MD5String("test"),
|
|
Phone: "0720",
|
|
Email: "test@gmail.com",
|
|
Remark: "test user",
|
|
Roles: schema.UserRoles{{RoleID: role.ID}},
|
|
}
|
|
|
|
var user schema.User
|
|
e.POST(baseAPI + "/users").WithJSON(userFormItem).Expect().Status(http.StatusOK).JSON().Decode(&util.ResponseResult{Data: &user})
|
|
assert.NotEmpty(user.ID)
|
|
assert.Equal(userFormItem.Username, user.Username)
|
|
assert.Equal(userFormItem.Name, user.Name)
|
|
assert.Equal(userFormItem.Phone, user.Phone)
|
|
assert.Equal(userFormItem.Email, user.Email)
|
|
assert.Equal(userFormItem.Remark, user.Remark)
|
|
assert.Equal(len(userFormItem.Roles), len(user.Roles))
|
|
|
|
var users schema.Users
|
|
e.GET(baseAPI+"/users").WithQuery("username", userFormItem.Username).Expect().Status(http.StatusOK).JSON().Decode(&util.ResponseResult{Data: &users})
|
|
assert.GreaterOrEqual(len(users), 1)
|
|
|
|
newName := "Test 1"
|
|
newStatus := schema.UserStatusFreezed
|
|
user.Name = newName
|
|
e.PUT(baseAPI + "/users/" + convert.ToString(user.ID)).WithJSON(user).Expect().Status(http.StatusOK)
|
|
|
|
var getUser schema.User
|
|
e.GET(baseAPI + "/users/" + convert.ToString(user.ID)).Expect().Status(http.StatusOK).JSON().Decode(&util.ResponseResult{Data: &getUser})
|
|
assert.Equal(newName, getUser.Name)
|
|
assert.Equal(newStatus, getUser.Status)
|
|
|
|
e.DELETE(baseAPI + "/users/" + convert.ToString(user.ID)).Expect().Status(http.StatusOK)
|
|
e.GET(baseAPI + "/users/" + convert.ToString(user.ID)).Expect().Status(http.StatusNotFound)
|
|
|
|
e.DELETE(baseAPI + "/roles/" + convert.ToString(role.ID)).Expect().Status(http.StatusOK)
|
|
e.GET(baseAPI + "/roles/" + convert.ToString(role.ID)).Expect().Status(http.StatusNotFound)
|
|
|
|
e.DELETE(baseAPI + "/menus/" + menu.ID).Expect().Status(http.StatusOK)
|
|
e.GET(baseAPI + "/menus/" + menu.ID).Expect().Status(http.StatusNotFound)
|
|
}
|
|
func TestTenant(t *testing.T) {
|
|
//db, _, err := wirex.InitDB(context.Background())
|
|
//if err != nil {
|
|
// fmt.Println(err)
|
|
// return
|
|
//}
|
|
//user := &dal.User{DB: db}
|
|
//user.Get(context.Background(), 1, schema.UserQueryOptions{})
|
|
|
|
md5 := util.EncryptToMD5("111111")
|
|
fmt.Println(md5)
|
|
|
|
//dt := "2020-11-08T08:18:46Z"
|
|
//date, err := util.SimpleCSTDate(dt)
|
|
//
|
|
//if err != nil {
|
|
// fmt.Println(err)
|
|
// return
|
|
//}
|
|
//fmt.Println(date)
|
|
}
|
|
|
|
type MyStruct struct {
|
|
Timestamp trace_schema.CustomTime `json:"timestamp"`
|
|
}
|
|
|
|
func TestJSON(t *testing.T) {
|
|
// 示例 JSON 数据
|
|
jsonData := `{"timestamp": "2020-11-08T08:18:46Z"}`
|
|
|
|
// 反序列化 JSON 数据
|
|
var data MyStruct
|
|
if err := json.Unmarshal([]byte(jsonData), &data); err != nil {
|
|
fmt.Println("Error unmarshaling JSON:", err)
|
|
return
|
|
}
|
|
fmt.Println("Unmarshaled time:", data.Timestamp.Time) // 输出: 2020-11-08 08:18:46 +0000 UTC
|
|
|
|
// 序列化回 JSON
|
|
jsonDataOut, err := json.Marshal(data)
|
|
if err != nil {
|
|
fmt.Println("Error marshaling JSON:", err)
|
|
return
|
|
}
|
|
fmt.Println("Marshaled JSON:", string(jsonDataOut)) // 输出: {"timestamp":"2020-11-08 08:18:46"}
|
|
now := time.Now()
|
|
data = MyStruct{trace_schema.CustomTime{Time: now}}
|
|
//data.Timestamp.Time = time.Now()
|
|
fmt.Println(data)
|
|
|
|
}
|
|
|
|
type Factory struct {
|
|
ID int64 `json:"id" gorm:"primaryKey;comment:Unique ID;"` // Unique ID
|
|
TenantId string `json:"tenant_id" gorm:"size:36;comment:租户ID;"` // 租户ID
|
|
Address string `json:"address" gorm:"size:255;comment:工厂地址;"` // 工厂地址
|
|
WorkShopLayout string `json:"work_shop_layout" gorm:"type:text;comment:工厂布局;"` // 工厂布局
|
|
Notes string `json:"notes" gorm:"size:1024;comment:备注说明;"` // 备注说明
|
|
Operator string `json:"operator" gorm:"size:50;comment:操作人;"` // 操作人
|
|
CreatedAt time.Time `json:"created_at" gorm:"index;comment:Upload time;"` // Upload time
|
|
}
|
|
|
|
func TestJSON1(t *testing.T) {
|
|
jsonData := &Factory{
|
|
ID: 1,
|
|
TenantId: "888",
|
|
Address: "1号车间",
|
|
CreatedAt: time.Now(),
|
|
}
|
|
//b, _ := util.MarshalWithTimeFormat(jsonData, "2006-01-02 15:04:05")
|
|
//fmt.Println(string(b))
|
|
|
|
//fromInt := decimal.NewFromInt(1)
|
|
//fmt.Println(fromInt)
|
|
|
|
jsontime.SetDefaultTimeFormat(time.DateTime, time.Local)
|
|
fmt.Println(time.Local)
|
|
var json = jsontime.ConfigWithCustomTimeFormat
|
|
bytes, _ := json.Marshal(jsonData)
|
|
fmt.Printf("%s", bytes)
|
|
}
|