Skip to content

Commit

Permalink
Unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NiJeTi committed Jan 2, 2025
1 parent f3943ff commit d42072e
Show file tree
Hide file tree
Showing 12 changed files with 811 additions and 15 deletions.
11 changes: 11 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ linters:
- wastedassign
- whitespace
- wrapcheck

linters-settings:
copyloopvar:
check-alias: true
Expand Down Expand Up @@ -97,6 +98,8 @@ linters-settings:
- anon
lll:
line-length: 80
nilnil:
detect-opposite: true
nolintlint:
require-explanation: true
require-specific: true
Expand Down Expand Up @@ -178,3 +181,11 @@ linters-settings:
yaml: kebab
testifylint:
enable-all: true

issues:
exclude-rules:
- path: _test\.go
linters:
- dupl
- path: _test\.go
text: "add-constant:"
8 changes: 4 additions & 4 deletions cmd/service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ func main() {
func run() (code int) {
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))

logger.Info("starting")
defer logger.Info("shutdown complete")

defer func() {
if err := recover(); err != nil {
logger.Error("panic", "error", err)
Expand All @@ -58,9 +61,6 @@ func run() (code int) {
)
defer cancel()

logger.Info("starting")
defer logger.Info("shutdown complete")

cfg, err := cfgpkg.ReadConfig[config]()
if err != nil {
logger.ErrorContext(ctx, "failed to read config", "error", err)
Expand All @@ -75,6 +75,7 @@ func run() (code int) {
logger.ErrorContext(ctx, "failed to connect to db", "error", err)
return codeErr
}
defer dbConn.Close()

dbProbe := db.NewProbe(dbConn.DB)

Expand Down Expand Up @@ -131,7 +132,6 @@ func run() (code int) {
logger.ErrorContext(ctx, "failed to set commands", "error", err)
return codeErr
}
defer dcRouter.UnsetCommands() //nolint:errcheck // shutdown

dcProbe := discord.NewProbe(dcRouter.Session())

Expand Down
11 changes: 11 additions & 0 deletions internal/discord/commands/responses/cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,22 @@ import (
"github.com/bwmarrin/discordgo"
)

func CastNoChannel() *discordgo.InteractionResponse {
return &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Either specify a channel or join one",
Flags: discordgo.MessageFlagsEphemeral,
},
}
}

func CastNoUsers() *discordgo.InteractionResponse {
return &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "There are no users in the voice channel",
Flags: discordgo.MessageFlagsEphemeral,
},
}
}
Expand Down
8 changes: 8 additions & 0 deletions internal/discord/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ func NewRouter(cfg Config, opts ...RouterOpt) (*Router, error) {
}

func (r *Router) Close() error {
if err := r.UnsetCommands(); err != nil {
return fmt.Errorf("failed to unset commands: %w", err)
}

if err := r.session.Close(); err != nil {
return fmt.Errorf("failed to close Discord session: %w", err)
}
Expand All @@ -66,6 +70,10 @@ func (r *Router) Session() *discordgo.Session {
}

func (r *Router) SetCommands(commands ...Command) error {
if err := r.UnsetCommands(); err != nil {
return fmt.Errorf("failed to unset previous commands: %w", err)
}

for _, cmd := range commands {
name := cmd.Description.Name

Expand Down
10 changes: 6 additions & 4 deletions internal/services/addQuote/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package addQuote
import (
"context"
"fmt"
"time"

"github.com/bwmarrin/discordgo"

Expand Down Expand Up @@ -37,10 +38,11 @@ func (s *Service) Exec(
}

quote := &models.Quote{
Author: author,
Text: text,
GuildID: models.ID(i.GuildID),
AddedBy: i.Member,
Author: author,
Text: text,
GuildID: models.ID(i.GuildID),
AddedBy: i.Member,
Timestamp: time.Now(),
}
err = s.db.AddUserQuoteOnGuild(ctx, quote)
if err != nil {
Expand Down
130 changes: 130 additions & 0 deletions internal/services/addQuote/service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package addQuote_test

import (
"context"
"errors"
"testing"

"github.com/bwmarrin/discordgo"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"

mocks "github.com/nijeti/cinema-keeper/internal/generated/mocks/services/addQuote"
"github.com/nijeti/cinema-keeper/internal/models"
"github.com/nijeti/cinema-keeper/internal/services/addQuote"
)

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

ctx := context.Background()
i := &discordgo.Interaction{
GuildID: "1",
Member: &discordgo.Member{
User: &discordgo.User{
ID: "2",
},
Nick: "addedBy",
Avatar: "avatar",
},
}
author := &discordgo.Member{
User: &discordgo.User{
ID: "1",
},
Nick: "author",
Avatar: "avatar",
}
const text = "text"

type setup func(t *testing.T, err error) *addQuote.Service

tests := map[string]struct {
err error
setup setup
}{
"guild_member_error": {
err: errors.New("guild member error"),
setup: func(t *testing.T, err error) *addQuote.Service {
d := mocks.NewMockDiscord(t)
db := mocks.NewMockDb(t)

d.EXPECT().GuildMember(
ctx, models.ID(i.GuildID), models.ID(author.User.ID),
).Return(nil, err)

return addQuote.New(d, db)
},
},
"db_error": {
err: errors.New("db error"),
setup: func(t *testing.T, err error) *addQuote.Service {
d := mocks.NewMockDiscord(t)
db := mocks.NewMockDb(t)

d.EXPECT().GuildMember(
ctx, models.ID(i.GuildID), models.ID(author.User.ID),
).Return(author, nil)

db.EXPECT().AddUserQuoteOnGuild(ctx, mock.Anything).Return(err)

return addQuote.New(d, db)
},
},
"respond_error": {
err: errors.New("respond error"),
setup: func(t *testing.T, err error) *addQuote.Service {
d := mocks.NewMockDiscord(t)
db := mocks.NewMockDb(t)

d.EXPECT().GuildMember(
ctx, models.ID(i.GuildID), models.ID(author.User.ID),
).Return(author, nil)

db.EXPECT().AddUserQuoteOnGuild(ctx, mock.Anything).Return(nil)

d.EXPECT().Respond(ctx, i, mock.Anything).Return(err)

return addQuote.New(d, db)
},
},
"success": {
err: nil,
setup: func(t *testing.T, _ error) *addQuote.Service {
d := mocks.NewMockDiscord(t)
db := mocks.NewMockDb(t)

d.EXPECT().GuildMember(
ctx, models.ID(i.GuildID), models.ID(author.User.ID),
).Return(author, nil)

db.EXPECT().AddUserQuoteOnGuild(
ctx, mock.Anything,
).RunAndReturn(
func(_ context.Context, quote *models.Quote) error {
assert.Equal(t, author, quote.Author)
assert.Equal(t, text, quote.Text)
assert.Equal(t, i.GuildID, quote.GuildID.String())
assert.Equal(t, i.Member, quote.AddedBy)

return nil
},
)

d.EXPECT().Respond(ctx, i, mock.Anything).Return(nil)

return addQuote.New(d, db)
},
},
}

for name, tt := range tests {
t.Run(
name, func(t *testing.T) {
s := tt.setup(t, tt.err)
err := s.Exec(ctx, i, models.ID(author.User.ID), text)
assert.ErrorIs(t, err, tt.err)
},
)
}
}
3 changes: 1 addition & 2 deletions internal/services/diceRoll/service_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//nolint:dupl // table-driven tests
package diceRoll_test

import (
Expand Down Expand Up @@ -106,7 +105,7 @@ func TestService_Exec(t *testing.T) {
"multiple_d100_roll": {
args: args{
size: dice.D100,
count: 10, //nolint:revive // random value
count: 10,
},
err: nil,
setup: func(t *testing.T, args args, err error) *diceRoll.Service {
Expand Down
4 changes: 1 addition & 3 deletions internal/services/lockVoiceChan/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ func (s *Service) Exec(
}

err = s.discord.EditChannel(
ctx,
models.ID(voiceState.ChannelID),
&discordgo.ChannelEdit{UserLimit: *limit},
ctx, models.ID(channel.ID), &discordgo.ChannelEdit{UserLimit: *limit},
)
if err != nil {
return fmt.Errorf("failed to edit channel user limit: %w", err)
Expand Down
Loading

0 comments on commit d42072e

Please sign in to comment.