add test for getUser & add readme

This commit is contained in:
Cubicroot 2021-06-19 16:59:30 +02:00
parent 46dd19b07d
commit e6f24c4458
6 changed files with 114 additions and 21 deletions

View file

@ -53,13 +53,10 @@ func TestApi_getApplication(t *testing.T) {
assert := assert.New(t)
gin.SetMode(gin.TestMode)
// Generate a mock database
db, err := mockups.GetEmptyDatabase()
assert.NoErrorf(err, "Could not get database: ", err)
applications := mockups.GetAllApplications()
mockups.AddApplicationsToDb(db, applications)
mockups.AddApplicationsToDb(TestDatabase, applications)
// No testing of invalid ids as that is tested in TestApi_getID already
testCases := make(map[uint]tests.Request)
testCases[500] = tests.Request{Name: "-", Method: "GET", Endpoint: "/", Data: "", ShouldStatus: 404}
testCases[1] = tests.Request{Name: "-", Method: "GET", Endpoint: "/", Data: "", ShouldStatus: 200}
@ -72,7 +69,7 @@ func TestApi_getApplication(t *testing.T) {
}
c.Set("id", id)
app, err := getApplication(c, db)
app, err := getApplication(c, TestDatabase)
if req.ShouldStatus >= 200 && req.ShouldStatus < 300 {
assert.Equalf(app.ID, id, "getApplication id was set to %d but resulting app id is %d", id, app.ID)
@ -85,3 +82,36 @@ func TestApi_getApplication(t *testing.T) {
}
}
func TestApi_getUser(t *testing.T) {
assert := assert.New(t)
gin.SetMode(gin.TestMode)
_, err := mockups.AddUsersToDb(TestDatabase, TestUsers)
assert.NoErrorf(err, "Adding users to database failed: %v", err)
// No testing of invalid ids as that is tested in TestApi_getID already
testCases := make(map[uint]tests.Request)
testCases[500] = tests.Request{Name: "-", Method: "GET", Endpoint: "/", Data: "", ShouldStatus: 404}
testCases[1] = tests.Request{Name: "-", Method: "GET", Endpoint: "/", Data: "", ShouldStatus: 200}
testCases[2] = tests.Request{Name: "-", Method: "GET", Endpoint: "/", Data: "", ShouldStatus: 200}
for id, req := range testCases {
w, c, err := req.GetRequest()
if err != nil {
t.Fatalf(err.Error())
}
c.Set("id", id)
user, err := getUser(c, TestDatabase)
if req.ShouldStatus >= 200 && req.ShouldStatus < 300 {
assert.Equalf(user.ID, id, "getUser id was set to %d but resulting app id is %d", id, user.ID)
assert.NoErrorf(err, "getUser with id %v (%t) returned an error altough it should not: %v", id, id, err)
} else {
assert.Errorf(err, "getUser with id %v (%t) returned no error altough it should", id, id)
}
assert.Equalf(w.Code, req.ShouldStatus, "getUser id was set to %v (%T) and should result in status code %d but code is %d", id, id, req.ShouldStatus, w.Code)
}
}