Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: unit tests #15

Merged
merged 2 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 22 additions & 20 deletions pkg/game/game-service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,32 @@ func TestCreate(t *testing.T) {
ctx := context.Background()

tests := []struct {
name string
inputPlayerIDs []string
inputName string
inputAdminID string
mockError *[]error
expectingError bool
name string
inputPlayerIDs []string
inputAdminID string
mockUpsertError *[]error
expectingError bool
}{
{
name: "simple create",
inputPlayerIDs: []string{"1", "2"},
inputName: "test",
inputAdminID: "1",
mockError: &[]error{nil},
name: "simple create",
inputPlayerIDs: []string{"1", "2"},
inputAdminID: "1",
mockUpsertError: &[]error{nil},
},
{
name: "duplicate player IDs",
inputPlayerIDs: []string{
"1",
"1",
},
inputName: "test",
inputAdminID: "1",
mockError: &[]error{nil},
inputAdminID: "1",
mockUpsertError: &[]error{nil},
expectingError: true,
},
{
name: "admin not in game",
inputPlayerIDs: []string{"1", "2"},
inputAdminID: "3",
expectingError: true,
},
{
Expand All @@ -43,17 +46,16 @@ func TestCreate(t *testing.T) {
"1",
"2",
},
inputName: "test",
inputAdminID: "1",
mockError: &[]error{errors.New("failed to upsert")},
expectingError: true,
inputAdminID: "1",
mockUpsertError: &[]error{errors.New("failed to upsert")},
expectingError: true,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
mockCol := &db.MockCollection[Game]{
MockUpsertErr: test.mockError,
MockUpsertErr: test.mockUpsertError,
}

ds := &Service{
Expand All @@ -68,7 +70,7 @@ func TestCreate(t *testing.T) {
}
} else {
if result.Name != test.name {
t.Errorf("expected name %s, got %s", test.inputName, result.Name)
t.Errorf("expected name %s, got %s", test.name, result.Name)
}
if result.AdminID != test.inputAdminID {
t.Errorf("expected admin id %s, got %s", test.inputAdminID, result.AdminID)
Expand Down
20 changes: 20 additions & 0 deletions pkg/game/game-utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,18 @@ func NewGame(playerIDs []string, name string, adminID string) (Game, error) {
return Game{}, err
}

// Verify the admin is in the list of players
adminFound := false
for _, playerID := range playerIDs {
if playerID == adminID {
adminFound = true
break
}
}
if !adminFound {
return Game{}, errors.New("admin not found in players")
}

// Randomise the order of the players
shuffledPlayerIDs := shuffle(playerIDs)

Expand Down Expand Up @@ -192,3 +204,11 @@ func containsAllUnique(referenceSlice, targetSlice []CardName) bool {
}
return true
}

// compare checks if targetSlice and referenceSlice are equivalent
func compare(referenceSlice, targetSlice []CardName) bool {
if len(referenceSlice) != len(targetSlice) {
return false
}
return containsAllUnique(referenceSlice, targetSlice)
}
2 changes: 1 addition & 1 deletion pkg/game/game_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ func TestGame_SelectSuit(t *testing.T) {
if err != nil {
t.Errorf("expected no error, got %v", err)
}
if !containsAllUnique(state.Cards, test.cards) {
if !compare(state.Cards, test.cards) {
t.Errorf("expected player to have all of the selected cards %v, got %v", test.cards, state.Cards)
}
}
Expand Down
Loading