github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/pkg/cmd/pr/shared/params_test.go (about)

     1  package shared
     2  
     3  import (
     4  	"net/http"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/ungtb10d/cli/v2/api"
     9  	"github.com/ungtb10d/cli/v2/internal/ghrepo"
    10  	"github.com/ungtb10d/cli/v2/pkg/httpmock"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func Test_listURLWithQuery(t *testing.T) {
    15  	trueBool := true
    16  	falseBool := false
    17  
    18  	type args struct {
    19  		listURL string
    20  		options FilterOptions
    21  	}
    22  
    23  	tests := []struct {
    24  		name    string
    25  		args    args
    26  		want    string
    27  		wantErr bool
    28  	}{
    29  		{
    30  			name: "blank",
    31  			args: args{
    32  				listURL: "https://example.com/path?a=b",
    33  				options: FilterOptions{
    34  					Entity: "issue",
    35  					State:  "open",
    36  				},
    37  			},
    38  			want:    "https://example.com/path?a=b&q=state%3Aopen+type%3Aissue",
    39  			wantErr: false,
    40  		},
    41  		{
    42  			name: "draft",
    43  			args: args{
    44  				listURL: "https://example.com/path",
    45  				options: FilterOptions{
    46  					Entity: "pr",
    47  					State:  "open",
    48  					Draft:  &trueBool,
    49  				},
    50  			},
    51  			want:    "https://example.com/path?q=draft%3Atrue+state%3Aopen+type%3Apr",
    52  			wantErr: false,
    53  		},
    54  		{
    55  			name: "non-draft",
    56  			args: args{
    57  				listURL: "https://example.com/path",
    58  				options: FilterOptions{
    59  					Entity: "pr",
    60  					State:  "open",
    61  					Draft:  &falseBool,
    62  				},
    63  			},
    64  			want:    "https://example.com/path?q=draft%3Afalse+state%3Aopen+type%3Apr",
    65  			wantErr: false,
    66  		},
    67  		{
    68  			name: "all",
    69  			args: args{
    70  				listURL: "https://example.com/path",
    71  				options: FilterOptions{
    72  					Entity:     "issue",
    73  					State:      "open",
    74  					Assignee:   "bo",
    75  					Author:     "ka",
    76  					BaseBranch: "trunk",
    77  					HeadBranch: "bug-fix",
    78  					Mention:    "nu",
    79  				},
    80  			},
    81  			want:    "https://example.com/path?q=assignee%3Abo+author%3Aka+base%3Atrunk+head%3Abug-fix+mentions%3Anu+state%3Aopen+type%3Aissue",
    82  			wantErr: false,
    83  		},
    84  		{
    85  			name: "spaces in values",
    86  			args: args{
    87  				listURL: "https://example.com/path",
    88  				options: FilterOptions{
    89  					Entity:    "pr",
    90  					State:     "open",
    91  					Labels:    []string{"docs", "help wanted"},
    92  					Milestone: `Codename "What Was Missing"`,
    93  				},
    94  			},
    95  			want:    "https://example.com/path?q=label%3A%22help+wanted%22+label%3Adocs+milestone%3A%22Codename+%5C%22What+Was+Missing%5C%22%22+state%3Aopen+type%3Apr",
    96  			wantErr: false,
    97  		},
    98  	}
    99  	for _, tt := range tests {
   100  		t.Run(tt.name, func(t *testing.T) {
   101  			got, err := ListURLWithQuery(tt.args.listURL, tt.args.options)
   102  			if (err != nil) != tt.wantErr {
   103  				t.Errorf("listURLWithQuery() error = %v, wantErr %v", err, tt.wantErr)
   104  				return
   105  			}
   106  			if got != tt.want {
   107  				t.Errorf("listURLWithQuery() = %v, want %v", got, tt.want)
   108  			}
   109  		})
   110  	}
   111  }
   112  
   113  func TestMeReplacer_Replace(t *testing.T) {
   114  	rtSuccess := &httpmock.Registry{}
   115  	rtSuccess.Register(
   116  		httpmock.GraphQL(`query UserCurrent\b`),
   117  		httpmock.StringResponse(`
   118  		{ "data": {
   119  			"viewer": { "login": "ResolvedLogin" }
   120  		} }
   121  		`),
   122  	)
   123  
   124  	rtFailure := &httpmock.Registry{}
   125  	rtFailure.Register(
   126  		httpmock.GraphQL(`query UserCurrent\b`),
   127  		httpmock.StatusStringResponse(500, `
   128  		{ "data": {
   129  			"viewer": { }
   130  		} }
   131  		`),
   132  	)
   133  
   134  	type args struct {
   135  		logins []string
   136  		client *api.Client
   137  		repo   ghrepo.Interface
   138  	}
   139  	tests := []struct {
   140  		name    string
   141  		args    args
   142  		verify  func(t httpmock.Testing)
   143  		want    []string
   144  		wantErr bool
   145  	}{
   146  		{
   147  			name: "succeeds resolving the userlogin",
   148  			args: args{
   149  				client: api.NewClientFromHTTP(&http.Client{Transport: rtSuccess}),
   150  				repo:   ghrepo.New("OWNER", "REPO"),
   151  				logins: []string{"some", "@me", "other"},
   152  			},
   153  			verify: rtSuccess.Verify,
   154  			want:   []string{"some", "ResolvedLogin", "other"},
   155  		},
   156  		{
   157  			name: "fails resolving the userlogin",
   158  			args: args{
   159  				client: api.NewClientFromHTTP(&http.Client{Transport: rtFailure}),
   160  				repo:   ghrepo.New("OWNER", "REPO"),
   161  				logins: []string{"some", "@me", "other"},
   162  			},
   163  			verify:  rtFailure.Verify,
   164  			want:    []string(nil),
   165  			wantErr: true,
   166  		},
   167  	}
   168  	for _, tt := range tests {
   169  		t.Run(tt.name, func(t *testing.T) {
   170  			me := NewMeReplacer(tt.args.client, tt.args.repo.RepoHost())
   171  			got, err := me.ReplaceSlice(tt.args.logins)
   172  			if (err != nil) != tt.wantErr {
   173  				t.Errorf("ReplaceAtMeLogin() error = %v, wantErr %v", err, tt.wantErr)
   174  				return
   175  			}
   176  			if !reflect.DeepEqual(got, tt.want) {
   177  				t.Errorf("ReplaceAtMeLogin() = %v, want %v", got, tt.want)
   178  			}
   179  
   180  			if tt.verify != nil {
   181  				tt.verify(t)
   182  			}
   183  		})
   184  	}
   185  }
   186  
   187  func Test_QueryHasStateClause(t *testing.T) {
   188  	tests := []struct {
   189  		searchQuery string
   190  		hasState    bool
   191  	}{
   192  		{
   193  			searchQuery: "is:closed is:merged",
   194  			hasState:    true,
   195  		},
   196  		{
   197  			searchQuery: "author:mislav",
   198  			hasState:    false,
   199  		},
   200  		{
   201  			searchQuery: "assignee:g14a mentions:vilmibm",
   202  			hasState:    false,
   203  		},
   204  		{
   205  			searchQuery: "merged:>2021-05-20",
   206  			hasState:    true,
   207  		},
   208  		{
   209  			searchQuery: "state:merged state:open",
   210  			hasState:    true,
   211  		},
   212  		{
   213  			searchQuery: "assignee:g14a is:closed",
   214  			hasState:    true,
   215  		},
   216  		{
   217  			searchQuery: "state:closed label:bug",
   218  			hasState:    true,
   219  		},
   220  	}
   221  	for _, tt := range tests {
   222  		gotState := QueryHasStateClause(tt.searchQuery)
   223  		assert.Equal(t, tt.hasState, gotState)
   224  	}
   225  }
   226  
   227  func Test_WithPrAndIssueQueryParams(t *testing.T) {
   228  	type args struct {
   229  		baseURL string
   230  		state   IssueMetadataState
   231  	}
   232  	tests := []struct {
   233  		name    string
   234  		args    args
   235  		want    string
   236  		wantErr bool
   237  	}{
   238  		{
   239  			name: "blank",
   240  			args: args{
   241  				baseURL: "",
   242  				state:   IssueMetadataState{},
   243  			},
   244  			want: "?body=",
   245  		},
   246  		{
   247  			name: "no values",
   248  			args: args{
   249  				baseURL: "http://example.com/hey",
   250  				state:   IssueMetadataState{},
   251  			},
   252  			want: "http://example.com/hey?body=",
   253  		},
   254  		{
   255  			name: "title and body",
   256  			args: args{
   257  				baseURL: "http://example.com/hey",
   258  				state: IssueMetadataState{
   259  					Title: "my title",
   260  					Body:  "my bodeh",
   261  				},
   262  			},
   263  			want: "http://example.com/hey?body=my+bodeh&title=my+title",
   264  		},
   265  	}
   266  	for _, tt := range tests {
   267  		t.Run(tt.name, func(t *testing.T) {
   268  			got, err := WithPrAndIssueQueryParams(nil, nil, tt.args.baseURL, tt.args.state)
   269  			if (err != nil) != tt.wantErr {
   270  				t.Errorf("WithPrAndIssueQueryParams() error = %v, wantErr %v", err, tt.wantErr)
   271  				return
   272  			}
   273  			if got != tt.want {
   274  				t.Errorf("WithPrAndIssueQueryParams() = %v, want %v", got, tt.want)
   275  			}
   276  		})
   277  	}
   278  }