-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added error handler, small refactoring
- Loading branch information
Showing
11 changed files
with
350 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,21 @@ | ||
package app | ||
|
||
import "fmt" | ||
import ( | ||
"errors" | ||
) | ||
|
||
var ErrComponentValidation = fmt.Errorf("component value validation error") | ||
func ReturnError(err error) error { | ||
return &ErrorMsg{ErrMsg: err.Error()} | ||
} | ||
|
||
type ErrorMsg struct { | ||
ErrMsg string `json:"errMsg"` | ||
} | ||
|
||
func (e *ErrorMsg) Error() string { | ||
return e.ErrMsg | ||
} | ||
|
||
var ErrPageNotFound = errors.New("page not found") | ||
|
||
var ErrComponentIsNotPresent = errors.New("component is not present") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package app | ||
|
||
import ( | ||
"database/sql/driver" | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
|
||
"github.com/DATA-DOG/go-sqlmock" | ||
"github.com/gin-gonic/gin" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/stackmon/otc-status-dashboard/internal/db" | ||
) | ||
|
||
var testApp *App | ||
var mock sqlmock.Sqlmock | ||
|
||
func TestGetIncidentsHandler(t *testing.T) { | ||
initTests(t) | ||
|
||
str := "2024-09-01T11:45:26.371Z" | ||
|
||
testTime, err := time.Parse(time.RFC3339, str) | ||
assert.NoError(t, err) | ||
|
||
prepareDB(t, testTime) | ||
|
||
var response = `{"data":[{"id":1,"title":"Incident title","impact":0,"components":[150],"start_date":"%s","system":false,"updates":[{"id":1,"status":"resolved","text":"Issue solved.","timestamp":"%s"}]}]}` | ||
|
||
w := httptest.NewRecorder() | ||
req, _ := http.NewRequest("GET", "/v1/incidents", nil) | ||
testApp.router.ServeHTTP(w, req) | ||
|
||
assert.Equal(t, 200, w.Code) | ||
|
||
assert.Equal(t, fmt.Sprintf(response, str, str), w.Body.String()) | ||
} | ||
|
||
func TestReturn404Handler(t *testing.T) { | ||
initTests(t) | ||
w := httptest.NewRecorder() | ||
req, _ := http.NewRequest("GET", "/anyendpoint", nil) | ||
testApp.router.ServeHTTP(w, req) | ||
|
||
assert.Equal(t, 404, w.Code) | ||
assert.Equal(t, `{"errorMsg":"page not found"}`, w.Body.String()) | ||
} | ||
|
||
func prepareDB(t *testing.T, testTime time.Time) { | ||
t.Helper() | ||
|
||
rows := sqlmock.NewRows([]string{"id", "text", "start_date", "end_date", "impact", "system"}). | ||
AddRow(1, "Incident title", testTime, nil, 0, false) | ||
mock.ExpectQuery("^SELECT (.+) FROM \"incident\"$").WillReturnRows(rows) | ||
|
||
rowsIncComp := sqlmock.NewRows([]string{"incident_id", "component_id"}). | ||
AddRow(1, 150) | ||
mock.ExpectQuery("^SELECT (.+) FROM \"incident_component_relation\"(.+)").WillReturnRows(rowsIncComp) | ||
|
||
rowsComp := sqlmock.NewRows([]string{"id", "name"}). | ||
AddRow(150, "Cloud Container Engine") | ||
mock.ExpectQuery("^SELECT (.+) FROM \"component\"(.+)").WillReturnRows(rowsComp) | ||
|
||
rowsStatus := sqlmock.NewRows([]string{"id", "incident_id", "timestamp", "text", "status"}). | ||
AddRow(1, 1, testTime, "Issue solved.", "resolved") | ||
mock.ExpectQuery("^SELECT (.+) FROM \"incident_status\"").WillReturnRows(rowsStatus) | ||
|
||
rowsCompAttr := sqlmock.NewRows([]string{"id", "component_id", "name", "value"}). | ||
AddRows([][]driver.Value{ | ||
{ | ||
859, 150, "category", "Container", | ||
}, | ||
{ | ||
860, 150, "region", "EU-DE", | ||
}, | ||
{ | ||
861, 150, "type", "cce", | ||
}, | ||
}..., | ||
) | ||
mock.ExpectQuery("^SELECT (.+) FROM \"component_attribute\"").WillReturnRows(rowsCompAttr) | ||
|
||
mock.NewRowsWithColumnDefinition() | ||
} | ||
|
||
func initTests(t *testing.T) { | ||
t.Helper() | ||
|
||
if testApp != nil && mock != nil { | ||
t.Log("testApp and mock are initialized") | ||
} | ||
|
||
t.Log("start initialisation") | ||
r := gin.Default() | ||
r.Use(ErrorHandle()) | ||
r.NoRoute(Return404) | ||
|
||
d, m, err := db.NewWithMock() | ||
assert.NoError(t, err) | ||
|
||
testApp = &App{router: r, Log: nil, conf: nil, DB: d, srv: nil} | ||
testApp.InitRoutes() | ||
mock = m | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.