-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtgbot_test.go
73 lines (56 loc) · 2.09 KB
/
tgbot_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package tgo
import (
"encoding/json"
"testing"
"github.com/nekomeowww/tgo/pkg/storage/queue"
"github.com/nekomeowww/tgo/pkg/storage/ttlcache"
"github.com/nekomeowww/xo/logger"
"github.com/redis/rueidis"
"github.com/samber/lo"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zapcore"
)
func TestAssignOneCallbackQueryData(t *testing.T) {
t.Run("InMemory", func(t *testing.T) {
data := struct {
Hello string `json:"hello"`
}{
Hello: "world",
}
logger, err := logger.NewLogger(logger.WithLevel(zapcore.DebugLevel), logger.WithAppName("tgo"), logger.WithNamespace("nekomeowww"))
require.NoError(t, err)
bot := BotAPI{logger: logger, queue: queue.NewInMemoryQueue(), ttlcache: ttlcache.NewInMemoryTTLCache()}
callbackQueryData, err := bot.AssignOneCallbackQueryData("test", data)
require.NoError(t, err)
routeHash, dataHash := bot.routeHashAndActionHashFromData(callbackQueryData)
require.NotEmpty(t, routeHash)
require.NotEmpty(t, dataHash)
dataStr, err := bot.fetchCallbackQueryActionData("test", dataHash)
require.NoError(t, err)
assert.Equal(t, string(lo.Must(json.Marshal(data))), dataStr)
})
t.Run("Rueidis", func(t *testing.T) {
c, err := rueidis.NewClient(rueidis.ClientOption{
InitAddress: []string{"localhost:6379"},
DisableCache: true,
})
require.NoError(t, err)
data := struct {
Hello string `json:"hello"`
}{
Hello: "world",
}
logger, err := logger.NewLogger(logger.WithLevel(zapcore.DebugLevel), logger.WithAppName("tgo"), logger.WithNamespace("nekomeowww"))
require.NoError(t, err)
bot := BotAPI{logger: logger, queue: queue.NewRueidisQueue(c), ttlcache: ttlcache.NewRueidisTTLCache(c)}
callbackQueryData, err := bot.AssignOneCallbackQueryData("test", data)
require.NoError(t, err)
routeHash, dataHash := bot.routeHashAndActionHashFromData(callbackQueryData)
require.NotEmpty(t, routeHash)
require.NotEmpty(t, dataHash)
dataStr, err := bot.fetchCallbackQueryActionData("test", dataHash)
require.NoError(t, err)
assert.Equal(t, string(lo.Must(json.Marshal(data))), dataStr)
})
}