Search Apps Documentation Source Content File Folder Download Copy Actions Download

storage_test.gno

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