|
| 1 | +package usecase_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/Doer-org/glyph/internal/domain/entity" |
| 10 | + "github.com/Doer-org/glyph/internal/domain/mock_repository" |
| 11 | + "github.com/Doer-org/glyph/internal/usecase" |
| 12 | + "github.com/golang/mock/gomock" |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | +) |
| 15 | + |
| 16 | +func Test_CreateComment(t *testing.T) { |
| 17 | + t.Skip() |
| 18 | + tests := []struct { |
| 19 | + name string |
| 20 | + comment *entity.Comment |
| 21 | + prepareMock func(m *mock_repository.MockICommentRepository) |
| 22 | + expectedResult *entity.Comment |
| 23 | + expectedError string |
| 24 | + }{ |
| 25 | + { |
| 26 | + name: "正常にコメントが作成される場合", |
| 27 | + comment: &entity.Comment{ |
| 28 | + Glyph_id: "glyph_id", |
| 29 | + User_id: "user_id", |
| 30 | + Contents: "Test comment", |
| 31 | + Created_at: time.Time{}, |
| 32 | + }, |
| 33 | + prepareMock: func(m *mock_repository.MockICommentRepository) { |
| 34 | + m.EXPECT().CreateComment(gomock.Any(), &entity.Comment{ |
| 35 | + Glyph_id: "glyph_id", |
| 36 | + User_id: "user_id", |
| 37 | + Contents: "Test comment", |
| 38 | + Created_at: time.Now(), |
| 39 | + }).Return(&entity.Comment{ |
| 40 | + Id: "comment_id", |
| 41 | + Glyph_id: "glyph_id", |
| 42 | + User_id: "user_id", |
| 43 | + Contents: "Test comment", |
| 44 | + Created_at: time.Now(), |
| 45 | + }, nil) |
| 46 | + }, |
| 47 | + expectedResult: &entity.Comment{ |
| 48 | + Id: "comment_id", |
| 49 | + Glyph_id: "glyph_id", |
| 50 | + User_id: "user_id", |
| 51 | + Contents: "Test comment", |
| 52 | + Created_at: time.Now(), |
| 53 | + }, |
| 54 | + expectedError: "", |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "コメントに不備がある場合、エラーが返される", |
| 58 | + comment: &entity.Comment{ |
| 59 | + Glyph_id: "", |
| 60 | + User_id: "user_id", |
| 61 | + Contents: "Test comment", |
| 62 | + Created_at: time.Time{}, |
| 63 | + }, |
| 64 | + prepareMock: func(m *mock_repository.MockICommentRepository) {}, |
| 65 | + expectedResult: nil, |
| 66 | + expectedError: "invalid comment", |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "タイムゾーンを取得できない場合、エラーが返される", |
| 70 | + comment: &entity.Comment{ |
| 71 | + Glyph_id: "glyph_id", |
| 72 | + User_id: "user_id", |
| 73 | + Contents: "Test comment", |
| 74 | + Created_at: time.Time{}, |
| 75 | + }, |
| 76 | + prepareMock: func(m *mock_repository.MockICommentRepository) {}, |
| 77 | + expectedResult: nil, |
| 78 | + expectedError: "can't get time", |
| 79 | + }, |
| 80 | + { |
| 81 | + name: "コメント作成時にエラーが発生する場合、エラーが返される", |
| 82 | + comment: &entity.Comment{ |
| 83 | + Glyph_id: "glyph_id", |
| 84 | + User_id: "user_id", |
| 85 | + Contents: "Test comment", |
| 86 | + Created_at: time.Time{}, |
| 87 | + }, |
| 88 | + prepareMock: func(m *mock_repository.MockICommentRepository) { |
| 89 | + m.EXPECT().CreateComment(gomock.Any(), &entity.Comment{ |
| 90 | + Glyph_id: "glyph_id", |
| 91 | + User_id: "user_id", |
| 92 | + Contents: "Test comment", |
| 93 | + Created_at: time.Time{}, |
| 94 | + }).Return(nil, errors.New("error creating comment")) |
| 95 | + }, |
| 96 | + expectedResult: nil, |
| 97 | + expectedError: "error creating comment", |
| 98 | + }, |
| 99 | + } |
| 100 | + |
| 101 | + for _, tt := range tests { |
| 102 | + t.Run(tt.name, func(t *testing.T) { |
| 103 | + ctrl := gomock.NewController(t) |
| 104 | + defer ctrl.Finish() |
| 105 | + |
| 106 | + mockRepo := mock_repository.NewMockICommentRepository(ctrl) |
| 107 | + tt.prepareMock(mockRepo) |
| 108 | + |
| 109 | + uc := usecase.NewCommentUsecase(mockRepo) |
| 110 | + result, err := uc.CreateComment(context.Background(), tt.comment) |
| 111 | + |
| 112 | + if tt.expectedError != "" { |
| 113 | + assert.EqualError(t, err, tt.expectedError) |
| 114 | + } else { |
| 115 | + assert.NoError(t, err) |
| 116 | + } |
| 117 | + |
| 118 | + assert.Equal(t, tt.expectedResult, result) |
| 119 | + }) |
| 120 | + } |
| 121 | +} |
0 commit comments