From 977c3967efb0cd3995b1857950f5d903dbb73faa Mon Sep 17 00:00:00 2001 From: mariolg Date: Wed, 15 Nov 2023 20:58:49 +0100 Subject: [PATCH] feat: fix sonarlint --- steps/mongo/session.go | 7 ++++++- steps/mongo/session_test.go | 11 ++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/steps/mongo/session.go b/steps/mongo/session.go index daaea82..66ceea8 100644 --- a/steps/mongo/session.go +++ b/steps/mongo/session.go @@ -31,6 +31,11 @@ import ( "go.mongodb.org/mongo-driver/mongo/options" ) +var ( + EMPTY = "[EMPTY]" + NULL = "[NULL]" +) + // Session contains the information of a MongoDB session. type Session struct { @@ -330,7 +335,7 @@ func GetFilterConverted(field, value string) primitive.M { } // If the passed value is "[EMPTY]" or "[NULL]", it evaluates to nil - if value == "[EMPTY]" || value == "[NULL]" { + if value == EMPTY || value == NULL { return bson.M{field: nil} } diff --git a/steps/mongo/session_test.go b/steps/mongo/session_test.go index fbcd62f..245ee6a 100644 --- a/steps/mongo/session_test.go +++ b/steps/mongo/session_test.go @@ -26,6 +26,8 @@ import ( "go.mongodb.org/mongo-driver/mongo" ) +var ISBOOLEAN = "is_boolean" + func TestPing(t *testing.T) { tests := []struct { name string @@ -363,8 +365,7 @@ func TestGetFilter(t *testing.T) { }) t.Run("Create filter with nil value", func(t *testing.T) { key := "name" - var value interface{} = nil - filter := GetFilter(key, value) + filter := GetFilter(key, nil) expectedFilter := bson.M{"name": nil} if !reflect.DeepEqual(filter, expectedFilter) { t.Errorf("Expected filter: %v, but got: %v", expectedFilter, filter) @@ -374,7 +375,7 @@ func TestGetFilter(t *testing.T) { func TestGetFilterConverted(t *testing.T) { t.Run("Convert to boolean (true)", func(t *testing.T) { - field := "is_boolean" + field := ISBOOLEAN value := "true" filter := GetFilterConverted(field, value) expectedFilter := primitive.M{field: true} @@ -383,7 +384,7 @@ func TestGetFilterConverted(t *testing.T) { } }) t.Run("Convert to boolean (false)", func(t *testing.T) { - field := "is_boolean" + field := ISBOOLEAN value := "false" filter := GetFilterConverted(field, value) expectedFilter := primitive.M{field: false} @@ -411,7 +412,7 @@ func TestGetFilterConverted(t *testing.T) { }) t.Run("Convert to nil (empty)", func(t *testing.T) { field := "nil or empty" - value := "[EMPTY]" + value := EMPTY filter := GetFilterConverted(field, value) expectedFilter := primitive.M{field: nil} if filter[field] != expectedFilter[field] {