unit-testing - 如何在结构内传递结构数组?
问题描述
我在结构中传递结构数组,在单元测试中,我使用了 ServiceAccounts 的 dto 和我的测试代码 TestStoreServiceAccounts。如何在结构中传递结构数组?结构的嵌套是我无法理解的.
func TestStoreServiceAccounts(t *testing.T) {
StoreServiceAccounts = func(serviceAccounts []models.ServiceAccount) ([]string, error) {
ServiceAccounts := []string{"service-account-details-inserted"}
return ServiceAccounts, nil
}
Data := dto.ServiceAccountRequestDTO{
ServiceAccounts : []{ //error on this line
WorkspaceID: 1,
ServiceAccountName: "sa-10",
ServiceAccountKey: dto.ServiceAccountKey{
Type: "service_account",
ProjectID: "abc",
PrivateKeyID: "123",
PrivateKey: "234",
ClientEmail: "read-clusters",
ClientID: "cdf",
AuthURI: "https://accounts.google.com/o/oaut",
TokenURI: "https://oauth2.googleapis.com/token",
AuthProviderX509CertURL: "https://www.googleapis.com",
ClientX509CertURL: "xwy",
},
CreatedAt: "2021-03-08 17:05:21.0",
UpdatedAt: "2021-03-08 17:05:21.0",
CreatedBy: "user-01",
UpdatedBy: "user-01",
},
}
responseData, err := clusterService.StoreServiceAccountsService(context.Background(), Data)
serviceAccountsdata := []string{"service-account-details-inserted"}
assert.Nil(t, err)
assert.NotNil(t, responseData)
assert.EqualValues(t, serviceAccountsdata, responseData)
}
// ServiceAccountRequestDTO - DTO of service account model
type ServiceAccountRequestDTO struct {
ServiceAccounts []struct {
WorkspaceID int64 `json:"workspace_id"`
ServiceAccountName string `json:"serviceAccountName"`
ServiceAccountKey ServiceAccountKey `json:"serviceAccountKey"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
CreatedBy string `json:"created_by"`
UpdatedBy string `json:"updated_by"`
} `json:"serviceAccounts"`
}```
解决方案
TL;DR:ServiceAccount
在 DTO 结构之外定义您的结构,以便您可以重用它。
由于您ServiceAccounts
在 struct 内部定义结构ServiceAccountRequestDTO
,因此在创建对象时还需要再次定义它,如下所示:
Data := dto.ServiceAccountRequestDTO{
ServiceAccounts : []struct{
WorkspaceID int64 `json:"workspace_id"`
ServiceAccountName string `json:"serviceAccountName"`
ServiceAccountKey ServiceAccountKey `json:"serviceAccountKey"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
CreatedBy string `json:"created_by"`
UpdatedBy string `json:"updated_by"`
} `json:"serviceAccounts"`{ //start of slice (it's technically not an array) of objects
{ // first object
WorkspaceID: 1,
ServiceAccountName: "sa-10",
ServiceAccountKey: dto.ServiceAccountKey{
Type: "service_account",
ProjectID: "abc",
PrivateKeyID: "123",
PrivateKey: "234",
ClientEmail: "read-clusters",
ClientID: "cdf",
AuthURI: "https://accounts.google.com/o/oaut",
TokenURI: "https://oauth2.googleapis.com/token",
AuthProviderX509CertURL: "https://www.googleapis.com",
ClientX509CertURL: "xwy",
},
CreatedAt: "2021-03-08 17:05:21.0",
UpdatedAt: "2021-03-08 17:05:21.0",
CreatedBy: "user-01",
UpdatedBy: "user-01",
},
// .. more ServiceAccount objects ...
},
}
现在,编写、阅读和复制大量代码当然是非常痛苦的。因此,您应该在外部定义结构
type ServiceAccount struct{
WorkspaceID int64 `json:"workspace_id"`
ServiceAccountName string `json:"serviceAccountName"`
ServiceAccountKey ServiceAccountKey `json:"serviceAccountKey"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
CreatedBy string `json:"created_by"`
UpdatedBy string `json:"updated_by"`
} `json:"serviceAccounts"`
}
type ServiceAccountRequestDTO struct {
ServiceAccounts []ServiceAccount
}
然后你可以像这样创建你的 DTO
Data := dto.ServiceAccountRequestDTO{
ServiceAccounts : []ServiceAccount{ //start of slice of objects
ServiceAccount { // first object
WorkspaceID: 1,
ServiceAccountName: "sa-10",
ServiceAccountKey: dto.ServiceAccountKey{
Type: "service_account",
ProjectID: "abc",
PrivateKeyID: "123",
PrivateKey: "234",
ClientEmail: "read-clusters",
ClientID: "cdf",
AuthURI: "https://accounts.google.com/o/oaut",
TokenURI: "https://oauth2.googleapis.com/token",
AuthProviderX509CertURL: "https://www.googleapis.com",
ClientX509CertURL: "xwy",
},
CreatedAt: "2021-03-08 17:05:21.0",
UpdatedAt: "2021-03-08 17:05:21.0",
CreatedBy: "user-01",
UpdatedBy: "user-01",
},
// .. more ServiceAccount objects ...
},
}
推荐阅读
- time-complexity - Computational Complexity Order
- python - 如何存储对数组的引用而不是文字值,Python Numpy + PIL
- angular - 来自 Websocket 的 Angular 实时视频
- azure - Windows 10(本地)和 Azure 上的 Windows Server 2016 之间的连接问题
- web-scraping - 使用代理进行 Java Web 抓取
- docker - 你喜欢如何为大型项目组织 docker-compose.yml?
- node.js - Fs.readFile 返回未定义
- sql-server - 更快的 I/O 可以减少索引中的页面拆分吗?
- python - 如何使用熊猫读取其中项目是引用的文本文件
- android - 连接到BLE后获取最后收到的数据?