Skip to content

Commit

Permalink
Add sample test strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulSonOfLars committed Dec 23, 2024
1 parent 3383b22 commit cebb65a
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
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
},
}

}

0 comments on commit cebb65a

Please sign in to comment.