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