|
|
@@ -0,0 +1,376 @@
|
|
|
+package testutil
|
|
|
+
|
|
|
+import (
|
|
|
+ "encoding/json"
|
|
|
+ "fmt"
|
|
|
+ "net/http"
|
|
|
+ "testing"
|
|
|
+ "time"
|
|
|
+
|
|
|
+ "wails-app/internal/dao"
|
|
|
+ "wails-app/internal/global"
|
|
|
+ "wails-app/internal/model/common/response"
|
|
|
+)
|
|
|
+
|
|
|
+// TestVehicleReEntry 同一车辆重复入场应被拦截
|
|
|
+func TestVehicleReEntry(t *testing.T) {
|
|
|
+ env := InitTestEnv(t)
|
|
|
+ createVehicle(t, env, "JingR00001", "RFID_REENTRY", 1)
|
|
|
+ entryResp, entryBody := env.DoPost("/vehicle/entry", map[string]interface{}{
|
|
|
+ "plate_number": "JingR00001", "rfid_tag": "RFID_REENTRY",
|
|
|
+ "parking_lot_id": 1, "parking_space_id": 1, "entry_image": "test.jpg",
|
|
|
+ })
|
|
|
+ AssertResponse(t, entryResp, entryBody, http.StatusOK, response.SUCCESS)
|
|
|
+
|
|
|
+ _, entryBody2 := env.DoPost("/vehicle/entry", map[string]interface{}{
|
|
|
+ "plate_number": "JingR00001", "rfid_tag": "RFID_REENTRY",
|
|
|
+ "parking_lot_id": 1, "parking_space_id": 2, "entry_image": "test2.jpg",
|
|
|
+ })
|
|
|
+ var result struct { Code int `json:"code"`; Msg string `json:"msg"` }
|
|
|
+ json.Unmarshal(entryBody2, &result)
|
|
|
+ if result.Code == response.SUCCESS {
|
|
|
+ t.Fatalf("BUG: Vehicle re-entry was NOT blocked! code=%d msg=%s", result.Code, result.Msg)
|
|
|
+ }
|
|
|
+ fmt.Println("PASS: Re-entry attempt correctly rejected")
|
|
|
+}
|
|
|
+
|
|
|
+// TestExitWithoutEntry 未入场车辆尝试出场应返回错误
|
|
|
+func TestExitWithoutEntry(t *testing.T) {
|
|
|
+ env := InitTestEnv(t)
|
|
|
+ _, exitPreviewBody := env.DoPost("/vehicle/exit/preview", map[string]interface{}{
|
|
|
+ "plate_number": "JingZ99999", "rfid_tag": "RFID_NOENTRY",
|
|
|
+ })
|
|
|
+ var result struct { Code int `json:"code"`; Msg string `json:"msg"` }
|
|
|
+ json.Unmarshal(exitPreviewBody, &result)
|
|
|
+ if result.Code == response.SUCCESS {
|
|
|
+ t.Fatalf("BUG: Exit preview succeeded for non-entered vehicle!")
|
|
|
+ }
|
|
|
+ fmt.Println("PASS: Exit without entry correctly rejected")
|
|
|
+}
|
|
|
+
|
|
|
+// TestPaymentInsufficientAmount 支付金额不足时的行为
|
|
|
+func TestPaymentInsufficientAmount(t *testing.T) {
|
|
|
+ env := InitTestEnv(t)
|
|
|
+ createVehicle(t, env, "JingP00001", "RFID_PAY", 1)
|
|
|
+ _, _ = env.DoPost("/vehicle/entry", map[string]interface{}{
|
|
|
+ "plate_number": "JingP00001", "rfid_tag": "RFID_PAY",
|
|
|
+ "parking_lot_id": 1, "parking_space_id": 1, "entry_image": "test.jpg",
|
|
|
+ })
|
|
|
+ entryTime := time.Now().Add(-3 * time.Hour)
|
|
|
+ global.GVA_DB.Model(&dao.VehicleRecord{}).Where("plate_number = ?", "JingP00001").Update("entry_time", entryTime)
|
|
|
+ _, exitConfirmBody := env.DoPost("/vehicle/exit/confirm", map[string]interface{}{
|
|
|
+ "plate_number": "JingP00001", "rfid_tag": "RFID_PAY",
|
|
|
+ "payment_method": "cash", "paid_amount": 0.0,
|
|
|
+ })
|
|
|
+ var result struct { Code int `json:"code"`; Msg string `json:"msg"` }
|
|
|
+ json.Unmarshal(exitConfirmBody, &result)
|
|
|
+ fmt.Printf("INFO: Insufficient payment response: code=%d msg=%s\n", result.Code, result.Msg)
|
|
|
+}
|
|
|
+
|
|
|
+// TestVIPExpired 过期 VIP 应恢复普通计费
|
|
|
+func TestVIPExpired(t *testing.T) {
|
|
|
+ env := InitTestEnv(t)
|
|
|
+ ownerResp, ownerBody := env.DoPost("/owner/create", map[string]interface{}{
|
|
|
+ "owner_name": "VIP Expired Test", "owner_surname": "VIP",
|
|
|
+ "owner_phone": "13900000001", "is_vip": true,
|
|
|
+ "vip_expire_time": time.Now().Add(-24*time.Hour).Format(time.RFC3339),
|
|
|
+ })
|
|
|
+ AssertResponse(t, ownerResp, ownerBody, http.StatusOK, response.SUCCESS)
|
|
|
+
|
|
|
+ var owner dao.Owner
|
|
|
+ env.DB.Where("owner_phone = ?", "13900000001").First(&owner)
|
|
|
+ createVehicleWithOwner(t, env, "JingV00001", "RFID_VIP_EXP", owner.ID)
|
|
|
+
|
|
|
+ _, _ = env.DoPost("/vehicle/entry", map[string]interface{}{
|
|
|
+ "plate_number": "JingV00001", "rfid_tag": "RFID_VIP_EXP",
|
|
|
+ "parking_lot_id": 1, "parking_space_id": 1, "entry_image": "test.jpg",
|
|
|
+ })
|
|
|
+ entryTime := time.Now().Add(-3 * time.Hour)
|
|
|
+ global.GVA_DB.Model(&dao.VehicleRecord{}).Where("plate_number = ?", "JingV00001").Update("entry_time", entryTime)
|
|
|
+
|
|
|
+ exitResp, exitBody := env.DoPost("/vehicle/exit/preview", map[string]interface{}{
|
|
|
+ "plate_number": "JingV00001", "rfid_tag": "RFID_VIP_EXP",
|
|
|
+ })
|
|
|
+ AssertResponse(t, exitResp, exitBody, http.StatusOK, response.SUCCESS)
|
|
|
+ var result struct {
|
|
|
+ Data struct {
|
|
|
+ Fee float64 `json:"fee"`
|
|
|
+ } `json:"data"`
|
|
|
+ }
|
|
|
+ json.Unmarshal(exitBody, &result)
|
|
|
+ if result.Data.Fee == 0 {
|
|
|
+ t.Fatalf("BUG: Expired VIP still getting free parking! Fee should be > 0")
|
|
|
+ }
|
|
|
+ fmt.Printf("PASS: Expired VIP charged correctly, fee=%.2f\n", result.Data.Fee)
|
|
|
+}
|
|
|
+
|
|
|
+// TestFreeDuration 免费时长内不应收费
|
|
|
+func TestFreeDuration(t *testing.T) {
|
|
|
+ env := InitTestEnv(t)
|
|
|
+ createVehicle(t, env, "JingF00001", "RFID_FREE", 1)
|
|
|
+ _, _ = env.DoPost("/vehicle/entry", map[string]interface{}{
|
|
|
+ "plate_number": "JingF00001", "rfid_tag": "RFID_FREE",
|
|
|
+ "parking_lot_id": 1, "parking_space_id": 1, "entry_image": "test.jpg",
|
|
|
+ })
|
|
|
+ entryTime := time.Now().Add(-10 * time.Minute)
|
|
|
+ global.GVA_DB.Model(&dao.VehicleRecord{}).Where("plate_number = ?", "JingF00001").Update("entry_time", entryTime)
|
|
|
+ exitResp, exitBody := env.DoPost("/vehicle/exit/preview", map[string]interface{}{
|
|
|
+ "plate_number": "JingF00001", "rfid_tag": "RFID_FREE",
|
|
|
+ })
|
|
|
+ AssertResponse(t, exitResp, exitBody, http.StatusOK, response.SUCCESS)
|
|
|
+ var preview struct { Fee float64 `json:"fee"` }
|
|
|
+ json.Unmarshal(exitBody, &preview)
|
|
|
+ if preview.Fee != 0 {
|
|
|
+ t.Fatalf("BUG: Vehicle within free duration should have zero fee, got %.2f", preview.Fee)
|
|
|
+ }
|
|
|
+ fmt.Println("PASS: Free duration correctly applied")
|
|
|
+}
|
|
|
+
|
|
|
+// TestDailyCap 超过每日封顶费用应封顶
|
|
|
+func TestDailyCap(t *testing.T) {
|
|
|
+ env := InitTestEnv(t)
|
|
|
+ createVehicle(t, env, "JingD00001", "RFID_CAP", 1)
|
|
|
+ _, _ = env.DoPost("/vehicle/entry", map[string]interface{}{
|
|
|
+ "plate_number": "JingD00001", "rfid_tag": "RFID_CAP",
|
|
|
+ "parking_lot_id": 1, "parking_space_id": 1, "entry_image": "test.jpg",
|
|
|
+ })
|
|
|
+ entryTime := time.Now().Add(-24 * time.Hour)
|
|
|
+ global.GVA_DB.Model(&dao.VehicleRecord{}).Where("plate_number = ?", "JingD00001").Update("entry_time", entryTime)
|
|
|
+ exitResp, exitBody := env.DoPost("/vehicle/exit/preview", map[string]interface{}{
|
|
|
+ "plate_number": "JingD00001", "rfid_tag": "RFID_CAP",
|
|
|
+ })
|
|
|
+ AssertResponse(t, exitResp, exitBody, http.StatusOK, response.SUCCESS)
|
|
|
+ var preview struct { Fee float64 `json:"fee"` }
|
|
|
+ json.Unmarshal(exitBody, &preview)
|
|
|
+ if preview.Fee > 50.0 {
|
|
|
+ t.Fatalf("BUG: Fee exceeds daily cap! Expected <= 50, got %.2f", preview.Fee)
|
|
|
+ }
|
|
|
+ fmt.Printf("PASS: Daily cap applied correctly, fee=%.2f\n", preview.Fee)
|
|
|
+}
|
|
|
+
|
|
|
+// TestNoFeeConfig 无收费配置时默认计费
|
|
|
+func TestNoFeeConfig(t *testing.T) {
|
|
|
+ env := InitTestEnv(t)
|
|
|
+ var normalType dao.VehicleType
|
|
|
+ env.DB.Where("is_system = ? AND name != ?", true, "临时车").First(&normalType)
|
|
|
+ if normalType.ID == 0 {
|
|
|
+ normalType = dao.VehicleType{Name: "特殊车", Remarks: "无收费配置"}
|
|
|
+ env.DB.Create(&normalType)
|
|
|
+ }
|
|
|
+ env.DB.Where("vehicle_type_id = ?", normalType.ID).Delete(&dao.FeeConfig{})
|
|
|
+ createVehicleWithOwner(t, env, "JingN00001", "RFID_NOFEE", normalType.ID)
|
|
|
+ _, _ = env.DoPost("/vehicle/entry", map[string]interface{}{
|
|
|
+ "plate_number": "JingN00001", "rfid_tag": "RFID_NOFEE",
|
|
|
+ "parking_lot_id": 1, "parking_space_id": 1, "entry_image": "test.jpg",
|
|
|
+ })
|
|
|
+ entryTime := time.Now().Add(-1 * time.Hour)
|
|
|
+ global.GVA_DB.Model(&dao.VehicleRecord{}).Where("plate_number = ?", "JingN00001").Update("entry_time", entryTime)
|
|
|
+ exitResp, exitBody := env.DoPost("/vehicle/exit/preview", map[string]interface{}{
|
|
|
+ "plate_number": "JingN00001", "rfid_tag": "RFID_NOFEE",
|
|
|
+ })
|
|
|
+ AssertResponse(t, exitResp, exitBody, http.StatusOK, response.SUCCESS)
|
|
|
+ var result struct {
|
|
|
+ Data struct {
|
|
|
+ Fee float64 `json:"fee"`
|
|
|
+ } `json:"data"`
|
|
|
+ }
|
|
|
+ json.Unmarshal(exitBody, &result)
|
|
|
+ if result.Data.Fee <= 0 {
|
|
|
+ t.Fatalf("BUG: No fee config should use default rate, got fee=%.2f", result.Data.Fee)
|
|
|
+ }
|
|
|
+ fmt.Printf("PASS: Default fee applied correctly, fee=%.2f\n", result.Data.Fee)
|
|
|
+}
|
|
|
+
|
|
|
+// TestShortlistByPlateOrRFID 黑白名单按车牌/RFID查询
|
|
|
+func TestShortlistByPlateOrRFID(t *testing.T) {
|
|
|
+ env := InitTestEnv(t)
|
|
|
+ createVehicle(t, env, "JingS00001", "RFID_FILTER", 1)
|
|
|
+ var vehicle dao.Vehicle
|
|
|
+ env.DB.Where("plate_number = ?", "JingS00001").First(&vehicle)
|
|
|
+ _, _ = env.DoPost("/shortlist/createShortlist", dao.Shortlist{
|
|
|
+ ListType: "黑名单", VehicleId: int(vehicle.ID), ExpirationTime: nil,
|
|
|
+ })
|
|
|
+
|
|
|
+ resp1, body1 := env.DoPost("/shortlist/queryShortlistList", map[string]interface{}{
|
|
|
+ "page": 1, "pageSize": 10, "list_type": "", "plate_number": "JingS00001",
|
|
|
+ })
|
|
|
+ AssertResponse(t, resp1, body1, http.StatusOK, response.SUCCESS)
|
|
|
+ list1 := ParsePageList(body1)
|
|
|
+ if len(list1) < 1 { t.Fatalf("Expected at least 1 shortlist record by plate number") }
|
|
|
+ fmt.Printf("PASS: Shortlist query by plate number returned %d records\n", len(list1))
|
|
|
+
|
|
|
+ resp2, body2 := env.DoPost("/shortlist/queryShortlistList", map[string]interface{}{
|
|
|
+ "page": 1, "pageSize": 10, "list_type": "", "rfid_tag": "RFID_FILTER",
|
|
|
+ })
|
|
|
+ AssertResponse(t, resp2, body2, http.StatusOK, response.SUCCESS)
|
|
|
+ list2 := ParsePageList(body2)
|
|
|
+ if len(list2) < 1 { t.Fatalf("Expected at least 1 shortlist record by RFID") }
|
|
|
+ fmt.Printf("PASS: Shortlist query by RFID returned %d records\n", len(list2))
|
|
|
+}
|
|
|
+
|
|
|
+// TestShortlistUpdate 黑白名单更新
|
|
|
+func TestShortlistUpdate(t *testing.T) {
|
|
|
+ env := InitTestEnv(t)
|
|
|
+ createVehicle(t, env, "JingU00001", "RFID_UPDATE", 1)
|
|
|
+ var vehicle dao.Vehicle
|
|
|
+ env.DB.Where("plate_number = ?", "JingU00001").First(&vehicle)
|
|
|
+ _, _ = env.DoPost("/shortlist/createShortlist", dao.Shortlist{
|
|
|
+ ListType: "白名单", VehicleId: int(vehicle.ID), ExpirationTime: nil,
|
|
|
+ })
|
|
|
+ queryResp, queryBody := env.DoGet("/shortlist/queryAllShortlists", nil)
|
|
|
+ AssertResponse(t, queryResp, queryBody, http.StatusOK, response.SUCCESS)
|
|
|
+ var result struct { Data []map[string]interface{} `json:"data"` }
|
|
|
+ json.Unmarshal(queryBody, &result)
|
|
|
+ if len(result.Data) == 0 { t.Fatalf("No shortlist found") }
|
|
|
+ idVal, ok := result.Data[0]["ID"]; if !ok { idVal = result.Data[0]["id"] }
|
|
|
+ id := int(idVal.(float64))
|
|
|
+ updateResp, updateBody := env.DoPut("/shortlist/updateShortlist", map[string]interface{}{
|
|
|
+ "ID": id, "list_type": "黑名单", "vehicle_id": vehicle.ID,
|
|
|
+ "expiration_time": time.Now().Add(30*24*time.Hour).Format(time.RFC3339),
|
|
|
+ })
|
|
|
+ AssertResponse(t, updateResp, updateBody, http.StatusOK, response.SUCCESS)
|
|
|
+ updatedResp, updatedBody := env.DoGet("/shortlist/queryAllShortlists", nil)
|
|
|
+ AssertResponse(t, updatedResp, updatedBody, http.StatusOK, response.SUCCESS)
|
|
|
+ fmt.Println("PASS: Shortlist update successful")
|
|
|
+}
|
|
|
+
|
|
|
+// TestShortlistDelete 黑白名单删除
|
|
|
+func TestShortlistDelete(t *testing.T) {
|
|
|
+ env := InitTestEnv(t)
|
|
|
+ createVehicle(t, env, "JingDel001", "RFID_DELETE", 1)
|
|
|
+ var vehicle dao.Vehicle
|
|
|
+ env.DB.Where("plate_number = ?", "JingDel001").First(&vehicle)
|
|
|
+ _, _ = env.DoPost("/shortlist/createShortlist", dao.Shortlist{
|
|
|
+ ListType: "黑名单", VehicleId: int(vehicle.ID), ExpirationTime: nil,
|
|
|
+ })
|
|
|
+ queryResp, queryBody := env.DoGet("/shortlist/queryAllShortlists", nil)
|
|
|
+ AssertResponse(t, queryResp, queryBody, http.StatusOK, response.SUCCESS)
|
|
|
+ var result struct { Data []map[string]interface{} `json:"data"` }
|
|
|
+ json.Unmarshal(queryBody, &result)
|
|
|
+ if len(result.Data) == 0 { t.Fatalf("No shortlist found") }
|
|
|
+ idVal, ok := result.Data[0]["ID"]; if !ok { idVal = result.Data[0]["id"] }
|
|
|
+ id := int(idVal.(float64))
|
|
|
+ deleteResp, deleteBody := env.DoDelete(fmt.Sprintf("/shortlist/deleteShortlist?id=%d", id))
|
|
|
+ AssertResponse(t, deleteResp, deleteBody, http.StatusOK, response.SUCCESS)
|
|
|
+ fmt.Println("PASS: Shortlist delete successful")
|
|
|
+}
|
|
|
+
|
|
|
+// TestOwnerVehicleRelationship 车主关联车辆查询
|
|
|
+func TestOwnerVehicleRelationship(t *testing.T) {
|
|
|
+ env := InitTestEnv(t)
|
|
|
+ ownerResp, ownerBody := env.DoPost("/owner/create", map[string]interface{}{
|
|
|
+ "owner_name": "Rel Test Owner", "owner_surname": "REL",
|
|
|
+ "owner_phone": "13911111111", "is_vip": false, "vip_expire_time": nil,
|
|
|
+ })
|
|
|
+ AssertResponse(t, ownerResp, ownerBody, http.StatusOK, response.SUCCESS)
|
|
|
+ var owner dao.Owner
|
|
|
+ env.DB.Where("owner_phone = ?", "13911111111").First(&owner)
|
|
|
+ createVehicleWithOwner(t, env, "JingO00001", "RFID_OWNER", owner.ID)
|
|
|
+ ownerListResp, ownerListBody := env.DoGet("/owner/list", map[string]string{"page": "1", "page_size": "10"})
|
|
|
+ AssertResponse(t, ownerListResp, ownerListBody, http.StatusOK, response.SUCCESS)
|
|
|
+ list := ParsePageList(ownerListBody)
|
|
|
+ found := false
|
|
|
+ for _, item := range list {
|
|
|
+ m, ok := item.(map[string]interface{})
|
|
|
+ if !ok { continue }
|
|
|
+ if m["owner_phone"] == "13911111111" { found = true; break }
|
|
|
+ }
|
|
|
+ if !found { t.Fatalf("Owner not found in list") }
|
|
|
+ fmt.Println("PASS: Owner-vehicle relationship verified")
|
|
|
+}
|
|
|
+
|
|
|
+// TestParkingLotCapacity 停车场容量检查
|
|
|
+func TestParkingLotCapacity(t *testing.T) {
|
|
|
+ env := InitTestEnv(t)
|
|
|
+ lotResp, lotBody := env.DoPost("/parking/lot/create", map[string]interface{}{
|
|
|
+ "lot_code": "TEST_EMPTY", "lot_name": "Empty Lot",
|
|
|
+ "capacity": 0, "description": "Zero capacity test",
|
|
|
+ })
|
|
|
+ AssertResponse(t, lotResp, lotBody, http.StatusOK, response.SUCCESS)
|
|
|
+ createVehicle(t, env, "JingC00001", "RFID_FULL", 1)
|
|
|
+ _, entryBody := env.DoPost("/vehicle/entry", map[string]interface{}{
|
|
|
+ "plate_number": "JingC00001", "rfid_tag": "RFID_FULL",
|
|
|
+ "parking_lot_id": 1, "parking_space_id": 1, "entry_image": "test.jpg",
|
|
|
+ })
|
|
|
+ var result struct { Code int `json:"code"`; Msg string `json:"msg"` }
|
|
|
+ json.Unmarshal(entryBody, &result)
|
|
|
+ fmt.Printf("INFO: Entry into zero-capacity lot: code=%d msg=%s\n", result.Code, result.Msg)
|
|
|
+}
|
|
|
+
|
|
|
+// TestBoothCRUD 岗亭CRUD
|
|
|
+func TestBoothCRUD(t *testing.T) {
|
|
|
+ env := InitTestEnv(t)
|
|
|
+ boothResp, boothBody := env.DoPost("/parking/booth/create", map[string]interface{}{
|
|
|
+ "booth_code": "BT001", "booth_name": "Test Booth",
|
|
|
+ "parking_lot_id": 1, "description": "Automated test booth",
|
|
|
+ })
|
|
|
+ AssertResponse(t, boothResp, boothBody, http.StatusOK, response.SUCCESS)
|
|
|
+ listResp, listBody := env.DoGet("/parking/booth/list", map[string]string{"page": "1", "page_size": "10"})
|
|
|
+ AssertResponse(t, listResp, listBody, http.StatusOK, response.SUCCESS)
|
|
|
+ list := ParsePageList(listBody)
|
|
|
+ if len(list) < 1 { t.Fatalf("Booth list is empty") }
|
|
|
+ fmt.Printf("PASS: Booth CRUD successful, total %d booths\n", len(list))
|
|
|
+}
|
|
|
+
|
|
|
+// TestChannelCRUD 通道CRUD
|
|
|
+func TestChannelCRUD(t *testing.T) {
|
|
|
+ env := InitTestEnv(t)
|
|
|
+ channelResp, channelBody := env.DoPost("/parking/channel/create", map[string]interface{}{
|
|
|
+ "channel_code": "CH-IN-TEST", "channel_name": "Test Entrance",
|
|
|
+ "direction": "in", "parking_lot_id": 1, "booth_id": 1,
|
|
|
+ "allow_temporary": true, "description": "Automated test channel",
|
|
|
+ })
|
|
|
+ AssertResponse(t, channelResp, channelBody, http.StatusOK, response.SUCCESS)
|
|
|
+ listResp, listBody := env.DoGet("/parking/channel/list", map[string]string{"page": "1", "page_size": "10"})
|
|
|
+ AssertResponse(t, listResp, listBody, http.StatusOK, response.SUCCESS)
|
|
|
+ list := ParsePageList(listBody)
|
|
|
+ if len(list) < 1 { t.Fatalf("Channel list is empty") }
|
|
|
+ fmt.Printf("PASS: Channel CRUD successful, total %d channels\n", len(list))
|
|
|
+}
|
|
|
+
|
|
|
+// TestDeviceCRUD 设备CRUD
|
|
|
+func TestDeviceCRUD(t *testing.T) {
|
|
|
+ env := InitTestEnv(t)
|
|
|
+ deviceResp, deviceBody := env.DoPost("/parking/device/create", map[string]interface{}{
|
|
|
+ "device_code": "DEV001", "device_name": "Test Device",
|
|
|
+ "device_type": "TCP", "connect_type": "tcp", "ip_address": "127.0.0.1",
|
|
|
+ "port": 9000, "is_active": true, "parking_lot_id": 1, "channel_id": 1,
|
|
|
+ "description": "Automated test device",
|
|
|
+ })
|
|
|
+ AssertResponse(t, deviceResp, deviceBody, http.StatusOK, response.SUCCESS)
|
|
|
+ listResp, listBody := env.DoGet("/parking/device/list", map[string]string{"page": "1", "page_size": "10"})
|
|
|
+ AssertResponse(t, listResp, listBody, http.StatusOK, response.SUCCESS)
|
|
|
+ list := ParsePageList(listBody)
|
|
|
+ if len(list) < 1 { t.Fatalf("Device list is empty") }
|
|
|
+ fmt.Printf("PASS: Device CRUD successful, total %d devices\n", len(list))
|
|
|
+}
|
|
|
+
|
|
|
+// TestVehicleTypeCRUD 自定义车辆类型CRUD
|
|
|
+func TestVehicleTypeCRUD(t *testing.T) {
|
|
|
+ env := InitTestEnv(t)
|
|
|
+ typeResp, typeBody := env.DoPost("/vehicle/type/create", map[string]interface{}{
|
|
|
+ "name": "新能源车", "remarks": "测试新能源车辆类型", "is_system": false,
|
|
|
+ })
|
|
|
+ AssertResponse(t, typeResp, typeBody, http.StatusOK, response.SUCCESS)
|
|
|
+ listResp, listBody := env.DoGet("/vehicle/type/all", nil)
|
|
|
+ AssertResponse(t, listResp, listBody, http.StatusOK, response.SUCCESS)
|
|
|
+ list := ParseDataList(listBody)
|
|
|
+ if len(list) < 1 { t.Fatalf("Vehicle type list is empty") }
|
|
|
+ fmt.Printf("PASS: Vehicle type CRUD successful, total %d types\n", len(list))
|
|
|
+}
|
|
|
+
|
|
|
+// ===================== Helper functions =====================
|
|
|
+
|
|
|
+func createVehicleWithOwner(t *testing.T, env *TestEnv, plateNumber, rfidTag string, ownerID uint) {
|
|
|
+ t.Helper()
|
|
|
+ _, body := env.DoPost("/vehicle/create", map[string]interface{}{
|
|
|
+ "plate_number": plateNumber, "vehicle_type_id": 1,
|
|
|
+ "vehicle_brand": "TestBrand", "vehicle_color": "TestColor",
|
|
|
+ "rfid_tag": rfidTag, "owner_id": ownerID,
|
|
|
+ })
|
|
|
+ var result struct { Code int `json:"code"`; Msg string `json:"msg"` }
|
|
|
+ json.Unmarshal(body, &result)
|
|
|
+ if result.Code != response.SUCCESS {
|
|
|
+ t.Fatalf("Failed to create vehicle with owner: %s", result.Msg)
|
|
|
+ }
|
|
|
+}
|