chore(mongodb): use require in tests

This commit is contained in:
Manuel de la Peña
2025-04-15 16:21:14 +02:00
parent a5733f7261
commit 61d00d996b

View File

@@ -22,7 +22,7 @@ const (
mongoDBReadyLog = "Waiting for connections"
)
func newTestStore(t testing.TB) (*Storage, error) {
func newTestStore(t testing.TB) *Storage {
t.Helper()
img := mongoDBImage
@@ -42,19 +42,15 @@ func newTestStore(t testing.TB) (*Storage, error) {
),
)
testcontainers.CleanupContainer(t, c)
if err != nil {
return nil, err
}
require.NoError(t, err)
conn, err := c.ConnectionString(ctx)
if err != nil {
return nil, err
}
require.NoError(t, err)
return New(Config{
ConnectionURI: conn,
Reset: true,
}), nil
})
}
func Test_MongoDB_Set(t *testing.T) {
@@ -63,11 +59,10 @@ func Test_MongoDB_Set(t *testing.T) {
val = []byte("doe")
)
testStore, err := newTestStore(t)
require.NoError(t, err)
testStore := newTestStore(t)
defer testStore.Close()
err = testStore.Set(key, val, 0)
err := testStore.Set(key, val, 0)
require.NoError(t, err)
}
@@ -77,11 +72,10 @@ func Test_MongoDB_Set_Override(t *testing.T) {
val = []byte("doe")
)
testStore, err := newTestStore(t)
require.NoError(t, err)
testStore := newTestStore(t)
defer testStore.Close()
err = testStore.Set(key, val, 0)
err := testStore.Set(key, val, 0)
require.NoError(t, err)
err = testStore.Set(key, val, 0)
@@ -94,11 +88,10 @@ func Test_MongoDB_Get(t *testing.T) {
val = []byte("doe")
)
testStore, err := newTestStore(t)
require.NoError(t, err)
testStore := newTestStore(t)
defer testStore.Close()
err = testStore.Set(key, val, 0)
err := testStore.Set(key, val, 0)
require.NoError(t, err)
result, err := testStore.Get(key)
@@ -113,11 +106,10 @@ func Test_MongoDB_Set_Expiration(t *testing.T) {
exp = 1 * time.Second
)
testStore, err := newTestStore(t)
require.NoError(t, err)
testStore := newTestStore(t)
defer testStore.Close()
err = testStore.Set(key, val, exp)
err := testStore.Set(key, val, exp)
require.NoError(t, err)
time.Sleep(1100 * time.Millisecond)
@@ -130,8 +122,7 @@ func Test_MongoDB_Set_Expiration(t *testing.T) {
func Test_MongoDB_Get_Expired(t *testing.T) {
key := "john"
testStore, err := newTestStore(t)
require.NoError(t, err)
testStore := newTestStore(t)
defer testStore.Close()
result, err := testStore.Get(key)
@@ -140,8 +131,7 @@ func Test_MongoDB_Get_Expired(t *testing.T) {
}
func Test_MongoDB_Get_NotExist(t *testing.T) {
testStore, err := newTestStore(t)
require.NoError(t, err)
testStore := newTestStore(t)
defer testStore.Close()
result, err := testStore.Get("notexist")
@@ -155,11 +145,10 @@ func Test_MongoDB_Delete(t *testing.T) {
val = []byte("doe")
)
testStore, err := newTestStore(t)
require.NoError(t, err)
testStore := newTestStore(t)
defer testStore.Close()
err = testStore.Set(key, val, 0)
err := testStore.Set(key, val, 0)
require.NoError(t, err)
err = testStore.Delete(key)
@@ -173,11 +162,10 @@ func Test_MongoDB_Delete(t *testing.T) {
func Test_MongoDB_Reset(t *testing.T) {
val := []byte("doe")
testStore, err := newTestStore(t)
require.NoError(t, err)
testStore := newTestStore(t)
defer testStore.Close()
err = testStore.Set("john1", val, 0)
err := testStore.Set("john1", val, 0)
require.NoError(t, err)
err = testStore.Set("john2", val, 0)
@@ -196,28 +184,25 @@ func Test_MongoDB_Reset(t *testing.T) {
}
func Test_MongoDB_Close(t *testing.T) {
testStore, err := newTestStore(t)
require.NoError(t, err)
require.Nil(t, testStore.Close())
testStore := newTestStore(t)
require.NoError(t, testStore.Close())
}
func Test_MongoDB_Conn(t *testing.T) {
testStore, err := newTestStore(t)
require.NoError(t, err)
testStore := newTestStore(t)
defer testStore.Close()
require.True(t, testStore.Conn() != nil)
}
func Benchmark_MongoDB_Set(b *testing.B) {
testStore, err := newTestStore(b)
require.NoError(b, err)
testStore := newTestStore(b)
defer testStore.Close()
b.ReportAllocs()
b.ResetTimer()
var err error
for i := 0; i < b.N; i++ {
err = testStore.Set("john", []byte("doe"), 0)
}
@@ -226,11 +211,10 @@ func Benchmark_MongoDB_Set(b *testing.B) {
}
func Benchmark_MongoDB_Get(b *testing.B) {
testStore, err := newTestStore(b)
require.NoError(b, err)
testStore := newTestStore(b)
defer testStore.Close()
err = testStore.Set("john", []byte("doe"), 0)
err := testStore.Set("john", []byte("doe"), 0)
require.NoError(b, err)
b.ReportAllocs()
@@ -244,13 +228,13 @@ func Benchmark_MongoDB_Get(b *testing.B) {
}
func Benchmark_MongoDB_SetAndDelete(b *testing.B) {
testStore, err := newTestStore(b)
require.NoError(b, err)
testStore := newTestStore(b)
defer testStore.Close()
b.ReportAllocs()
b.ResetTimer()
var err error
for i := 0; i < b.N; i++ {
_ = testStore.Set("john", []byte("doe"), 0)
err = testStore.Delete("john")