|
| 1 | +package news_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/leetcode-golang-classroom/golang-rest-api-sample/interanl/services/news" |
| 7 | +) |
| 8 | + |
| 9 | +func TestNewsPostBody_Validate(t *testing.T) { |
| 10 | + testCases := []struct { |
| 11 | + name string |
| 12 | + req news.NewsPostReqBody |
| 13 | + expectedErr bool |
| 14 | + }{ |
| 15 | + { |
| 16 | + name: "author empty", |
| 17 | + req: news.NewsPostReqBody{}, |
| 18 | + expectedErr: true, |
| 19 | + }, |
| 20 | + { |
| 21 | + name: "title empty", |
| 22 | + req: news.NewsPostReqBody{ |
| 23 | + Author: "test-author", |
| 24 | + }, |
| 25 | + expectedErr: true, |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "summary invalid", |
| 29 | + req: news.NewsPostReqBody{ |
| 30 | + Author: "test-author", |
| 31 | + Title: "test-title", |
| 32 | + }, |
| 33 | + expectedErr: true, |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "time invalid", |
| 37 | + req: news.NewsPostReqBody{ |
| 38 | + Author: "test-author", |
| 39 | + Title: "test-title", |
| 40 | + Summary: "test-summary", |
| 41 | + CreatedAt: "Invalid", |
| 42 | + }, |
| 43 | + expectedErr: true, |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "source invalid", |
| 47 | + req: news.NewsPostReqBody{ |
| 48 | + Author: "test-author", |
| 49 | + Title: "test-title", |
| 50 | + Summary: "test-summary", |
| 51 | + CreatedAt: "2024-04-07T05:13:27+00:00", |
| 52 | + }, |
| 53 | + expectedErr: true, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "tags is empty", |
| 57 | + req: news.NewsPostReqBody{ |
| 58 | + Author: "test-author", |
| 59 | + Title: "test-title", |
| 60 | + Summary: "test-summary", |
| 61 | + CreatedAt: "2024-04-07T05:13:27+00:00", |
| 62 | + Source: "https://tests-news.com", |
| 63 | + }, |
| 64 | + expectedErr: true, |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "validate", |
| 68 | + req: news.NewsPostReqBody{ |
| 69 | + Author: "test-author", |
| 70 | + Title: "test-title", |
| 71 | + Summary: "test-summary", |
| 72 | + CreatedAt: "2024-04-07T05:13:27+00:00", |
| 73 | + Source: "https://tests-news.com", |
| 74 | + Tags: []string{"test-tag"}, |
| 75 | + }, |
| 76 | + }, |
| 77 | + } |
| 78 | + for _, tc := range testCases { |
| 79 | + t.Run(tc.name, func(t *testing.T) { |
| 80 | + err := tc.req.Validate() |
| 81 | + if tc.expectedErr && err == nil { |
| 82 | + t.Fatal("expected error but got nil") |
| 83 | + } |
| 84 | + if !tc.expectedErr && err != nil { |
| 85 | + t.Fatal("expected nil but got error") |
| 86 | + } |
| 87 | + }) |
| 88 | + } |
| 89 | +} |
0 commit comments