github.com/google/go-github/v33@v33.0.0/github/issues_milestones_test.go (about)

     1  // Copyright 2014 The go-github AUTHORS. All rights reserved.
     2  //
     3  // Use of this source code is governed by a BSD-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package github
     7  
     8  import (
     9  	"context"
    10  	"encoding/json"
    11  	"fmt"
    12  	"net/http"
    13  	"reflect"
    14  	"testing"
    15  )
    16  
    17  func TestIssuesService_ListMilestones(t *testing.T) {
    18  	client, mux, _, teardown := setup()
    19  	defer teardown()
    20  
    21  	mux.HandleFunc("/repos/o/r/milestones", func(w http.ResponseWriter, r *http.Request) {
    22  		testMethod(t, r, "GET")
    23  		testFormValues(t, r, values{
    24  			"state":     "closed",
    25  			"sort":      "due_date",
    26  			"direction": "asc",
    27  			"page":      "2",
    28  		})
    29  		fmt.Fprint(w, `[{"number":1}]`)
    30  	})
    31  
    32  	opt := &MilestoneListOptions{"closed", "due_date", "asc", ListOptions{Page: 2}}
    33  	milestones, _, err := client.Issues.ListMilestones(context.Background(), "o", "r", opt)
    34  	if err != nil {
    35  		t.Errorf("IssuesService.ListMilestones returned error: %v", err)
    36  	}
    37  
    38  	want := []*Milestone{{Number: Int(1)}}
    39  	if !reflect.DeepEqual(milestones, want) {
    40  		t.Errorf("IssuesService.ListMilestones returned %+v, want %+v", milestones, want)
    41  	}
    42  }
    43  
    44  func TestIssuesService_ListMilestones_invalidOwner(t *testing.T) {
    45  	client, _, _, teardown := setup()
    46  	defer teardown()
    47  
    48  	_, _, err := client.Issues.ListMilestones(context.Background(), "%", "r", nil)
    49  	testURLParseError(t, err)
    50  }
    51  
    52  func TestIssuesService_GetMilestone(t *testing.T) {
    53  	client, mux, _, teardown := setup()
    54  	defer teardown()
    55  
    56  	mux.HandleFunc("/repos/o/r/milestones/1", func(w http.ResponseWriter, r *http.Request) {
    57  		testMethod(t, r, "GET")
    58  		fmt.Fprint(w, `{"number":1}`)
    59  	})
    60  
    61  	milestone, _, err := client.Issues.GetMilestone(context.Background(), "o", "r", 1)
    62  	if err != nil {
    63  		t.Errorf("IssuesService.GetMilestone returned error: %v", err)
    64  	}
    65  
    66  	want := &Milestone{Number: Int(1)}
    67  	if !reflect.DeepEqual(milestone, want) {
    68  		t.Errorf("IssuesService.GetMilestone returned %+v, want %+v", milestone, want)
    69  	}
    70  }
    71  
    72  func TestIssuesService_GetMilestone_invalidOwner(t *testing.T) {
    73  	client, _, _, teardown := setup()
    74  	defer teardown()
    75  
    76  	_, _, err := client.Issues.GetMilestone(context.Background(), "%", "r", 1)
    77  	testURLParseError(t, err)
    78  }
    79  
    80  func TestIssuesService_CreateMilestone(t *testing.T) {
    81  	client, mux, _, teardown := setup()
    82  	defer teardown()
    83  
    84  	input := &Milestone{Title: String("t")}
    85  
    86  	mux.HandleFunc("/repos/o/r/milestones", func(w http.ResponseWriter, r *http.Request) {
    87  		v := new(Milestone)
    88  		json.NewDecoder(r.Body).Decode(v)
    89  
    90  		testMethod(t, r, "POST")
    91  		if !reflect.DeepEqual(v, input) {
    92  			t.Errorf("Request body = %+v, want %+v", v, input)
    93  		}
    94  
    95  		fmt.Fprint(w, `{"number":1}`)
    96  	})
    97  
    98  	milestone, _, err := client.Issues.CreateMilestone(context.Background(), "o", "r", input)
    99  	if err != nil {
   100  		t.Errorf("IssuesService.CreateMilestone returned error: %v", err)
   101  	}
   102  
   103  	want := &Milestone{Number: Int(1)}
   104  	if !reflect.DeepEqual(milestone, want) {
   105  		t.Errorf("IssuesService.CreateMilestone returned %+v, want %+v", milestone, want)
   106  	}
   107  }
   108  
   109  func TestIssuesService_CreateMilestone_invalidOwner(t *testing.T) {
   110  	client, _, _, teardown := setup()
   111  	defer teardown()
   112  
   113  	_, _, err := client.Issues.CreateMilestone(context.Background(), "%", "r", nil)
   114  	testURLParseError(t, err)
   115  }
   116  
   117  func TestIssuesService_EditMilestone(t *testing.T) {
   118  	client, mux, _, teardown := setup()
   119  	defer teardown()
   120  
   121  	input := &Milestone{Title: String("t")}
   122  
   123  	mux.HandleFunc("/repos/o/r/milestones/1", func(w http.ResponseWriter, r *http.Request) {
   124  		v := new(Milestone)
   125  		json.NewDecoder(r.Body).Decode(v)
   126  
   127  		testMethod(t, r, "PATCH")
   128  		if !reflect.DeepEqual(v, input) {
   129  			t.Errorf("Request body = %+v, want %+v", v, input)
   130  		}
   131  
   132  		fmt.Fprint(w, `{"number":1}`)
   133  	})
   134  
   135  	milestone, _, err := client.Issues.EditMilestone(context.Background(), "o", "r", 1, input)
   136  	if err != nil {
   137  		t.Errorf("IssuesService.EditMilestone returned error: %v", err)
   138  	}
   139  
   140  	want := &Milestone{Number: Int(1)}
   141  	if !reflect.DeepEqual(milestone, want) {
   142  		t.Errorf("IssuesService.EditMilestone returned %+v, want %+v", milestone, want)
   143  	}
   144  }
   145  
   146  func TestIssuesService_EditMilestone_invalidOwner(t *testing.T) {
   147  	client, _, _, teardown := setup()
   148  	defer teardown()
   149  
   150  	_, _, err := client.Issues.EditMilestone(context.Background(), "%", "r", 1, nil)
   151  	testURLParseError(t, err)
   152  }
   153  
   154  func TestIssuesService_DeleteMilestone(t *testing.T) {
   155  	client, mux, _, teardown := setup()
   156  	defer teardown()
   157  
   158  	mux.HandleFunc("/repos/o/r/milestones/1", func(w http.ResponseWriter, r *http.Request) {
   159  		testMethod(t, r, "DELETE")
   160  	})
   161  
   162  	_, err := client.Issues.DeleteMilestone(context.Background(), "o", "r", 1)
   163  	if err != nil {
   164  		t.Errorf("IssuesService.DeleteMilestone returned error: %v", err)
   165  	}
   166  }
   167  
   168  func TestIssuesService_DeleteMilestone_invalidOwner(t *testing.T) {
   169  	client, _, _, teardown := setup()
   170  	defer teardown()
   171  
   172  	_, err := client.Issues.DeleteMilestone(context.Background(), "%", "r", 1)
   173  	testURLParseError(t, err)
   174  }