|
| 1 | +package providers |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/darkweak/souin/cache/types" |
| 6 | + "github.com/darkweak/souin/tests" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/darkweak/souin/configurationtypes" |
| 11 | + "github.com/darkweak/souin/errors" |
| 12 | +) |
| 13 | + |
| 14 | +const REDISVALUE = "My first data" |
| 15 | + |
| 16 | +func getRedisClientAndMatchedURL(key string) (types.AbstractProviderInterface, configurationtypes.URL) { |
| 17 | + return tests.GetCacheProviderClientAndMatchedURL(key, func(configurationInterface configurationtypes.AbstractConfigurationInterface) (types.AbstractProviderInterface, error) { |
| 18 | + provider, _ := RedisConnectionFactory(configurationInterface) |
| 19 | + |
| 20 | + return provider, nil |
| 21 | + }) |
| 22 | +} |
| 23 | + |
| 24 | +func TestIShouldBeAbleToReadAndWriteDataInRedis(t *testing.T) { |
| 25 | + client, u := getRedisClientAndMatchedURL("Test") |
| 26 | + client.Set("Test", []byte(REDISVALUE), u, time.Duration(10)*time.Second) |
| 27 | + res := client.Get("Test") |
| 28 | + if REDISVALUE != string(res) { |
| 29 | + errors.GenerateError(t, fmt.Sprintf("%s not corresponding to %s", res, REDISVALUE)) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func TestRedis_GetRequestInCache(t *testing.T) { |
| 34 | + client, _ := getRedisClientAndMatchedURL(NONEXISTENTKEY) |
| 35 | + res := client.Get(NONEXISTENTKEY) |
| 36 | + if string(res) != "" { |
| 37 | + errors.GenerateError(t, fmt.Sprintf("Key %s should not exist", NONEXISTENTKEY)) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func TestRedis_SetRequestInCache_OneByte(t *testing.T) { |
| 42 | + client, u := getRedisClientAndMatchedURL(BYTEKEY) |
| 43 | + client.Set(BYTEKEY, []byte{65}, u, time.Duration(20) * time.Second) |
| 44 | +} |
| 45 | + |
| 46 | +func TestRedis_SetRequestInCache_TTL(t *testing.T) { |
| 47 | + key := "MyEmptyKey" |
| 48 | + client, matchedURL := getRedisClientAndMatchedURL(key) |
| 49 | + nv := []byte("Hello world") |
| 50 | + setValueThenVerify(client, key, nv, matchedURL, time.Duration(20) * time.Second, t) |
| 51 | +} |
| 52 | + |
| 53 | +func TestRedis_SetRequestInCache_NoTTL(t *testing.T) { |
| 54 | + client, matchedURL := getRedisClientAndMatchedURL(BYTEKEY) |
| 55 | + nv := []byte("New value") |
| 56 | + setValueThenVerify(client, BYTEKEY, nv, matchedURL, 0, t) |
| 57 | +} |
| 58 | + |
| 59 | +func TestRedis_DeleteRequestInCache(t *testing.T) { |
| 60 | + client, _ := RedisConnectionFactory(tests.MockConfiguration()) |
| 61 | + client.Delete(BYTEKEY) |
| 62 | + time.Sleep(1 * time.Second) |
| 63 | + if 0 < len(client.Get(BYTEKEY)) { |
| 64 | + errors.GenerateError(t, fmt.Sprintf("Key %s should not exist", BYTEKEY)) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func TestRedis_Init(t *testing.T) { |
| 69 | + client, _ := RedisConnectionFactory(tests.MockConfiguration()) |
| 70 | + err := client.Init() |
| 71 | + |
| 72 | + if nil != err { |
| 73 | + errors.GenerateError(t, "Impossible to init Redis provider") |
| 74 | + } |
| 75 | +} |
0 commit comments