Skip to content

Commit

Permalink
Patch duplication issue on Sonarcloud
Browse files Browse the repository at this point in the history
  • Loading branch information
0to1a committed Oct 7, 2022
1 parent 7d5691e commit 2ffe08e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
- name: Run Test
run: |
go test -v ./framework -covermode=count -coverprofile=coverage.out
go test -v ./framework/... -covermode=count -coverprofile=coverage.out
go tool cover -func=coverage.out -o=coverage.out
- name: Go Coverage Badge # Pass the `coverage.out` output to this action
Expand Down
2 changes: 1 addition & 1 deletion framework/database-mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestCheckClientMysql(t *testing.T) {
db, _, _ := sqlmock.New()
defer db.Close()

t.Run("Error no connection", func(t *testing.T) {
t.Run("Error not connect", func(t *testing.T) {
dbMysql.client = nil
DatabaseMysql = nil
assert.Equal(t, (*sql.DB)(nil), dbMysql.CheckClient())
Expand Down
26 changes: 15 additions & 11 deletions framework/database-redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ import (
"time"
)

const (
miniRedisError = "an error '%s' was not expected when opening a stub database connection"
)

func TestConnectRedis(t *testing.T) {
mr, err := miniredis.Run()
if err != nil {
log.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
log.Fatalf(miniRedisError, err)
}

t.Run("Error no connection", func(t *testing.T) {
t.Run("Without connection", func(t *testing.T) {
redisConf := RedisDatabase{Host: mr.Addr(), Password: "hello", Database: 10}
assert.Equal(t, (*redis.Client)(nil), redisConf.Connect())
})
Expand All @@ -28,11 +32,11 @@ func TestConnectRedis(t *testing.T) {
func TestCheckClientRedis(t *testing.T) {
mr, err := miniredis.Run()
if err != nil {
log.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
log.Fatalf(miniRedisError, err)
}
redisMock := RedisDatabase{Host: mr.Addr()}

t.Run("Error no connection", func(t *testing.T) {
t.Run("Without connection", func(t *testing.T) {
redisMock.client = nil
RedisDB = nil
assert.Equal(t, (*redis.Client)(nil), redisMock.CheckClient())
Expand All @@ -52,7 +56,7 @@ func TestCheckClientRedis(t *testing.T) {
func TestCheckPrefix(t *testing.T) {
redisMock := RedisDatabase{}

t.Run("Error no connection", func(t *testing.T) {
t.Run("Not connect", func(t *testing.T) {
redisMock.Prefix = ""
RedisPrefix = ""
assert.Equal(t, "", redisMock.CheckPrefix())
Expand All @@ -74,7 +78,7 @@ func TestCheckPrefix(t *testing.T) {
func TestSetCompress(t *testing.T) {
mr, err := miniredis.Run()
if err != nil {
log.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
log.Fatalf(miniRedisError, err)
}
redisMock := RedisDatabase{Host: mr.Addr()}
redisMock.Connect()
Expand Down Expand Up @@ -103,7 +107,7 @@ func TestSetCompress(t *testing.T) {
output := redisMock.SetCompress("test", "1", 1, "data")
assert.Equal(t, false, output)
})
t.Run("Error no connection", func(t *testing.T) {
t.Run("No client", func(t *testing.T) {
redisMock.client = nil
RedisDB = nil
output := redisMock.Set("test", "1", 1, "data")
Expand All @@ -114,7 +118,7 @@ func TestSetCompress(t *testing.T) {
func TestGetCompress(t *testing.T) {
mr, err := miniredis.Run()
if err != nil {
log.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
log.Fatalf(miniRedisError, err)
}
redisMock := RedisDatabase{Host: mr.Addr()}
redisMock.Connect()
Expand Down Expand Up @@ -147,7 +151,7 @@ func TestGetCompress(t *testing.T) {
isOkay, _ := redisMock.GetCompress("test", "1")
assert.Equal(t, false, isOkay)
})
t.Run("Error disconnect", func(t *testing.T) {
t.Run("Disconnect server", func(t *testing.T) {
mr.Close()
isOkay, _ := redisMock.Get("test", "1")
assert.Equal(t, false, isOkay)
Expand All @@ -169,7 +173,7 @@ func TestGetCompress(t *testing.T) {
func TestRemove(t *testing.T) {
mr, err := miniredis.Run()
if err != nil {
log.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
log.Fatalf(miniRedisError, err)
}
redisMock := RedisDatabase{Host: mr.Addr()}
redisMock.Connect()
Expand Down Expand Up @@ -199,7 +203,7 @@ func TestRemove(t *testing.T) {
err := redisMock.removeData(redisMock.client, hash)
assert.NotEqual(t, nil, err)
})
t.Run("Error disconnect", func(t *testing.T) {
t.Run("Disconnect when delete", func(t *testing.T) {
mr.Close()
isOkay := redisMock.Remove("test", "1")
assert.Equal(t, false, isOkay)
Expand Down
11 changes: 6 additions & 5 deletions framework/utils-module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,31 @@ import (
)

func TestCheckModuleVersion(t *testing.T) {
moduleName := "module name"
target := structure.ModularStruct{
Name: "module name",
Name: moduleName,
MinVersion: 10,
MaxVersion: 11,
}

t.Run("Module on same version", func(t *testing.T) {
assert.Panic(t, nil, func() {
utils.checkModuleVersion("module name", 10, target)
utils.checkModuleVersion(moduleName, 10, target)
})
})
t.Run("Module on maximum version", func(t *testing.T) {
assert.Panic(t, nil, func() {
utils.checkModuleVersion("module name", 11, target)
utils.checkModuleVersion(moduleName, 11, target)
})
})
t.Run("Module is older", func(t *testing.T) {
assert.Panic(t, "Module 'module name' incompatible, target version: 10 exist version: 1", func() {
utils.checkModuleVersion("module name", 1, target)
utils.checkModuleVersion(moduleName, 1, target)
})
})
t.Run("Module is newest", func(t *testing.T) {
assert.Panic(t, "Module 'module name' incompatible, target version: 11 exist version: 100", func() {
utils.checkModuleVersion("module name", 100, target)
utils.checkModuleVersion(moduleName, 100, target)
})
})
}
Expand Down
14 changes: 9 additions & 5 deletions framework/webserver-index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import (
"time"
)

const (
pathFolder = "log-test"
)

func TestCreateService(t *testing.T) {
t.Run("Running service", func(t *testing.T) {
e := echo.New()
Expand Down Expand Up @@ -45,7 +49,7 @@ func TestSetupLogFile(t *testing.T) {
}()

t.Run("Setup normally", func(t *testing.T) {
webserver.SetupLogFile("log-test", "hello", "world")
webserver.SetupLogFile(pathFolder, "hello", "world")
if _, err := os.Stat("log-test/hello"); err != nil {
t.Fatal(err)
}
Expand All @@ -54,12 +58,12 @@ func TestSetupLogFile(t *testing.T) {
})
t.Run("Filename log error", func(t *testing.T) {
assert.Panic(t, "Failed to create request log file:open log-test/: is a directory", func() {
webserver.SetupLogFile("log-test", "", "world")
webserver.SetupLogFile(pathFolder, "", "world")
})
})
t.Run("Filename error", func(t *testing.T) {
assert.Panic(t, "Failed to create request log file:open log-test/: is a directory", func() {
webserver.SetupLogFile("log-test", "hello", "")
webserver.SetupLogFile(pathFolder, "hello", "")
})
webserver.logFile.Close()
})
Expand All @@ -69,14 +73,14 @@ func TestResultAPI(t *testing.T) {
defer func() {
webserver.logFile.Close()
webserver.logErrFile.Close()
err := os.RemoveAll("log-test")
err := os.RemoveAll(pathFolder)
if err != nil {
t.Fatal(err)
}
}()

e := echo.New()
e.Use(webserver.Logger("log-test", "hello", "world"))
e.Use(webserver.Logger(pathFolder, "hello", "world"))
e.GET("/test", func(context echo.Context) error {
return webserver.ResultAPI(context, 200, "ok", "ok")
})
Expand Down

0 comments on commit 2ffe08e

Please sign in to comment.