...

Source file src/edge-infra.dev/pkg/f8n/devinfra/repo/owners/policybot/policy/common/methods_test.go

Documentation: edge-infra.dev/pkg/f8n/devinfra/repo/owners/policybot/policy/common

     1  // Copyright 2018 Palantir Technologies, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package common
    16  
    17  import (
    18  	"context"
    19  	"regexp"
    20  	"sort"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  	"github.com/stretchr/testify/require"
    26  
    27  	"edge-infra.dev/pkg/f8n/devinfra/repo/owners/policybot/pull"
    28  	"edge-infra.dev/pkg/f8n/devinfra/repo/owners/policybot/pull/pulltest"
    29  )
    30  
    31  func TestCandidates(t *testing.T) {
    32  	now := time.Now()
    33  
    34  	ctx := context.Background()
    35  	prctx := &pulltest.Context{
    36  		CommentsValue: []*pull.Comment{
    37  			{
    38  				CreatedAt: now.Add(0 * time.Minute),
    39  				Body:      "I like to comment!",
    40  				Author:    "rrandom",
    41  			},
    42  			{
    43  				CreatedAt: now.Add(2 * time.Minute),
    44  				Body:      "Looks good to me :+1:",
    45  				Author:    "mhaypenny",
    46  			},
    47  			{
    48  				CreatedAt: now.Add(4 * time.Minute),
    49  				Body:      ":lgtm:",
    50  				Author:    "ttest",
    51  			},
    52  			{
    53  				CreatedAt: now.Add(8 * time.Minute),
    54  				Body:      "I approve this, because it looks good to me.",
    55  				Author:    "wstrawmoney",
    56  			},
    57  		},
    58  		ReviewsValue: []*pull.Review{
    59  			{
    60  				CreatedAt: now.Add(1 * time.Minute),
    61  				Author:    "rrandom",
    62  				State:     pull.ReviewCommented,
    63  			},
    64  			{
    65  				CreatedAt: now.Add(3 * time.Minute),
    66  				Author:    "mhaypenny",
    67  				State:     pull.ReviewChangesRequested,
    68  				Body:      "pr needs work",
    69  			},
    70  			{
    71  				CreatedAt: now.Add(5 * time.Minute),
    72  				Author:    "ttest",
    73  				State:     pull.ReviewApproved,
    74  			},
    75  			{
    76  				CreatedAt: now.Add(7 * time.Minute),
    77  				Author:    "santaclaus",
    78  				Body:      "nice",
    79  				State:     pull.ReviewApproved,
    80  			},
    81  			{
    82  				CreatedAt: now.Add(9 * time.Minute),
    83  				Author:    "dasherdancer",
    84  				Body:      "nIcE",
    85  				State:     pull.ReviewApproved,
    86  			},
    87  		},
    88  	}
    89  
    90  	t.Run("comments", func(t *testing.T) {
    91  		m := &Methods{
    92  			Comments: []string{":+1:", ":lgtm:"},
    93  		}
    94  
    95  		cs, err := m.Candidates(ctx, prctx)
    96  		require.NoError(t, err)
    97  
    98  		sort.Sort(CandidatesByCreationTime(cs))
    99  
   100  		require.Len(t, cs, 2, "incorrect number of candidates found")
   101  		assert.Equal(t, "mhaypenny", cs[0].User)
   102  		assert.Equal(t, "ttest", cs[1].User)
   103  	})
   104  
   105  	t.Run("commentPatterns", func(t *testing.T) {
   106  		m := &Methods{
   107  			CommentPatterns: []Regexp{
   108  				NewCompiledRegexp(regexp.MustCompile("^(?i:looks good to me)")),
   109  			},
   110  		}
   111  
   112  		cs, err := m.Candidates(ctx, prctx)
   113  		require.NoError(t, err)
   114  
   115  		sort.Sort(CandidatesByCreationTime(cs))
   116  
   117  		require.Len(t, cs, 1, "incorrect number of candidates found")
   118  		assert.Equal(t, "mhaypenny", cs[0].User)
   119  	})
   120  
   121  	t.Run("githubReviewCommentPatterns", func(t *testing.T) {
   122  		githubReview := true
   123  		m := &Methods{
   124  			GithubReview:      &githubReview,
   125  			GithubReviewState: pull.ReviewApproved,
   126  			GithubReviewCommentPatterns: []Regexp{
   127  				NewCompiledRegexp(regexp.MustCompile("(?i)nice")),
   128  			},
   129  		}
   130  
   131  		cs, err := m.Candidates(ctx, prctx)
   132  		require.NoError(t, err)
   133  
   134  		sort.Sort(CandidatesByCreationTime(cs))
   135  
   136  		require.Len(t, cs, 2, "incorrect number of candidates found")
   137  		assert.Equal(t, "santaclaus", cs[0].User)
   138  		assert.Equal(t, "dasherdancer", cs[1].User)
   139  	})
   140  
   141  	t.Run("reviews", func(t *testing.T) {
   142  		githubReview := true
   143  		m := &Methods{
   144  			GithubReview:      &githubReview,
   145  			GithubReviewState: pull.ReviewChangesRequested,
   146  		}
   147  
   148  		cs, err := m.Candidates(ctx, prctx)
   149  		require.NoError(t, err)
   150  
   151  		sort.Sort(CandidatesByCreationTime(cs))
   152  
   153  		require.Len(t, cs, 1, "incorrect number of candidates found")
   154  		assert.Equal(t, "mhaypenny", cs[0].User)
   155  	})
   156  
   157  	t.Run("deduplicate", func(t *testing.T) {
   158  		githubReview := true
   159  		m := &Methods{
   160  			Comments:          []string{":+1:", ":lgtm:"},
   161  			GithubReview:      &githubReview,
   162  			GithubReviewState: pull.ReviewApproved,
   163  		}
   164  
   165  		cs, err := m.Candidates(ctx, prctx)
   166  		require.NoError(t, err)
   167  
   168  		sort.Sort(CandidatesByCreationTime(cs))
   169  
   170  		require.Len(t, cs, 4, "incorrect number of candidates found")
   171  		assert.Equal(t, "mhaypenny", cs[0].User)
   172  		assert.Equal(t, "ttest", cs[1].User)
   173  		assert.Equal(t, "santaclaus", cs[2].User)
   174  		assert.Equal(t, "dasherdancer", cs[3].User)
   175  	})
   176  }
   177  
   178  func TestCandidatesByCreationTime(t *testing.T) {
   179  	cs := []*Candidate{
   180  		{
   181  			User:      "c",
   182  			CreatedAt: time.Date(2018, 6, 29, 12, 0, 0, 0, time.UTC),
   183  		},
   184  		{
   185  			User:      "a",
   186  			CreatedAt: time.Date(2018, 6, 28, 0, 0, 0, 0, time.UTC),
   187  		},
   188  		{
   189  			User:      "d",
   190  			CreatedAt: time.Date(2018, 6, 29, 14, 0, 0, 0, time.UTC),
   191  		},
   192  		{
   193  			User:      "b",
   194  			CreatedAt: time.Date(2018, 6, 29, 10, 0, 0, 0, time.UTC),
   195  		},
   196  	}
   197  
   198  	sort.Sort(CandidatesByCreationTime(cs))
   199  
   200  	for i, u := range []string{"a", "b", "c", "d"} {
   201  		assert.Equalf(t, u, cs[i].User, "candidate at position %d is incorrect", i)
   202  	}
   203  }
   204  

View as plain text