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

Add example testing strategy to echobot sample #212

Merged
merged 1 commit into from
Jan 1, 2025
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
1 change: 1 addition & 0 deletions samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ In this case, the bot has commands to start a conversation, which then causes it
## samples/echoBot

This bot is as basic as it gets - it simply repeats everything you say.
The main_test.go file contains example code to demonstrate how to implement the gotgbot.BotClient interface for it to be used in tests.

## samples/echoMultiBot

Expand Down
1 change: 1 addition & 0 deletions samples/echoBot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
)

// This bot is as basic as it gets - it simply repeats everything you say.
// The main_test.go file contains example code to demonstrate how to implement the gotgbot.BotClient interface for it to be used in tests.
func main() {
// Get token from the environment variable
token := os.Getenv("TOKEN")
Expand Down
97 changes: 97 additions & 0 deletions samples/echoBot/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package main

import (
"context"
"encoding/json"
"fmt"
"math/rand"
"testing"

"github.com/PaulSonOfLars/gotgbot/v2"
"github.com/PaulSonOfLars/gotgbot/v2/ext"
)

type RequestFunc func(ctx context.Context, token string, method string, params map[string]string, data map[string]gotgbot.FileReader, opts *gotgbot.RequestOpts) (json.RawMessage, error)

// We define a TestBotClient which allows us to overwrite a single method on a per-test basis; meaning each test can verify specific behaviour.
type TestBotClient struct {
RequestFunc RequestFunc
}

func (b TestBotClient) GetAPIURL(opts *gotgbot.RequestOpts) string {
// implement me if needed
panic("not implemented")
}

func (b TestBotClient) FileURL(token string, tgFilePath string, opts *gotgbot.RequestOpts) string {
// implement me if needed
panic("not implemented")
}

// Define wrapper around existing RequestWithContext method.
func (b TestBotClient) RequestWithContext(ctx context.Context, token string, method string, params map[string]string, data map[string]gotgbot.FileReader, opts *gotgbot.RequestOpts) (json.RawMessage, error) {
if b.RequestFunc == nil {
panic("no requestfunc provided for test")
}
return b.RequestFunc(ctx, token, method, params, data, opts)
}

func NewBotClient(f RequestFunc) gotgbot.BotClient {
return TestBotClient{
RequestFunc: f,
}
}

func Test_echo(t *testing.T) {
const echoMessage = "Hello"
sendMessageCount := 0

b := testBot(func(ctx context.Context, token string, method string, params map[string]string, data map[string]gotgbot.FileReader, opts *gotgbot.RequestOpts) (json.RawMessage, error) {
if method != "sendMessage" {
t.Fatalf("Only expected API calls to sendMessage, got %s", method)
}

sentText := params["text"]
if sentText != echoMessage {
t.Errorf("expected text to be %s, got %s", echoMessage, sentText)
}

sendMessageCount++
return json.Marshal(gotgbot.Message{
MessageId: rand.Int63(),
Text: sentText,
})
})

err := echo(b, ext.NewContext(b, testMessage(echoMessage), nil))
if err != nil {
t.Fatal(err)
}
}

func testBot(f RequestFunc) *gotgbot.Bot {
id := rand.Int63()

return &gotgbot.Bot{
Token: fmt.Sprintf("%d:TEST", id),
User: gotgbot.User{
Id: id,
IsBot: true,
FirstName: "Testing",
Username: "TestBot",
},
BotClient: NewBotClient(f),
}
}

func testMessage(text string) *gotgbot.Update {
return &gotgbot.Update{
UpdateId: rand.Int63(),
Message: &gotgbot.Message{
MessageId: rand.Int63(),
Text: text,
// populate other fields as needed
},
}

}
Loading