github.com/abayer/test-infra@v0.0.5/velodrome/transform/plugins/fake_open_wrapper_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  	"testing"
    21  	"time"
    22  
    23  	"k8s.io/test-infra/velodrome/sql"
    24  
    25  	"github.com/golang/mock/gomock"
    26  )
    27  
    28  func stringPointer(s string) *string {
    29  	return &s
    30  }
    31  
    32  func TestFakeOpenWrapper(t *testing.T) {
    33  	ctrl := gomock.NewController(t)
    34  	defer ctrl.Finish()
    35  
    36  	plugin := NewMockPlugin(ctrl)
    37  	fakeOpen := NewFakeOpenPluginWrapper(plugin)
    38  
    39  	gomock.InOrder(
    40  		plugin.EXPECT().ReceiveIssue(sql.Issue{
    41  			ID:             "1",
    42  			User:           "User1",
    43  			IssueCreatedAt: time.Unix(0, 20),
    44  		}).Return([]Point{{}}),
    45  		plugin.EXPECT().ReceiveIssue(sql.Issue{
    46  			ID:             "2",
    47  			User:           "User2",
    48  			IssueCreatedAt: time.Unix(0, 30),
    49  		}).Return([]Point{{}}),
    50  		plugin.EXPECT().ReceiveIssue(sql.Issue{
    51  			ID:             "3",
    52  			User:           "User3",
    53  			IssueCreatedAt: time.Unix(0, 50),
    54  		}).Return([]Point{{}}),
    55  		plugin.EXPECT().ReceiveIssueEvent(sql.IssueEvent{
    56  			ID:             "1",
    57  			Actor:          stringPointer("Actor1"),
    58  			Event:          "event1",
    59  			EventCreatedAt: time.Unix(0, 10),
    60  		}).Return([]Point{{}}),
    61  		plugin.EXPECT().ReceiveIssueEvent(sql.IssueEvent{
    62  			IssueID:        "1",
    63  			Actor:          stringPointer("User1"),
    64  			Event:          "opened",
    65  			EventCreatedAt: time.Unix(0, 20),
    66  		}).Return([]Point{{}}),
    67  		plugin.EXPECT().ReceiveIssueEvent(sql.IssueEvent{
    68  			IssueID:        "2",
    69  			Actor:          stringPointer("User2"),
    70  			Event:          "opened",
    71  			EventCreatedAt: time.Unix(0, 30),
    72  		}).Return([]Point{{}}),
    73  		plugin.EXPECT().ReceiveIssueEvent(sql.IssueEvent{
    74  			ID:             "2",
    75  			Actor:          stringPointer("Actor2"),
    76  			Event:          "event2",
    77  			EventCreatedAt: time.Unix(0, 40),
    78  		}).Return([]Point{{}}),
    79  		plugin.EXPECT().ReceiveIssueEvent(sql.IssueEvent{
    80  			IssueID:        "3",
    81  			Actor:          stringPointer("User3"),
    82  			Event:          "opened",
    83  			EventCreatedAt: time.Unix(0, 50),
    84  		}).Return([]Point{{}}),
    85  		plugin.EXPECT().ReceiveIssueEvent(sql.IssueEvent{
    86  			ID:             "3",
    87  			Actor:          stringPointer("Actor3"),
    88  			Event:          "event3",
    89  			EventCreatedAt: time.Unix(0, 50),
    90  		}).Return([]Point{{}}),
    91  	)
    92  
    93  	got := fakeOpen.ReceiveIssue(sql.Issue{
    94  		ID:             "1",
    95  		User:           "User1",
    96  		IssueCreatedAt: time.Unix(0, 20),
    97  	})
    98  	if len(got) != 1 {
    99  		t.Errorf("ReceiveIssue 1 pass-through failed: length(%+v) = %d, want %d", got, len(got), 1)
   100  	}
   101  	got = fakeOpen.ReceiveIssue(sql.Issue{
   102  		ID:             "2",
   103  		User:           "User2",
   104  		IssueCreatedAt: time.Unix(0, 30),
   105  	})
   106  	if len(got) != 1 {
   107  		t.Errorf("ReceiveIssue 2 pass-through failed: length(%+v) = %d, want %d", got, len(got), 1)
   108  	}
   109  	got = fakeOpen.ReceiveIssue(sql.Issue{
   110  		ID:             "3",
   111  		User:           "User3",
   112  		IssueCreatedAt: time.Unix(0, 50),
   113  	})
   114  	if len(got) != 1 {
   115  		t.Errorf("ReceiveIssue 3 pass-through failed: length(%+v) = %d, want %d", got, len(got), 1)
   116  	}
   117  	got = fakeOpen.ReceiveIssueEvent(sql.IssueEvent{
   118  		ID:             "1",
   119  		Actor:          stringPointer("Actor1"),
   120  		Event:          "event1",
   121  		EventCreatedAt: time.Unix(0, 10),
   122  	})
   123  	if len(got) != 1 {
   124  		t.Errorf("ReceiveIssueEvent 1 pass-through failed: length(%+v) = %d, want %d", got, len(got), 1)
   125  	}
   126  	got = fakeOpen.ReceiveIssueEvent(sql.IssueEvent{
   127  		ID:             "2",
   128  		Actor:          stringPointer("Actor2"),
   129  		Event:          "event2",
   130  		EventCreatedAt: time.Unix(0, 40),
   131  	})
   132  	// This receives points for each opened event we inserted before IssueEvent 2
   133  	if len(got) != 3 {
   134  		t.Errorf("ReceiveIssueEvent 2 pass-through failed: length(%+v) = %d, want %d", got, len(got), 3)
   135  	}
   136  	got = fakeOpen.ReceiveIssueEvent(sql.IssueEvent{
   137  		ID:             "3",
   138  		Actor:          stringPointer("Actor3"),
   139  		Event:          "event3",
   140  		EventCreatedAt: time.Unix(0, 50),
   141  	})
   142  	if len(got) != 2 {
   143  		t.Errorf("ReceiveIssueEvent 3 pass-through failed: length(%+v) = %d, want %d", got, len(got), 2)
   144  	}
   145  }