This repository was archived by the owner on Feb 4, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathstate_test.go
More file actions
133 lines (106 loc) · 4.46 KB
/
state_test.go
File metadata and controls
133 lines (106 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package state_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"encoding/json"
"testing"
identitytestdata "github.com/hyperledger-labs/cckit/identity/testdata"
"github.com/hyperledger-labs/cckit/state"
"github.com/hyperledger-labs/cckit/state/testdata"
"github.com/hyperledger-labs/cckit/state/testdata/schema"
testcc "github.com/hyperledger-labs/cckit/testing"
expectcc "github.com/hyperledger-labs/cckit/testing/expect"
)
func TestState(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "State suite")
}
var (
booksCC *testcc.MockStub
Owner = identitytestdata.Certificates[0].MustIdentity(`SOME_MSP`)
)
var _ = Describe(`State`, func() {
BeforeSuite(func() {
//Create books chaincode mock - struct based schema
booksCC = testcc.NewMockStub(`books`, testdata.NewBooksCC())
booksCC.From(Owner).Init()
})
Describe(`Struct based schema`, func() {
It("Allow to insert entries", func() {
expectcc.ResponseOk(booksCC.From(Owner).Invoke(`bookInsert`, &testdata.Books[0]))
expectcc.ResponseOk(booksCC.From(Owner).Invoke(`bookInsert`, &testdata.Books[1]))
expectcc.ResponseOk(booksCC.From(Owner).Invoke(`bookInsert`, &testdata.Books[2]))
})
It("Disallow to insert entries with same keys", func() {
expectcc.ResponseError(booksCC.From(Owner).Invoke(`bookInsert`, &testdata.Books[2]))
})
It("Allow to get entry list", func() {
books := expectcc.PayloadIs(booksCC.Invoke(`bookList`), &[]schema.Book{}).([]schema.Book)
Expect(len(books)).To(Equal(3))
for i := range testdata.Books {
Expect(books[i]).To(Equal(testdata.Books[i]))
}
})
It("allow to get list with pagination", func() {
req := &schema.BookListRequest{ // query first page
PageSize: 2,
}
pr := booksCC.Invoke(`bookListPaginated`, req)
res := expectcc.PayloadIs(pr, &schema.BookList{}).(schema.BookList)
Expect(len(res.Items)).To(Equal(2))
Expect(res.Next == ``).To(Equal(false))
for i := range testdata.Books[0:2] {
Expect(res.Items[i].Id).To(Equal(testdata.Books[i].Id))
}
req2 := &schema.BookListRequest{ // query second page
PageSize: 2,
Bookmark: res.Next,
}
pr2 := booksCC.Invoke(`bookListPaginated`, req2)
res2 := expectcc.PayloadIs(pr2, &schema.BookList{}).(schema.BookList)
Expect(len(res2.Items)).To(Equal(1))
Expect(res2.Next == ``).To(Equal(true))
for i := range testdata.Books[2:3] {
Expect(res.Items[i].Id).To(Equal(testdata.Books[i].Id))
}
})
It("Allow to get entry ids", func() {
ids := expectcc.PayloadIs(booksCC.Invoke(`bookIds`), &[]string{}).([]string)
Expect(len(ids)).To(Equal(3))
for i := range testdata.Books {
Expect(ids[i]).To(Equal(testdata.MustCreateCompositeKey(schema.BookEntity, []string{testdata.Books[i].Id})))
}
})
It("Allow to get entry converted to target type", func() {
book1FromCC := expectcc.PayloadIs(booksCC.Invoke(`bookGet`, testdata.Books[0].Id), &schema.Book{}).(schema.Book)
Expect(book1FromCC).To(Equal(testdata.Books[0]))
})
It("Allow to get entry json", func() {
book2JsonFromCC := booksCC.Invoke(`bookGet`, testdata.Books[2].Id).Payload
book2Json, _ := json.Marshal(testdata.Books[2])
Expect(book2JsonFromCC).To(Equal(book2Json))
})
It("Allow to upsert entry", func() {
bookToUpdate := testdata.Books[2]
bookToUpdate.Title = `thirdiest title`
bookUpdated := expectcc.PayloadIs(booksCC.Invoke(`bookUpsert`, &bookToUpdate), &schema.Book{}).(schema.Book)
Expect(bookUpdated.Title).To(Equal(bookToUpdate.Title))
bookFromCC := expectcc.PayloadIs(booksCC.Invoke(`bookGet`, bookToUpdate.Id), &schema.Book{}).(schema.Book)
Expect(bookFromCC).To(Equal(bookUpdated))
})
It("Allow to upsert entry with tx state caching", func() {
bookToUpdate := testdata.Books[1]
bookToUpdate.Title = `once more strange uniq title`
bookUpdated := expectcc.PayloadIs(booksCC.Invoke(`bookUpsertWithCache`, &bookToUpdate), &schema.Book{}).(schema.Book)
Expect(bookUpdated.Title).To(Equal(bookToUpdate.Title))
bookFromCC := expectcc.PayloadIs(booksCC.Invoke(`bookGet`, bookToUpdate.Id), &schema.Book{}).(schema.Book)
Expect(bookFromCC).To(Equal(bookToUpdate))
})
It("Allow to delete entry", func() {
expectcc.ResponseOk(booksCC.From(Owner).Invoke(`bookDelete`, testdata.Books[0].Id))
books := expectcc.PayloadIs(booksCC.Invoke(`bookList`), &[]schema.Book{}).([]schema.Book)
Expect(len(books)).To(Equal(2))
expectcc.ResponseError(booksCC.Invoke(`bookGet`, testdata.Books[0].Id), state.ErrKeyNotFound)
})
})
})