mirror of
https://github.com/gofiber/storage.git
synced 2025-10-04 08:16:36 +08:00
update tests
This commit is contained in:
@@ -5,7 +5,6 @@ package coherence
|
|||||||
*/
|
*/
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@@ -117,6 +116,25 @@ func Test_Coherence_Set_And_Get(t *testing.T) {
|
|||||||
require.NotNil(t, testStore.Conn())
|
require.NotNil(t, testStore.Conn())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_Coherence_Set_And_GetWithContext(t *testing.T) {
|
||||||
|
var val []byte
|
||||||
|
|
||||||
|
testStore := newTestStore(t)
|
||||||
|
defer testStore.Close()
|
||||||
|
|
||||||
|
err := testStore.Set(key1, value1, 0)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
cancel()
|
||||||
|
|
||||||
|
val, err = testStore.GetWithContext(ctx, key1)
|
||||||
|
require.Error(t, err)
|
||||||
|
require.Empty(t, val)
|
||||||
|
|
||||||
|
require.NotNil(t, testStore.Conn())
|
||||||
|
}
|
||||||
|
|
||||||
func Test_Coherence_SetContext_And_Get(t *testing.T) {
|
func Test_Coherence_SetContext_And_Get(t *testing.T) {
|
||||||
var val []byte
|
var val []byte
|
||||||
|
|
||||||
@@ -124,14 +142,13 @@ func Test_Coherence_SetContext_And_Get(t *testing.T) {
|
|||||||
defer testStore.Close()
|
defer testStore.Close()
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), time.Microsecond)
|
ctx, cancel := context.WithTimeout(context.Background(), time.Microsecond)
|
||||||
defer cancel()
|
cancel()
|
||||||
|
|
||||||
err := testStore.SetWithContext(ctx, key1, value1, 1*time.Nanosecond)
|
err := testStore.SetWithContext(ctx, key1, value1, 1*time.Nanosecond)
|
||||||
require.ErrorIs(t, err, context.DeadlineExceeded)
|
require.ErrorIs(t, err, context.DeadlineExceeded)
|
||||||
|
|
||||||
val, err = testStore.Get(key1)
|
val, err = testStore.Get(key1)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
fmt.Println(string(val))
|
|
||||||
require.True(t, len(val) == 0)
|
require.True(t, len(val) == 0)
|
||||||
|
|
||||||
require.NotNil(t, testStore.Conn())
|
require.NotNil(t, testStore.Conn())
|
||||||
@@ -245,6 +262,43 @@ func Test_Coherence_Reset(t *testing.T) {
|
|||||||
require.True(t, len(val) == 0)
|
require.True(t, len(val) == 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_Coherence_ResetWithContext(t *testing.T) {
|
||||||
|
var val []byte
|
||||||
|
|
||||||
|
testStore := newTestStore(t)
|
||||||
|
defer testStore.Close()
|
||||||
|
|
||||||
|
err := testStore.Set(key1, value1, 0)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
err = testStore.Set(key2, value2, 0)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
// check the keys exist
|
||||||
|
val, err = testStore.Get(key1)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, value1, val)
|
||||||
|
|
||||||
|
val, err = testStore.Get(key2)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, value2, val)
|
||||||
|
|
||||||
|
// reset the store, this should remove both entries
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
cancel()
|
||||||
|
err = testStore.ResetWithContext(ctx)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
// check the keys have expired
|
||||||
|
val, err = testStore.Get(key1)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.False(t, len(val) == 0)
|
||||||
|
|
||||||
|
val, err = testStore.Get(key2)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.False(t, len(val) == 0)
|
||||||
|
}
|
||||||
|
|
||||||
func Test_Coherence_Set_And_Delete(t *testing.T) {
|
func Test_Coherence_Set_And_Delete(t *testing.T) {
|
||||||
var val []byte
|
var val []byte
|
||||||
|
|
||||||
@@ -263,6 +317,27 @@ func Test_Coherence_Set_And_Delete(t *testing.T) {
|
|||||||
require.True(t, len(val) == 0)
|
require.True(t, len(val) == 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_Coherence_Set_And_DeleteWithContext(t *testing.T) {
|
||||||
|
var val []byte
|
||||||
|
|
||||||
|
testStore := newTestStore(t)
|
||||||
|
defer testStore.Close()
|
||||||
|
|
||||||
|
err := testStore.Set(key1, value1, 0)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
cancel()
|
||||||
|
|
||||||
|
err = testStore.DeleteWithContext(ctx, key1)
|
||||||
|
require.Error(t, err)
|
||||||
|
|
||||||
|
// ensure the key has gone
|
||||||
|
val, err = testStore.Get(key1)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.False(t, len(val) == 0)
|
||||||
|
}
|
||||||
|
|
||||||
// TestCoherenceWithScope ensures we can create multiple session stores with multiple scopes.
|
// TestCoherenceWithScope ensures we can create multiple session stores with multiple scopes.
|
||||||
func Test_Coherence_With_Scope(t *testing.T) {
|
func Test_Coherence_With_Scope(t *testing.T) {
|
||||||
var val []byte
|
var val []byte
|
||||||
|
@@ -96,7 +96,7 @@ func New(config ...Config) *Storage {
|
|||||||
|
|
||||||
// Drop table if set to true
|
// Drop table if set to true
|
||||||
if cfg.Reset {
|
if cfg.Reset {
|
||||||
if _, err = db.Exec(strings.Replace(dropQuery, "%s", cfg.Table, -1)); err != nil {
|
if _, err = db.Exec(strings.ReplaceAll(dropQuery, "%s", cfg.Table)); err != nil {
|
||||||
_ = db.Close()
|
_ = db.Close()
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@@ -104,7 +104,7 @@ func New(config ...Config) *Storage {
|
|||||||
|
|
||||||
// Init database queries
|
// Init database queries
|
||||||
for _, query := range initQuery {
|
for _, query := range initQuery {
|
||||||
if _, err := db.Exec(strings.Replace(query, "%s", cfg.Table, -1)); err != nil {
|
if _, err := db.Exec(strings.ReplaceAll(query, "%s", cfg.Table)); err != nil {
|
||||||
_ = db.Close()
|
_ = db.Close()
|
||||||
|
|
||||||
panic(err)
|
panic(err)
|
||||||
|
@@ -89,7 +89,7 @@ func Test_Neo4jStore_SetWithContext(t *testing.T) {
|
|||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
err := testStore.SetWithContext(ctx, key, val, 10*time.Millisecond)
|
err := testStore.SetWithContext(ctx, key, val, 10*time.Millisecond)
|
||||||
require.ErrorIs(t, err, context.DeadlineExceeded)
|
require.ErrorContains(t, err, context.DeadlineExceeded.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_Neo4jStore_Upsert(t *testing.T) {
|
func Test_Neo4jStore_Upsert(t *testing.T) {
|
||||||
|
Reference in New Issue
Block a user