Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

post_storage_test.gno

4.73 Kb · 246 lines
  1package boards_test
  2
  3import (
  4	"testing"
  5
  6	"gno.land/p/nt/urequire/v0"
  7
  8	"gno.land/p/gnoland/boards/v0"
  9)
 10
 11func TestPostStorageGet(t *testing.T) {
 12	tests := []struct {
 13		name   string
 14		setup  func() boards.PostStorage
 15		postID boards.ID
 16		found  bool
 17	}{
 18		{
 19			name: "single post",
 20			setup: func() boards.PostStorage {
 21				s := boards.NewPostStorage()
 22				s.Add(&boards.Post{ID: 1})
 23				return s
 24			},
 25			postID: 1,
 26			found:  true,
 27		},
 28		{
 29			name: "multiple posts",
 30			setup: func() boards.PostStorage {
 31				s := boards.NewPostStorage()
 32				s.Add(&boards.Post{ID: 1})
 33				s.Add(&boards.Post{ID: 2})
 34				s.Add(&boards.Post{ID: 3})
 35				return s
 36			},
 37			postID: 2,
 38			found:  true,
 39		},
 40		{
 41			name: "not found",
 42			setup: func() boards.PostStorage {
 43				return boards.NewPostStorage()
 44			},
 45			postID: 404,
 46		},
 47	}
 48
 49	for _, tt := range tests {
 50		t.Run(tt.name, func(t *testing.T) {
 51			s := tt.setup()
 52
 53			post, found := s.Get(tt.postID)
 54
 55			if !tt.found {
 56				urequire.False(t, found, "expect post not to be found")
 57				urequire.True(t, post == nil, "expect post to be nil")
 58				return
 59			}
 60
 61			urequire.True(t, found, "expect post to be found")
 62			urequire.False(t, post == nil, "expect post not to be nil")
 63			urequire.Equal(t, tt.postID.String(), post.ID.String(), "expect post ID to match")
 64		})
 65	}
 66}
 67
 68func TestPostStorageRemove(t *testing.T) {
 69	tests := []struct {
 70		name    string
 71		setup   func() boards.PostStorage
 72		postID  boards.ID
 73		removed bool
 74	}{
 75		{
 76			name: "ok",
 77			setup: func() boards.PostStorage {
 78				s := boards.NewPostStorage()
 79				s.Add(&boards.Post{ID: 1})
 80				s.Add(&boards.Post{ID: 2})
 81				return s
 82			},
 83			postID:  2,
 84			removed: true,
 85		},
 86		{
 87			name: "not found",
 88			setup: func() boards.PostStorage {
 89				return boards.NewPostStorage()
 90			},
 91			postID: 404,
 92		},
 93	}
 94
 95	for _, tt := range tests {
 96		t.Run(tt.name, func(t *testing.T) {
 97			s := tt.setup()
 98
 99			post, removed := s.Remove(tt.postID)
100
101			if !tt.removed {
102				urequire.False(t, removed, "expect post not to be removed")
103				urequire.True(t, post == nil, "expect post to be nil")
104				return
105			}
106
107			urequire.True(t, removed, "expect post to be removed")
108			urequire.False(t, post == nil, "expect post not to be nil")
109			urequire.Equal(t, tt.postID.String(), post.ID.String(), "expect post ID to match")
110
111			_, found := s.Get(tt.postID)
112			urequire.False(t, found, "expect post not to be found")
113		})
114	}
115}
116
117func TestPostStorageAdd(t *testing.T) {
118	tests := []struct {
119		name   string
120		post   *boards.Post
121		errMsg string
122	}{
123		{
124			name: "ok",
125			post: &boards.Post{ID: 1},
126		},
127		{
128			name:   "nil post",
129			post:   nil,
130			errMsg: "saving nil posts is not allowed",
131		},
132	}
133
134	for _, tt := range tests {
135		t.Run(tt.name, func(t *testing.T) {
136			s := boards.NewPostStorage()
137
138			err := s.Add(tt.post)
139
140			if tt.errMsg != "" {
141				urequire.Error(t, err, "expect an error")
142				urequire.ErrorContains(t, err, tt.errMsg, "expect error to match")
143				return
144			}
145
146			urequire.NoError(t, err, "expect no error")
147
148			_, found := s.Get(tt.post.ID)
149			urequire.True(t, found, "expect post to be found")
150		})
151	}
152}
153
154func TestPostStorageSize(t *testing.T) {
155	tests := []struct {
156		name  string
157		setup func() boards.PostStorage
158		size  int
159	}{
160		{
161			name: "empty",
162			setup: func() boards.PostStorage {
163				return boards.NewPostStorage()
164			},
165			size: 0,
166		},
167		{
168			name: "one post",
169			setup: func() boards.PostStorage {
170				s := boards.NewPostStorage()
171				s.Add(&boards.Post{ID: 1})
172				return s
173			},
174			size: 1,
175		},
176		{
177			name: "multiple posts",
178			setup: func() boards.PostStorage {
179				s := boards.NewPostStorage()
180				s.Add(&boards.Post{ID: 1})
181				s.Add(&boards.Post{ID: 2})
182				return s
183			},
184			size: 2,
185		},
186	}
187
188	for _, tt := range tests {
189		t.Run(tt.name, func(t *testing.T) {
190			s := tt.setup()
191
192			urequire.Equal(t, tt.size, s.Size())
193		})
194	}
195}
196
197func TestPostStorageIterate(t *testing.T) {
198	tests := []struct {
199		name    string
200		setup   func() boards.PostStorage
201		reverse bool
202		ids     []boards.ID
203	}{
204		{
205			name: "default",
206			setup: func() boards.PostStorage {
207				s := boards.NewPostStorage()
208				s.Add(&boards.Post{ID: 1})
209				s.Add(&boards.Post{ID: 2})
210				s.Add(&boards.Post{ID: 3})
211				return s
212			},
213			ids: []boards.ID{1, 2, 3},
214		},
215		{
216			name: "reverse",
217			setup: func() boards.PostStorage {
218				s := boards.NewPostStorage()
219				s.Add(&boards.Post{ID: 1})
220				s.Add(&boards.Post{ID: 2})
221				s.Add(&boards.Post{ID: 3})
222				return s
223			},
224			reverse: true,
225			ids:     []boards.ID{3, 2, 1},
226		},
227	}
228
229	for _, tt := range tests {
230		t.Run(tt.name, func(t *testing.T) {
231			s := tt.setup()
232			count := s.Size()
233			if tt.reverse {
234				count = -count
235			}
236
237			var i int
238			s.Iterate(0, count, func(p *boards.Post) bool {
239				urequire.True(t, tt.ids[i] == p.ID, "expect post ID to match")
240
241				i++
242				return false
243			})
244		})
245	}
246}