github.com/cli/cli@v1.14.1-0.20210902173923-1af6a669e342/api/export_pr_test.go (about) 1 package api 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "strings" 7 "testing" 8 9 "github.com/MakeNowJust/heredoc" 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 ) 13 14 func TestIssue_ExportData(t *testing.T) { 15 tests := []struct { 16 name string 17 fields []string 18 inputJSON string 19 outputJSON string 20 }{ 21 { 22 name: "simple", 23 fields: []string{"number", "title"}, 24 inputJSON: heredoc.Doc(` 25 { "title": "Bugs hugs", "number": 2345 } 26 `), 27 outputJSON: heredoc.Doc(` 28 { 29 "number": 2345, 30 "title": "Bugs hugs" 31 } 32 `), 33 }, 34 { 35 name: "milestone", 36 fields: []string{"number", "milestone"}, 37 inputJSON: heredoc.Doc(` 38 { "number": 2345, "milestone": {"title": "The next big thing"} } 39 `), 40 outputJSON: heredoc.Doc(` 41 { 42 "milestone": { 43 "number": 0, 44 "title": "The next big thing", 45 "description": "", 46 "dueOn": null 47 }, 48 "number": 2345 49 } 50 `), 51 }, 52 { 53 name: "project cards", 54 fields: []string{"projectCards"}, 55 inputJSON: heredoc.Doc(` 56 { "projectCards": { "nodes": [ 57 { 58 "project": { "name": "Rewrite" }, 59 "column": { "name": "TO DO" } 60 } 61 ] } } 62 `), 63 outputJSON: heredoc.Doc(` 64 { 65 "projectCards": [ 66 { 67 "project": { 68 "name": "Rewrite" 69 }, 70 "column": { 71 "name": "TO DO" 72 } 73 } 74 ] 75 } 76 `), 77 }, 78 } 79 for _, tt := range tests { 80 t.Run(tt.name, func(t *testing.T) { 81 var issue Issue 82 dec := json.NewDecoder(strings.NewReader(tt.inputJSON)) 83 require.NoError(t, dec.Decode(&issue)) 84 85 exported := issue.ExportData(tt.fields) 86 87 buf := bytes.Buffer{} 88 enc := json.NewEncoder(&buf) 89 enc.SetIndent("", "\t") 90 require.NoError(t, enc.Encode(exported)) 91 assert.Equal(t, tt.outputJSON, buf.String()) 92 }) 93 } 94 } 95 96 func TestPullRequest_ExportData(t *testing.T) { 97 tests := []struct { 98 name string 99 fields []string 100 inputJSON string 101 outputJSON string 102 }{ 103 { 104 name: "simple", 105 fields: []string{"number", "title"}, 106 inputJSON: heredoc.Doc(` 107 { "title": "Bugs hugs", "number": 2345 } 108 `), 109 outputJSON: heredoc.Doc(` 110 { 111 "number": 2345, 112 "title": "Bugs hugs" 113 } 114 `), 115 }, 116 { 117 name: "milestone", 118 fields: []string{"number", "milestone"}, 119 inputJSON: heredoc.Doc(` 120 { "number": 2345, "milestone": {"title": "The next big thing"} } 121 `), 122 outputJSON: heredoc.Doc(` 123 { 124 "milestone": { 125 "number": 0, 126 "title": "The next big thing", 127 "description": "", 128 "dueOn": null 129 }, 130 "number": 2345 131 } 132 `), 133 }, 134 { 135 name: "status checks", 136 fields: []string{"statusCheckRollup"}, 137 inputJSON: heredoc.Doc(` 138 { "statusCheckRollup": { "nodes": [ 139 { "commit": { "statusCheckRollup": { "contexts": { "nodes": [ 140 { 141 "__typename": "CheckRun", 142 "name": "mycheck", 143 "status": "COMPLETED", 144 "conclusion": "SUCCESS", 145 "startedAt": "2020-08-31T15:44:24+02:00", 146 "completedAt": "2020-08-31T15:45:24+02:00", 147 "detailsUrl": "http://example.com/details" 148 } 149 ] } } } } 150 ] } } 151 `), 152 outputJSON: heredoc.Doc(` 153 { 154 "statusCheckRollup": [ 155 { 156 "__typename": "CheckRun", 157 "name": "mycheck", 158 "status": "COMPLETED", 159 "conclusion": "SUCCESS", 160 "startedAt": "2020-08-31T15:44:24+02:00", 161 "completedAt": "2020-08-31T15:45:24+02:00", 162 "detailsUrl": "http://example.com/details" 163 } 164 ] 165 } 166 `), 167 }, 168 } 169 for _, tt := range tests { 170 t.Run(tt.name, func(t *testing.T) { 171 var pr PullRequest 172 dec := json.NewDecoder(strings.NewReader(tt.inputJSON)) 173 require.NoError(t, dec.Decode(&pr)) 174 175 exported := pr.ExportData(tt.fields) 176 177 buf := bytes.Buffer{} 178 enc := json.NewEncoder(&buf) 179 enc.SetIndent("", "\t") 180 require.NoError(t, enc.Encode(exported)) 181 assert.Equal(t, tt.outputJSON, buf.String()) 182 }) 183 } 184 }