github.com/abayer/test-infra@v0.0.5/velodrome/transform/plugins/comment_counter_test.go (about)

     1  /*
     2  Copyright 2017 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 plugins
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  	"time"
    23  
    24  	"k8s.io/test-infra/velodrome/sql"
    25  )
    26  
    27  func commentPoint(t time.Time) Point {
    28  	return Point{
    29  		Values: map[string]interface{}{
    30  			"comment": 1,
    31  		},
    32  		Date: t,
    33  	}
    34  }
    35  
    36  func TestCommentCounter(t *testing.T) {
    37  	tests := []struct {
    38  		pattern  string
    39  		comments []sql.Comment
    40  		expected []Point
    41  	}{
    42  		{
    43  			pattern: "",
    44  			comments: []sql.Comment{
    45  				{
    46  					Body:             "Something",
    47  					CommentCreatedAt: time.Unix(10, 0),
    48  				},
    49  				{
    50  					Body:             "Anything",
    51  					CommentCreatedAt: time.Unix(20, 0),
    52  				},
    53  				{
    54  					Body:             "It doesn't matter",
    55  					CommentCreatedAt: time.Unix(30, 0),
    56  				},
    57  			},
    58  			expected: []Point{
    59  				commentPoint(time.Unix(10, 0)),
    60  				commentPoint(time.Unix(20, 0)),
    61  				commentPoint(time.Unix(30, 0)),
    62  			},
    63  		},
    64  		{
    65  			pattern: `(?m)/lgtm\s*$`,
    66  			comments: []sql.Comment{
    67  				{
    68  					Body:             "/lgtm cancel",
    69  					CommentCreatedAt: time.Unix(10, 0),
    70  				},
    71  				{
    72  					Body:             "/lgtm",
    73  					CommentCreatedAt: time.Unix(20, 0),
    74  				},
    75  				{
    76  					Body:             "/lgtm    \nOr not.",
    77  					CommentCreatedAt: time.Unix(30, 0),
    78  				},
    79  			},
    80  			expected: []Point{
    81  				commentPoint(time.Unix(20, 0)),
    82  				commentPoint(time.Unix(30, 0)),
    83  			},
    84  		},
    85  	}
    86  
    87  	for _, test := range tests {
    88  		plugin := CommentCounterPlugin{pattern: []string{test.pattern}}
    89  		if err := plugin.CheckFlags(); err != nil {
    90  			t.Fatalf("Failed to initial comment counter (%s): %s", test.pattern, err)
    91  		}
    92  		got := []Point{}
    93  		for _, comment := range test.comments {
    94  			got = append(got, plugin.ReceiveComment(comment)...)
    95  		}
    96  		want := test.expected
    97  		if !reflect.DeepEqual(got, want) {
    98  			t.Errorf(`CommentCounterPlugin{pattern: "%s".ReceiveComment = %+v, want %+v`,
    99  				test.pattern, got, want)
   100  		}
   101  	}
   102  }