mirror of
https://gitee.com/wanwujie/sub2api
synced 2026-04-12 02:54:44 +08:00
42 lines
946 B
Go
42 lines
946 B
Go
|
|
//go:build unit
|
||
|
|
|
||
|
|
package repository
|
||
|
|
|
||
|
|
import (
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
"github.com/stretchr/testify/require"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestSafeDateFormat(t *testing.T) {
|
||
|
|
tests := []struct {
|
||
|
|
name string
|
||
|
|
granularity string
|
||
|
|
expected string
|
||
|
|
}{
|
||
|
|
// 合法值
|
||
|
|
{"hour", "hour", "YYYY-MM-DD HH24:00"},
|
||
|
|
{"day", "day", "YYYY-MM-DD"},
|
||
|
|
{"week", "week", "IYYY-IW"},
|
||
|
|
{"month", "month", "YYYY-MM"},
|
||
|
|
|
||
|
|
// 非法值回退到默认
|
||
|
|
{"空字符串", "", "YYYY-MM-DD"},
|
||
|
|
{"未知粒度 year", "year", "YYYY-MM-DD"},
|
||
|
|
{"未知粒度 minute", "minute", "YYYY-MM-DD"},
|
||
|
|
|
||
|
|
// 恶意字符串
|
||
|
|
{"SQL 注入尝试", "'; DROP TABLE users; --", "YYYY-MM-DD"},
|
||
|
|
{"带引号", "day'", "YYYY-MM-DD"},
|
||
|
|
{"带括号", "day)", "YYYY-MM-DD"},
|
||
|
|
{"Unicode", "日", "YYYY-MM-DD"},
|
||
|
|
}
|
||
|
|
|
||
|
|
for _, tc := range tests {
|
||
|
|
t.Run(tc.name, func(t *testing.T) {
|
||
|
|
got := safeDateFormat(tc.granularity)
|
||
|
|
require.Equal(t, tc.expected, got, "safeDateFormat(%q)", tc.granularity)
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|