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

thread_test.gno

5.28 Kb · 181 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 TestNewThread(t *testing.T) {
 12	tests := []struct {
 13		name        string
 14		board       *boards.Board
 15		creator     address
 16		title, body string
 17		errMsg      string
 18	}{
 19		{
 20			name:    "ok",
 21			board:   boards.New(1),
 22			creator: "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
 23			title:   "Test",
 24			body:    "Foo",
 25		},
 26		{
 27			name:   "nil board",
 28			board:  nil,
 29			errMsg: "thread requires a parent board",
 30		},
 31		{
 32			name:    "invalid creator",
 33			board:   boards.New(1),
 34			creator: "foo",
 35			errMsg:  "invalid thread creator address: foo",
 36		},
 37		{
 38			name:    "empty title",
 39			board:   boards.New(1),
 40			creator: "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
 41			title:   "",
 42			errMsg:  "thread title is required",
 43		},
 44		{
 45			name:    "empty body",
 46			board:   boards.New(1),
 47			creator: "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
 48			title:   "Test",
 49			body:    "",
 50			errMsg:  "thread body is required",
 51		},
 52	}
 53
 54	for _, tt := range tests {
 55		t.Run(tt.name, func(t *testing.T) {
 56			thread, err := boards.NewThread(tt.board, tt.creator, tt.title, tt.body)
 57
 58			if tt.errMsg != "" {
 59				urequire.Error(t, err, "expect an error")
 60				urequire.ErrorContains(t, err, tt.errMsg, "expect error to match")
 61				return
 62			}
 63
 64			urequire.NoError(t, err, "expect no error")
 65			urequire.True(t, tt.board.ThreadsSequence.Last() == thread.ID, "expect ID to match")
 66			urequire.True(t, thread.ThreadID == thread.ID, "expect thread ID to match")
 67			urequire.False(t, thread.Board == nil, "expect board to be assigned")
 68			urequire.True(t, tt.board.ID == thread.Board.ID, "expect board ID to match")
 69			urequire.Equal(t, tt.title, thread.Title, "expect title to match")
 70			urequire.Equal(t, tt.body, thread.Body, "expect body to match")
 71			urequire.True(t, thread.Replies != nil, "expect thread to support sub-replies")
 72			urequire.True(t, thread.Reposts != nil, "expect thread to support reposts")
 73			urequire.True(t, thread.Flags != nil, "expect thread to support flagging")
 74			urequire.Equal(t, tt.creator, thread.Creator, "expect creator to match")
 75			urequire.False(t, thread.CreatedAt.IsZero(), "expect creation date to be assigned")
 76		})
 77	}
 78}
 79
 80func TestNewRepost(t *testing.T) {
 81	tests := []struct {
 82		name       string
 83		origThread *boards.Post
 84		dstBoard   *boards.Board
 85		creator    address
 86		errMsg     string
 87	}{
 88		{
 89			name: "ok",
 90			origThread: boards.MustNewThread(
 91				boards.New(1),
 92				"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
 93				"Title",
 94				"Body",
 95			),
 96			dstBoard: boards.New(2),
 97			creator:  "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
 98		},
 99		{
100			name:       "nil original thread",
101			origThread: nil,
102			errMsg:     "thread to repost is required",
103		},
104		{
105			name:       "original thread without board",
106			origThread: &boards.Post{ID: 1},
107			errMsg:     "original thread has no board assigned",
108		},
109		{
110			name: "nil destination board",
111			origThread: boards.MustNewThread(
112				boards.New(1),
113				"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
114				"Title",
115				"Body",
116			),
117			dstBoard: nil,
118			errMsg:   "thread repost requires a destination board",
119		},
120		{
121			name: "original thread is not a thread",
122			origThread: &boards.Post{
123				ID:       1,
124				ThreadID: 2,
125				Board:    boards.New(1),
126			},
127			dstBoard: boards.New(2),
128			errMsg:   "post must be a thread to be reposted to another board",
129		},
130		{
131			name: "original thread is a repost",
132			origThread: &boards.Post{
133				ID:              1,
134				ThreadID:        1,
135				OriginalBoardID: 1,
136				Board:           boards.New(1),
137			},
138			dstBoard: boards.New(2),
139			errMsg:   "reposting a thread that is a repost is not allowed",
140		},
141		{
142			name: "invalid creator",
143			origThread: boards.MustNewThread(
144				boards.New(1),
145				"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
146				"Title",
147				"Body",
148			),
149			dstBoard: boards.New(2),
150			creator:  "foo",
151			errMsg:   "invalid thread repost creator address: foo",
152		},
153	}
154
155	for _, tt := range tests {
156		t.Run(tt.name, func(t *testing.T) {
157			thread, err := boards.NewRepost(tt.origThread, tt.dstBoard, tt.creator)
158
159			if tt.errMsg != "" {
160				urequire.Error(t, err, "expect an error")
161				urequire.ErrorContains(t, err, tt.errMsg, "expect error to match")
162				return
163			}
164
165			urequire.NoError(t, err, "expect no error")
166			urequire.True(t, thread.ID == tt.dstBoard.ThreadsSequence.Last(), "expect ID to match")
167			urequire.True(t, thread.ThreadID == thread.ID, "expect thread ID to match")
168			urequire.True(t, thread.ParentID == tt.origThread.ID, "expect parent ID to match")
169			urequire.True(t, thread.OriginalBoardID == tt.origThread.Board.ID, "expect original board ID to match")
170			urequire.False(t, thread.Board == nil, "expect board to be assigned")
171			urequire.True(t, thread.Board.ID == tt.dstBoard.ID, "expect board ID to match")
172			urequire.Empty(t, thread.Title, "expect title to be empty")
173			urequire.Empty(t, thread.Body, "expect body to be empty")
174			urequire.True(t, thread.Replies != nil, "expect thread to support sub-replies")
175			urequire.True(t, thread.Reposts != nil, "expect thread to support reposts")
176			urequire.True(t, thread.Flags != nil, "expect thread to support flagging")
177			urequire.Equal(t, tt.creator, thread.Creator, "expect creator to match")
178			urequire.False(t, thread.CreatedAt.IsZero(), "expect creation date to be assigned")
179		})
180	}
181}