app-cloudep-tweeting-service/internal/domain/redis_test.go

46 lines
1.3 KiB
Go
Raw Normal View History

2024-09-01 13:49:28 +00:00
package domain
import (
"testing"
"github.com/stretchr/testify/assert"
)
// TestRedisKeyToString 測試 ToString 方法
func TestRedisKeyToString(t *testing.T) {
key := RedisKey("user:timeline")
expected := "user:timeline"
result := key.ToString()
assert.Equal(t, expected, result, "ToString should return the correct string representation of RedisKey")
}
// TestRedisKeyWith 測試 With 方法
func TestRedisKeyWith(t *testing.T) {
key := RedisKey("user:timeline")
subKey := "12345"
expected := "user:timeline:12345"
result := key.With(subKey)
assert.Equal(t, RedisKey(expected), result, "With should correctly concatenate the RedisKey with the provided subKey")
}
// TestRedisKeyWithMultiple 測試 With 方法與多個參數
func TestRedisKeyWithMultiple(t *testing.T) {
key := RedisKey("user:timeline")
subKeys := []string{"12345", "posts"}
expected := "user:timeline:12345:posts"
result := key.With(subKeys...)
assert.Equal(t, RedisKey(expected), result, "With should correctly concatenate the RedisKey with multiple provided subKeys")
}
// TestRedisKeyWithEmpty 測試 With 方法與空參數
func TestRedisKeyWithEmpty(t *testing.T) {
key := RedisKey("user:timeline")
expected := "user:timeline"
result := key.With()
assert.Equal(t, RedisKey(expected), result, "With should return the original key when no subKeys are provided")
}