modelRT/diagram/context_test.go

39 lines
1.1 KiB
Go
Raw Normal View History

package diagram
import (
"context"
"testing"
"modelRT/common"
"modelRT/constants"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestClientTokenFromContext(t *testing.T) {
ctx := context.WithValue(context.Background(), constants.CtxKeyClientToken, "test-token")
token, err := clientTokenFromContext(ctx)
require.NoError(t, err)
assert.Equal(t, "test-token", token)
}
func TestClientTokenFromContextReturnsErrorWhenMissing(t *testing.T) {
for _, ctx := range []context.Context{nil, context.Background()} {
_, err := clientTokenFromContext(ctx)
require.Error(t, err)
assert.ErrorIs(t, err, common.ErrGetClientToken)
}
}
func TestRedisConstructorsReturnErrorInsteadOfPanickingWithoutToken(t *testing.T) {
ctx := context.Background()
_, err := NewRedisZSet(ctx, "zset", 0, false)
assert.ErrorIs(t, err, common.ErrGetClientToken)
_, err = NewRedisSet(ctx, "set", 0, false)
assert.ErrorIs(t, err, common.ErrGetClientToken)
_, err = NewRedisHash(ctx, "hash", 0, false)
assert.ErrorIs(t, err, common.ErrGetClientToken)
}