chore: use testcontainers-go module for couchbase in tests

This commit is contained in:
Manuel de la Peña
2024-08-21 16:51:57 +02:00
parent f35a941b18
commit 2c3433f532
5 changed files with 256 additions and 48 deletions

View File

@@ -1,32 +1,70 @@
package couchbase
import (
"context"
"os"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go/modules/couchbase"
)
func newTestStore(t testing.TB) *Storage {
const (
// couchbaseImage is the default image used for running couchbase in tests.
couchbaseImage = "couchbase:enterprise-7.1.1"
couchbaseImageEnvVar string = "TEST_COUCHBASE_IMAGE"
couchbaseUser string = "default"
couchbasePass string = "password"
couchbaseBucket string = "fiber_storage"
)
func newTestStore(t testing.TB) (*Storage, error) {
t.Helper()
img := couchbaseImage
if imgFromEnv := os.Getenv(couchbaseImageEnvVar); imgFromEnv != "" {
img = imgFromEnv
}
ctx := context.Background()
bucket := couchbase.NewBucket(couchbaseBucket).WithReplicas(1).WithFlushEnabled(true)
c, err := couchbase.Run(ctx,
img,
couchbase.WithAdminCredentials(couchbaseUser, couchbasePass),
couchbase.WithBuckets(bucket),
)
if err != nil {
return nil, err
}
conn, err := c.ConnectionString(ctx)
if err != nil {
return nil, err
}
return New(Config{
Username: "admin",
Password: "123456",
Host: "127.0.0.1:8091",
Bucket: "fiber_storage",
})
Username: couchbaseUser,
Password: couchbasePass,
Host: conn,
Bucket: couchbaseBucket,
}), nil
}
func TestSetCouchbase_ShouldReturnNoError(t *testing.T) {
testStore := newTestStore(t)
testStore, err := newTestStore(t)
require.NoError(t, err)
err := testStore.Set("test", []byte("test"), 0)
err = testStore.Set("test", []byte("test"), 0)
require.NoError(t, err)
}
func TestGetCouchbase_ShouldReturnNil_WhenDocumentNotFound(t *testing.T) {
testStore := newTestStore(t)
testStore, err := newTestStore(t)
require.NoError(t, err)
val, err := testStore.Get("not_found_key")
@@ -35,9 +73,10 @@ func TestGetCouchbase_ShouldReturnNil_WhenDocumentNotFound(t *testing.T) {
}
func TestSetAndGet_GetShouldReturn_SetValueWithoutError(t *testing.T) {
testStore := newTestStore(t)
testStore, err := newTestStore(t)
require.NoError(t, err)
err := testStore.Set("test", []byte("fiber_test_value"), 0)
err = testStore.Set("test", []byte("fiber_test_value"), 0)
require.NoError(t, err)
val, err := testStore.Get("test")
@@ -47,9 +86,10 @@ func TestSetAndGet_GetShouldReturn_SetValueWithoutError(t *testing.T) {
}
func TestSetAndGet_GetShouldReturnNil_WhenTTLExpired(t *testing.T) {
testStore := newTestStore(t)
testStore, err := newTestStore(t)
require.NoError(t, err)
err := testStore.Set("test", []byte("fiber_test_value"), 3*time.Second)
err = testStore.Set("test", []byte("fiber_test_value"), 3*time.Second)
require.NoError(t, err)
time.Sleep(6 * time.Second)
@@ -61,9 +101,10 @@ func TestSetAndGet_GetShouldReturnNil_WhenTTLExpired(t *testing.T) {
}
func TestSetAndDelete_DeleteShouldReturn_NoError(t *testing.T) {
testStore := newTestStore(t)
testStore, err := newTestStore(t)
require.NoError(t, err)
err := testStore.Set("test", []byte("fiber_test_value"), 0)
err = testStore.Set("test", []byte("fiber_test_value"), 0)
require.NoError(t, err)
err = testStore.Delete("test")
@@ -74,9 +115,10 @@ func TestSetAndDelete_DeleteShouldReturn_NoError(t *testing.T) {
}
func TestSetAndReset_ResetShouldReturn_NoError(t *testing.T) {
testStore := newTestStore(t)
testStore, err := newTestStore(t)
require.NoError(t, err)
err := testStore.Set("test", []byte("fiber_test_value"), 0)
err = testStore.Set("test", []byte("fiber_test_value"), 0)
require.NoError(t, err)
err = testStore.Reset()
@@ -87,25 +129,27 @@ func TestSetAndReset_ResetShouldReturn_NoError(t *testing.T) {
}
func TestClose_CloseShouldReturn_NoError(t *testing.T) {
testStore := newTestStore(t)
testStore, err := newTestStore(t)
require.NoError(t, err)
err := testStore.Close()
err = testStore.Close()
require.NoError(t, err)
}
func TestGetConn_ReturnsNotNil(t *testing.T) {
testStore := newTestStore(t)
testStore, err := newTestStore(t)
require.NoError(t, err)
require.True(t, testStore.Conn() != nil)
}
func Benchmark_Couchbase_Set(b *testing.B) {
testStore := newTestStore(b)
testStore, err := newTestStore(b)
require.NoError(b, err)
b.ReportAllocs()
b.ResetTimer()
var err error
for i := 0; i < b.N; i++ {
err = testStore.Set("john", []byte("doe"), 0)
}
@@ -114,9 +158,10 @@ func Benchmark_Couchbase_Set(b *testing.B) {
}
func Benchmark_Couchbase_Get(b *testing.B) {
testStore := newTestStore(b)
testStore, err := newTestStore(b)
require.NoError(b, err)
err := testStore.Set("john", []byte("doe"), 0)
err = testStore.Set("john", []byte("doe"), 0)
require.NoError(b, err)
b.ReportAllocs()
@@ -130,12 +175,12 @@ func Benchmark_Couchbase_Get(b *testing.B) {
}
func Benchmark_Couchbase_SetAndDelete(b *testing.B) {
testStore := newTestStore(b)
testStore, err := newTestStore(b)
require.NoError(b, err)
b.ReportAllocs()
b.ResetTimer()
var err error
for i := 0; i < b.N; i++ {
_ = testStore.Set("john", []byte("doe"), 0)
err = testStore.Delete("john")