正在显示
15 个修改的文件
包含
557 行增加
和
1 行删除
1 | +package dto | ||
2 | + | ||
3 | +import "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain" | ||
4 | + | ||
5 | +type BlockChain struct { | ||
6 | + PrimaryId string `json:"primaryId"` | ||
7 | + // 数据块hash | ||
8 | + BlockHash string `json:"blockHash"` | ||
9 | +} | ||
10 | + | ||
11 | +type BlockChains []*BlockChain | ||
12 | + | ||
13 | +func (b *BlockChain) LoadDto(upChain *domain.UpChain) { | ||
14 | + b.PrimaryId = upChain.PrimaryId | ||
15 | + b.BlockHash = upChain.Hash | ||
16 | +} | ||
17 | + | ||
18 | +func NewBlockChains(upChains []*domain.UpChain) BlockChains { | ||
19 | + var rsp = make([]*BlockChain, 0) | ||
20 | + for i := range upChains { | ||
21 | + item := new(BlockChain) | ||
22 | + item.LoadDto(upChains[i]) | ||
23 | + rsp = append(rsp, item) | ||
24 | + } | ||
25 | + return rsp | ||
26 | +} |
1 | +package query | ||
2 | + | ||
3 | +import ( | ||
4 | + "fmt" | ||
5 | + "reflect" | ||
6 | + "strings" | ||
7 | + | ||
8 | + "github.com/beego/beego/v2/core/validation" | ||
9 | +) | ||
10 | + | ||
11 | +type GetBlockChainTokenQuery struct { | ||
12 | + // 操作类型: | ||
13 | + //1-交易哈希溯源 | ||
14 | + //2-溯源ID溯源 | ||
15 | + //3-验真 | ||
16 | + Type int `cname:"操作类型" json:"type" valid:"Required"` | ||
17 | + // 参数 | ||
18 | + UpChainId int64 `cname:"上链Id" json:"upChainId,omitempty" valid:"Required"` | ||
19 | +} | ||
20 | + | ||
21 | +func (listBlockChain *GetBlockChainTokenQuery) Valid(validation *validation.Validation) { | ||
22 | + //validation.SetError("CustomValid", "未实现的自定义认证") | ||
23 | +} | ||
24 | + | ||
25 | +func (listBlockChain *GetBlockChainTokenQuery) ValidateQuery() error { | ||
26 | + valid := validation.Validation{} | ||
27 | + b, err := valid.Valid(listBlockChain) | ||
28 | + if err != nil { | ||
29 | + return err | ||
30 | + } | ||
31 | + if !b { | ||
32 | + elem := reflect.TypeOf(listBlockChain).Elem() | ||
33 | + for _, validErr := range valid.Errors { | ||
34 | + field, isExist := elem.FieldByName(validErr.Field) | ||
35 | + if isExist { | ||
36 | + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1)) | ||
37 | + } else { | ||
38 | + return fmt.Errorf(validErr.Message) | ||
39 | + } | ||
40 | + } | ||
41 | + } | ||
42 | + return nil | ||
43 | +} |
1 | +package query | ||
2 | + | ||
3 | +import ( | ||
4 | + "fmt" | ||
5 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain" | ||
6 | + "reflect" | ||
7 | + "strings" | ||
8 | + | ||
9 | + "github.com/beego/beego/v2/core/validation" | ||
10 | +) | ||
11 | + | ||
12 | +type ListBlockChainQuery struct { | ||
13 | + OperateInfo *domain.OperateInfo `json:"-"` | ||
14 | + // 查询偏离量 | ||
15 | + Offset int `cname:"查询偏离量" json:"offset,omitempty"` | ||
16 | + // 查询限制 | ||
17 | + Limit int `cname:"查询限制" json:"limit,omitempty"` | ||
18 | + // 数据来源 例如:app.model | ||
19 | + Source string `cname:"数据来源 例如:app.model" json:"source" valid:"Required"` | ||
20 | + // 来源数据唯一ID列表 | ||
21 | + PrimaryIdList []string `cname:"来源数据唯一ID列表" json:"primaryIdList"` | ||
22 | + // 过滤重复的primaryId | ||
23 | + EnableDistinctPrimaryId bool `cname:"过滤重复的primaryId" json:"enableDistinctPrimaryId"` | ||
24 | + // 关闭查询限制 | ||
25 | + DisableLimit bool `cname:"关闭查询限制" json:"disableLimit,omitempty"` | ||
26 | +} | ||
27 | + | ||
28 | +func (listBlockChain *ListBlockChainQuery) Valid(validation *validation.Validation) { | ||
29 | + //validation.SetError("CustomValid", "未实现的自定义认证") | ||
30 | +} | ||
31 | + | ||
32 | +func (listBlockChain *ListBlockChainQuery) ValidateQuery() error { | ||
33 | + valid := validation.Validation{} | ||
34 | + b, err := valid.Valid(listBlockChain) | ||
35 | + if err != nil { | ||
36 | + return err | ||
37 | + } | ||
38 | + if !b { | ||
39 | + elem := reflect.TypeOf(listBlockChain).Elem() | ||
40 | + for _, validErr := range valid.Errors { | ||
41 | + field, isExist := elem.FieldByName(validErr.Field) | ||
42 | + if isExist { | ||
43 | + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1)) | ||
44 | + } else { | ||
45 | + return fmt.Errorf(validErr.Message) | ||
46 | + } | ||
47 | + } | ||
48 | + } | ||
49 | + return nil | ||
50 | +} |
@@ -4,10 +4,14 @@ import ( | @@ -4,10 +4,14 @@ import ( | ||
4 | "fmt" | 4 | "fmt" |
5 | "github.com/linmadan/egglib-go/core/application" | 5 | "github.com/linmadan/egglib-go/core/application" |
6 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/blockChain/command" | 6 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/blockChain/command" |
7 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/blockChain/dto" | ||
8 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/blockChain/query" | ||
7 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/factory" | 9 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/factory" |
8 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain" | 10 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain" |
9 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/blockchain" | 11 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/blockchain" |
12 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/utils" | ||
10 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/log" | 13 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/log" |
14 | + "strconv" | ||
11 | "time" | 15 | "time" |
12 | ) | 16 | ) |
13 | 17 | ||
@@ -79,6 +83,97 @@ func (blockChainService *BlockChainService) UpChain(upChainCommand *command.UpCh | @@ -79,6 +83,97 @@ func (blockChainService *BlockChainService) UpChain(upChainCommand *command.UpCh | ||
79 | return upChain, nil | 83 | return upChain, nil |
80 | } | 84 | } |
81 | 85 | ||
86 | +// 区块链列表 | ||
87 | +func (blockChainService *BlockChainService) ListBlockChain(listBlockChain *query.ListBlockChainQuery) (interface{}, error) { | ||
88 | + if err := listBlockChain.ValidateQuery(); err != nil { | ||
89 | + return nil, application.ThrowError(application.ARG_ERROR, err.Error()) | ||
90 | + } | ||
91 | + transactionContext, err := factory.CreateTransactionContext(nil) | ||
92 | + if err != nil { | ||
93 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
94 | + } | ||
95 | + if err := transactionContext.StartTransaction(); err != nil { | ||
96 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
97 | + } | ||
98 | + defer func() { | ||
99 | + transactionContext.RollbackTransaction() | ||
100 | + }() | ||
101 | + | ||
102 | + upChainRepository, _, _ := factory.FastPgUpChain(transactionContext, 0) | ||
103 | + queryOptions := utils.ObjectToMap(listBlockChain) | ||
104 | + | ||
105 | + _, upChains, err := upChainRepository.Find(queryOptions) | ||
106 | + if err != nil { | ||
107 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
108 | + } | ||
109 | + response := dto.NewBlockChains(upChains) | ||
110 | + if err := transactionContext.CommitTransaction(); err != nil { | ||
111 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
112 | + } | ||
113 | + return response, nil | ||
114 | +} | ||
115 | + | ||
116 | +func (blockChainService *BlockChainService) GetBlockChainToken(listBlockChain *query.GetBlockChainTokenQuery) (interface{}, error) { | ||
117 | + if err := listBlockChain.ValidateQuery(); err != nil { | ||
118 | + return nil, application.ThrowError(application.ARG_ERROR, err.Error()) | ||
119 | + } | ||
120 | + transactionContext, err := factory.CreateTransactionContext(nil) | ||
121 | + if err != nil { | ||
122 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
123 | + } | ||
124 | + if err := transactionContext.StartTransaction(); err != nil { | ||
125 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
126 | + } | ||
127 | + defer func() { | ||
128 | + transactionContext.RollbackTransaction() | ||
129 | + }() | ||
130 | + | ||
131 | + upChainRepository, _, _ := factory.FastPgUpChain(transactionContext, 0) | ||
132 | + | ||
133 | + upChain, err := upChainRepository.FindOne(map[string]interface{}{"upChainId": listBlockChain.UpChainId}) | ||
134 | + if err != nil { | ||
135 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
136 | + } | ||
137 | + var request = blockchain.GetTokenRequest{ | ||
138 | + Type: listBlockChain.Type, | ||
139 | + ShowValue: true, | ||
140 | + } | ||
141 | + bsn := newBSNBlockChain() | ||
142 | + switch listBlockChain.Type { | ||
143 | + case blockchain.QueryByHashId: | ||
144 | + request.TsTxId = upChain.Hash | ||
145 | + case blockchain.QueryByIssueId: | ||
146 | + request.TsTxId = upChain.Hash | ||
147 | + request.IssueId = upChain.IssueId | ||
148 | + default: | ||
149 | + return nil, application.ThrowError(application.ARG_ERROR, "unknown type "+strconv.Itoa(listBlockChain.Type)) | ||
150 | + } | ||
151 | + getTokenResponse, err := bsn.GetToken(&request) | ||
152 | + if err != nil { | ||
153 | + log.Logger.Error(err.Error()) | ||
154 | + return nil, application.ThrowError(application.BUSINESS_ERROR, "不存在") | ||
155 | + } | ||
156 | + if err := transactionContext.CommitTransaction(); err != nil { | ||
157 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
158 | + } | ||
159 | + return map[string]interface{}{ | ||
160 | + "token": getTokenResponse.Token, | ||
161 | + "browseUrl": fmt.Sprintf("%v?token=%v", blockchain.BlockBrowserAddress, getTokenResponse.Token), | ||
162 | + }, nil | ||
163 | +} | ||
164 | + | ||
165 | +func newBSNBlockChain() *blockchain.BSNBlockChain { | ||
166 | + // 2.上链 | ||
167 | + bc := &blockchain.BSNBlockChain{ | ||
168 | + PublicPem: []byte(blockchain.PubPem), | ||
169 | + Host: blockchain.Host, | ||
170 | + PublicKey: blockchain.PubKey, | ||
171 | + PrivatePem: blockchain.PriK, | ||
172 | + EnableDebugLog: true, | ||
173 | + } | ||
174 | + return bc | ||
175 | +} | ||
176 | + | ||
82 | func NewBlockChainService(options map[string]interface{}) *BlockChainService { | 177 | func NewBlockChainService(options map[string]interface{}) *BlockChainService { |
83 | newBlockChainService := &BlockChainService{} | 178 | newBlockChainService := &BlockChainService{} |
84 | return newBlockChainService | 179 | return newBlockChainService |
@@ -17,3 +17,5 @@ yiAKchKkbMi6nt7EUndRIflaipUPUBSPHWnt7dFElpVZsnyAzNJcvOUCAwEAAQ== | @@ -17,3 +17,5 @@ yiAKchKkbMi6nt7EUndRIflaipUPUBSPHWnt7dFElpVZsnyAzNJcvOUCAwEAAQ== | ||
17 | var PubKey = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANh+sdA9ZoOUG131OzGSpbcqUY/nmZLTyiAKchKkbMi6nt7EUndRIflaipUPUBSPHWnt7dFElpVZsnyAzNJcvOUCAwEAAQ==" | 17 | var PubKey = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANh+sdA9ZoOUG131OzGSpbcqUY/nmZLTyiAKchKkbMi6nt7EUndRIflaipUPUBSPHWnt7dFElpVZsnyAzNJcvOUCAwEAAQ==" |
18 | 18 | ||
19 | var Host = "http://101.34.29.149:9092/test" | 19 | var Host = "http://101.34.29.149:9092/test" |
20 | + | ||
21 | +var BlockBrowserAddress = "http://101.34.29.149/browser" |
@@ -55,3 +55,8 @@ func (o *UpToChainOptions) WithNeedHash() *UpToChainOptions { | @@ -55,3 +55,8 @@ func (o *UpToChainOptions) WithNeedHash() *UpToChainOptions { | ||
55 | o.NeedHash = true | 55 | o.NeedHash = true |
56 | return o | 56 | return o |
57 | } | 57 | } |
58 | + | ||
59 | +const ( | ||
60 | + QueryByHashId = iota + 1 | ||
61 | + QueryByIssueId | ||
62 | +) |
@@ -132,7 +132,21 @@ func (repository *UpChainRepository) Find(queryOptions map[string]interface{}) ( | @@ -132,7 +132,21 @@ func (repository *UpChainRepository) Find(queryOptions map[string]interface{}) ( | ||
132 | var upChainModels []*models.UpChain | 132 | var upChainModels []*models.UpChain |
133 | upChains := make([]*domain.UpChain, 0) | 133 | upChains := make([]*domain.UpChain, 0) |
134 | query := sqlbuilder.BuildQuery(tx.Model(&upChainModels), queryOptions) | 134 | query := sqlbuilder.BuildQuery(tx.Model(&upChainModels), queryOptions) |
135 | - query.SetOffsetAndLimit(20) | 135 | + |
136 | + if v, ok := queryOptions["disableLimit"]; !(ok && v.(bool)) { | ||
137 | + query.SetOffsetAndLimit(20) | ||
138 | + } | ||
139 | + if v, ok := queryOptions["source"]; ok && len(v.(string)) > 0 { | ||
140 | + query.Where(`source = ?`, v) | ||
141 | + } | ||
142 | + if v, ok := queryOptions["primaryIdList"]; ok && len(v.([]string)) > 0 { | ||
143 | + query.Where(`primary_id in (?)`, pg.In(v.([]string))) | ||
144 | + } | ||
145 | + if v, ok := queryOptions["enableDistinctPrimaryId"]; ok && v.(bool) { | ||
146 | + query.DistinctOn(`primary_id`) | ||
147 | + query.SetOrderDirect("primary_id", "DESC") | ||
148 | + } | ||
149 | + | ||
136 | query.SetOrderDirect("up_chain_id", "DESC") | 150 | query.SetOrderDirect("up_chain_id", "DESC") |
137 | if count, err := query.SelectAndCount(); err != nil { | 151 | if count, err := query.SelectAndCount(); err != nil { |
138 | return 0, upChains, err | 152 | return 0, upChains, err |
1 | +package controllers | ||
2 | + | ||
3 | +import ( | ||
4 | + "github.com/linmadan/egglib-go/web/beego" | ||
5 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/blockChain/query" | ||
6 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/blockChain/service" | ||
7 | +) | ||
8 | + | ||
9 | +type BlockChainController struct { | ||
10 | + beego.BaseController | ||
11 | +} | ||
12 | + | ||
13 | +//func (controller *BlockChainController) CreateBlockChain() { | ||
14 | +// blockChainService := service.NewBlockChainService(nil) | ||
15 | +// createBlockChainCommand := &command.CreateBlockChainCommand{} | ||
16 | +// controller.Unmarshal(createBlockChainCommand) | ||
17 | +// data, err := blockChainService.CreateBlockChain(createBlockChainCommand) | ||
18 | +// controller.Response(data, err) | ||
19 | +//} | ||
20 | +// | ||
21 | +//func (controller *BlockChainController) UpdateBlockChain() { | ||
22 | +// blockChainService := service.NewBlockChainService(nil) | ||
23 | +// updateBlockChainCommand := &command.UpdateBlockChainCommand{} | ||
24 | +// controller.Unmarshal(updateBlockChainCommand) | ||
25 | +// blockChainId, _ := controller.GetString(":blockChainId") | ||
26 | +// updateBlockChainCommand.BlockChainId = blockChainId | ||
27 | +// data, err := blockChainService.UpdateBlockChain(updateBlockChainCommand) | ||
28 | +// controller.Response(data, err) | ||
29 | +//} | ||
30 | +// | ||
31 | +//func (controller *BlockChainController) GetBlockChain() { | ||
32 | +// blockChainService := service.NewBlockChainService(nil) | ||
33 | +// getBlockChainQuery := &query.GetBlockChainQuery{} | ||
34 | +// blockChainId, _ := controller.GetString(":blockChainId") | ||
35 | +// getBlockChainQuery.BlockChainId = blockChainId | ||
36 | +// data, err := blockChainService.GetBlockChain(getBlockChainQuery) | ||
37 | +// controller.Response(data, err) | ||
38 | +//} | ||
39 | +// | ||
40 | +//func (controller *BlockChainController) RemoveBlockChain() { | ||
41 | +// blockChainService := service.NewBlockChainService(nil) | ||
42 | +// removeBlockChainCommand := &command.RemoveBlockChainCommand{} | ||
43 | +// controller.Unmarshal(removeBlockChainCommand) | ||
44 | +// blockChainId, _ := controller.GetString(":blockChainId") | ||
45 | +// removeBlockChainCommand.BlockChainId = blockChainId | ||
46 | +// data, err := blockChainService.RemoveBlockChain(removeBlockChainCommand) | ||
47 | +// controller.Response(data, err) | ||
48 | +//} | ||
49 | + | ||
50 | +func (controller *BlockChainController) ListBlockChain() { | ||
51 | + blockChainService := service.NewBlockChainService(nil) | ||
52 | + listBlockChainQuery := &query.ListBlockChainQuery{} | ||
53 | + Must(controller.Unmarshal(listBlockChainQuery)) | ||
54 | + listBlockChainQuery.OperateInfo = ParseOperateInfo(controller.BaseController) | ||
55 | + data, err := blockChainService.ListBlockChain(listBlockChainQuery) | ||
56 | + controller.Response(data, err) | ||
57 | +} | ||
58 | + | ||
59 | +func (controller *BlockChainController) GetBlockChainToken() { | ||
60 | + blockChainService := service.NewBlockChainService(nil) | ||
61 | + listBlockChainQuery := &query.GetBlockChainTokenQuery{} | ||
62 | + Must(controller.Unmarshal(listBlockChainQuery)) | ||
63 | + //listBlockChainQuery.OperateInfo = ParseOperateInfo(controller.BaseController) | ||
64 | + data, err := blockChainService.GetBlockChainToken(listBlockChainQuery) | ||
65 | + controller.Response(data, err) | ||
66 | +} |
pkg/port/beego/routers/block_chain_router.go
0 → 100644
1 | +package routers | ||
2 | + | ||
3 | +import ( | ||
4 | + "github.com/beego/beego/v2/server/web" | ||
5 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/port/beego/controllers" | ||
6 | +) | ||
7 | + | ||
8 | +func init() { | ||
9 | + //web.Router("/block-chains/", &controllers.BlockChainController{}, "Post:CreateBlockChain") | ||
10 | + //web.Router("/block-chains/:blockChainId", &controllers.BlockChainController{}, "Put:UpdateBlockChain") | ||
11 | + //web.Router("/block-chains/:blockChainId", &controllers.BlockChainController{}, "Get:GetBlockChain") | ||
12 | + //web.Router("/block-chains/:blockChainId", &controllers.BlockChainController{}, "Delete:RemoveBlockChain") | ||
13 | + web.Router("/block-chains/", &controllers.BlockChainController{}, "Post:ListBlockChain") | ||
14 | + web.Router("/block-chains/token", &controllers.BlockChainController{}, "Post:GetBlockChainToken") | ||
15 | +} |
1 | +package block_chain | ||
2 | + | ||
3 | +import ( | ||
4 | + "net/http" | ||
5 | + "net/http/httptest" | ||
6 | + "testing" | ||
7 | + | ||
8 | + "github.com/beego/beego/v2/server/web" | ||
9 | + . "github.com/onsi/ginkgo" | ||
10 | + . "github.com/onsi/gomega" | ||
11 | + _ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg" | ||
12 | + _ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/port/beego" | ||
13 | +) | ||
14 | + | ||
15 | +func TestBlockChain(t *testing.T) { | ||
16 | + RegisterFailHandler(Fail) | ||
17 | + RunSpecs(t, "Beego Port BlockChain Correlations Test Case Suite") | ||
18 | +} | ||
19 | + | ||
20 | +var handler http.Handler | ||
21 | +var server *httptest.Server | ||
22 | + | ||
23 | +var _ = BeforeSuite(func() { | ||
24 | + handler = web.BeeApp.Handlers | ||
25 | + server = httptest.NewServer(handler) | ||
26 | +}) | ||
27 | + | ||
28 | +var _ = AfterSuite(func() { | ||
29 | + server.Close() | ||
30 | +}) |
1 | +package block_chain | ||
2 | + | ||
3 | +import ( | ||
4 | + "net/http" | ||
5 | + | ||
6 | + "github.com/gavv/httpexpect" | ||
7 | + . "github.com/onsi/ginkgo" | ||
8 | + . "github.com/onsi/gomega" | ||
9 | + pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg" | ||
10 | +) | ||
11 | + | ||
12 | +var _ = Describe("创建", func() { | ||
13 | + Describe("提交数据创建", func() { | ||
14 | + Context("提交正确的新上链数据数据", func() { | ||
15 | + It("返回上链数据数据", func() { | ||
16 | + httpExpect := httpexpect.New(GinkgoT(), server.URL) | ||
17 | + body := map[string]interface{}{ | ||
18 | + "upChainId": "int64", | ||
19 | + } | ||
20 | + httpExpect.POST("/block-chains/"). | ||
21 | + WithJSON(body). | ||
22 | + Expect(). | ||
23 | + Status(http.StatusOK). | ||
24 | + JSON(). | ||
25 | + Object(). | ||
26 | + ContainsKey("code").ValueEqual("code", 0). | ||
27 | + ContainsKey("msg").ValueEqual("msg", "ok"). | ||
28 | + ContainsKey("data").Value("data").Object(). | ||
29 | + ContainsKey("upChainId").ValueNotEqual("upChainId", BeZero()) | ||
30 | + }) | ||
31 | + }) | ||
32 | + }) | ||
33 | + AfterEach(func() { | ||
34 | + _, err := pG.DB.Exec("DELETE FROM up_chains WHERE true") | ||
35 | + Expect(err).NotTo(HaveOccurred()) | ||
36 | + }) | ||
37 | +}) |
1 | +package block_chain | ||
2 | + | ||
3 | +import ( | ||
4 | + "github.com/go-pg/pg/v10" | ||
5 | + "net/http" | ||
6 | + | ||
7 | + "github.com/gavv/httpexpect" | ||
8 | + . "github.com/onsi/ginkgo" | ||
9 | + . "github.com/onsi/gomega" | ||
10 | + pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg" | ||
11 | +) | ||
12 | + | ||
13 | +var _ = Describe("返回", func() { | ||
14 | + var upChainId int64 | ||
15 | + BeforeEach(func() { | ||
16 | + _, err := pG.DB.QueryOne( | ||
17 | + pg.Scan(&upChainId), | ||
18 | + "INSERT INTO up_chains (up_chain_id, source, primary_id, issue_id, data, hash, up_chain_status, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?) RETURNING up_chain_id", | ||
19 | + "testUpChainId", "testSource", "testPrimaryId", "testIssueId", "testData", "testHash", "testUpChainStatus", "testCreatedAt") | ||
20 | + Expect(err).NotTo(HaveOccurred()) | ||
21 | + }) | ||
22 | + Describe("根据upChainId参数返回上链数据", func() { | ||
23 | + Context("传入有效的upChainId", func() { | ||
24 | + It("返回上链数据数据", func() { | ||
25 | + httpExpect := httpexpect.New(GinkgoT(), server.URL) | ||
26 | + httpExpect.GET("/block-chains/{blockChainId}"). | ||
27 | + Expect(). | ||
28 | + Status(http.StatusOK). | ||
29 | + JSON(). | ||
30 | + Object(). | ||
31 | + ContainsKey("code").ValueEqual("code", 0). | ||
32 | + ContainsKey("msg").ValueEqual("msg", "ok"). | ||
33 | + ContainsKey("data").Value("data").Object() | ||
34 | + }) | ||
35 | + }) | ||
36 | + }) | ||
37 | + AfterEach(func() { | ||
38 | + _, err := pG.DB.Exec("DELETE FROM up_chains WHERE true") | ||
39 | + Expect(err).NotTo(HaveOccurred()) | ||
40 | + }) | ||
41 | +}) |
1 | +package block_chain | ||
2 | + | ||
3 | +import ( | ||
4 | + "github.com/go-pg/pg/v10" | ||
5 | + "net/http" | ||
6 | + | ||
7 | + "github.com/gavv/httpexpect" | ||
8 | + . "github.com/onsi/ginkgo" | ||
9 | + . "github.com/onsi/gomega" | ||
10 | + pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg" | ||
11 | +) | ||
12 | + | ||
13 | +var _ = Describe("返回列表", func() { | ||
14 | + var upChainId int64 | ||
15 | + BeforeEach(func() { | ||
16 | + _, err := pG.DB.QueryOne( | ||
17 | + pg.Scan(&upChainId), | ||
18 | + "INSERT INTO up_chains (up_chain_id, source, primary_id, issue_id, data, hash, up_chain_status, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?) RETURNING up_chain_id", | ||
19 | + "testUpChainId", "testSource", "testPrimaryId", "testIssueId", "testData", "testHash", "testUpChainStatus", "testCreatedAt") | ||
20 | + Expect(err).NotTo(HaveOccurred()) | ||
21 | + }) | ||
22 | + Describe("根据参数返回上链数据列表", func() { | ||
23 | + Context("传入有效的参数", func() { | ||
24 | + It("返回上链数据数据列表", func() { | ||
25 | + httpExpect := httpexpect.New(GinkgoT(), server.URL) | ||
26 | + httpExpect.GET("/block-chains/"). | ||
27 | + WithQuery("offset", "int"). | ||
28 | + WithQuery("limit", "int"). | ||
29 | + Expect(). | ||
30 | + Status(http.StatusOK). | ||
31 | + JSON(). | ||
32 | + Object(). | ||
33 | + ContainsKey("code").ValueEqual("code", 0). | ||
34 | + ContainsKey("msg").ValueEqual("msg", "ok"). | ||
35 | + ContainsKey("data").Value("data").Object(). | ||
36 | + ContainsKey("count").ValueEqual("count", 1). | ||
37 | + ContainsKey("upChains").Value("upChains").Array() | ||
38 | + }) | ||
39 | + }) | ||
40 | + }) | ||
41 | + AfterEach(func() { | ||
42 | + _, err := pG.DB.Exec("DELETE FROM up_chains WHERE true") | ||
43 | + Expect(err).NotTo(HaveOccurred()) | ||
44 | + }) | ||
45 | +}) |
1 | +package block_chain | ||
2 | + | ||
3 | +import ( | ||
4 | + "github.com/go-pg/pg/v10" | ||
5 | + "net/http" | ||
6 | + | ||
7 | + "github.com/gavv/httpexpect" | ||
8 | + . "github.com/onsi/ginkgo" | ||
9 | + . "github.com/onsi/gomega" | ||
10 | + pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg" | ||
11 | +) | ||
12 | + | ||
13 | +var _ = Describe("移除", func() { | ||
14 | + var upChainId int64 | ||
15 | + BeforeEach(func() { | ||
16 | + _, err := pG.DB.QueryOne( | ||
17 | + pg.Scan(&upChainId), | ||
18 | + "INSERT INTO up_chains (up_chain_id, source, primary_id, issue_id, data, hash, up_chain_status, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?) RETURNING up_chain_id", | ||
19 | + "testUpChainId", "testSource", "testPrimaryId", "testIssueId", "testData", "testHash", "testUpChainStatus", "testCreatedAt") | ||
20 | + Expect(err).NotTo(HaveOccurred()) | ||
21 | + }) | ||
22 | + Describe("根据参数移除", func() { | ||
23 | + Context("传入有效的upChainId", func() { | ||
24 | + It("返回被移除上链数据的数据", func() { | ||
25 | + httpExpect := httpexpect.New(GinkgoT(), server.URL) | ||
26 | + httpExpect.DELETE("/block-chains/{blockChainId}"). | ||
27 | + Expect(). | ||
28 | + Status(http.StatusOK). | ||
29 | + JSON(). | ||
30 | + Object(). | ||
31 | + ContainsKey("code").ValueEqual("code", 0). | ||
32 | + ContainsKey("msg").ValueEqual("msg", "ok"). | ||
33 | + ContainsKey("data").Value("data").Object() | ||
34 | + }) | ||
35 | + }) | ||
36 | + }) | ||
37 | + AfterEach(func() { | ||
38 | + _, err := pG.DB.Exec("DELETE FROM up_chains WHERE true") | ||
39 | + Expect(err).NotTo(HaveOccurred()) | ||
40 | + }) | ||
41 | +}) |
1 | +package block_chain | ||
2 | + | ||
3 | +import ( | ||
4 | + "github.com/go-pg/pg/v10" | ||
5 | + "net/http" | ||
6 | + | ||
7 | + "github.com/gavv/httpexpect" | ||
8 | + . "github.com/onsi/ginkgo" | ||
9 | + . "github.com/onsi/gomega" | ||
10 | + pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg" | ||
11 | +) | ||
12 | + | ||
13 | +var _ = Describe("更新", func() { | ||
14 | + var upChainId int64 | ||
15 | + BeforeEach(func() { | ||
16 | + _, err := pG.DB.QueryOne( | ||
17 | + pg.Scan(&upChainId), | ||
18 | + "INSERT INTO up_chains (up_chain_id, source, primary_id, issue_id, data, hash, up_chain_status, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?) RETURNING up_chain_id", | ||
19 | + "testUpChainId", "testSource", "testPrimaryId", "testIssueId", "testData", "testHash", "testUpChainStatus", "testCreatedAt") | ||
20 | + Expect(err).NotTo(HaveOccurred()) | ||
21 | + }) | ||
22 | + Describe("提交数据更新", func() { | ||
23 | + Context("提交正确的上链数据数据", func() { | ||
24 | + It("返回更新后的上链数据数据", func() { | ||
25 | + httpExpect := httpexpect.New(GinkgoT(), server.URL) | ||
26 | + body := map[string]interface{}{ | ||
27 | + "upChainId": "int64", | ||
28 | + } | ||
29 | + httpExpect.PUT("/block-chains/{blockChainId}"). | ||
30 | + WithJSON(body). | ||
31 | + Expect(). | ||
32 | + Status(http.StatusOK). | ||
33 | + JSON(). | ||
34 | + Object(). | ||
35 | + ContainsKey("code").ValueEqual("code", 0). | ||
36 | + ContainsKey("msg").ValueEqual("msg", "ok"). | ||
37 | + ContainsKey("data").Value("data").Object(). | ||
38 | + ContainsKey("upChainId").ValueEqual("upChainId", upChainId) | ||
39 | + }) | ||
40 | + }) | ||
41 | + }) | ||
42 | + AfterEach(func() { | ||
43 | + _, err := pG.DB.Exec("DELETE FROM up_chains WHERE true") | ||
44 | + Expect(err).NotTo(HaveOccurred()) | ||
45 | + }) | ||
46 | +}) |
-
请 注册 或 登录 后发表评论