Search Apps Documentation Source Content File Folder Download Copy Actions Download

repost_storage_test.gno

5.51 Kb · 297 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 TestRepostStorageGet(t *testing.T) {
 12	tests := []struct {
 13		name              string
 14		setup             func() boards.RepostStorage
 15		boardID, repostID boards.ID
 16		found             bool
 17	}{
 18		{
 19			name: "single repost",
 20			setup: func() boards.RepostStorage {
 21				s := boards.NewRepostStorage()
 22				s.Add(&boards.Post{
 23					ID:    1,
 24					Board: &boards.Board{ID: 1},
 25				})
 26				return s
 27			},
 28			boardID:  1,
 29			repostID: 1,
 30			found:    true,
 31		},
 32		{
 33			name: "multiple reposts",
 34			setup: func() boards.RepostStorage {
 35				s := boards.NewRepostStorage()
 36				s.Add(&boards.Post{
 37					ID:    2,
 38					Board: &boards.Board{ID: 1},
 39				})
 40				s.Add(&boards.Post{
 41					ID:    5,
 42					Board: &boards.Board{ID: 2},
 43				})
 44				s.Add(&boards.Post{
 45					ID:    10,
 46					Board: &boards.Board{ID: 3},
 47				})
 48				return s
 49			},
 50			boardID:  1,
 51			repostID: 2,
 52			found:    true,
 53		},
 54		{
 55			name: "not found",
 56			setup: func() boards.RepostStorage {
 57				return boards.NewRepostStorage()
 58			},
 59			boardID: 404,
 60		},
 61	}
 62
 63	for _, tt := range tests {
 64		t.Run(tt.name, func(t *testing.T) {
 65			s := tt.setup()
 66
 67			repostID, found := s.Get(tt.boardID)
 68
 69			if !tt.found {
 70				urequire.False(t, found, "expect repost not to be found")
 71				urequire.True(t, int(repostID) == 0, "expect repost ID to be 0")
 72				return
 73			}
 74
 75			urequire.True(t, found, "expect post to be found")
 76			urequire.Equal(t, tt.repostID.String(), repostID.String(), "expect repost ID to match")
 77		})
 78	}
 79}
 80
 81func TestRepostStorageAdd(t *testing.T) {
 82	tests := []struct {
 83		name   string
 84		repost *boards.Post
 85		errMsg string
 86	}{
 87		{
 88			name: "ok",
 89			repost: &boards.Post{
 90				ID:    1,
 91				Board: &boards.Board{ID: 1},
 92			},
 93		},
 94		{
 95			name:   "nil repost",
 96			repost: nil,
 97			errMsg: "saving nil reposts is not allowed",
 98		},
 99	}
100
101	for _, tt := range tests {
102		t.Run(tt.name, func(t *testing.T) {
103			s := boards.NewRepostStorage()
104
105			err := s.Add(tt.repost)
106
107			if tt.errMsg != "" {
108				urequire.Error(t, err, "expect an error")
109				urequire.ErrorContains(t, err, tt.errMsg, "expect error to match")
110				return
111			}
112
113			urequire.NoError(t, err, "expect no error")
114
115			_, found := s.Get(tt.repost.ID)
116			urequire.True(t, found, "expect repost to be found")
117		})
118	}
119}
120
121func TestRepostStorageRemove(t *testing.T) {
122	tests := []struct {
123		name    string
124		setup   func() boards.RepostStorage
125		boardID boards.ID
126		removed bool
127	}{
128		{
129			name: "ok",
130			setup: func() boards.RepostStorage {
131				s := boards.NewRepostStorage()
132				s.Add(&boards.Post{
133					ID:    100,
134					Board: &boards.Board{ID: 1},
135				})
136				return s
137			},
138			boardID: 1,
139			removed: true,
140		},
141		{
142			name: "not found",
143			setup: func() boards.RepostStorage {
144				return boards.NewRepostStorage()
145			},
146			boardID: 404,
147		},
148	}
149
150	for _, tt := range tests {
151		t.Run(tt.name, func(t *testing.T) {
152			s := tt.setup()
153
154			removed := s.Remove(tt.boardID)
155
156			if !tt.removed {
157				urequire.False(t, removed, "expect repost not to be removed")
158				return
159			}
160
161			urequire.True(t, removed, "expect repost to be removed")
162
163			_, found := s.Get(tt.boardID)
164			urequire.False(t, found, "expect repost not to be found")
165		})
166	}
167}
168
169func TestRepostStorageSize(t *testing.T) {
170	tests := []struct {
171		name  string
172		setup func() boards.RepostStorage
173		size  int
174	}{
175		{
176			name: "empty",
177			setup: func() boards.RepostStorage {
178				return boards.NewRepostStorage()
179			},
180			size: 0,
181		},
182		{
183			name: "one repost",
184			setup: func() boards.RepostStorage {
185				s := boards.NewRepostStorage()
186				s.Add(&boards.Post{
187					ID:    1,
188					Board: &boards.Board{ID: 1},
189				})
190				return s
191			},
192			size: 1,
193		},
194		{
195			name: "multiple reposts",
196			setup: func() boards.RepostStorage {
197				s := boards.NewRepostStorage()
198				s.Add(&boards.Post{
199					ID:    1,
200					Board: &boards.Board{ID: 1},
201				})
202				s.Add(&boards.Post{
203					ID:    1,
204					Board: &boards.Board{ID: 2},
205				})
206				return s
207			},
208			size: 2,
209		},
210	}
211
212	for _, tt := range tests {
213		t.Run(tt.name, func(t *testing.T) {
214			s := tt.setup()
215
216			urequire.Equal(t, tt.size, s.Size())
217		})
218	}
219}
220
221func TestRepostStorageIterate(t *testing.T) {
222	tests := []struct {
223		name    string
224		setup   func() boards.RepostStorage
225		reverse bool
226		ids     [][2]boards.ID
227	}{
228		{
229			name: "default",
230			setup: func() boards.RepostStorage {
231				s := boards.NewRepostStorage()
232				s.Add(&boards.Post{
233					ID:    10,
234					Board: &boards.Board{ID: 1},
235				})
236				s.Add(&boards.Post{
237					ID:    20,
238					Board: &boards.Board{ID: 2},
239				})
240				s.Add(&boards.Post{
241					ID:    30,
242					Board: &boards.Board{ID: 3},
243				})
244				return s
245			},
246			ids: [][2]boards.ID{
247				{1, 10},
248				{2, 20},
249				{3, 30},
250			},
251		},
252		{
253			name: "reverse",
254			setup: func() boards.RepostStorage {
255				s := boards.NewRepostStorage()
256				s.Add(&boards.Post{
257					ID:    10,
258					Board: &boards.Board{ID: 1},
259				})
260				s.Add(&boards.Post{
261					ID:    20,
262					Board: &boards.Board{ID: 2},
263				})
264				s.Add(&boards.Post{
265					ID:    30,
266					Board: &boards.Board{ID: 3},
267				})
268				return s
269			},
270			reverse: true,
271			ids: [][2]boards.ID{
272				{3, 30},
273				{2, 20},
274				{1, 10},
275			},
276		},
277	}
278
279	for _, tt := range tests {
280		t.Run(tt.name, func(t *testing.T) {
281			s := tt.setup()
282			count := s.Size()
283			if tt.reverse {
284				count = -count
285			}
286
287			var i int
288			s.Iterate(0, count, func(boardID, repostID boards.ID) bool {
289				urequire.True(t, tt.ids[i][0] == boardID, "expect board ID to match")
290				urequire.True(t, tt.ids[i][1] == repostID, "expect repost ID to match")
291
292				i++
293				return false
294			})
295		})
296	}
297}