github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/pkg/search/result_test.go (about) 1 package search 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "strings" 7 "testing" 8 "time" 9 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 ) 13 14 func TestRepositoryExportData(t *testing.T) { 15 var createdAt = time.Date(2021, 2, 28, 12, 30, 0, 0, time.UTC) 16 tests := []struct { 17 name string 18 fields []string 19 repo Repository 20 output string 21 }{ 22 { 23 name: "exports requested fields", 24 fields: []string{"createdAt", "description", "fullName", "isArchived", "isFork", "isPrivate", "pushedAt"}, 25 repo: Repository{ 26 CreatedAt: createdAt, 27 Description: "description", 28 FullName: "ungtb10d/cli", 29 IsArchived: true, 30 IsFork: false, 31 IsPrivate: false, 32 PushedAt: createdAt, 33 }, 34 output: `{"createdAt":"2021-02-28T12:30:00Z","description":"description","fullName":"ungtb10d/cli","isArchived":true,"isFork":false,"isPrivate":false,"pushedAt":"2021-02-28T12:30:00Z"}`, 35 }, 36 } 37 for _, tt := range tests { 38 t.Run(tt.name, func(t *testing.T) { 39 exported := tt.repo.ExportData(tt.fields) 40 buf := bytes.Buffer{} 41 enc := json.NewEncoder(&buf) 42 require.NoError(t, enc.Encode(exported)) 43 assert.Equal(t, tt.output, strings.TrimSpace(buf.String())) 44 }) 45 } 46 } 47 48 func TestIssueExportData(t *testing.T) { 49 var updatedAt = time.Date(2021, 2, 28, 12, 30, 0, 0, time.UTC) 50 tests := []struct { 51 name string 52 fields []string 53 issue Issue 54 output string 55 }{ 56 { 57 name: "exports requested fields", 58 fields: []string{"assignees", "body", "commentsCount", "labels", "isLocked", "repository", "title", "updatedAt"}, 59 issue: Issue{ 60 Assignees: []User{{Login: "test"}}, 61 Body: "body", 62 CommentsCount: 1, 63 Labels: []Label{{Name: "label1"}, {Name: "label2"}}, 64 IsLocked: true, 65 RepositoryURL: "https://github.com/owner/repo", 66 Title: "title", 67 UpdatedAt: updatedAt, 68 }, 69 output: `{"assignees":[{"id":"","login":"test","type":""}],"body":"body","commentsCount":1,"isLocked":true,"labels":[{"color":"","description":"","id":"","name":"label1"},{"color":"","description":"","id":"","name":"label2"}],"repository":{"name":"repo","nameWithOwner":"owner/repo"},"title":"title","updatedAt":"2021-02-28T12:30:00Z"}`, 70 }, 71 { 72 name: "state when issue", 73 fields: []string{"isPullRequest", "state"}, 74 issue: Issue{ 75 StateInternal: "closed", 76 }, 77 output: `{"isPullRequest":false,"state":"closed"}`, 78 }, 79 { 80 name: "state when pull request", 81 fields: []string{"isPullRequest", "state"}, 82 issue: Issue{ 83 PullRequest: PullRequest{ 84 MergedAt: time.Now(), 85 URL: "a-url", 86 }, 87 StateInternal: "closed", 88 }, 89 output: `{"isPullRequest":true,"state":"merged"}`, 90 }, 91 } 92 for _, tt := range tests { 93 t.Run(tt.name, func(t *testing.T) { 94 exported := tt.issue.ExportData(tt.fields) 95 buf := bytes.Buffer{} 96 enc := json.NewEncoder(&buf) 97 require.NoError(t, enc.Encode(exported)) 98 assert.Equal(t, tt.output, strings.TrimSpace(buf.String())) 99 }) 100 } 101 }