feat(order): 订单列表支持创建时间筛选

This commit is contained in:
yml
2026-11-22 15:53:23 +08:00
parent 952ec71219
commit 6fcd5bac9c
9 changed files with 235 additions and 2 deletions
@@ -0,0 +1,80 @@
package order
import (
"net/http"
"net/http/httptest"
"testing"
"time"
"hfb_sys/backend/internal/timeutil"
"github.com/gin-gonic/gin"
)
func TestParseAdminOrderTimeRange(t *testing.T) {
gin.SetMode(gin.TestMode)
loc := timeutil.ShanghaiLocation()
tests := []struct {
name string
target string
wantStart time.Time
wantEnd time.Time
wantOK bool
wantStatus int
}{
{
name: "日期按北京时间自然日解析",
target: "/admin/orders?start_date=2026-11-01&end_date=2026-11-02",
wantStart: time.Date(2026, 11, 1, 0, 0, 0, 0, loc),
wantEnd: time.Date(2026, 11, 2, 23, 59, 59, int(time.Second-time.Nanosecond), loc),
wantOK: true,
wantStatus: http.StatusOK,
},
{
name: "精确时间保持原值",
target: "/admin/orders?start_time=2026-11-01%2009:30:00&end_time=2026-11-01%2018:45:59",
wantStart: time.Date(2026, 11, 1, 9, 30, 0, 0, loc),
wantEnd: time.Date(2026, 11, 1, 18, 45, 59, 0, loc),
wantOK: true,
wantStatus: http.StatusOK,
},
{
name: "开始日期格式错误",
target: "/admin/orders?start_date=invalid",
wantOK: false,
wantStatus: http.StatusBadRequest,
},
{
name: "结束日期早于开始日期",
target: "/admin/orders?start_date=2026-11-03&end_date=2026-11-02",
wantOK: false,
wantStatus: http.StatusBadRequest,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
recorder := httptest.NewRecorder()
c, _ := gin.CreateTestContext(recorder)
c.Request = httptest.NewRequest(http.MethodGet, tt.target, nil)
start, end, ok := parseAdminOrderTimeRange(c)
if ok != tt.wantOK {
t.Fatalf("parseAdminOrderTimeRange() ok = %v, want %v", ok, tt.wantOK)
}
if recorder.Code != tt.wantStatus {
t.Fatalf("response status = %d, want %d", recorder.Code, tt.wantStatus)
}
if !tt.wantOK {
return
}
if start == nil || !start.Equal(tt.wantStart) {
t.Fatalf("start = %v, want %v", start, tt.wantStart)
}
if end == nil || !end.Equal(tt.wantEnd) {
t.Fatalf("end = %v, want %v", end, tt.wantEnd)
}
})
}
}