package redis

import (
	"testing"

	"git.rosy.net.cn/jx-callback/business/jxutils/cache"
	"git.rosy.net.cn/jx-callback/globals/testinit1"
)

var (
	cacher cache.ICacher
)

func init() {
	testinit1.Init()
	cacher = New("localhost", 6379, "123456") //ysq本地redis
}

type TestType struct {
	Key string `json:"key"`
}

func TestIt(t *testing.T) {
	cacher.FlushKeys("")
	if cacher.Get("key") != nil {
		t.Fatal("should get nothing")
	}
	if err := cacher.Set("key", map[string]interface{}{
		"key": "value",
	}, 0); err != nil {
		t.Fatal(err)
	}

	if cacher.Get("key") == nil {
		t.Fatal("should not get nothing")
	}

	result := new(TestType)
	cacher.GetAs("key", result)
	if result.Key != "value" {
		t.Fatal("should get value")
	}
}

func TestKeys(t *testing.T) {
	cacher.FlushKeys("")
	keys, err := cacher.Keys("")
	if err != nil {
		t.Fatal(err)
	}
	if len(keys) != 0 {
		t.Fatal("keys should be empty here")
	}
	cacher.Set("key1", 1, 0)
	cacher.Set("key2", 2, 0)
	cacher.Set("key22", 3, 0)
	cacher.Set("key23", 4, 0)
	cacher.Set("key24", 5, 0)
	keys, err = cacher.Keys("")
	if len(keys) != 5 {
		t.Fatal("keys should be 5 here")
	}
	keys, err = cacher.Keys("key")
	if len(keys) != 5 {
		t.Fatal("keys should be 5 here")
	}
	keys, err = cacher.Keys("key2")
	if len(keys) != 4 {
		t.Fatal("keys should be 4 here")
	}
	keys, err = cacher.Keys("key22")
	if len(keys) != 1 {
		t.Fatal("keys should be 1 here")
	}
}
