Skip to content

Commit

Permalink
Merge pull request #840 from traPtitech/test-cmp2
Browse files Browse the repository at this point in the history
  • Loading branch information
H1rono authored Dec 31, 2024
2 parents a637f70 + 0ed1a52 commit c51d498
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 77 deletions.
4 changes: 2 additions & 2 deletions model/group_impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestEntRepository_GetGroups(t *testing.T) {
t.Parallel()
groups, err := repo2.GetGroups(ctx)
require.NoError(t, err)
assert.Equal(t, 0, len(groups))
assert.Empty(t, groups)
})
}

Expand Down Expand Up @@ -317,7 +317,7 @@ func TestEntRepository_GetMembers(t *testing.T) {

got, err := repo2.GetMembers(ctx, group.ID)
assert.NoError(t, err)
assert.Equal(t, got, []*Member{})
assert.Empty(t, got)
})
}

Expand Down
9 changes: 5 additions & 4 deletions router/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/traPtitech/Jomon/ent"
"github.com/traPtitech/Jomon/model"
"github.com/traPtitech/Jomon/testutil"
"go.uber.org/mock/gomock"
)

Expand Down Expand Up @@ -49,11 +50,11 @@ func TestHandler_GetAdmins(t *testing.T) {
require.NoError(t, err)

assert.NoError(t, h.Handlers.GetAdmins(c))
assert.Equal(t, http.StatusOK, rec.Code)
testutil.AssertEqual(t, http.StatusOK, rec.Code)
var res []uuid.UUID
err = json.Unmarshal(rec.Body.Bytes(), &res)
require.NoError(t, err)
assert.Equal(t, []uuid.UUID{admin.ID}, res)
testutil.RequireEqual(t, []uuid.UUID{admin.ID}, res)
})

t.Run("Success2", func(t *testing.T) {
Expand All @@ -78,11 +79,11 @@ func TestHandler_GetAdmins(t *testing.T) {
require.NoError(t, err)

assert.NoError(t, h.Handlers.GetAdmins(c))
assert.Equal(t, http.StatusOK, rec.Code)
testutil.AssertEqual(t, http.StatusOK, rec.Code)
var res []uuid.UUID
err = json.Unmarshal(rec.Body.Bytes(), &res)
require.NoError(t, err)
assert.Equal(t, []uuid.UUID{}, res)
assert.Empty(t, res)
})

t.Run("FailedWithError", func(t *testing.T) {
Expand Down
62 changes: 1 addition & 61 deletions router/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3655,64 +3655,4 @@ func TestHandlers_PutStatus(t *testing.T) {
})
}

// func TestHandlers_PostComment(t *testing.T) {
// t.Parallel()

// t.Run("Success", func(t *testing.T) {
// t.Parallel()
// ctrl := gomock.NewController(t)

// date := time.Now()

// comment := &model.Comment{
// ID: uuid.New(),
// User: uuid.New(),
// Comment: random.AlphaNumeric(t, 20),
// CreatedAt: date,
// UpdatedAt: date,
// }

// requestID := uuid.New()
// reqComment := Comment{
// Comment: comment.Comment,
// }
// reqBody, err := json.Marshal(reqComment)
// require.NoError(t, err)

// e := echo.New()
// req, err := http.NewRequest(
// http.MethodPost,
// fmt.Sprintf("/api/requests/%s/comments", requestID),
// bytes.NewReader(reqBody))
// assert.NoError(t, err)
// req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
// rec := httptest.NewRecorder()
// c := e.NewContext(req, rec)
// c.SetPath("api/requests/:requestID/comments")
// c.SetParamNames("requestID")
// c.SetParamValues(requestID.String())

// h, err := NewTestHandlers(t, ctrl)
// assert.NoError(t, err)

// h.Repository.MockCommentRepository.
// EXPECT().
// CreateComment(c.Request().Context(), comment.Comment, requestID, comment.User).
// Return(comment, nil)

// res := &CommentDetail{
// ID: comment.ID,
// User: comment.User,
// Comment: comment.Comment,
// CreatedAt: comment.CreatedAt,
// UpdatedAt: comment.UpdatedAt,
// }
// resBody, err := json.Marshal(res)
// require.NoError(t, err)

// if assert.NoError(t, h.Handlers.PostComment(c)) {
// assert.Equal(t, http.StatusOK, rec.Code)
// assert.Equal(t, string(resBody), strings.TrimRight(rec.Body.String(), "\n"))
// }
// })
// }
// TODO: TestHandlers_PostComment
39 changes: 29 additions & 10 deletions router/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -543,14 +542,24 @@ func TestHandlers_PostTransaction(t *testing.T) {
group := tx1.Group.ID

e := echo.New()
// FIXME: json.Marshalを使う
reqBody := fmt.Sprintf(
`{"title": "%s", "amount": %d, "targets": ["%s"], "tags": ["%s"], "group": "%s"}`,
tx1.Title, tx1.Amount, tx1.Target, tag.ID, group)
reqBody, err := json.Marshal(&struct {
Title string `json:"title"`
Amount int `json:"amount"`
Targets []string `json:"targets"`
Tags []uuid.UUID `json:"tags"`
Group *uuid.UUID `json:"group"`
}{
Title: tx1.Title,
Amount: tx1.Amount,
Targets: []string{tx1.Target},
Tags: []uuid.UUID{tag.ID},
Group: &group,
})
require.NoError(t, err)
req, err := http.NewRequest(
http.MethodPost,
"/api/transactions",
strings.NewReader(reqBody))
bytes.NewReader(reqBody))
require.NoError(t, err)
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
Expand Down Expand Up @@ -788,6 +797,7 @@ func TestHandlers_PutTransaction(t *testing.T) {

updated := &model.TransactionResponse{
ID: tx.ID,
Title: random.AlphaNumeric(t, 20),
Amount: random.Numeric(t, 1000000),
Target: random.AlphaNumeric(t, 20),
Tags: []*model.Tag{
Expand All @@ -810,13 +820,22 @@ func TestHandlers_PutTransaction(t *testing.T) {
})

e := echo.New()
reqBody := fmt.Sprintf(
`{"amount": %d, "target": "%s", "tags": ["%s"]}`,
updated.Amount, updated.Target, updatedTag.ID)
reqBody, err := json.Marshal(&struct {
Title string `json:"title"`
Amount int `json:"amount"`
Target string `json:"target"`
Tags []uuid.UUID `json:"tags"`
}{
Title: updated.Title,
Amount: updated.Amount,
Target: updated.Target,
Tags: []uuid.UUID{updatedTag.ID},
})
require.NoError(t, err)
req, err := http.NewRequest(
http.MethodPut,
fmt.Sprintf("/api/transactions/%s", tx.ID),
strings.NewReader(reqBody))
bytes.NewReader(reqBody))
require.NoError(t, err)
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
Expand Down

0 comments on commit c51d498

Please sign in to comment.