clean: removed too deeply nested tests

This commit is contained in:
Florian Sylvain
2023-08-12 20:52:32 +02:00
parent 1539e06ec8
commit e575062209
+32 -20
View File
@@ -11,12 +11,7 @@ import (
"testing"
)
func TestArticle(t *testing.T) {
StartServerIfNotAlready()
WaitForServer()
t.Run("Create", func(t *testing.T) {
t.Run("Should return an article with the given title and body", func(t *testing.T) {
var TestCreateArticleSuccess = func(t *testing.T) {
jsonBody, err := json.Marshal(map[string]string{
"title": "Test Title",
"body": "Test Body",
@@ -41,9 +36,9 @@ func TestArticle(t *testing.T) {
assert.Equal(t, http.StatusOK, r.StatusCode)
assert.Equal(t, "Test Title", response.Title)
assert.Equal(t, "Test Body", response.Body)
})
}
t.Run("Should return an error if the title is missing", func(t *testing.T) {
var TestCreateArticleFailTitleMissing = func(t *testing.T) {
jsonBody, err := json.Marshal(map[string]string{
"body": "Test Body",
})
@@ -54,9 +49,9 @@ func TestArticle(t *testing.T) {
r, err := ApiRequest("POST", "/article", bytes.NewBuffer(jsonBody))
assert.Equal(t, http.StatusBadRequest, r.StatusCode)
})
}
t.Run("Should return an error if the title is too short", func(t *testing.T) {
var TestCreateArticleTitleTooShort = func(t *testing.T) {
jsonBody, err := json.Marshal(map[string]string{
"title": "Te",
"body": "Test Body",
@@ -68,11 +63,9 @@ func TestArticle(t *testing.T) {
r, err := ApiRequest("POST", "/article", bytes.NewBuffer(jsonBody))
assert.Equal(t, http.StatusBadRequest, r.StatusCode)
})
})
}
t.Run("Get", func(t *testing.T) {
t.Run("Should return an article with the given id", func(t *testing.T) {
var TestGetArticleSuccess = func(t *testing.T) {
var createdArticle article.Article
var articleToCreate = article.Article{
Title: "Test Title",
@@ -98,11 +91,9 @@ func TestArticle(t *testing.T) {
assert.Equal(t, createdArticle.ID, response.ID)
assert.Equal(t, createdArticle.Title, response.Title)
assert.Equal(t, createdArticle.Body, response.Body)
})
})
}
t.Run("GetAll", func(t *testing.T) {
t.Run("Should return all articles", func(t *testing.T) {
var TestGetAllArticlesSuccess = func(t *testing.T) {
var createdArticle article.Article
var articleToCreate = article.Article{
Title: "Test Title",
@@ -127,6 +118,27 @@ func TestArticle(t *testing.T) {
assert.Equal(t, http.StatusOK, r.StatusCode)
assert.Equal(t, createdArticle.Title, response[0].Title)
assert.Equal(t, createdArticle.Body, response[0].Body)
})
})
}
var TestArticleCreate = func(t *testing.T) {
t.Run("Should return an article with the given title and body", TestCreateArticleSuccess)
t.Run("Should return an error if the title is missing", TestCreateArticleFailTitleMissing)
t.Run("Should return an error if the title is too short", TestCreateArticleTitleTooShort)
}
var TestArticleGet = func(t *testing.T) {
t.Run("Should return an article with the given id", TestGetArticleSuccess)
}
var TestArticleGetAll = func(t *testing.T) {
t.Run("Should return all articles", TestGetAllArticlesSuccess)
}
func TestArticle(t *testing.T) {
StartServerIfNotAlready()
WaitForServer()
t.Run("Create", TestArticleCreate)
t.Run("Get", TestArticleGet)
t.Run("GetAll", TestArticleGetAll)
}