github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/velodrome/fetcher/comments_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  	"reflect"
    21  	"testing"
    22  	"time"
    23  
    24  	"k8s.io/test-infra/velodrome/sql"
    25  	sqltest "k8s.io/test-infra/velodrome/sql/testing"
    26  
    27  	"github.com/google/go-github/github"
    28  )
    29  
    30  func TestFindLatestCommentUpdate(t *testing.T) {
    31  	config := sqltest.SQLiteConfig{File: ":memory:"}
    32  	tests := []struct {
    33  		comments       []sql.Comment
    34  		issueID        int
    35  		expectedLatest time.Time
    36  		repository     string
    37  	}{
    38  		// If we don't have any comment, return 1900/1/1 0:0:0 UTC
    39  		{
    40  			[]sql.Comment{},
    41  			1,
    42  			time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC),
    43  			"ONE",
    44  		},
    45  		// There are no comment for this issue/repository, return the min date
    46  		{
    47  			[]sql.Comment{
    48  				{IssueID: "1", CommentUpdatedAt: time.Date(1999, 1, 1, 0, 0, 0, 0, time.UTC), Repository: "ONE"},
    49  				{IssueID: "1", CommentUpdatedAt: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), Repository: "ONE"},
    50  				{IssueID: "2", CommentUpdatedAt: time.Date(1999, 1, 1, 0, 0, 0, 0, time.UTC), Repository: "TWO"},
    51  			},
    52  			2,
    53  			time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC),
    54  			"ONE",
    55  		},
    56  		// Only pick selected issue (and selected repo)
    57  		{
    58  			[]sql.Comment{
    59  				{IssueID: "1", CommentUpdatedAt: time.Date(1999, 1, 1, 0, 0, 0, 0, time.UTC), Repository: "ONE"},
    60  				{IssueID: "1", CommentUpdatedAt: time.Date(2001, 1, 1, 0, 0, 0, 0, time.UTC), Repository: "TWO"},
    61  				{IssueID: "1", CommentUpdatedAt: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), Repository: "ONE"},
    62  				{IssueID: "2", CommentUpdatedAt: time.Date(2002, 1, 1, 0, 0, 0, 0, time.UTC), Repository: "ONE"},
    63  			},
    64  			1,
    65  			time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC),
    66  			"ONE",
    67  		},
    68  		// Can pick pull-request comments
    69  		{
    70  			[]sql.Comment{
    71  				{IssueID: "1", PullRequest: true, CommentUpdatedAt: time.Date(1999, 1, 1, 0, 0, 0, 0, time.UTC), Repository: "ONE"},
    72  				{IssueID: "1", PullRequest: false, CommentUpdatedAt: time.Date(2001, 1, 1, 0, 0, 0, 0, time.UTC), Repository: "ONE"},
    73  				{IssueID: "1", PullRequest: true, CommentUpdatedAt: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), Repository: "ONE"},
    74  			},
    75  			1,
    76  			time.Date(2001, 1, 1, 0, 0, 0, 0, time.UTC),
    77  			"ONE",
    78  		},
    79  		// Can pick issue comments
    80  		{
    81  			[]sql.Comment{
    82  				{IssueID: "1", PullRequest: false, CommentUpdatedAt: time.Date(1999, 1, 1, 0, 0, 0, 0, time.UTC), Repository: "ONE"},
    83  				{IssueID: "1", PullRequest: true, CommentUpdatedAt: time.Date(2001, 1, 1, 0, 0, 0, 0, time.UTC), Repository: "ONE"},
    84  				{IssueID: "1", PullRequest: false, CommentUpdatedAt: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), Repository: "ONE"},
    85  			},
    86  			1,
    87  			time.Date(2001, 1, 1, 0, 0, 0, 0, time.UTC),
    88  			"ONE",
    89  		},
    90  	}
    91  
    92  	for _, test := range tests {
    93  		db, err := config.CreateDatabase()
    94  		if err != nil {
    95  			t.Fatal("Failed to create database:", err)
    96  		}
    97  
    98  		for _, comment := range test.comments {
    99  			db.Create(&comment)
   100  		}
   101  
   102  		actualLatest := findLatestCommentUpdate(test.issueID, db, test.repository)
   103  		if actualLatest != test.expectedLatest {
   104  			t.Error("Actual:", actualLatest,
   105  				"doesn't match expected:", test.expectedLatest)
   106  		}
   107  	}
   108  }
   109  
   110  func TestUpdateComments(t *testing.T) {
   111  	config := sqltest.SQLiteConfig{File: ":memory:"}
   112  
   113  	tests := []struct {
   114  		before           []sql.Comment
   115  		newIssueComments map[int][]*github.IssueComment
   116  		newPullComments  map[int][]*github.PullRequestComment
   117  		after            []sql.Comment
   118  		updateID         int
   119  		isPullRequest    bool
   120  	}{
   121  		// No new comments
   122  		{
   123  			before: []sql.Comment{
   124  				*makeComment(12, 1, "Body", "Login", "full/repo",
   125  					time.Date(2000, time.January, 1, 19, 30, 0, 0, time.UTC),
   126  					time.Date(2001, time.January, 1, 19, 30, 0, 0, time.UTC), true),
   127  			},
   128  			newIssueComments: map[int][]*github.IssueComment{},
   129  			newPullComments:  map[int][]*github.PullRequestComment{},
   130  			after: []sql.Comment{
   131  				*makeComment(12, 1, "Body", "Login", "full/repo",
   132  					time.Date(2000, time.January, 1, 19, 30, 0, 0, time.UTC),
   133  					time.Date(2001, time.January, 1, 19, 30, 0, 0, time.UTC), true),
   134  			},
   135  			updateID:      1,
   136  			isPullRequest: true,
   137  		},
   138  		// New comments, include PR
   139  		{
   140  			before: []sql.Comment{
   141  				*makeComment(12, 1, "Body", "Login", "full/repo",
   142  					time.Date(2000, time.January, 1, 19, 30, 0, 0, time.UTC),
   143  					time.Date(2001, time.January, 1, 19, 30, 0, 0, time.UTC), true),
   144  			},
   145  			newIssueComments: map[int][]*github.IssueComment{
   146  				3: {
   147  					makeGithubIssueComment(2, "IssueBody", "SomeLogin",
   148  						time.Date(2000, time.January, 1, 19, 30, 0, 0, time.UTC),
   149  						time.Date(2001, time.January, 1, 19, 30, 0, 0, time.UTC)),
   150  					makeGithubIssueComment(3, "AnotherBody", "AnotherLogin",
   151  						time.Date(2000, time.January, 1, 19, 30, 0, 0, time.UTC),
   152  						time.Date(2001, time.January, 1, 19, 30, 0, 0, time.UTC)),
   153  				},
   154  			},
   155  			newPullComments: map[int][]*github.PullRequestComment{
   156  				2: {
   157  					makeGithubPullComment(4, "Body", "Login",
   158  						time.Date(2000, time.January, 1, 19, 30, 0, 0, time.UTC),
   159  						time.Date(2001, time.February, 1, 19, 30, 0, 0, time.UTC)),
   160  				},
   161  				3: {
   162  					makeGithubPullComment(5, "SecondBody", "OtherLogin",
   163  						time.Date(2000, time.December, 1, 19, 30, 0, 0, time.UTC),
   164  						time.Date(2001, time.November, 1, 19, 30, 0, 0, time.UTC)),
   165  				},
   166  			},
   167  			after: []sql.Comment{
   168  				*makeComment(12, 1, "Body", "Login", "full/repo",
   169  					time.Date(2000, time.January, 1, 19, 30, 0, 0, time.UTC),
   170  					time.Date(2001, time.January, 1, 19, 30, 0, 0, time.UTC), true),
   171  				*makeComment(3, 2, "IssueBody", "SomeLogin", "full/repo",
   172  					time.Date(2000, time.January, 1, 19, 30, 0, 0, time.UTC),
   173  					time.Date(2001, time.January, 1, 19, 30, 0, 0, time.UTC), false),
   174  				*makeComment(3, 3, "AnotherBody", "AnotherLogin", "full/repo",
   175  					time.Date(2000, time.January, 1, 19, 30, 0, 0, time.UTC),
   176  					time.Date(2001, time.January, 1, 19, 30, 0, 0, time.UTC), false),
   177  				*makeComment(3, 5, "SecondBody", "OtherLogin", "full/repo",
   178  					time.Date(2000, time.December, 1, 19, 30, 0, 0, time.UTC),
   179  					time.Date(2001, time.November, 1, 19, 30, 0, 0, time.UTC), true),
   180  			},
   181  			updateID:      3,
   182  			isPullRequest: true,
   183  		},
   184  		// Only interesting new comment is in PR, and we don't take PR
   185  		{
   186  			before: []sql.Comment{
   187  				*makeComment(12, 1, "Body", "Login", "full/repo",
   188  					time.Date(2000, time.January, 1, 19, 30, 0, 0, time.UTC),
   189  					time.Date(2001, time.January, 1, 19, 30, 0, 0, time.UTC), true),
   190  			},
   191  			newIssueComments: map[int][]*github.IssueComment{
   192  				3: {
   193  					makeGithubIssueComment(2, "IssueBody", "SomeLogin",
   194  						time.Date(2000, time.January, 1, 19, 30, 0, 0, time.UTC),
   195  						time.Date(2001, time.January, 1, 19, 30, 0, 0, time.UTC)),
   196  					makeGithubIssueComment(3, "AnotherBody", "AnotherLogin",
   197  						time.Date(2000, time.January, 1, 19, 30, 0, 0, time.UTC),
   198  						time.Date(2001, time.January, 1, 19, 30, 0, 0, time.UTC)),
   199  				},
   200  			},
   201  			newPullComments: map[int][]*github.PullRequestComment{
   202  				2: {
   203  					makeGithubPullComment(4, "Body", "Login",
   204  						time.Date(2000, time.January, 1, 19, 30, 0, 0, time.UTC),
   205  						time.Date(2001, time.February, 1, 19, 30, 0, 0, time.UTC)),
   206  				},
   207  				3: {
   208  					makeGithubPullComment(5, "SecondBody", "OtherLogin",
   209  						time.Date(2000, time.December, 1, 19, 30, 0, 0, time.UTC),
   210  						time.Date(2001, time.November, 1, 19, 30, 0, 0, time.UTC)),
   211  				},
   212  			},
   213  			after: []sql.Comment{
   214  				*makeComment(12, 1, "Body", "Login", "full/repo",
   215  					time.Date(2000, time.January, 1, 19, 30, 0, 0, time.UTC),
   216  					time.Date(2001, time.January, 1, 19, 30, 0, 0, time.UTC), true),
   217  			},
   218  			updateID:      2,
   219  			isPullRequest: false,
   220  		},
   221  		// New modified comment
   222  		{
   223  			before: []sql.Comment{
   224  				*makeComment(12, 1, "Body", "Login", "full/repo",
   225  					time.Date(2000, time.January, 1, 19, 30, 0, 0, time.UTC),
   226  					time.Date(2001, time.January, 1, 19, 30, 0, 0, time.UTC), true),
   227  			},
   228  			newIssueComments: map[int][]*github.IssueComment{},
   229  			newPullComments: map[int][]*github.PullRequestComment{
   230  				12: {
   231  					makeGithubPullComment(1, "IssueBody", "SomeLogin",
   232  						time.Date(2000, time.January, 1, 19, 30, 0, 0, time.UTC),
   233  						time.Date(2001, time.January, 1, 19, 30, 0, 0, time.UTC)),
   234  				},
   235  			},
   236  			after: []sql.Comment{
   237  				*makeComment(12, 1, "IssueBody", "SomeLogin", "full/repo",
   238  					time.Date(2000, time.January, 1, 19, 30, 0, 0, time.UTC),
   239  					time.Date(2001, time.January, 1, 19, 30, 0, 0, time.UTC), true),
   240  			},
   241  			updateID:      12,
   242  			isPullRequest: true,
   243  		},
   244  	}
   245  
   246  	for _, test := range tests {
   247  		db, err := config.CreateDatabase()
   248  		if err != nil {
   249  			t.Fatal("Failed to create database:", err)
   250  		}
   251  
   252  		for _, comment := range test.before {
   253  			db.Create(&comment)
   254  		}
   255  
   256  		client := FakeClient{PullComments: test.newPullComments, IssueComments: test.newIssueComments, Repository: "FULL/REPO"}
   257  		UpdateComments(test.updateID, test.isPullRequest, db, client)
   258  		var comments []sql.Comment
   259  		if err := db.Order("ID").Find(&comments).Error; err != nil {
   260  			t.Fatal(err)
   261  		}
   262  		if !reflect.DeepEqual(comments, test.after) {
   263  			t.Error("Actual:", comments,
   264  				"doesn't match expected:", test.after)
   265  		}
   266  	}
   267  }