reply_test.gno
2.58 Kb · 96 lines
1package boards_test
2
3import (
4 "testing"
5
6 "gno.land/p/nt/urequire/v0"
7
8 "gno.land/p/gnoland/boards"
9)
10
11func TestNewReply(t *testing.T) {
12 tests := []struct {
13 name string
14 parent func() *boards.Post
15 creator address
16 body string
17 errMsg string
18 }{
19 {
20 name: "ok",
21 parent: func() *boards.Post {
22 board := boards.New(1)
23 id := board.ThreadsSequence.Next()
24 return &boards.Post{
25 ID: id,
26 ThreadID: id,
27 Board: board,
28 }
29 },
30 creator: "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
31 body: "Foo",
32 },
33 {
34 name: "nil parent",
35 parent: func() *boards.Post { return nil },
36 errMsg: "reply requires a parent thread or reply",
37 },
38 {
39 name: "parent without thread ID",
40 parent: func() *boards.Post {
41 return &boards.Post{ID: 1}
42 },
43 errMsg: "parent has no thread ID assigned",
44 },
45 {
46 name: "parent without board",
47 parent: func() *boards.Post {
48 return &boards.Post{ID: 1, ThreadID: 1}
49 },
50 errMsg: "parent has no board assigned",
51 },
52 {
53 name: "invalid creator",
54 parent: func() *boards.Post {
55 return &boards.Post{ID: 1, ThreadID: 1, Board: boards.New(1)}
56 },
57 creator: "foo",
58 errMsg: "invalid reply creator address: foo",
59 },
60 {
61 name: "empty body",
62 parent: func() *boards.Post {
63 return &boards.Post{ID: 1, ThreadID: 1, Board: boards.New(1)}
64 },
65 creator: "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
66 body: "",
67 errMsg: "reply body is required",
68 },
69 }
70
71 for _, tt := range tests {
72 t.Run(tt.name, func(t *testing.T) {
73 parent := tt.parent()
74
75 reply, err := boards.NewReply(parent, tt.creator, tt.body)
76
77 if tt.errMsg != "" {
78 urequire.Error(t, err, "expect an error")
79 urequire.ErrorContains(t, err, tt.errMsg, "expect error to match")
80 return
81 }
82
83 urequire.NoError(t, err, "expect no error")
84 urequire.True(t, parent.Board.ThreadsSequence.Last() == reply.ID, "expect ID to match")
85 urequire.True(t, parent.ID == reply.ParentID, "expect parent ID to match")
86 urequire.True(t, parent.ThreadID == reply.ThreadID, "expect thread ID to match")
87 urequire.False(t, reply.Board == nil, "expect board to be assigned")
88 urequire.True(t, parent.Board.ID == reply.Board.ID, "expect board ID to match")
89 urequire.Equal(t, tt.body, reply.Body, "expect body to match")
90 urequire.True(t, reply.Replies != nil, "expect reply to support sub-replies")
91 urequire.True(t, reply.Flags != nil, "expect reply to support flagging")
92 urequire.Equal(t, tt.creator, reply.Creator, "expect creator to match")
93 urequire.False(t, reply.CreatedAt.IsZero(), "expect creation date to be assigned")
94 })
95 }
96}