Skip to content

Commit

Permalink
chore: add tests to discord/loot
Browse files Browse the repository at this point in the history
  • Loading branch information
Antony Ramos committed Sep 24, 2023
1 parent 8e1327f commit 68f6589
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 19 deletions.
46 changes: 27 additions & 19 deletions internal/controller/discord/loot.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,24 +143,28 @@ func (d Discord) ListLootsOnPlayerHandler(
lootList, err := d.LootUseCase.ListLootOnPLayer(ctx, playerName)
if err != nil {
msg := "Erreur lors de la récupération des loots: " + HumanReadableError(err)
if !d.Fake {
_ = session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: msg,
},
})
}
return fmt.Errorf("discord - ListLootsOnPlayerHandler - d.LootUseCase.ListLootOnPLayer: %w", err)

Check warning on line 154 in internal/controller/discord/loot.go

View check run for this annotation

Codecov / codecov/patch

internal/controller/discord/loot.go#L145-L154

Added lines #L145 - L154 were not covered by tests
}
msg := "Tous les loots de " + playerName + ":\n"
for _, loot := range lootList {
msg += loot.Name + " " + loot.Raid.Date.String() + " " + loot.Raid.Difficulty + "\n"
}

Check warning on line 159 in internal/controller/discord/loot.go

View check run for this annotation

Codecov / codecov/patch

internal/controller/discord/loot.go#L158-L159

Added lines #L158 - L159 were not covered by tests
if !d.Fake {
_ = session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: msg,
},
})
return fmt.Errorf("discord - ListLootsOnPlayerHandler - d.LootUseCase.ListLootOnPLayer: %w", err)
}
msg := "Tous les loots de " + playerName + ":\n"
for _, loot := range lootList {
msg += loot.Name + " " + loot.Raid.Date.String() + " " + loot.Raid.Difficulty + "\n"
}
_ = session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: msg,
},
})
return nil
}

Expand All @@ -185,21 +189,25 @@ func (d Discord) DeleteLootHandler(
err = d.LootUseCase.DeleteLoot(ctx, id)
if err != nil {
msg := "Erreur lors de la suppression du loot: " + HumanReadableError(err)
if !d.Fake {
_ = session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: msg,
},
})
}
return fmt.Errorf("discord - DeleteLootHandler - d.LootUseCase.DeleteLoot: %w", err)

Check warning on line 200 in internal/controller/discord/loot.go

View check run for this annotation

Codecov / codecov/patch

internal/controller/discord/loot.go#L191-L200

Added lines #L191 - L200 were not covered by tests
}
msg := "Loot supprimé avec succès"
if !d.Fake {
_ = session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: msg,
},
})
return fmt.Errorf("discord - DeleteLootHandler - d.LootUseCase.DeleteLoot: %w", err)
}
msg := "Loot supprimé avec succès"
_ = session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: msg,
},
})
return nil
}

Expand Down
94 changes: 94 additions & 0 deletions internal/controller/discord/loot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
discordHandler "github.com/antony-ramos/guildops/internal/controller/discord"
"github.com/antony-ramos/guildops/internal/controller/discord/mocks"
"github.com/bwmarrin/discordgo"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

Expand Down Expand Up @@ -66,3 +67,96 @@ func TestDiscord_AttributeLootHandler(t *testing.T) {
mockLootUseCase.AssertExpectations(t)
})
}

func TestDiscord_ListLootsOnPlayerHandler(t *testing.T) {
t.Parallel()

t.Run("Success", func(t *testing.T) {
t.Parallel()
mockLootUseCase := mocks.NewLootUseCase(t)

discord := discordHandler.Discord{
AbsenceUseCase: nil,
PlayerUseCase: nil,
StrikeUseCase: nil,
LootUseCase: mockLootUseCase,
RaidUseCase: nil,
Fake: true,
}

mockLootUseCase.On("ListLootOnPLayer", mock.Anything, mock.Anything).
Return(nil, nil)

session := &discordgo.Session{StateEnabled: true, State: discordgo.NewState()}
interaction := &discordgo.InteractionCreate{
Interaction: &discordgo.Interaction{
Type: discordgo.InteractionApplicationCommand,
Data: discordgo.ApplicationCommandInteractionData{
ID: "mock",
Name: "mock",
TargetID: "mock",
Resolved: &discordgo.ApplicationCommandInteractionDataResolved{},
Options: []*discordgo.ApplicationCommandInteractionDataOption{
{
Name: "player-name",
Type: discordgo.ApplicationCommandOptionString,
Value: "TestPlayer",
},
},
},
},
}

err := discord.ListLootsOnPlayerHandler(context.Background(), session, interaction)
if err != nil {
return
}
mockLootUseCase.AssertExpectations(t)
})
}

// DeleteLootHandler.
func TestDiscord_DeleteLootHandler(t *testing.T) {
t.Parallel()

t.Run("Success", func(t *testing.T) {
t.Parallel()
mockLootUseCase := mocks.NewLootUseCase(t)

discord := discordHandler.Discord{
AbsenceUseCase: nil,
PlayerUseCase: nil,
StrikeUseCase: nil,
LootUseCase: mockLootUseCase,
RaidUseCase: nil,
Fake: true,
}

mockLootUseCase.On("DeleteLoot", mock.Anything, mock.Anything).
Return(nil)

session := &discordgo.Session{StateEnabled: true, State: discordgo.NewState()}
interaction := &discordgo.InteractionCreate{
Interaction: &discordgo.Interaction{
Type: discordgo.InteractionApplicationCommand,
Data: discordgo.ApplicationCommandInteractionData{
ID: "mock",
Name: "mock",
TargetID: "mock",
Resolved: &discordgo.ApplicationCommandInteractionDataResolved{},
Options: []*discordgo.ApplicationCommandInteractionDataOption{
{
Name: "id",
Type: discordgo.ApplicationCommandOptionString,
Value: "1",
},
},
},
},
}

err := discord.DeleteLootHandler(context.Background(), session, interaction)
assert.NoError(t, err)
mockLootUseCase.AssertExpectations(t)
})
}

0 comments on commit 68f6589

Please sign in to comment.