People: Add unit tests #22
This commit is contained in:
parent
6bcfb29fa2
commit
885b2b0e00
1 changed files with 67 additions and 0 deletions
|
@ -44,3 +44,70 @@ func TestGetSubject(t *testing.T) {
|
|||
assert.Equal(t, http.StatusNotFound, r.Code)
|
||||
})
|
||||
}
|
||||
|
||||
func TestLikeSubject(t *testing.T) {
|
||||
t.Run("like not existing subject", func(t *testing.T) {
|
||||
app, router, _ := NewApiTest()
|
||||
LikeSubject(router)
|
||||
r := PerformRequest(app, "POST", "/api/v1/subjects/8775789/like")
|
||||
assert.Equal(t, http.StatusNotFound, r.Code)
|
||||
})
|
||||
t.Run("like existing subject", func(t *testing.T) {
|
||||
app, router, _ := NewApiTest()
|
||||
|
||||
// Register routes.
|
||||
GetSubject(router)
|
||||
LikeSubject(router)
|
||||
|
||||
r := PerformRequest(app, "GET", "/api/v1/subjects/jqy3y652h8njw0sx")
|
||||
t.Log(r.Body.String())
|
||||
val := gjson.Get(r.Body.String(), "Slug")
|
||||
assert.Equal(t, "joe-biden", val.String())
|
||||
val2 := gjson.Get(r.Body.String(), "Favorite")
|
||||
assert.Equal(t, "false", val2.String())
|
||||
|
||||
r2 := PerformRequest(app, "POST", "/api/v1/subjects/jqy3y652h8njw0sx/like")
|
||||
t.Log(r2.Body.String())
|
||||
assert.Equal(t, http.StatusOK, r2.Code)
|
||||
|
||||
r3 := PerformRequest(app, "GET", "/api/v1/subjects/jqy3y652h8njw0sx")
|
||||
t.Log(r3.Body.String())
|
||||
val3 := gjson.Get(r3.Body.String(), "Slug")
|
||||
assert.Equal(t, "joe-biden", val3.String())
|
||||
val4 := gjson.Get(r3.Body.String(), "Favorite")
|
||||
assert.Equal(t, "true", val4.String())
|
||||
})
|
||||
}
|
||||
func TestDislikeSubject(t *testing.T) {
|
||||
t.Run("dislike not existing subject", func(t *testing.T) {
|
||||
app, router, _ := NewApiTest()
|
||||
DislikeSubject(router)
|
||||
r := PerformRequest(app, "DELETE", "/api/v1/subjects/8775789/like")
|
||||
assert.Equal(t, http.StatusNotFound, r.Code)
|
||||
})
|
||||
t.Run("dislike existing subject", func(t *testing.T) {
|
||||
app, router, _ := NewApiTest()
|
||||
|
||||
// Register routes.
|
||||
GetSubject(router)
|
||||
DislikeSubject(router)
|
||||
|
||||
r := PerformRequest(app, "GET", "/api/v1/subjects/jqu0xs11qekk9jx8")
|
||||
t.Log(r.Body.String())
|
||||
val := gjson.Get(r.Body.String(), "Slug")
|
||||
assert.Equal(t, "john-doe", val.String())
|
||||
val2 := gjson.Get(r.Body.String(), "Favorite")
|
||||
assert.Equal(t, "true", val2.String())
|
||||
|
||||
r2 := PerformRequest(app, "DELETE", "/api/v1/subjects/jqu0xs11qekk9jx8/like")
|
||||
t.Log(r2.Body.String())
|
||||
assert.Equal(t, http.StatusOK, r2.Code)
|
||||
|
||||
r3 := PerformRequest(app, "GET", "/api/v1/subjects/jqu0xs11qekk9jx8")
|
||||
t.Log(r3.Body.String())
|
||||
val3 := gjson.Get(r3.Body.String(), "Slug")
|
||||
assert.Equal(t, "john-doe", val3.String())
|
||||
val4 := gjson.Get(r3.Body.String(), "Favorite")
|
||||
assert.Equal(t, "false", val4.String())
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue