github.com/abayer/test-infra@v0.0.5/velodrome/transform/plugins/state_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 statePoint(t time.Time, values map[string]interface{}) Point {
    28  	return Point{
    29  		Date:   t,
    30  		Values: values,
    31  	}
    32  }
    33  
    34  func TestStatePlugin(t *testing.T) {
    35  	tests := []struct {
    36  		description string
    37  		percentiles []int
    38  		events      []sql.IssueEvent
    39  		expected    []Point
    40  	}{
    41  		{
    42  			description: "merged",
    43  			percentiles: []int{50, 100},
    44  			events: []sql.IssueEvent{
    45  				// No change
    46  				{
    47  					IssueID:        "1",
    48  					Event:          "opened",
    49  					EventCreatedAt: time.Unix(10*60, 0),
    50  				},
    51  				// 1 is merged
    52  				{
    53  					IssueID:        "1",
    54  					Event:          "merged",
    55  					EventCreatedAt: time.Unix(20*60, 0),
    56  				},
    57  				// 1 is merged again, no change
    58  				{
    59  					IssueID:        "1",
    60  					Event:          "merged",
    61  					EventCreatedAt: time.Unix(30*60, 0),
    62  				},
    63  				// 2 is merged
    64  				{
    65  					IssueID:        "2",
    66  					Event:          "merged",
    67  					EventCreatedAt: time.Unix(40*60, 0),
    68  				},
    69  			},
    70  			expected: []Point{
    71  				{
    72  					Date: time.Unix(20*60, 0),
    73  					Values: map[string]interface{}{
    74  						"count": 1,
    75  						"sum":   0,
    76  						"50%":   0,
    77  						"100%":  0,
    78  					},
    79  				},
    80  				{
    81  					Date: time.Unix(40*60, 0),
    82  					Values: map[string]interface{}{
    83  						"count": 2,
    84  						"sum":   20,
    85  						"50%":   0,
    86  						"100%":  int(20 * time.Minute),
    87  					},
    88  				}},
    89  		},
    90  	}
    91  
    92  	for _, test := range tests {
    93  		plugin := StatePlugin{
    94  			desc:        test.description,
    95  			percentiles: test.percentiles,
    96  		}
    97  		if err := plugin.CheckFlags(); err != nil {
    98  			t.Fatalf("Failed to CheckFlags(): %s", err)
    99  		}
   100  		got := []Point{}
   101  		for _, event := range test.events {
   102  			got = append(got, plugin.ReceiveIssueEvent(event)...)
   103  		}
   104  		want := test.expected
   105  		if !reflect.DeepEqual(got, want) {
   106  			t.Errorf(`StatePlugin{desc: "%s".ReceiveIssueEvent = %+v, want %+v`,
   107  				test.description, got, want)
   108  		}
   109  	}
   110  }