package vConfig import ( "testing" ) func TestParseDMConnectionString(t *testing.T) { tests := []struct { name string connStr string want *Config wantErr bool errMsg string }{ { name: "normal case with default port", connStr: "dm://user:pass@127.0.0.1/DAMENG?schema=MY_SCHEMA", want: &Config{ User: "user", Pwd: "pass", IP: "127.0.0.1", Port: "5236", Schema: "MY_SCHEMA", }, wantErr: false, }, { name: "explicit port", connStr: "dm://user:pass@10.200.21.19:5296/DAMENG?schema=HY_ZGGL_GZ", want: &Config{ User: "user", Pwd: "pass", IP: "10.200.21.19", Port: "5296", Schema: "HY_ZGGL_GZ", }, wantErr: false, }, { name: "missing schema", connStr: "dm://user:pass@127.0.0.1/DAMENG", wantErr: true, errMsg: "缺少schema参数", }, { name: "invalid format", connStr: "dm://invalid_user_info_format", wantErr: true, errMsg: "缺少用户信息", }, { name: "empty password", connStr: "dm://user@127.0.0.1/DAMENG?schema=TEST", wantErr: true, errMsg: "密码解析失败", }, { name: "special characters in password", connStr: "dm://user:p%40ssw0rd%21%40%23@127.0.0.1/DAMENG?schema=TEST", want: &Config{ User: "user", Pwd: "p@ssw0rd!@#", IP: "127.0.0.1", Port: "5236", Schema: "TEST", }, wantErr: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, err := LoadConfig(tt.connStr) if (err != nil) != tt.wantErr { t.Errorf("ParseDMConnectionString() error = %v, wantErr %v", err, tt.wantErr) return } if tt.wantErr { if err.Error() != tt.errMsg { t.Errorf("ParseDMConnectionString() error = %v, wantErrMsg %v", err.Error(), tt.errMsg) } return } if got.User != tt.want.User || got.Pwd != tt.want.Pwd || got.IP != tt.want.IP || got.Port != tt.want.Port || got.Schema != tt.want.Schema { t.Errorf("ParseDMConnectionString() = %+v, want %+v", got, tt.want) } }) } }