Skip to content

Commit

Permalink
Write tests
Browse files Browse the repository at this point in the history
Signed-off-by: Christiaan de Die le Clercq <[email protected]>
  • Loading branch information
Techwolf12 committed Feb 9, 2024
1 parent 10867d7 commit c791422
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 18 deletions.
41 changes: 23 additions & 18 deletions pkg/services/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,45 +19,50 @@ type telegramService struct {
opts TelegramOptions
}

func (s telegramService) Send(notification Notification, dest Destination) error {
bot, err := tgbotapi.NewBotAPI(s.opts.Token)
if err != nil {
return err
}
func buildTelegramMessageOptions(notification Notification, dest Destination) (*tgbotapi.MessageConfig, error) {
msg := tgbotapi.MessageConfig{}

if strings.HasPrefix(dest.Recipient, "-") {
chatChannel := strings.Split(dest.Recipient, "|")

chatID, err := strconv.ParseInt(chatChannel[0], 10, 64)
if err != nil {
return err
return nil, err

Check warning on line 30 in pkg/services/telegram.go

View check run for this annotation

Codecov / codecov/patch

pkg/services/telegram.go#L30

Added line #L30 was not covered by tests
}

// Init message with ParseMode is 'Markdown'
msg := tgbotapi.NewMessage(chatID, notification.Message)
msg = tgbotapi.NewMessage(chatID, notification.Message)
msg.ParseMode = "Markdown"

if len(chatChannel) > 1 {
threadID, err := strconv.Atoi(chatChannel[1])
if err != nil {
return err
return nil, err
}

Check warning on line 41 in pkg/services/telegram.go

View check run for this annotation

Codecov / codecov/patch

pkg/services/telegram.go#L40-L41

Added lines #L40 - L41 were not covered by tests
msg.MessageThreadID = threadID
}

_, err = bot.Send(msg)
if err != nil {
return err
}
} else {
// Init message with ParseMode is 'Markdown'
msg := tgbotapi.NewMessageToChannel("@"+dest.Recipient, notification.Message)
msg = tgbotapi.NewMessageToChannel("@"+dest.Recipient, notification.Message)
msg.ParseMode = "Markdown"
}
return &msg, nil
}

_, err := bot.Send(msg)
if err != nil {
return err
}
func (s telegramService) Send(notification Notification, dest Destination) error {
bot, err := tgbotapi.NewBotAPI(s.opts.Token)
if err != nil {
return err
}

Check warning on line 56 in pkg/services/telegram.go

View check run for this annotation

Codecov / codecov/patch

pkg/services/telegram.go#L52-L56

Added lines #L52 - L56 were not covered by tests

msg, err := buildTelegramMessageOptions(notification, dest)
if err != nil {
return err
}

Check warning on line 61 in pkg/services/telegram.go

View check run for this annotation

Codecov / codecov/patch

pkg/services/telegram.go#L58-L61

Added lines #L58 - L61 were not covered by tests

_, err = bot.Send(msg)
if err != nil {
return err

Check warning on line 65 in pkg/services/telegram.go

View check run for this annotation

Codecov / codecov/patch

pkg/services/telegram.go#L63-L65

Added lines #L63 - L65 were not covered by tests
}

return nil
Expand Down
54 changes: 54 additions & 0 deletions pkg/services/telegram_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package services

import (
"reflect"
"testing"

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)

func TestBuildTelegramMessageOptions(t *testing.T) {
tests := []struct {
name string
notification Notification
dest Destination
want *tgbotapi.MessageConfig
wantErr bool
}{
{
name: "Message to chat",
notification: Notification{Message: "Test message"},
dest: Destination{Recipient: "-123456"},
want: &tgbotapi.MessageConfig{Text: "Test message", BaseChat: tgbotapi.BaseChat{ChatConfig: tgbotapi.ChatConfig{ChatID: -123456}, MessageThreadID: 0}, ParseMode: "Markdown"},
wantErr: false,
},
{
name: "Message to chat with thread",
notification: Notification{Message: "Test message"},
dest: Destination{Recipient: "-123456|1"},
want: &tgbotapi.MessageConfig{Text: "Test message", BaseChat: tgbotapi.BaseChat{ChatConfig: tgbotapi.ChatConfig{ChatID: -123456}, MessageThreadID: 1}, ParseMode: "Markdown"},
wantErr: false,
},
{
name: "Message to channel",
notification: Notification{Message: "Test message"},
dest: Destination{Recipient: "123456"},
want: &tgbotapi.MessageConfig{Text: "Test message", BaseChat: tgbotapi.BaseChat{ChatConfig: tgbotapi.ChatConfig{ChannelUsername: "@123456"}}, ParseMode: "Markdown"},
wantErr: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := buildTelegramMessageOptions(tt.notification, tt.dest)
if (err != nil) != tt.wantErr {
t.Errorf("buildTelegramMessageOptions() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("buildTelegramMessageOptions() = %v, want %v", got, tt.want)
}
println(got.Text)
})
}
}

0 comments on commit c791422

Please sign in to comment.