github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/velodrome/fetcher/client_test.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/google/go-github/github"
    24  )
    25  
    26  type FakeClient struct {
    27  	Repository    string
    28  	Issues        []*github.Issue
    29  	IssueEvents   map[int][]*github.IssueEvent
    30  	IssueComments map[int][]*github.IssueComment
    31  	PullComments  map[int][]*github.PullRequestComment
    32  }
    33  
    34  func (client FakeClient) RepositoryName() string {
    35  	return client.Repository
    36  }
    37  
    38  func (client FakeClient) FetchIssues(latest time.Time, c chan *github.Issue) {
    39  	for _, issue := range client.Issues {
    40  		c <- issue
    41  	}
    42  	close(c)
    43  }
    44  
    45  func (client FakeClient) FetchIssueEvents(issueID int, latest *int, c chan *github.IssueEvent) {
    46  	for _, event := range client.IssueEvents[issueID] {
    47  		c <- event
    48  	}
    49  	close(c)
    50  }
    51  
    52  func (client FakeClient) FetchIssueComments(issueID int, latest time.Time, c chan *github.IssueComment) {
    53  	for _, comment := range client.IssueComments[issueID] {
    54  		c <- comment
    55  	}
    56  	close(c)
    57  }
    58  
    59  func (client FakeClient) FetchPullComments(issueID int, latest time.Time, c chan *github.PullRequestComment) {
    60  	for _, comment := range client.PullComments[issueID] {
    61  		c <- comment
    62  	}
    63  	close(c)
    64  }
    65  
    66  func createIssueEvent(ID int) *github.IssueEvent {
    67  	return &github.IssueEvent{ID: &ID}
    68  }
    69  
    70  func TestHasID(t *testing.T) {
    71  	tests := []struct {
    72  		events []*github.IssueEvent
    73  		ID     int
    74  		isIn   bool
    75  	}{
    76  		{
    77  			[]*github.IssueEvent{},
    78  			1,
    79  			false,
    80  		},
    81  		{
    82  			[]*github.IssueEvent{
    83  				createIssueEvent(1),
    84  			},
    85  			1,
    86  			true,
    87  		},
    88  		{
    89  			[]*github.IssueEvent{
    90  				createIssueEvent(0),
    91  				createIssueEvent(2),
    92  			},
    93  			1,
    94  			false,
    95  		},
    96  		{
    97  			[]*github.IssueEvent{
    98  				createIssueEvent(2),
    99  				createIssueEvent(3),
   100  				createIssueEvent(1),
   101  			},
   102  			1,
   103  			true,
   104  		},
   105  	}
   106  
   107  	for _, test := range tests {
   108  		found := hasID(test.events, test.ID)
   109  		if found != test.isIn {
   110  			if found {
   111  				t.Error(test.ID, "was found in", test.events, "but shouldn't")
   112  			} else {
   113  				t.Error(test.ID, "wasn't found in", test.events)
   114  			}
   115  		}
   116  	}
   117  }