Skip to content

Commit ab9ee48

Browse files
Matin Gohar FarMatin Gohar Far
authored andcommitted
added tests for new canceled status
1 parent f1553ae commit ab9ee48

5 files changed

Lines changed: 173 additions & 1 deletion

File tree

server/src/api/votings_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,43 @@ func (suite *VotingTestSuite) TestCloseVoting() {
114114

115115
}
116116

117+
func (suite *VotingTestSuite) TestCancelVoting() {
118+
119+
testParameterBundles := *TestParameterBundles{}.
120+
Append("all ok", http.StatusOK, nil, false, false, nil).
121+
Append("unexpected error", http.StatusInternalServerError, errors.New("oops"), false, false, nil)
122+
123+
for _, tt := range testParameterBundles {
124+
suite.Run(tt.name, func() {
125+
s := new(Server)
126+
//s.basePath = "/"
127+
votingMock := votings.NewMockVotingService(suite.T())
128+
notesMock := notes.NewMockNotesService(suite.T())
129+
130+
boardId, _ := uuid.NewRandom()
131+
votingId, _ := uuid.NewRandom()
132+
s.votings = votingMock
133+
s.notes = notesMock
134+
135+
req := technical_helper.NewTestRequestBuilder("PUT", "/", strings.NewReader(`{"status": "ABORTED"}`))
136+
req.Req = logger.InitTestLoggerRequest(req.Request())
137+
req.AddToContext(identifiers.BoardIdentifier, boardId).
138+
AddToContext(identifiers.VotingIdentifier, votingId)
139+
140+
notesMock.EXPECT().GetAll(mock.Anything, boardId).Return([]*notes.Note{}, nil)
141+
142+
votingMock.EXPECT().Cancel(mock.Anything, votingId, boardId, []votings.Note(nil)).
143+
Return(&votings.Voting{Status: votings.Canceled}, tt.err)
144+
145+
rr := httptest.NewRecorder()
146+
s.updateVoting(rr, req.Request())
147+
suite.Equal(tt.expectedCode, rr.Result().StatusCode)
148+
votingMock.AssertExpectations(suite.T())
149+
votingMock.AssertNumberOfCalls(suite.T(), "Cancel", 1)
150+
})
151+
}
152+
}
153+
117154
func (suite *VotingTestSuite) TestGetVoting() {
118155
s := new(Server)
119156
votingMock := votings.NewMockVotingService(suite.T())

server/src/votings/database_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,46 @@ func (suite *DatabaseVotingTestSuite) Test_Database_GetVotes() {
283283
assert.Len(t, dbVotes, 18)
284284
}
285285

286+
func (suite *DatabaseVotingTestSuite) Test_Database_Cancel() {
287+
t := suite.T()
288+
database := NewVotingDatabase(suite.db)
289+
290+
votingId := suite.baseData.Votings["Update"].ID
291+
boardId := suite.baseData.Boards["Update"].ID
292+
293+
dbVoting, err := database.Close(context.Background(),
294+
DatabaseVotingUpdate{
295+
ID: votingId,
296+
Board: boardId,
297+
Status: Canceled,
298+
},
299+
)
300+
301+
assert.Nil(t, err)
302+
assert.Equal(t, votingId, dbVoting.ID)
303+
assert.Equal(t, boardId, dbVoting.Board)
304+
assert.Equal(t, Canceled, dbVoting.Status)
305+
assert.Equal(t, 7, dbVoting.VoteLimit)
306+
assert.True(t, dbVoting.AllowMultipleVotes)
307+
assert.False(t, dbVoting.ShowVotesOfOthers)
308+
assert.False(t, dbVoting.IsAnonymous)
309+
assert.NotNil(t, dbVoting.CreatedAt)
310+
311+
// Verify notes are NOT re-ranked after cancel
312+
noteDatabase := notes.NewNotesDatabase(suite.db)
313+
dbNotes, notesErr := noteDatabase.GetAll(context.Background(), boardId)
314+
assert.Nil(t, notesErr)
315+
316+
noteRankMap := make(map[uuid.UUID]int)
317+
for _, n := range dbNotes {
318+
noteRankMap[n.ID] = n.Rank
319+
}
320+
321+
assert.Equal(t, 0, noteRankMap[suite.baseData.Notes["Update2"].ID])
322+
assert.Equal(t, 0, noteRankMap[suite.baseData.Notes["Update1"].ID])
323+
assert.Equal(t, 0, noteRankMap[suite.baseData.Notes["Update3"].ID])
324+
}
325+
286326
func (suite *DatabaseVotingTestSuite) seedVotes(db *bun.DB) {
287327
log.Println("Seeding voting database test votes")
288328

server/src/votings/service_integration_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,35 @@ func (suite *VotingServiceIntegrationTestSuite) Test_CloseVoting() {
226226
assert.Equal(t, 6, votingData.Voting.VotingResults.Total)
227227
}
228228

229+
func (suite *VotingServiceIntegrationTestSuite) Test_CancelVoting() {
230+
t := suite.T()
231+
ctx := context.Background()
232+
233+
votingId := suite.baseData.Votings["Update"].ID
234+
boardId := suite.baseData.Boards["Update"].ID
235+
236+
events := suite.broker.GetBoardChannel(ctx, boardId)
237+
238+
affectedNotes := []Note{
239+
{ID: suite.baseData.Notes["Update1"].ID, Author: suite.baseData.Notes["Update1"].AuthorID, Text: suite.baseData.Notes["Update1"].Text, Position: NotePosition{Column: suite.baseData.Notes["Update1"].ColumnID}},
240+
{ID: suite.baseData.Notes["Update2"].ID, Author: suite.baseData.Notes["Update2"].AuthorID, Text: suite.baseData.Notes["Update2"].Text, Position: NotePosition{Column: suite.baseData.Notes["Update2"].ColumnID}},
241+
{ID: suite.baseData.Notes["Update3"].ID, Author: suite.baseData.Notes["Update3"].AuthorID, Text: suite.baseData.Notes["Update3"].Text, Position: NotePosition{Column: suite.baseData.Notes["Update3"].ColumnID}},
242+
}
243+
voting, err := suite.votingService.Cancel(ctx, votingId, boardId, affectedNotes)
244+
245+
require.NoError(t, err)
246+
assert.Equal(t, votingId, voting.ID)
247+
assert.Equal(t, Canceled, voting.Status)
248+
assert.Nil(t, voting.VotingResults)
249+
250+
msg := <-events
251+
assert.Equal(t, realtime.BoardEventVotingUpdated, msg.Type)
252+
votingData, err := technical_helper.Unmarshal[UpdateVoting](msg.Data)
253+
require.NoError(t, err)
254+
assert.Equal(t, Canceled, votingData.Voting.Status)
255+
assert.Nil(t, votingData.Voting.VotingResults)
256+
}
257+
229258
func (suite *VotingServiceIntegrationTestSuite) Test_CloseVoting_Sorted_Cards() {
230259
t := suite.T()
231260
ctx := context.Background()

server/src/votings/service_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,72 @@ func TestCloseVoting(t *testing.T) {
281281
assert.Equal(t, Closed, voting.Status)
282282
}
283283

284+
func TestCancelVoting(t *testing.T) {
285+
boardId := uuid.New()
286+
votingID := uuid.New()
287+
288+
mockDb := NewMockVotingDatabase(t)
289+
mockDb.EXPECT().Close(mock.Anything, DatabaseVotingUpdate{ID: votingID, Board: boardId, Status: Canceled}).
290+
Return(DatabaseVoting{ID: votingID, Board: boardId, Status: Canceled}, nil)
291+
292+
mockBroker := realtime.NewMockClient(t)
293+
mockBroker.EXPECT().Publish(mock.Anything, mock.AnythingOfType("string"), mock.Anything).Return(nil)
294+
broker := new(realtime.Broker)
295+
broker.Con = mockBroker
296+
297+
service := NewVotingService(mockDb, broker)
298+
voting, err := service.Cancel(context.Background(), votingID, boardId, nil)
299+
300+
assert.NoError(t, err)
301+
assert.NotNil(t, voting)
302+
assert.Equal(t, Canceled, voting.Status)
303+
}
304+
305+
func TestCancelVoting_NotFound(t *testing.T) {
306+
boardId := uuid.New()
307+
votingID := uuid.New()
308+
309+
mockDb := NewMockVotingDatabase(t)
310+
mockDb.EXPECT().Close(mock.Anything, DatabaseVotingUpdate{ID: votingID, Board: boardId, Status: Canceled}).
311+
Return(DatabaseVoting{}, sql.ErrNoRows)
312+
313+
mockBroker := realtime.NewMockClient(t)
314+
broker := new(realtime.Broker)
315+
broker.Con = mockBroker
316+
317+
service := NewVotingService(mockDb, broker)
318+
voting, err := service.Cancel(context.Background(), votingID, boardId, nil)
319+
320+
assert.Nil(t, voting)
321+
assert.NotNil(t, err)
322+
323+
var votingErr VotingError
324+
assert.ErrorAs(t, err, &votingErr)
325+
326+
assert.Equal(t, NotFound, votingErr.Category)
327+
}
328+
329+
func TestCancelVoting_Failed(t *testing.T) {
330+
boardId := uuid.New()
331+
votingID := uuid.New()
332+
dbError := errors.New("failed to cancel")
333+
334+
mockDb := NewMockVotingDatabase(t)
335+
mockDb.EXPECT().Close(mock.Anything, DatabaseVotingUpdate{ID: votingID, Board: boardId, Status: Canceled}).
336+
Return(DatabaseVoting{}, dbError)
337+
338+
mockBroker := realtime.NewMockClient(t)
339+
broker := new(realtime.Broker)
340+
broker.Con = mockBroker
341+
342+
service := NewVotingService(mockDb, broker)
343+
voting, err := service.Cancel(context.Background(), votingID, boardId, nil)
344+
345+
assert.Nil(t, voting)
346+
assert.NotNil(t, err)
347+
assert.ErrorIs(t, err, dbError)
348+
}
349+
284350
func TestCloseVoting_NotFound(t *testing.T) {
285351
boardId := uuid.New()
286352
votingID := uuid.New()

server/src/votings/voting_status_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
func TestVotingStatusEnum(t *testing.T) {
11-
values := []VotingStatus{Open, Closed}
11+
values := []VotingStatus{Open, Closed, Canceled}
1212
for _, value := range values {
1313
var votingStatus VotingStatus
1414
err := votingStatus.UnmarshalJSON(fmt.Appendf(nil, "\"%s\"", value))

0 commit comments

Comments
 (0)