Browse Source

添加sdk-测试通过

qdy 3 weeks ago
parent
commit
eb3f302209

+ 8
- 2
config/subconfigs/configure_config.go View File

9
 // ConfigureConfig 配置中心配置
9
 // ConfigureConfig 配置中心配置
10
 type ConfigureConfig struct {
10
 type ConfigureConfig struct {
11
 	core.BaseConfig
11
 	core.BaseConfig
12
-	Url   string `yaml:"url" desc:"配置中心地址"`
13
-	Token string `yaml:"token" desc:"访问配置中心的token"`
12
+	Url      string `yaml:"url" desc:"配置中心地址"`
13
+	Token    string `yaml:"token" desc:"访问配置中心的token"`
14
+	Username string `yaml:"username" desc:"Basic认证用户名"`
15
+	Password string `yaml:"password" desc:"Basic认证密码"`
14
 }
16
 }
15
 
17
 
16
 func (c *ConfigureConfig) Description() string {
18
 func (c *ConfigureConfig) Description() string {
34
 	if c.Url == "" {
36
 	if c.Url == "" {
35
 		return fmt.Errorf("configure center url must be positive")
37
 		return fmt.Errorf("configure center url must be positive")
36
 	}
38
 	}
39
+	// 至少提供一种认证方式
40
+	if c.Token == "" && (c.Username == "" || c.Password == "") {
41
+		return fmt.Errorf("configure center authentication required: provide either token or username/password")
42
+	}
37
 	return nil
43
 	return nil
38
 }
44
 }
39
 
45
 

+ 190
- 0
config/subconfigs/configure_config_test.go View File

1
+package subconfigs
2
+
3
+import (
4
+	"testing"
5
+)
6
+
7
+// TestConfigureConfigValidation 测试配置中心配置验证
8
+func TestConfigureConfigValidation(t *testing.T) {
9
+	tests := []struct {
10
+		name        string
11
+		config      *ConfigureConfig
12
+		expectError bool
13
+		description string
14
+	}{
15
+		{
16
+			name: "Valid token authentication",
17
+			config: &ConfigureConfig{
18
+				Url:   "http://localhost:8080",
19
+				Token: "test-token-123",
20
+			},
21
+			expectError: false,
22
+			description: "使用Token认证应该通过验证",
23
+		},
24
+		{
25
+			name: "Valid basic authentication",
26
+			config: &ConfigureConfig{
27
+				Url:      "http://localhost:8080",
28
+				Username: "admin",
29
+				Password: "123",
30
+			},
31
+			expectError: false,
32
+			description: "使用Basic认证应该通过验证",
33
+		},
34
+		{
35
+			name: "Valid both authentication methods",
36
+			config: &ConfigureConfig{
37
+				Url:      "http://localhost:8080",
38
+				Token:    "test-token-123",
39
+				Username: "admin",
40
+				Password: "123",
41
+			},
42
+			expectError: false,
43
+			description: "同时提供Token和Basic认证应该通过验证",
44
+		},
45
+		{
46
+			name: "Missing URL",
47
+			config: &ConfigureConfig{
48
+				Url:   "",
49
+				Token: "test-token-123",
50
+			},
51
+			expectError: true,
52
+			description: "缺少URL应该验证失败",
53
+		},
54
+		{
55
+			name: "Missing authentication",
56
+			config: &ConfigureConfig{
57
+				Url: "http://localhost:8080",
58
+			},
59
+			expectError: true,
60
+			description: "缺少认证方式应该验证失败",
61
+		},
62
+		{
63
+			name: "Basic auth missing username",
64
+			config: &ConfigureConfig{
65
+				Url:      "http://localhost:8080",
66
+				Username: "",
67
+				Password: "123",
68
+			},
69
+			expectError: true,
70
+			description: "Basic认证缺少用户名应该验证失败",
71
+		},
72
+		{
73
+			name: "Basic auth missing password",
74
+			config: &ConfigureConfig{
75
+				Url:      "http://localhost:8080",
76
+				Username: "admin",
77
+				Password: "",
78
+			},
79
+			expectError: true,
80
+			description: "Basic认证缺少密码应该验证失败",
81
+		},
82
+		{
83
+			name: "Token auth empty token",
84
+			config: &ConfigureConfig{
85
+				Url:   "http://localhost:8080",
86
+				Token: "",
87
+			},
88
+			expectError: true,
89
+			description: "Token认证token为空应该验证失败",
90
+		},
91
+	}
92
+
93
+	for _, tt := range tests {
94
+		t.Run(tt.name, func(t *testing.T) {
95
+			err := tt.config.Validate()
96
+
97
+			if tt.expectError {
98
+				if err == nil {
99
+					t.Errorf("%s: 期望错误,但验证通过", tt.description)
100
+				} else {
101
+					t.Logf("%s: 验证正确返回错误: %v", tt.description, err)
102
+				}
103
+			} else {
104
+				if err != nil {
105
+					t.Errorf("%s: 期望验证通过,但得到错误: %v", tt.description, err)
106
+				} else {
107
+					t.Logf("%s: 验证通过", tt.description)
108
+				}
109
+			}
110
+		})
111
+	}
112
+}
113
+
114
+// TestConfigureConfigDefaults 测试配置默认值
115
+func TestConfigureConfigDefaults(t *testing.T) {
116
+	config := &ConfigureConfig{}
117
+	config.SetDefaults()
118
+
119
+	if config.Url != "http://localhost:8080" {
120
+		t.Errorf("期望默认URL: http://localhost:8080, 实际: %s", config.Url)
121
+	}
122
+
123
+	if config.Token != "123" {
124
+		t.Errorf("期望默认Token: 123, 实际: %s", config.Token)
125
+	}
126
+
127
+	t.Logf("默认配置: URL=%s, Token=%s", config.Url, config.Token)
128
+}
129
+
130
+// TestConfigureConfigIsConfigured 测试配置检查
131
+func TestConfigureConfigIsConfigured(t *testing.T) {
132
+	tests := []struct {
133
+		name     string
134
+		config   *ConfigureConfig
135
+		expected bool
136
+	}{
137
+		{
138
+			name: "Configured with URL",
139
+			config: &ConfigureConfig{
140
+				Url: "http://localhost:8080",
141
+			},
142
+			expected: true,
143
+		},
144
+		{
145
+			name: "Not configured empty URL",
146
+			config: &ConfigureConfig{
147
+				Url: "",
148
+			},
149
+			expected: false,
150
+		},
151
+	}
152
+
153
+	for _, tt := range tests {
154
+		t.Run(tt.name, func(t *testing.T) {
155
+			result := tt.config.IsConfigured()
156
+			if result != tt.expected {
157
+				t.Errorf("期望IsConfigured()返回 %v, 实际: %v", tt.expected, result)
158
+			} else {
159
+				t.Logf("IsConfigured()正确返回: %v", result)
160
+			}
161
+		})
162
+	}
163
+}
164
+
165
+// TestConfigureConfigAuthFields 测试认证字段存在性
166
+func TestConfigureConfigAuthFields(t *testing.T) {
167
+	// 测试结构体字段是否包含新的认证字段
168
+	config := &ConfigureConfig{
169
+		Url:      "http://localhost:8080",
170
+		Token:    "test-token",
171
+		Username: "test-user",
172
+		Password: "test-pass",
173
+	}
174
+
175
+	if config.Url == "" {
176
+		t.Error("URL字段不存在或为空")
177
+	}
178
+	if config.Token == "" {
179
+		t.Error("Token字段不存在或为空")
180
+	}
181
+	if config.Username == "" {
182
+		t.Error("Username字段不存在或为空")
183
+	}
184
+	if config.Password == "" {
185
+		t.Error("Password字段不存在或为空")
186
+	}
187
+
188
+	t.Logf("配置中心配置包含所有认证字段: URL=%s, Token=%s, Username=%s, Password=%s",
189
+		config.Url, config.Token, config.Username, config.Password)
190
+}

+ 4
- 4
sdk/configure/client.go View File

349
 }
349
 }
350
 
350
 
351
 // BatchSaveTableAliasFlow 批量保存表别名字典流水
351
 // BatchSaveTableAliasFlow 批量保存表别名字典流水
352
-func (c *Client) BatchSaveTableAliasFlow(ctx context.Context, req *BatchTableAliasRequest, tenantID string) ([]DicTableAliasFlowDB, error) {
353
-	endpoint := fmt.Sprintf("/api/dic-table-alias-flow/batch-save?tenantID=%s", tenantID)
352
+func (c *Client) BatchSaveTableAliasFlow(ctx context.Context, req *BatchTableAliasRequest) ([]DicTableAliasFlowDB, error) {
353
+	endpoint := "/api/dic-table-alias-flow/batch-save"
354
 
354
 
355
 	var result ResponseWrapper[[]DicTableAliasFlowDB]
355
 	var result ResponseWrapper[[]DicTableAliasFlowDB]
356
 	if err := c.doRequest(ctx, http.MethodPost, endpoint, req, &result); err != nil {
356
 	if err := c.doRequest(ctx, http.MethodPost, endpoint, req, &result); err != nil {
401
 }
401
 }
402
 
402
 
403
 // BatchSaveTableFieldAliasFlow 批量保存字段别名字典流水
403
 // BatchSaveTableFieldAliasFlow 批量保存字段别名字典流水
404
-func (c *Client) BatchSaveTableFieldAliasFlow(ctx context.Context, req *BatchTableFieldAliasRequest, tenantID string) ([]DicTableFieldAliasFlowDB, error) {
405
-	endpoint := fmt.Sprintf("/api/dic-table-field-alias-flow/batch-save?tenantID=%s", tenantID)
404
+func (c *Client) BatchSaveTableFieldAliasFlow(ctx context.Context, req *BatchTableFieldAliasRequest) ([]DicTableFieldAliasFlowDB, error) {
405
+	endpoint := "/api/dic-table-field-alias-flow/batch-save"
406
 
406
 
407
 	var result ResponseWrapper[[]DicTableFieldAliasFlowDB]
407
 	var result ResponseWrapper[[]DicTableFieldAliasFlowDB]
408
 	if err := c.doRequest(ctx, http.MethodPost, endpoint, req, &result); err != nil {
408
 	if err := c.doRequest(ctx, http.MethodPost, endpoint, req, &result); err != nil {

+ 30
- 4
sdk/configure/config.go View File

37
 }
37
 }
38
 
38
 
39
 // DefaultConfig 返回默认配置
39
 // DefaultConfig 返回默认配置
40
-// 从全局配置中获取ConfigureConfig的URL和Token
40
+// 从全局配置中获取ConfigureConfig的URL和认证凭证
41
 func DefaultConfig() ClientConfig {
41
 func DefaultConfig() ClientConfig {
42
 	cfg := config.GetConfigureConfig()
42
 	cfg := config.GetConfigureConfig()
43
-	return ClientConfig{
43
+	clientCfg := ClientConfig{
44
 		BaseURL:     cfg.Url,
44
 		BaseURL:     cfg.Url,
45
-		AuthType:    AuthTypeToken,
46
-		Token:       cfg.Token,
47
 		HTTPTimeout: 30 * time.Second,
45
 		HTTPTimeout: 30 * time.Second,
48
 		Insecure:    false,
46
 		Insecure:    false,
49
 	}
47
 	}
48
+
49
+	// 优先使用Token认证,其次使用Basic认证
50
+	if cfg.Token != "" {
51
+		clientCfg.AuthType = AuthTypeToken
52
+		clientCfg.Token = cfg.Token
53
+	} else if cfg.Username != "" && cfg.Password != "" {
54
+		clientCfg.AuthType = AuthTypeBasic
55
+		clientCfg.Username = cfg.Username
56
+		clientCfg.Password = cfg.Password
57
+	} else {
58
+		// 默认使用Token,但Token为空(向后兼容,可能使用默认token)
59
+		clientCfg.AuthType = AuthTypeToken
60
+		clientCfg.Token = cfg.Token // 可能为空,由Validate检查
61
+	}
62
+
63
+	return clientCfg
50
 }
64
 }
51
 
65
 
52
 // Validate 验证配置
66
 // Validate 验证配置
55
 		return ErrConfigInvalidURL
69
 		return ErrConfigInvalidURL
56
 	}
70
 	}
57
 
71
 
72
+	// 如果AuthType未指定,根据提供的凭证推断
73
+	if c.AuthType == "" {
74
+		if c.Token != "" {
75
+			c.AuthType = AuthTypeToken
76
+		} else if c.Username != "" && c.Password != "" {
77
+			c.AuthType = AuthTypeBasic
78
+		} else {
79
+			return ErrConfigInvalidAuth
80
+		}
81
+	}
82
+
83
+	// 验证指定AuthType的凭证
58
 	switch c.AuthType {
84
 	switch c.AuthType {
59
 	case AuthTypeBasic:
85
 	case AuthTypeBasic:
60
 		if c.Username == "" || c.Password == "" {
86
 		if c.Username == "" || c.Password == "" {

+ 0
- 9
sdk/configure/test/example.go View File

111
 		TableType:   "实体表",
111
 		TableType:   "实体表",
112
 		Name:        "示例表001",
112
 		Name:        "示例表001",
113
 		Description: "这是一个示例表",
113
 		Description: "这是一个示例表",
114
-		Creator:     "admin",
115
-		CreatedAt:   time.Now(),
116
-		UpdatedAt:   time.Now(),
117
 		Fields: []configure.DicTableFieldRequest{
114
 		Fields: []configure.DicTableFieldRequest{
118
 			{
115
 			{
119
 				FieldID:     "example_table_001.id",
116
 				FieldID:     "example_table_001.id",
123
 				FieldName:   "id",
120
 				FieldName:   "id",
124
 				FieldNameCN: "主键ID",
121
 				FieldNameCN: "主键ID",
125
 				Description: "主键字段",
122
 				Description: "主键字段",
126
-				Creator:     "admin",
127
-				CreatedAt:   time.Now(),
128
-				UpdatedAt:   time.Now(),
129
 			},
123
 			},
130
 			{
124
 			{
131
 				FieldID:     "example_table_001.name",
125
 				FieldID:     "example_table_001.name",
135
 				FieldName:   "name",
129
 				FieldName:   "name",
136
 				FieldNameCN: "名称",
130
 				FieldNameCN: "名称",
137
 				Description: "名称字段",
131
 				Description: "名称字段",
138
-				Creator:     "admin",
139
-				CreatedAt:   time.Now(),
140
-				UpdatedAt:   time.Now(),
141
 			},
132
 			},
142
 		},
133
 		},
143
 	}
134
 	}

+ 0
- 20
sdk/configure/types.go View File

16
 
16
 
17
 	// 子表字段(字段列表)
17
 	// 子表字段(字段列表)
18
 	Fields []DicTableFieldRequest `json:"fields"` // 表字段列表
18
 	Fields []DicTableFieldRequest `json:"fields"` // 表字段列表
19
-
20
-	// 系统字段(创建时自动填充)
21
-	Creator   string    `json:"creator,omitempty"`   // 创建人
22
-	CreatedAt time.Time `json:"createdAt,omitempty"` // 创建时间
23
-	UpdatedAt time.Time `json:"updatedAt,omitempty"` // 更新时间
24
 }
19
 }
25
 
20
 
26
 // DicTableFieldRequest 数据库表字段字典请求(子表)
21
 // DicTableFieldRequest 数据库表字段字典请求(子表)
33
 	FieldName   string `json:"fieldName" binding:"required"` // 字段名称
28
 	FieldName   string `json:"fieldName" binding:"required"` // 字段名称
34
 	FieldNameCN string `json:"fieldNameCN"`                  // 字段中文名称(ERP中业务名称)
29
 	FieldNameCN string `json:"fieldNameCN"`                  // 字段中文名称(ERP中业务名称)
35
 	Description string `json:"description"`                  // 字段描述
30
 	Description string `json:"description"`                  // 字段描述
36
-
37
-	// 系统字段(创建时自动填充)
38
-	Creator   string    `json:"creator,omitempty"`   // 创建人
39
-	CreatedAt time.Time `json:"createdAt,omitempty"` // 创建时间
40
-	UpdatedAt time.Time `json:"updatedAt,omitempty"` // 更新时间
41
 }
31
 }
42
 
32
 
43
 // DicTableDetail 数据库表字典详情(主表+子表)
33
 // DicTableDetail 数据库表字典详情(主表+子表)
164
 	ID         string `json:"id,omitempty"`                  // 主键(创建时可选,更新时必填)
154
 	ID         string `json:"id,omitempty"`                  // 主键(创建时可选,更新时必填)
165
 	TableID    string `json:"tableID" binding:"required"`    // 表ID
155
 	TableID    string `json:"tableID" binding:"required"`    // 表ID
166
 	TableAlias string `json:"tableAlias" binding:"required"` // 别名
156
 	TableAlias string `json:"tableAlias" binding:"required"` // 别名
167
-
168
-	// 系统字段(创建时自动填充)
169
-	Creator   string    `json:"creator,omitempty"`   // 创建人
170
-	CreatedAt time.Time `json:"createdAt,omitempty"` // 创建时间
171
-	UpdatedAt time.Time `json:"updatedAt,omitempty"` // 更新时间
172
 }
157
 }
173
 
158
 
174
 // 批量表别名字典请求
159
 // 批量表别名字典请求
210
 	FieldAlias     string `json:"fieldAlias" binding:"required"` // 字段别名
195
 	FieldAlias     string `json:"fieldAlias" binding:"required"` // 字段别名
211
 	Description    string `json:"description"`                   // 字段别名描述
196
 	Description    string `json:"description"`                   // 字段别名描述
212
 	WhereCondition string `json:"whereCondition"`                // 此别名获取数据的查询条件描述
197
 	WhereCondition string `json:"whereCondition"`                // 此别名获取数据的查询条件描述
213
-
214
-	// 系统字段(创建时自动填充)
215
-	Creator   string    `json:"creator,omitempty"`   // 创建人
216
-	CreatedAt time.Time `json:"createdAt,omitempty"` // 创建时间
217
-	UpdatedAt time.Time `json:"updatedAt,omitempty"` // 更新时间
218
 }
198
 }
219
 
199
 
220
 // 批量字段别名字典请求
200
 // 批量字段别名字典请求

+ 0
- 13
test_sdk.go View File

47
 		TableType:   "实体表",
47
 		TableType:   "实体表",
48
 		Name:        "SDK测试表001",
48
 		Name:        "SDK测试表001",
49
 		Description: "SDK集成测试表",
49
 		Description: "SDK集成测试表",
50
-		Creator:     "admin",
51
-		CreatedAt:   time.Now(),
52
-		UpdatedAt:   time.Now(),
53
 		Fields: []configure.DicTableFieldRequest{
50
 		Fields: []configure.DicTableFieldRequest{
54
 			{
51
 			{
55
 				FieldID:     "sdk_test_table_001.id",
52
 				FieldID:     "sdk_test_table_001.id",
59
 				FieldName:   "id",
56
 				FieldName:   "id",
60
 				FieldNameCN: "主键ID",
57
 				FieldNameCN: "主键ID",
61
 				Description: "SDK测试主键字段",
58
 				Description: "SDK测试主键字段",
62
-				Creator:     "admin",
63
-				CreatedAt:   time.Now(),
64
-				UpdatedAt:   time.Now(),
65
 			},
59
 			},
66
 			{
60
 			{
67
 				FieldID:     "sdk_test_table_001.name",
61
 				FieldID:     "sdk_test_table_001.name",
71
 				FieldName:   "name",
65
 				FieldName:   "name",
72
 				FieldNameCN: "名称",
66
 				FieldNameCN: "名称",
73
 				Description: "SDK测试名称字段",
67
 				Description: "SDK测试名称字段",
74
-				Creator:     "admin",
75
-				CreatedAt:   time.Now(),
76
-				UpdatedAt:   time.Now(),
77
 			},
68
 			},
78
 		},
69
 		},
79
 	}
70
 	}
110
 
101
 
111
 	req.Description = "更新后的SDK测试表描述"
102
 	req.Description = "更新后的SDK测试表描述"
112
 	req.Name = "更新后的SDK测试表001"
103
 	req.Name = "更新后的SDK测试表001"
113
-	req.UpdatedAt = time.Now()
114
 
104
 
115
 	// 添加一个新字段
105
 	// 添加一个新字段
116
 	req.Fields = append(req.Fields, configure.DicTableFieldRequest{
106
 	req.Fields = append(req.Fields, configure.DicTableFieldRequest{
121
 		FieldName:   "status",
111
 		FieldName:   "status",
122
 		FieldNameCN: "状态",
112
 		FieldNameCN: "状态",
123
 		Description: "SDK测试状态字段",
113
 		Description: "SDK测试状态字段",
124
-		Creator:     "admin",
125
-		CreatedAt:   time.Now(),
126
-		UpdatedAt:   time.Now(),
127
 	})
114
 	})
128
 
115
 
129
 	updatedDetail, err := client.SaveTable(ctx4, req)
116
 	updatedDetail, err := client.SaveTable(ctx4, req)

Loading…
Cancel
Save