github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/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  							"checkSuite": {"workflowRun": {"workflow": {"name": "myworkflow"}}},
   144  							"status": "COMPLETED",
   145  							"conclusion": "SUCCESS",
   146  							"startedAt": "2020-08-31T15:44:24+02:00",
   147  							"completedAt": "2020-08-31T15:45:24+02:00",
   148  							"detailsUrl": "http://example.com/details"
   149  						},
   150  						{
   151  							"__typename": "StatusContext",
   152  							"context": "mycontext",
   153  							"state": "SUCCESS",
   154  							"createdAt": "2020-08-31T15:44:24+02:00",
   155  							"targetUrl": "http://example.com/details"
   156  						}
   157  					] } } } }
   158  				] } }
   159  			`),
   160  			outputJSON: heredoc.Doc(`
   161  				{
   162  					"statusCheckRollup": [
   163  						{
   164  							"__typename": "CheckRun",
   165  							"name": "mycheck",
   166  							"workflowName": "myworkflow",
   167  							"status": "COMPLETED",
   168  							"conclusion": "SUCCESS",
   169  							"startedAt": "2020-08-31T15:44:24+02:00",
   170  							"completedAt": "2020-08-31T15:45:24+02:00",
   171  							"detailsUrl": "http://example.com/details"
   172  						},
   173  						{
   174  							"__typename": "StatusContext",
   175  							"context": "mycontext",
   176  							"state": "SUCCESS",
   177  							"startedAt": "2020-08-31T15:44:24+02:00",
   178  							"targetUrl": "http://example.com/details"
   179  						}
   180  					]
   181  				}
   182  			`),
   183  		},
   184  	}
   185  	for _, tt := range tests {
   186  		t.Run(tt.name, func(t *testing.T) {
   187  			var pr PullRequest
   188  			dec := json.NewDecoder(strings.NewReader(tt.inputJSON))
   189  			require.NoError(t, dec.Decode(&pr))
   190  
   191  			exported := pr.ExportData(tt.fields)
   192  
   193  			buf := bytes.Buffer{}
   194  			enc := json.NewEncoder(&buf)
   195  			enc.SetIndent("", "\t")
   196  			require.NoError(t, enc.Encode(exported))
   197  
   198  			var gotData interface{}
   199  			dec = json.NewDecoder(&buf)
   200  			require.NoError(t, dec.Decode(&gotData))
   201  			var expectData interface{}
   202  			require.NoError(t, json.Unmarshal([]byte(tt.outputJSON), &expectData))
   203  
   204  			assert.Equal(t, expectData, gotData)
   205  		})
   206  	}
   207  }