Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tests
Browse files Browse the repository at this point in the history
CubicrootXYZ committed Oct 4, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 0dedea4 commit 8cf23e8
Showing 4 changed files with 122 additions and 4 deletions.
2 changes: 0 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -7,8 +7,6 @@ services:
environment:
- 'MYSQL_ROOT_PASSWORD=mypass'
- 'MYSQL_DATABASE=remindme'
command:
- "--default-authentication-plugin=mysql_native_password"
ports:
- 3306:3306

Original file line number Diff line number Diff line change
@@ -48,7 +48,6 @@ func (action *RescheduleRepeatingAction) Selector() []string {

// HandleEvent is where the reaction event and the related message get's send to if it matches the Selector.
func (action *RescheduleRepeatingAction) HandleEvent(event *matrix.ReactionEvent, reactionToMessage *matrixdb.MatrixMessage) {
// TODO tests & add new reaction
l := action.logger.WithFields(
map[string]any{
"reaction": event.Content.RelatesTo.Key,
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package reaction_test

import (
"errors"
"testing"

"github.com/CubicrootXYZ/gologger"
"github.com/CubicrootXYZ/matrix-reminder-and-calendar-bot/internal/connectors/matrix/actions/reaction"
matrixdb "github.com/CubicrootXYZ/matrix-reminder-and-calendar-bot/internal/connectors/matrix/database"
"github.com/CubicrootXYZ/matrix-reminder-and-calendar-bot/internal/connectors/matrix/mautrixcl"
"github.com/CubicrootXYZ/matrix-reminder-and-calendar-bot/internal/connectors/matrix/messenger"
"github.com/CubicrootXYZ/matrix-reminder-and-calendar-bot/internal/connectors/matrix/tests"
"github.com/CubicrootXYZ/matrix-reminder-and-calendar-bot/internal/database"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
)

func TestRescheduleRepeatAction(t *testing.T) {
action := &reaction.RescheduleRepeatingAction{}

assert.NotEmpty(t, action.Name())

title, desc, examples := action.GetDocu()
assert.NotEmpty(t, title)
assert.NotEmpty(t, desc)
assert.NotEmpty(t, examples)

assert.NotNil(t, action.Selector())
}

func TestRescheduleRepeatingAction_Selector(t *testing.T) {
action := &reaction.RescheduleRepeatingAction{}

examples := []string{}

_, _, examplesFromDocu := action.GetDocu()
examples = append(examples, examplesFromDocu...)

reactions := action.Selector()
for _, example := range examples {
matches := false
for _, reaction := range reactions {
if example == reaction {
matches = true
break
}
}
assert.Truef(t, matches, "%s is not part of reactions", example)
}
}

func TestRescheduleRepeatingAction_HandleEvent(t *testing.T) {
// Setup
ctrl := gomock.NewController(t)
defer ctrl.Finish()
db := database.NewMockService(ctrl)
matrixDB := matrixdb.NewMockService(ctrl)
client := mautrixcl.NewMockClient(ctrl)
msngr := messenger.NewMockMessenger(ctrl)

action := &reaction.RescheduleRepeatingAction{}
action.Configure(
gologger.New(gologger.LogLevelDebug, 0),
client,
msngr,
matrixDB,
db,
nil,
)

t.Run("success case", func(_ *testing.T) {
msg := tests.TestMessage(
tests.WithTestEvent(),
)

// Expectations
db.EXPECT().NewEvent(gomock.Any()).Return(nil, nil)

msngr.EXPECT().DeleteMessageAsync(gomock.Any()).Return(nil)

// Execute
action.HandleEvent(tests.TestReactionEvent(
tests.ReactionWithKey("✅"),
), msg)
})

t.Run("new event fails", func(_ *testing.T) {
msg := tests.TestMessage(
tests.WithTestEvent(),
)

// Expectations
db.EXPECT().NewEvent(gomock.Any()).Return(nil, errors.New("test"))

msngr.EXPECT().SendMessageAsync(messenger.PlainTextMessage(
"Whoopsie, can not update the event as requested.",
"!room123",
)).Return(nil)

// Execute
action.HandleEvent(tests.TestReactionEvent(
tests.ReactionWithKey("✅"),
), msg)
})

t.Run("delete message fails", func(_ *testing.T) {
msg := tests.TestMessage(
tests.WithTestEvent(),
)

// Expectations
db.EXPECT().NewEvent(gomock.Any()).Return(nil, nil)

msngr.EXPECT().DeleteMessageAsync(gomock.Any()).Return(errors.New("test"))

// Execute
action.HandleEvent(tests.TestReactionEvent(
tests.ReactionWithKey("✅"),
), msg)
})
}
2 changes: 1 addition & 1 deletion internal/connectors/matrix/send_reminders.go
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ import (
"github.com/CubicrootXYZ/matrix-reminder-and-calendar-bot/internal/daemon"
)

var ReminderReactions = []string{"✅", "▶️", "⏩", "1️⃣", "4️⃣"}
var ReminderReactions = []string{"✅", "▶️", "⏩", "1️⃣", "4️⃣", "🔂"}

func (service *service) SendReminder(event *daemon.Event, output *daemon.Output) error {
room, err := service.matrixDatabase.GetRoomByID(output.OutputID)

0 comments on commit 8cf23e8

Please sign in to comment.