Skip to content

Commit

Permalink
sonar: melhorando condigo com feedback do sonar
Browse files Browse the repository at this point in the history
  • Loading branch information
fonteeboa committed Dec 13, 2023
1 parent 5957c23 commit b85b919
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 57 deletions.
9 changes: 5 additions & 4 deletions internal/services/generalFunctions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/stretchr/testify/assert"

"github.com/fonteeBoa/go-log-guardian/internal/services"
"github.com/fonteeBoa/go-log-guardian/internal/testUtils/mocks"
"github.com/fonteeBoa/go-log-guardian/pkg/domain"
)

Expand Down Expand Up @@ -40,8 +41,8 @@ func TestDebugPrintsMessageForLogDebugPriority(t *testing.T) {
}()

priority := domain.LOG_DEBUG
genericErrMsg := "Test generic error"
errMsg := "Test specific error"
genericErrMsg := mocks.MockGeneralError
errMsg := mocks.MockSpecificError

services.Debug(priority, genericErrMsg, errMsg)

Expand All @@ -67,8 +68,8 @@ func TestDebugDoesNotPrintMessageForNonLogDebugPriority(t *testing.T) {
}()

priority := domain.LOG_INFO
genericErrMsg := "Test generic error"
errMsg := "Test specific error"
genericErrMsg := mocks.MockGeneralError
errMsg := mocks.MockSpecificError

services.Debug(priority, genericErrMsg, errMsg)

Expand Down
19 changes: 10 additions & 9 deletions internal/services/logFunctions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import (
"github.com/stretchr/testify/assert"

"github.com/fonteeBoa/go-log-guardian/internal/services"
"github.com/fonteeBoa/go-log-guardian/internal/testUtils/mocks"
"github.com/fonteeBoa/go-log-guardian/pkg/domain"
)

func TestFunction(t *testing.T) {
priority := domain.LOG_INFO
functionName := "TestFunction"
genericErrMsg := "Test generic error"
errMsg := "Test specific error"
genericErrMsg := mocks.MockGeneralError
errMsg := mocks.MockSpecificError

logFunction := services.Function(priority, functionName, genericErrMsg, errMsg)

Expand All @@ -27,10 +28,10 @@ func TestFunction(t *testing.T) {

func TestDatabase(t *testing.T) {
priority := domain.LOG_DEBUG
tableName := "TestTable"
tableName := mocks.MockTable
query := "SELECT * FROM TestTable"
genericErrMsg := "Test generic error"
errMsg := "Test specific error"
genericErrMsg := mocks.MockGeneralError
errMsg := mocks.MockSpecificError

databaseLog := services.Database(priority, tableName, query, genericErrMsg, errMsg)

Expand All @@ -49,8 +50,8 @@ func TestRequest(t *testing.T) {
statusCode := 500
path := "/test/path"
responseSize := 1024
genericErrMsg := "Test generic error"
errMsg := "Test specific error"
genericErrMsg := mocks.MockGeneralError
errMsg := mocks.MockSpecificError

requestLog := services.Request(priority, method, statusCode, path, responseSize, genericErrMsg, errMsg)

Expand All @@ -67,8 +68,8 @@ func TestRequest(t *testing.T) {

func TestDetails(t *testing.T) {
priority := domain.LOG_CRIT
genericErrMsg := "Test generic error"
errMsg := "Test specific error"
genericErrMsg := mocks.MockGeneralError
errMsg := mocks.MockSpecificError

logDetails := services.Details(priority, genericErrMsg, errMsg)

Expand Down
20 changes: 11 additions & 9 deletions internal/services/newlogs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import (
"github.com/stretchr/testify/assert"

"github.com/fonteeBoa/go-log-guardian/internal/services"
"github.com/fonteeBoa/go-log-guardian/internal/testUtils/mocks"

"github.com/fonteeBoa/go-log-guardian/pkg/domain"
)

func TestNewLogDetails(t *testing.T) {
priority := domain.LOG_INFO
genericErrMsg := "Test generic error"
errMsg := "Test specific error"
genericErrMsg := mocks.MockGeneralError
errMsg := mocks.MockSpecificError

logDetails := services.NewLogDetails(priority, genericErrMsg, errMsg)

Expand All @@ -26,8 +28,8 @@ func TestNewLogDetails(t *testing.T) {
func TestNewFunctionLog(t *testing.T) {
priority := domain.LOG_DEBUG
functionName := "TestFunction"
genericErrMsg := "Test generic error"
errMsg := "Test specific error"
genericErrMsg := mocks.MockGeneralError
errMsg := mocks.MockSpecificError

functionLog := services.NewFunctionLog(priority, functionName, genericErrMsg, errMsg)

Expand All @@ -41,10 +43,10 @@ func TestNewFunctionLog(t *testing.T) {

func TestNewDatabaseLog(t *testing.T) {
priority := domain.LOG_NOTICE
tableName := "TestTable"
tableName := mocks.MockTable
query := "SELECT * FROM TestTable"
genericErrMsg := "Test generic error"
errMsg := "Test specific error"
genericErrMsg := mocks.MockGeneralError
errMsg := mocks.MockSpecificError

databaseLog := services.NewDatabaseLog(priority, tableName, query, genericErrMsg, errMsg)

Expand All @@ -63,8 +65,8 @@ func TestNewRequestLog(t *testing.T) {
statusCode := 500
path := "/test/path"
responseSize := 1024
genericErrMsg := "Test generic error"
errMsg := "Test specific error"
genericErrMsg := mocks.MockGeneralError
errMsg := mocks.MockSpecificError

requestLog := services.NewRequestLog(priority, method, statusCode, path, responseSize, genericErrMsg, errMsg)

Expand Down
8 changes: 8 additions & 0 deletions internal/testUtils/mocks/mocksGerais.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package mocks

const (
MockError = "error"
MockGeneralError = "general.error"
MockTable = "TestTable"
MockSpecificError = "Test specific error"
)
71 changes: 36 additions & 35 deletions pkg/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pkg_test
import (
"testing"

"github.com/fonteeBoa/go-log-guardian/internal/testUtils/mocks"
"github.com/fonteeBoa/go-log-guardian/pkg"
"github.com/fonteeBoa/go-log-guardian/pkg/domain"
)
Expand All @@ -12,13 +13,13 @@ func TestLogFunction(t *testing.T) {
LogDetails: domain.LogDetails{
Priority: domain.LOG_ERR,
LogLevel: "debug",
GenericErrorMessage: "generic error",
ErrorMessage: "specific error",
GenericErrorMessage: mocks.MockGeneralError,
ErrorMessage: mocks.MockSpecificError,
},
FunctionName: "testFunction",
}

success, result := pkg.LogFunction(domain.LOG_ERR, "testFunction", "generic error", "specific error")
success, result := pkg.LogFunction(domain.LOG_ERR, "testFunction", mocks.MockGeneralError, mocks.MockSpecificError)

if !success {
t.Errorf("LogFunction failed to save log")
Expand All @@ -39,14 +40,14 @@ func TestLogDataBase(t *testing.T) {
LogDetails: domain.LogDetails{
Priority: domain.LOG_ERR,
LogLevel: "debug",
GenericErrorMessage: "generic error",
ErrorMessage: "specific error",
GenericErrorMessage: mocks.MockGeneralError,
ErrorMessage: mocks.MockSpecificError,
},
TableName: "testTable",
TableName: mocks.MockTable,
Query: "testQuery",
}

success, result := pkg.LogDataBase(domain.LOG_ERR, "testTable", "testQuery", "generic error", "specific error")
success, result := pkg.LogDataBase(domain.LOG_ERR, mocks.MockTable, "testQuery", mocks.MockGeneralError, mocks.MockSpecificError)

if !success {
t.Errorf("LogDataBase failed to save log")
Expand All @@ -68,16 +69,16 @@ func TestLogRequests(t *testing.T) {
LogDetails: domain.LogDetails{
Priority: domain.LOG_ERR,
LogLevel: "debug",
GenericErrorMessage: "generic error",
ErrorMessage: "specific error",
GenericErrorMessage: mocks.MockGeneralError,
ErrorMessage: mocks.MockSpecificError,
},
Method: "GET",
StatusCode: 200,
Path: "/test",
ResponseSize: 100,
}

success, result := pkg.LogRequests(domain.LOG_ERR, "GET", 200, "/test", 100, "generic error", "specific error")
success, result := pkg.LogRequests(domain.LOG_ERR, "GET", 200, "/test", 100, mocks.MockGeneralError, mocks.MockSpecificError)

if !success {
t.Errorf("LogRequests failed to save log")
Expand All @@ -99,11 +100,11 @@ func TestLog(t *testing.T) {
expectedLog := domain.LogDetails{
Priority: domain.LOG_DEBUG,
LogLevel: "debug",
GenericErrorMessage: "generic error",
ErrorMessage: "specific error",
GenericErrorMessage: mocks.MockGeneralError,
ErrorMessage: mocks.MockSpecificError,
}

success, result := pkg.Log(domain.LOG_DEBUG, "generic error", "specific error")
success, result := pkg.Log(domain.LOG_DEBUG, mocks.MockGeneralError, mocks.MockSpecificError)

if !success {
t.Errorf("Log failed to save log")
Expand All @@ -121,11 +122,11 @@ func TestLogEmerg(t *testing.T) {
expectedLog := domain.LogDetails{
Priority: domain.LOG_EMERG,
LogLevel: "LOG_EMERG",
GenericErrorMessage: "generic error",
ErrorMessage: "specific error",
GenericErrorMessage: mocks.MockGeneralError,
ErrorMessage: mocks.MockSpecificError,
}

success, result := pkg.LogEmerg("generic error", "specific error")
success, result := pkg.LogEmerg(mocks.MockGeneralError, mocks.MockSpecificError)

if !success {
t.Errorf("LogEmerg failed to save log")
Expand All @@ -143,11 +144,11 @@ func TestLogCritical(t *testing.T) {
expectedLog := domain.LogDetails{
Priority: domain.LOG_CRIT,
LogLevel: "LOG_CRIT",
GenericErrorMessage: "generic error",
ErrorMessage: "specific error",
GenericErrorMessage: mocks.MockGeneralError,
ErrorMessage: mocks.MockSpecificError,
}

success, result := pkg.LogCritical("generic error", "specific error")
success, result := pkg.LogCritical(mocks.MockGeneralError, mocks.MockSpecificError)

if !success {
t.Errorf("LogCritical failed to save log")
Expand All @@ -165,11 +166,11 @@ func TestLogError(t *testing.T) {
expectedLog := domain.LogDetails{
Priority: domain.LOG_ERR,
LogLevel: "LOG_ERR",
GenericErrorMessage: "generic error",
ErrorMessage: "specific error",
GenericErrorMessage: mocks.MockGeneralError,
ErrorMessage: mocks.MockSpecificError,
}

success, result := pkg.LogError(domain.LOG_ERR, "generic error", "specific error")
success, result := pkg.LogError(domain.LOG_ERR, mocks.MockGeneralError, mocks.MockSpecificError)

if !success {
t.Errorf("LogError failed to save log")
Expand All @@ -187,11 +188,11 @@ func TestLogAlert(t *testing.T) {
expectedLog := domain.LogDetails{
Priority: domain.LOG_ALERT,
LogLevel: "LOG_ALERT",
GenericErrorMessage: "generic error",
ErrorMessage: "specific error",
GenericErrorMessage: mocks.MockGeneralError,
ErrorMessage: mocks.MockSpecificError,
}

success, result := pkg.LogAlert("generic error", "specific error")
success, result := pkg.LogAlert(mocks.MockGeneralError, mocks.MockSpecificError)

if !success {
t.Errorf("LogAlert failed to save log")
Expand All @@ -209,11 +210,11 @@ func TestLogWarning(t *testing.T) {
expectedLog := domain.LogDetails{
Priority: domain.LOG_WARNING,
LogLevel: "LOG_WARNING",
GenericErrorMessage: "generic error",
ErrorMessage: "specific error",
GenericErrorMessage: mocks.MockGeneralError,
ErrorMessage: mocks.MockSpecificError,
}

success, result := pkg.LogWarning("generic error", "specific error")
success, result := pkg.LogWarning(mocks.MockGeneralError, mocks.MockSpecificError)

if !success {
t.Errorf("LogWarning failed to save log")
Expand All @@ -231,11 +232,11 @@ func TestLogNotice(t *testing.T) {
expectedLog := domain.LogDetails{
Priority: domain.LOG_NOTICE,
LogLevel: "LOG_NOTICE",
GenericErrorMessage: "generic error",
ErrorMessage: "specific error",
GenericErrorMessage: mocks.MockGeneralError,
ErrorMessage: mocks.MockSpecificError,
}

success, result := pkg.LogNotice("generic error", "specific error")
success, result := pkg.LogNotice(mocks.MockGeneralError, mocks.MockSpecificError)

if !success {
t.Errorf("LogNotice failed to save log")
Expand All @@ -253,11 +254,11 @@ func TestLogInfo(t *testing.T) {
expectedLog := domain.LogDetails{
Priority: domain.LOG_INFO,
LogLevel: "LOG_INFO",
GenericErrorMessage: "generic error",
ErrorMessage: "specific error",
GenericErrorMessage: mocks.MockGeneralError,
ErrorMessage: mocks.MockSpecificError,
}

success, result := pkg.LogInfo("generic error", "specific error")
success, result := pkg.LogInfo(mocks.MockGeneralError, mocks.MockSpecificError)

if !success {
t.Errorf("LogInfo failed to save log")
Expand All @@ -272,7 +273,7 @@ func TestLogInfo(t *testing.T) {

// Teste para a função LogDebug
func TestLogDebug(t *testing.T) {
success := pkg.LogDebug("generic error", "specific error")
success := pkg.LogDebug(mocks.MockGeneralError, mocks.MockSpecificError)

if !success {
t.Errorf("LogDebug failed")
Expand Down

0 comments on commit b85b919

Please sign in to comment.