Skip to content

Commit

Permalink
test: reduce redundant cases (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
kumachan-mis authored Sep 7, 2024
1 parent 4488d7a commit 5f033b3
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 461 deletions.
4 changes: 2 additions & 2 deletions fixtures/firebase-export-metadata.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"version": "13.15.1",
"version": "13.16.0",
"firestore": {
"version": "1.19.7",
"version": "1.19.8",
"path": "firestore_export",
"metadata_file": "firestore_export/firestore_export.overall_export_metadata"
}
Expand Down
Binary file not shown.
Binary file modified fixtures/firestore_export/all_namespaces/all_kinds/output-0
Binary file not shown.
202 changes: 38 additions & 164 deletions internal/api/chapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,56 +220,22 @@ func TestChapterListDomainValidationError(t *testing.T) {
}

func TestChapterListInvalidRequestFormat(t *testing.T) {
tt := []struct {
name string
request string
}{
{
name: "should return error when user id is not string",
request: `{"user": {"id":123}, "project": {"id": "PROJECT_WITHOUT_DESCRIPTION"}}`,
},
{
name: "should return error when project id is not string",
request: `{"user": {"id": "user-id"}, "project": {"id": 123}}`,
},
{
name: "should return error when user is not object",
request: `{"user": 123, "project": {"id": "PROJECT_WITHOUT_DESCRIPTION"}}`,
},
{
name: "should return error when project is not object",
request: `{"user": {"id": "user-id"}, "project": "PROJECT_WITHOUT_DESCRIPTION"}`,
},
{
name: "should return error when request body is invalid JSON",
request: `{"user": {"id": "user-id", "project": {"id": "PROJECT_WITHOUT_DESCRIPTION"}`,
},
{
name: "should return error when request body is empty",
request: "",
},
}

for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
router := setupChapterRouter()
router := setupChapterRouter()

recorder := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/api/chapters/list", strings.NewReader(tc.request))
recorder := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/api/chapters/list", strings.NewReader(""))

router.ServeHTTP(recorder, req)
router.ServeHTTP(recorder, req)

assert.Equal(t, http.StatusBadRequest, recorder.Code)
assert.Equal(t, http.StatusBadRequest, recorder.Code)

var responseBody map[string]any
assert.NoError(t, json.Unmarshal(recorder.Body.Bytes(), &responseBody))
assert.Equal(t, map[string]any{
"message": "invalid request format",
"user": map[string]any{},
"project": map[string]any{},
}, responseBody)
})
}
var responseBody map[string]any
assert.NoError(t, json.Unmarshal(recorder.Body.Bytes(), &responseBody))
assert.Equal(t, map[string]any{
"message": "invalid request format",
"user": map[string]any{},
"project": map[string]any{},
}, responseBody)
}

func TestChapterListInternalError(t *testing.T) {
Expand Down Expand Up @@ -592,65 +558,23 @@ func TestChapterCreateDomainValidationError(t *testing.T) {
}

func TestChapterCreateInvalidRequestFormat(t *testing.T) {
tt := []struct {
name string
request string
}{
{
name: "should return error when user id is not string",
request: `{"user": {"id":123}, "project": {"id": "PROJECT_WITH_DESCRIPTION_TO_UPDATE_FROM_API"}, "chapter": {"name": "Chapter One"}}`,
},
{
name: "should return error when project id is not string",
request: `{"user": {"id": "user-id"}, "project": {"id": 123}, "chapter": {"name": "Chapter One"}}`,
},
{
name: "should return error when chapter name is not string",
request: `{"user": {"id": "user-id"}, "project": {"id": "PROJECT_WITH_DESCRIPTION_TO_UPDATE_FROM_API"}, "chapter": {"name": 123}}`,
},
{
name: "should return error when user is not object",
request: `{"user": 123, "project": {"id": "PROJECT_WITH_DESCRIPTION_TO_UPDATE_FROM_API"}, "chapter": {"name": "Chapter One"}}`,
},
{
name: "should return error when project is not object",
request: `{"user": {"id": "user-id"}, "project": "PROJECT_WITH_DESCRIPTION_TO_UPDATE_FROM_API", "chapter": {"name": "Chapter One"}}`,
},
{
name: "should return error when chapter is not object",
request: `{"user": {"id": "user-id"}, "project": {"id": "PROJECT_WITH_DESCRIPTION_TO_UPDATE_FROM_API"}, "chapter": "Chapter One"}`,
},
{
name: "should return error when request body is invalid JSON",
request: `{"user": {"id": "user-id", "project": {"id": "PROJECT_WITH_DESCRIPTION_TO_UPDATE_FROM_API"}, "chapter": {"name": "Chapter One"}`,
},
{
name: "should return error when request body is empty",
request: "",
},
}

for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
router := setupChapterRouter()
router := setupChapterRouter()

recorder := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/api/chapters/create", strings.NewReader(tc.request))
recorder := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/api/chapters/create", strings.NewReader(""))

router.ServeHTTP(recorder, req)
router.ServeHTTP(recorder, req)

assert.Equal(t, http.StatusBadRequest, recorder.Code)
assert.Equal(t, http.StatusBadRequest, recorder.Code)

var responseBody map[string]any
assert.NoError(t, json.Unmarshal(recorder.Body.Bytes(), &responseBody))
assert.Equal(t, map[string]any{
"message": "invalid request format",
"user": map[string]any{},
"project": map[string]any{},
"chapter": map[string]any{},
}, responseBody)
})
}
var responseBody map[string]any
assert.NoError(t, json.Unmarshal(recorder.Body.Bytes(), &responseBody))
assert.Equal(t, map[string]any{
"message": "invalid request format",
"user": map[string]any{},
"project": map[string]any{},
"chapter": map[string]any{},
}, responseBody)
}

func TestChapterUpdate(t *testing.T) {
Expand Down Expand Up @@ -967,73 +891,23 @@ func TestChapterUpdateDomainValidationError(t *testing.T) {
}

func TestChapterUpdateInvalidRequestFormat(t *testing.T) {
tt := []struct {
name string
request string
}{
{
name: "should return error when user id is not string",
request: `{"user": {"id":123}, "project": {"id": "PROJECT_WITHOUT_DESCRIPTION_TO_UPDATE_FROM_API"}, "chapter": {"id": "CHAPTER_ONE", "name": "Updated Chapter One"}}`,
},
{
name: "should return error when project id is not string",
request: `{"user": {"id": "user-id"}, "project": {"id": 123}, "chapter": {"id": "CHAPTER_ONE", "name": "Updated Chapter One"}}`,
},
{
name: "should return error when chapter id is not string",
request: `{"user": {"id": "user-id"}, "project": {"id": "PROJECT_WITHOUT_DESCRIPTION_TO_UPDATE_FROM_API"}, "chapter": {"id": 123, "name": "Updated Chapter One"}}`,
},
{
name: "should return error when chapter name is not string",
request: `{"user": {"id": "user-id"}, "project": {"id": "PROJECT_WITHOUT_DESCRIPTION_TO_UPDATE_FROM_API"}, "chapter": {"id": "CHAPTER_ONE", "name": 123}}`,
},
{
name: "should return error when chapter number is not number",
request: `{"user": {"id": "user-id"}, "project": {"id": "PROJECT_WITHOUT_DESCRIPTION_TO_UPDATE_FROM_API"}, "chapter": {"id": "CHAPTER_ONE", "name": "Updated Chapter One", "number": "1"}}`,
},
{
name: "should return error when user is not object",
request: `{"user": 123, "project": {"id": "PROJECT_WITHOUT_DESCRIPTION_TO_UPDATE_FROM_API"}, "chapter": {"id": "CHAPTER_ONE", "name": "Updated Chapter One"}}`,
},
{
name: "should return error when project is not object",
request: `{"user": {"id": "user-id"}, "project": "PROJECT_WITHOUT_DESCRIPTION_TO_UPDATE_FROM_API", "chapter": {"id": "CHAPTER_ONE", "name": "Updated Chapter One"}}`,
},
{
name: "should return error when chapter is not object",
request: `{"user": {"id": "user-id"}, "project": {"id": "PROJECT_WITHOUT_DESCRIPTION_TO_UPDATE_FROM_API"}, "chapter": "Updated Chapter One"}`,
},
{
name: "should return error when request body is invalid JSON",
request: `{"user": {"id": "user-id", "project": {"id": "PROJECT_WITHOUT_DESCRIPTION_TO_UPDATE_FROM_API"}, "chapter": {"id": "CHAPTER_ONE", "name": "Updated Chapter One"}`,
},
{
name: "should return error when request body is empty",
request: "",
},
}

for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
router := setupChapterRouter()
router := setupChapterRouter()

recorder := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/api/chapters/update", strings.NewReader(tc.request))
recorder := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/api/chapters/update", strings.NewReader(""))

router.ServeHTTP(recorder, req)
router.ServeHTTP(recorder, req)

assert.Equal(t, http.StatusBadRequest, recorder.Code)
assert.Equal(t, http.StatusBadRequest, recorder.Code)

var responseBody map[string]any
assert.NoError(t, json.Unmarshal(recorder.Body.Bytes(), &responseBody))
assert.Equal(t, map[string]any{
"message": "invalid request format",
"user": map[string]any{},
"project": map[string]any{},
"chapter": map[string]any{},
}, responseBody)
})
}
var responseBody map[string]any
assert.NoError(t, json.Unmarshal(recorder.Body.Bytes(), &responseBody))
assert.Equal(t, map[string]any{
"message": "invalid request format",
"user": map[string]any{},
"project": map[string]any{},
"chapter": map[string]any{},
}, responseBody)
}

func setupChapterRouter() *gin.Engine {
Expand Down
Loading

0 comments on commit 5f033b3

Please sign in to comment.