Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
efectn committed Sep 15, 2023
1 parent 09e12a7 commit 8326651
Show file tree
Hide file tree
Showing 13 changed files with 74 additions and 152 deletions.
16 changes: 3 additions & 13 deletions arangodb/arangodb_test.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
package arangodb

import (
"os"
"testing"
"time"

"github.com/stretchr/testify/require"
)

var testStore *Storage

func TestMain(m *testing.M) {
testStore = New(Config{
Reset: true,
})

code := m.Run()

_ = testStore.Close()
os.Exit(code)
}
var testStore = New(Config{
Reset: true,
})

func Test_ArangoDB_Set(t *testing.T) {
var (
Expand Down
16 changes: 3 additions & 13 deletions badger/badger_test.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
package badger

import (
"os"
"testing"
"time"

"github.com/stretchr/testify/require"
)

var testStore *Storage

func TestMain(m *testing.M) {
testStore = New(Config{
Reset: true,
})

code := m.Run()

_ = testStore.Close()
os.Exit(code)
}
var testStore = New(Config{
Reset: true,
})

func Test_Badger_Set(t *testing.T) {
var (
Expand Down
3 changes: 2 additions & 1 deletion bbolt/bbolt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ var testStore *Storage

func TestMain(m *testing.M) {
testStore = New(Config{
Reset: true,
Bucket: "fiber-bucket",
Reset: true,
})

code := m.Run()
Expand Down
8 changes: 7 additions & 1 deletion bbolt/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bbolt

import (
"errors"
"github.com/gofiber/utils/v2"
"go.etcd.io/bbolt"
)
Expand All @@ -15,6 +16,11 @@ func createBucket(cfg Config, conn *bbolt.DB) error {

func removeBucket(cfg Config, conn *bbolt.DB) error {
return conn.Update(func(tx *bbolt.Tx) error {
return tx.DeleteBucket(utils.UnsafeBytes(cfg.Bucket))
err := tx.DeleteBucket(utils.UnsafeBytes(cfg.Bucket))
if errors.Is(err, bbolt.ErrBucketNotFound) {
return nil
}

return err
})
}
40 changes: 27 additions & 13 deletions couchbase/couchbase_test.go
Original file line number Diff line number Diff line change
@@ -1,44 +1,42 @@
package couchbase

import (
"os"
"testing"
"time"

"github.com/stretchr/testify/require"
)

var testStore *Storage

func TestMain(m *testing.M) {
testStore = New(Config{
func newTestStore(t testing.TB) *Storage {
t.Helper()
return New(Config{
Username: "admin",
Password: "123456",
Host: "127.0.0.1:8091",
Bucket: "fiber_storage",
})

code := m.Run()

_ = testStore.Reset()
_ = testStore.Close()
os.Exit(code)
}

func TestSetCouchbase_ShouldReturnNoError(t *testing.T) {
testStore := newTestStore(t)

err := testStore.Set("test", []byte("test"), 0)

require.NoError(t, err)
}

func TestGetCouchbase_ShouldReturnNil_WhenDocumentNotFound(t *testing.T) {
testStore := newTestStore(t)

val, err := testStore.Get("not_found_key")

require.NoError(t, err)
require.Zero(t, len(val))
}

func TestSetAndGet_GetShouldReturn_SettedValueWithoutError(t *testing.T) {
func TestSetAndGet_GetShouldReturn_SetValueWithoutError(t *testing.T) {
testStore := newTestStore(t)

err := testStore.Set("test", []byte("fiber_test_value"), 0)
require.NoError(t, err)

Expand All @@ -49,6 +47,8 @@ func TestSetAndGet_GetShouldReturn_SettedValueWithoutError(t *testing.T) {
}

func TestSetAndGet_GetShouldReturnNil_WhenTTLExpired(t *testing.T) {
testStore := newTestStore(t)

err := testStore.Set("test", []byte("fiber_test_value"), 3*time.Second)
require.NoError(t, err)

Expand All @@ -61,6 +61,8 @@ func TestSetAndGet_GetShouldReturnNil_WhenTTLExpired(t *testing.T) {
}

func TestSetAndDelete_DeleteShouldReturn_NoError(t *testing.T) {
testStore := newTestStore(t)

err := testStore.Set("test", []byte("fiber_test_value"), 0)
require.NoError(t, err)

Expand All @@ -72,6 +74,8 @@ func TestSetAndDelete_DeleteShouldReturn_NoError(t *testing.T) {
}

func TestSetAndReset_ResetShouldReturn_NoError(t *testing.T) {
testStore := newTestStore(t)

err := testStore.Set("test", []byte("fiber_test_value"), 0)
require.NoError(t, err)

Expand All @@ -83,15 +87,21 @@ func TestSetAndReset_ResetShouldReturn_NoError(t *testing.T) {
}

func TestClose_CloseShouldReturn_NoError(t *testing.T) {
testStore := newTestStore(t)

err := testStore.Close()
require.NoError(t, err)
}

func TestGetConn_ReturnsNotNill(t *testing.T) {
func TestGetConn_ReturnsNotNil(t *testing.T) {
testStore := newTestStore(t)

require.True(t, testStore.Conn() != nil)
}

func Benchmark_Couchbase_Set(b *testing.B) {
testStore := newTestStore(b)

b.ReportAllocs()
b.ResetTimer()

Expand All @@ -104,6 +114,8 @@ func Benchmark_Couchbase_Set(b *testing.B) {
}

func Benchmark_Couchbase_Get(b *testing.B) {
testStore := newTestStore(b)

err := testStore.Set("john", []byte("doe"), 0)
require.NoError(b, err)

Expand All @@ -118,6 +130,8 @@ func Benchmark_Couchbase_Get(b *testing.B) {
}

func Benchmark_Couchbase_Delete(b *testing.B) {
testStore := newTestStore(b)

err := testStore.Set("john", []byte("doe"), 0)
require.NoError(b, err)

Expand Down
13 changes: 1 addition & 12 deletions memory/memory_test.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,14 @@
package memory

import (
"os"
"testing"
"time"

"github.com/gofiber/utils/v2"
"github.com/stretchr/testify/require"
)

var testStore *Storage

func TestMain(m *testing.M) {
testStore = New()

code := m.Run()

_ = testStore.Reset()
_ = testStore.Close()
os.Exit(code)
}
var testStore = New()

func Test_Storage_Memory_Set(t *testing.T) {
var (
Expand Down
21 changes: 6 additions & 15 deletions mssql/mssql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,12 @@ import (
"github.com/stretchr/testify/require"
)

var testStore *Storage

func TestMain(m *testing.M) {
testStore = New(Config{
Database: os.Getenv("MSSQL_DATABASE"),
Username: os.Getenv("MSSQL_USERNAME"),
Password: os.Getenv("MSSQL_PASSWORD"),
Reset: true,
})

code := m.Run()

_ = testStore.Close()
os.Exit(code)
}
var testStore = New(Config{
Database: os.Getenv("MSSQL_DATABASE"),
Username: os.Getenv("MSSQL_USERNAME"),
Password: os.Getenv("MSSQL_PASSWORD"),
Reset: true,
})

func Test_MSSQL_Set(t *testing.T) {
var (
Expand Down
21 changes: 6 additions & 15 deletions mysql/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,12 @@ import (
"github.com/stretchr/testify/require"
)

var testStore *Storage

func TestMain(m *testing.M) {
testStore = New(Config{
Database: os.Getenv("MYSQL_DATABASE"),
Username: os.Getenv("MYSQL_USERNAME"),
Password: os.Getenv("MYSQL_PASSWORD"),
Reset: true,
})

code := m.Run()

_ = testStore.Close()
os.Exit(code)
}
var testStore = New(Config{
Database: os.Getenv("MYSQL_DATABASE"),
Username: os.Getenv("MYSQL_USERNAME"),
Password: os.Getenv("MYSQL_PASSWORD"),
Reset: true,
})

func Test_MYSQL_New(t *testing.T) {
newConfigStore := New(Config{
Expand Down
19 changes: 4 additions & 15 deletions pebble/pebble_test.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,16 @@
package pebble

import (
"os"
"testing"
"time"

"github.com/stretchr/testify/require"
)

var testStore *Storage

func TestMain(m *testing.M) {
testStore = New(Config{
Path: "test.db",
WriteOptions: nil,
})

code := m.Run()

_ = testStore.Reset()
_ = testStore.Close()
os.Exit(code)
}
var testStore = New(Config{
Path: "test.db",
WriteOptions: nil,
})

func Test_Pebble_Set(t *testing.T) {
var (
Expand Down
21 changes: 6 additions & 15 deletions postgres/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,12 @@ import (
"github.com/stretchr/testify/require"
)

var testStore *Storage

func TestMain(m *testing.M) {
testStore = New(Config{
Database: os.Getenv("POSTGRES_DATABASE"),
Username: os.Getenv("POSTGRES_USERNAME"),
Password: os.Getenv("POSTGRES_PASSWORD"),
Reset: true,
})

code := m.Run()

_ = testStore.Close()
os.Exit(code)
}
var testStore = New(Config{
Database: os.Getenv("POSTGRES_DATABASE"),
Username: os.Getenv("POSTGRES_USERNAME"),
Password: os.Getenv("POSTGRES_PASSWORD"),
Reset: true,
})

func Test_Postgres_Set(t *testing.T) {
var (
Expand Down
16 changes: 3 additions & 13 deletions redis/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,15 @@ package redis
import (
"crypto/tls"
"log"
"os"
"testing"
"time"

"github.com/stretchr/testify/require"
)

var testStore *Storage

func TestMain(m *testing.M) {
testStore = New(Config{
Reset: true,
})

code := m.Run()

_ = testStore.Close()
os.Exit(code)
}
var testStore = New(Config{
Reset: true,
})

func Test_Redis_Set(t *testing.T) {
var (
Expand Down
Loading

0 comments on commit 8326651

Please sign in to comment.