mirror of
https://github.com/gofiber/storage.git
synced 2025-10-07 17:41:57 +08:00
chore(mysql): apply TCK suite to MySQL
This commit is contained in:
@@ -2,8 +2,14 @@ module github.com/gofiber/storage/mysql/v2
|
|||||||
|
|
||||||
go 1.23.0
|
go 1.23.0
|
||||||
|
|
||||||
|
replace (
|
||||||
|
github.com/gofiber/storage => ../
|
||||||
|
github.com/gofiber/storage/testhelpers/tck => ../testhelpers/tck
|
||||||
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/go-sql-driver/mysql v1.9.3
|
github.com/go-sql-driver/mysql v1.9.3
|
||||||
|
github.com/gofiber/storage/testhelpers/tck v1.0.0
|
||||||
github.com/stretchr/testify v1.10.0
|
github.com/stretchr/testify v1.10.0
|
||||||
github.com/testcontainers/testcontainers-go v0.38.0
|
github.com/testcontainers/testcontainers-go v0.38.0
|
||||||
github.com/testcontainers/testcontainers-go/modules/mysql v0.38.0
|
github.com/testcontainers/testcontainers-go/modules/mysql v0.38.0
|
||||||
@@ -30,6 +36,7 @@ require (
|
|||||||
github.com/go-logr/logr v1.4.2 // indirect
|
github.com/go-logr/logr v1.4.2 // indirect
|
||||||
github.com/go-logr/stdr v1.2.2 // indirect
|
github.com/go-logr/stdr v1.2.2 // indirect
|
||||||
github.com/go-ole/go-ole v1.2.6 // indirect
|
github.com/go-ole/go-ole v1.2.6 // indirect
|
||||||
|
github.com/gofiber/storage v1.3.3 // indirect
|
||||||
github.com/gogo/protobuf v1.3.2 // indirect
|
github.com/gogo/protobuf v1.3.2 // indirect
|
||||||
github.com/google/uuid v1.6.0 // indirect
|
github.com/google/uuid v1.6.0 // indirect
|
||||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 // indirect
|
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 // indirect
|
||||||
|
@@ -7,7 +7,9 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/gofiber/storage/testhelpers/tck"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
"github.com/stretchr/testify/suite"
|
||||||
"github.com/testcontainers/testcontainers-go"
|
"github.com/testcontainers/testcontainers-go"
|
||||||
"github.com/testcontainers/testcontainers-go/modules/mysql"
|
"github.com/testcontainers/testcontainers-go/modules/mysql"
|
||||||
"github.com/testcontainers/testcontainers-go/wait"
|
"github.com/testcontainers/testcontainers-go/wait"
|
||||||
@@ -22,20 +24,34 @@ const (
|
|||||||
mysqlDatabase string = "fiber"
|
mysqlDatabase string = "fiber"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type MySQLStorageTCK struct{}
|
||||||
|
|
||||||
|
func (s *MySQLStorageTCK) NewStoreWithContainer() func(ctx context.Context, tb testing.TB) (*Storage, testcontainers.Container, error) {
|
||||||
|
return func(ctx context.Context, tb testing.TB) (*Storage, testcontainers.Container, error) {
|
||||||
|
c := mustStartMySQL(tb)
|
||||||
|
|
||||||
|
conn, err := c.ConnectionString(ctx)
|
||||||
|
require.NoError(tb, err)
|
||||||
|
|
||||||
|
store := New(Config{
|
||||||
|
ConnectionURI: conn,
|
||||||
|
Reset: true,
|
||||||
|
})
|
||||||
|
|
||||||
|
return store, c, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func newTestStore(t testing.TB) *Storage {
|
func newTestStore(t testing.TB) *Storage {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
c := mustStartMySQL(t)
|
suite := MySQLStorageTCK{}
|
||||||
|
store, _, err := suite.NewStoreWithContainer()(ctx, t)
|
||||||
conn, err := c.ConnectionString(ctx)
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
return New(Config{
|
return store
|
||||||
ConnectionURI: conn,
|
|
||||||
Reset: true,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func mustStartMySQL(t testing.TB) *mysql.MySQLContainer {
|
func mustStartMySQL(t testing.TB) *mysql.MySQLContainer {
|
||||||
@@ -77,222 +93,6 @@ func Test_MYSQL_New(t *testing.T) {
|
|||||||
defer newConfigStore.Close()
|
defer newConfigStore.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_MYSQL_Set(t *testing.T) {
|
|
||||||
var (
|
|
||||||
key = "john"
|
|
||||||
val = []byte("doe")
|
|
||||||
)
|
|
||||||
|
|
||||||
testStore := newTestStore(t)
|
|
||||||
defer testStore.Close()
|
|
||||||
|
|
||||||
err := testStore.Set(key, val, 0)
|
|
||||||
require.NoError(t, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_MYSQL_SetWithContext(t *testing.T) {
|
|
||||||
var (
|
|
||||||
key = "john"
|
|
||||||
val = []byte("doe")
|
|
||||||
)
|
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
|
||||||
cancel()
|
|
||||||
|
|
||||||
testStore := newTestStore(t)
|
|
||||||
defer testStore.Close()
|
|
||||||
|
|
||||||
err := testStore.SetWithContext(ctx, key, val, 0)
|
|
||||||
require.ErrorIs(t, err, context.Canceled)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_MYSQL_Set_Override(t *testing.T) {
|
|
||||||
var (
|
|
||||||
key = "john"
|
|
||||||
val = []byte("doe")
|
|
||||||
)
|
|
||||||
|
|
||||||
testStore := newTestStore(t)
|
|
||||||
defer testStore.Close()
|
|
||||||
|
|
||||||
err := testStore.Set(key, val, 0)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
err = testStore.Set(key, val, 0)
|
|
||||||
require.NoError(t, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_MYSQL_Get(t *testing.T) {
|
|
||||||
var (
|
|
||||||
key = "john"
|
|
||||||
val = []byte("doe")
|
|
||||||
)
|
|
||||||
|
|
||||||
testStore := newTestStore(t)
|
|
||||||
defer testStore.Close()
|
|
||||||
|
|
||||||
err := testStore.Set(key, val, 0)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
result, err := testStore.Get(key)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, val, result)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_MYSQL_GetWithContext(t *testing.T) {
|
|
||||||
var (
|
|
||||||
key = "john"
|
|
||||||
val = []byte("doe")
|
|
||||||
)
|
|
||||||
|
|
||||||
testStore := newTestStore(t)
|
|
||||||
defer testStore.Close()
|
|
||||||
|
|
||||||
err := testStore.Set(key, val, 0)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
|
||||||
cancel()
|
|
||||||
|
|
||||||
result, err := testStore.GetWithContext(ctx, key)
|
|
||||||
require.ErrorIs(t, err, context.Canceled)
|
|
||||||
require.Zero(t, len(result))
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_MYSQL_Set_Expiration(t *testing.T) {
|
|
||||||
var (
|
|
||||||
key = "john"
|
|
||||||
val = []byte("doe")
|
|
||||||
exp = 1 * time.Second
|
|
||||||
)
|
|
||||||
|
|
||||||
testStore := newTestStore(t)
|
|
||||||
defer testStore.Close()
|
|
||||||
|
|
||||||
err := testStore.Set(key, val, exp)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
time.Sleep(1100 * time.Millisecond)
|
|
||||||
|
|
||||||
result, err := testStore.Get(key)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Zero(t, len(result), "Key should have expired")
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_MYSQL_Get_Expired(t *testing.T) {
|
|
||||||
key := "john"
|
|
||||||
|
|
||||||
testStore := newTestStore(t)
|
|
||||||
defer testStore.Close()
|
|
||||||
|
|
||||||
result, err := testStore.Get(key)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Zero(t, len(result))
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_MYSQL_Get_NotExist(t *testing.T) {
|
|
||||||
testStore := newTestStore(t)
|
|
||||||
defer testStore.Close()
|
|
||||||
|
|
||||||
result, err := testStore.Get("notexist")
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Zero(t, len(result))
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_MYSQL_Delete(t *testing.T) {
|
|
||||||
var (
|
|
||||||
key = "john"
|
|
||||||
val = []byte("doe")
|
|
||||||
)
|
|
||||||
|
|
||||||
testStore := newTestStore(t)
|
|
||||||
defer testStore.Close()
|
|
||||||
|
|
||||||
err := testStore.Set(key, val, 0)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
err = testStore.Delete(key)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
result, err := testStore.Get(key)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Zero(t, len(result))
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_MYSQL_DeleteWithContext(t *testing.T) {
|
|
||||||
var (
|
|
||||||
key = "john"
|
|
||||||
val = []byte("doe")
|
|
||||||
)
|
|
||||||
|
|
||||||
testStore := newTestStore(t)
|
|
||||||
defer testStore.Close()
|
|
||||||
|
|
||||||
err := testStore.Set(key, val, 0)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
|
||||||
cancel()
|
|
||||||
|
|
||||||
err = testStore.DeleteWithContext(ctx, key)
|
|
||||||
require.ErrorIs(t, err, context.Canceled)
|
|
||||||
|
|
||||||
result, err := testStore.Get(key)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, val, result)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_MYSQL_Reset(t *testing.T) {
|
|
||||||
val := []byte("doe")
|
|
||||||
|
|
||||||
testStore := newTestStore(t)
|
|
||||||
defer testStore.Close()
|
|
||||||
|
|
||||||
err := testStore.Set("john1", val, 0)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
err = testStore.Set("john2", val, 0)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
err = testStore.Reset()
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
result, err := testStore.Get("john1")
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Zero(t, len(result))
|
|
||||||
|
|
||||||
result, err = testStore.Get("john2")
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Zero(t, len(result))
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_MYSQL_ResetWithContext(t *testing.T) {
|
|
||||||
val := []byte("doe")
|
|
||||||
|
|
||||||
testStore := newTestStore(t)
|
|
||||||
defer testStore.Close()
|
|
||||||
|
|
||||||
err := testStore.Set("john1", val, 0)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
err = testStore.Set("john2", val, 0)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
|
||||||
cancel()
|
|
||||||
|
|
||||||
err = testStore.ResetWithContext(ctx)
|
|
||||||
require.ErrorIs(t, err, context.Canceled)
|
|
||||||
|
|
||||||
result, err := testStore.Get("john1")
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, val, result)
|
|
||||||
|
|
||||||
result, err = testStore.Get("john2")
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, val, result)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_MYSQL_GC(t *testing.T) {
|
func Test_MYSQL_GC(t *testing.T) {
|
||||||
testVal := []byte("doe")
|
testVal := []byte("doe")
|
||||||
|
|
||||||
@@ -332,11 +132,6 @@ func Test_MYSQL_Non_UTF8(t *testing.T) {
|
|||||||
require.Equal(t, val, result)
|
require.Equal(t, val, result)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_MYSQL_Close(t *testing.T) {
|
|
||||||
testStore := newTestStore(t)
|
|
||||||
require.NoError(t, testStore.Close())
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_MYSQL_Conn(t *testing.T) {
|
func Test_MYSQL_Conn(t *testing.T) {
|
||||||
testStore := newTestStore(t)
|
testStore := newTestStore(t)
|
||||||
defer testStore.Close()
|
defer testStore.Close()
|
||||||
@@ -344,6 +139,13 @@ func Test_MYSQL_Conn(t *testing.T) {
|
|||||||
require.True(t, testStore.Conn() != nil)
|
require.True(t, testStore.Conn() != nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMySQLStorageTCK(t *testing.T) {
|
||||||
|
s, err := tck.New(context.Background(), t, &MySQLStorageTCK{}, tck.PerTest)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
suite.Run(t, &s)
|
||||||
|
}
|
||||||
|
|
||||||
func Benchmark_MYSQL_Set(b *testing.B) {
|
func Benchmark_MYSQL_Set(b *testing.B) {
|
||||||
testStore := newTestStore(b)
|
testStore := newTestStore(b)
|
||||||
defer testStore.Close()
|
defer testStore.Close()
|
||||||
|
Reference in New Issue
Block a user