Search Apps Documentation Source Content File Folder Download Copy Actions Download

replies_storage_test.gno

7.66 Kb · 319 lines
  1package boards2_test
  2
  3import (
  4	"testing"
  5
  6	"gno.land/p/gnoland/boards"
  7	"gno.land/p/nt/urequire"
  8
  9	boards2 "gno.land/r/boards000/v1rc1"
 10)
 11
 12func TestReplyStorageGet(t *testing.T) {
 13	creator := address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
 14	tests := []struct {
 15		name   string
 16		setup  func(thread *boards.Post) boards.PostStorage
 17		postID boards.ID
 18		found  bool
 19	}{
 20		{
 21			name: "single reply",
 22			setup: func(t *boards.Post) boards.PostStorage {
 23				s := boards2.NewReplyStorage()
 24				s.Add(boards.MustNewReply(t, creator, "A")) // ID=2
 25				return s
 26			},
 27			postID: 2,
 28			found:  true,
 29		},
 30		{
 31			name: "multiple replies",
 32			setup: func(t *boards.Post) boards.PostStorage {
 33				s := boards2.NewReplyStorage()
 34				s.Add(boards.MustNewReply(t, creator, "A"))
 35				s.Add(boards.MustNewReply(t, creator, "B")) // ID=3
 36				s.Add(boards.MustNewReply(t, creator, "C"))
 37				return s
 38			},
 39			postID: 3,
 40			found:  true,
 41		},
 42		{
 43			name: "single sub reply",
 44			setup: func(t *boards.Post) boards.PostStorage {
 45				parent := boards.MustNewReply(t, creator, "A")
 46				s := boards2.NewReplyStorage()
 47				s.Add(parent)
 48				s.Add(boards.MustNewReply(parent, creator, "B")) // ID=3
 49				return s
 50			},
 51			postID: 3,
 52			found:  true,
 53		},
 54		{
 55			name: "multiple sub replies",
 56			setup: func(t *boards.Post) boards.PostStorage {
 57				parent := boards.MustNewReply(t, creator, "A")
 58				s := boards2.NewReplyStorage()
 59				s.Add(parent)
 60				s.Add(boards.MustNewReply(parent, creator, "C"))
 61				s.Add(boards.MustNewReply(parent, creator, "D")) // ID=4
 62				s.Add(boards.MustNewReply(parent, creator, "E"))
 63				return s
 64			},
 65			postID: 4,
 66			found:  true,
 67		},
 68		{
 69			name: "not found",
 70			setup: func(*boards.Post) boards.PostStorage {
 71				return boards2.NewReplyStorage()
 72			},
 73			postID: 404,
 74		},
 75	}
 76
 77	for _, tt := range tests {
 78		t.Run(tt.name, func(t *testing.T) {
 79			board := boards.New(1)
 80			thread := boards.MustNewThread(board, creator, "Title", "Body")
 81			s := tt.setup(thread)
 82
 83			reply, found := s.Get(tt.postID)
 84
 85			if !tt.found {
 86				urequire.False(t, found, "expect reply not to be found")
 87				urequire.True(t, reply == nil, "expect reply to be nil")
 88				return
 89			}
 90
 91			urequire.True(t, found, "expect reply to be found")
 92			urequire.False(t, reply == nil, "expect reply not to be nil")
 93			urequire.Equal(t, tt.postID.String(), reply.ID.String(), "expect reply ID to match")
 94		})
 95	}
 96}
 97
 98func TestReplyStorageRemove(t *testing.T) {
 99	creator := address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
100	tests := []struct {
101		name    string
102		setup   func(thread *boards.Post) boards.PostStorage
103		postID  boards.ID
104		removed bool
105	}{
106		{
107			name: "reply",
108			setup: func(t *boards.Post) boards.PostStorage {
109				s := boards2.NewReplyStorage()
110				s.Add(boards.MustNewReply(t, creator, "A"))
111				s.Add(boards.MustNewReply(t, creator, "B")) // ID=3
112				return s
113			},
114			postID:  3,
115			removed: true,
116		},
117		{
118			name: "sub reply",
119			setup: func(t *boards.Post) boards.PostStorage {
120				parent := boards.MustNewReply(t, creator, "A")
121				s := boards2.NewReplyStorage()
122				s.Add(parent)
123				s.Add(boards.MustNewReply(parent, creator, "A"))
124				s.Add(boards.MustNewReply(parent, creator, "B")) // ID=4
125				return s
126			},
127			postID:  4,
128			removed: true,
129		},
130		{
131			name: "not found",
132			setup: func(*boards.Post) boards.PostStorage {
133				return boards2.NewReplyStorage()
134			},
135			postID: 404,
136		},
137	}
138
139	for _, tt := range tests {
140		t.Run(tt.name, func(t *testing.T) {
141			board := boards.New(1)
142			thread := boards.MustNewThread(board, creator, "Title", "Body")
143			s := tt.setup(thread)
144
145			reply, removed := s.Remove(tt.postID)
146
147			if !tt.removed {
148				urequire.False(t, removed, "expect reply not to be removed")
149				urequire.True(t, reply == nil, "expect reply to be nil")
150				return
151			}
152
153			urequire.True(t, removed, "expect reply to be removed")
154			urequire.False(t, reply == nil, "expect reply not to be nil")
155			urequire.Equal(t, tt.postID.String(), reply.ID.String(), "expect reply ID to match")
156
157			_, found := s.Get(tt.postID)
158			urequire.False(t, found, "expect reply not to be found")
159		})
160	}
161}
162
163func TestReplyStorageAdd(t *testing.T) {
164	tests := []struct {
165		name   string
166		post   *boards.Post
167		errMsg string
168	}{
169		{
170			name: "reply",
171			post: &boards.Post{ID: 2, ParentID: 1, ThreadID: 1},
172		},
173		{
174			name: "sub reply",
175			post: &boards.Post{ID: 3, ParentID: 2, ThreadID: 1},
176		},
177		{
178			name:   "nil reply",
179			post:   nil,
180			errMsg: "saving nil replies is not allowed",
181		},
182	}
183
184	for _, tt := range tests {
185		t.Run(tt.name, func(t *testing.T) {
186			s := boards2.NewReplyStorage()
187
188			err := s.Add(tt.post)
189
190			if tt.errMsg != "" {
191				urequire.Error(t, err, "expect an error")
192				urequire.ErrorContains(t, err, tt.errMsg, "expect error to match")
193				return
194			}
195
196			urequire.NoError(t, err, "expect no error")
197
198			_, found := s.Get(tt.post.ID)
199			urequire.True(t, found, "expect reply to be found")
200		})
201	}
202}
203
204func TestReplyStorageSize(t *testing.T) {
205	creator := address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
206	tests := []struct {
207		name  string
208		setup func(thread *boards.Post) boards.PostStorage
209		size  int
210	}{
211		{
212			name: "empty",
213			setup: func(*boards.Post) boards.PostStorage {
214				return boards2.NewReplyStorage()
215			},
216			size: 0,
217		},
218		{
219			name: "one reply",
220			setup: func(t *boards.Post) boards.PostStorage {
221				s := boards2.NewReplyStorage()
222				s.Add(boards.MustNewReply(t, creator, "A"))
223				return s
224			},
225			size: 1,
226		},
227		{
228			name: "multiple replies",
229			setup: func(t *boards.Post) boards.PostStorage {
230				s := boards2.NewReplyStorage()
231				s.Add(boards.MustNewReply(t, creator, "A"))
232				s.Add(boards.MustNewReply(t, creator, "B"))
233				return s
234			},
235			size: 2,
236		},
237		{
238			name: "multiple replies and sub reply",
239			setup: func(t *boards.Post) boards.PostStorage {
240				parent := boards.MustNewReply(t, creator, "A")
241				s := boards2.NewReplyStorage()
242				s.Add(parent)
243				s.Add(boards.MustNewReply(t, creator, "B"))
244				s.Add(boards.MustNewReply(parent, creator, "A2"))
245				return s
246			},
247			size: 2, // Sub-replies are not counted
248		},
249	}
250
251	for _, tt := range tests {
252		t.Run(tt.name, func(t *testing.T) {
253			board := boards.New(1)
254			thread := boards.MustNewThread(board, creator, "Title", "Body")
255			s := tt.setup(thread)
256
257			urequire.Equal(t, tt.size, s.Size())
258		})
259	}
260}
261
262func TestReplyStorageIterate(t *testing.T) {
263	creator := address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
264	tests := []struct {
265		name    string
266		setup   func(thread *boards.Post) boards.PostStorage
267		reverse bool
268		ids     []boards.ID
269	}{
270		{
271			name: "default",
272			setup: func(t *boards.Post) boards.PostStorage {
273				parent := boards.MustNewReply(t, creator, "A")
274				s := boards2.NewReplyStorage()
275				s.Add(parent)
276				s.Add(boards.MustNewReply(t, creator, "B"))
277				s.Add(boards.MustNewReply(t, creator, "C"))
278				s.Add(boards.MustNewReply(parent, creator, "A2")) // Sub-replies are ignored
279				return s
280			},
281			ids: []boards.ID{2, 3, 4},
282		},
283		{
284			name: "reverse",
285			setup: func(t *boards.Post) boards.PostStorage {
286				parent := boards.MustNewReply(t, creator, "A")
287				s := boards2.NewReplyStorage()
288				s.Add(parent)
289				s.Add(boards.MustNewReply(t, creator, "B"))
290				s.Add(boards.MustNewReply(t, creator, "C"))
291				s.Add(boards.MustNewReply(parent, creator, "A2")) // Sub-replies are ignored
292				return s
293			},
294			reverse: true,
295			ids:     []boards.ID{4, 3, 2},
296		},
297	}
298
299	for _, tt := range tests {
300		t.Run(tt.name, func(t *testing.T) {
301			board := boards.New(1)
302			thread := boards.MustNewThread(board, creator, "Title", "Body")
303			s := tt.setup(thread)
304
305			count := s.Size()
306			if tt.reverse {
307				count = -count
308			}
309
310			var i int
311			s.Iterate(0, count, func(p *boards.Post) bool {
312				urequire.True(t, tt.ids[i] == p.ID, "expect post ID to match")
313
314				i++
315				return false
316			})
317		})
318	}
319}