Files
tyapi-server/internal/domains/api/services/form_config_service_test.go
2025-08-27 22:19:19 +08:00

134 lines
3.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package services
import (
"testing"
)
func TestFormConfigService_GetFormConfig(t *testing.T) {
service := NewFormConfigService()
// 测试获取存在的API配置
config, err := service.GetFormConfig("IVYZ9363")
if err != nil {
t.Fatalf("获取表单配置失败: %v", err)
}
if config == nil {
t.Fatal("表单配置不应为空")
}
if config.ApiCode != "IVYZ9363" {
t.Errorf("期望API代码为 IVYZ9363实际为 %s", config.ApiCode)
}
if len(config.Fields) == 0 {
t.Fatal("字段列表不应为空")
}
// 验证字段信息
expectedFields := map[string]bool{
"man_name": false,
"man_id_card": false,
"woman_name": false,
"woman_id_card": false,
}
for _, field := range config.Fields {
if _, exists := expectedFields[field.Name]; !exists {
t.Errorf("意外的字段: %s", field.Name)
}
expectedFields[field.Name] = true
}
for fieldName, found := range expectedFields {
if !found {
t.Errorf("缺少字段: %s", fieldName)
}
}
// 测试获取不存在的API配置
config, err = service.GetFormConfig("NONEXISTENT")
if err != nil {
t.Fatalf("获取不存在的API配置不应返回错误: %v", err)
}
if config != nil {
t.Fatal("不存在的API配置应返回nil")
}
}
func TestFormConfigService_FieldValidation(t *testing.T) {
service := NewFormConfigService()
config, err := service.GetFormConfig("FLXG3D56")
if err != nil {
t.Fatalf("获取表单配置失败: %v", err)
}
if config == nil {
t.Fatal("表单配置不应为空")
}
// 验证手机号字段
var mobileField *FormField
for _, field := range config.Fields {
if field.Name == "mobile_no" {
mobileField = &field
break
}
}
if mobileField == nil {
t.Fatal("应找到mobile_no字段")
}
if !mobileField.Required {
t.Error("mobile_no字段应为必填")
}
if mobileField.Type != "tel" {
t.Errorf("mobile_no字段类型应为tel实际为%s", mobileField.Type)
}
if !contains(mobileField.Validation, "手机号格式") {
t.Errorf("mobile_no字段验证规则应包含'手机号格式',实际为: %s", mobileField.Validation)
}
}
func TestFormConfigService_FieldLabels(t *testing.T) {
service := NewFormConfigService()
config, err := service.GetFormConfig("IVYZ9363")
if err != nil {
t.Fatalf("获取表单配置失败: %v", err)
}
// 验证字段标签
expectedLabels := map[string]string{
"man_name": "男方姓名",
"man_id_card": "男方身份证",
"woman_name": "女方姓名",
"woman_id_card": "女方身份证",
}
for _, field := range config.Fields {
if expectedLabel, exists := expectedLabels[field.Name]; exists {
if field.Label != expectedLabel {
t.Errorf("字段 %s 的标签应为 %s实际为 %s", field.Name, expectedLabel, field.Label)
}
}
}
}
// 辅助函数
func contains(s, substr string) bool {
return len(s) >= len(substr) && (s == substr || (len(s) > len(substr) && (s[:len(substr)] == substr || s[len(s)-len(substr):] == substr || func() bool {
for i := 0; i <= len(s)-len(substr); i++ {
if s[i:i+len(substr)] == substr {
return true
}
}
return false
}())))
}