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