github.com/abayer/test-infra@v0.0.5/velodrome/fetcher/issue-events_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 "strings" 22 "testing" 23 "time" 24 25 "k8s.io/test-infra/velodrome/sql" 26 sqltest "k8s.io/test-infra/velodrome/sql/testing" 27 28 "github.com/google/go-github/github" 29 ) 30 31 func TestFindLatestUpdate(t *testing.T) { 32 config := sqltest.SQLiteConfig{File: ":memory:"} 33 tests := []struct { 34 events []sql.IssueEvent 35 expectedLatest int 36 repository string 37 issueID int 38 }{ 39 // If we don't have any issue, return 1900/1/1 0:0:0 UTC 40 { 41 []sql.IssueEvent{}, 42 0, 43 "ONE", 44 1, 45 }, 46 { 47 []sql.IssueEvent{ 48 {ID: "2", IssueID: "7", EventCreatedAt: time.Date(1999, 1, 1, 0, 0, 0, 0, time.UTC), Repository: "ONE"}, 49 {ID: "7", IssueID: "7", EventCreatedAt: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), Repository: "ONE"}, 50 }, 51 0, 52 "TWO", 53 7, 54 }, 55 { 56 []sql.IssueEvent{ 57 {ID: "2", IssueID: "7", EventCreatedAt: time.Date(1999, 1, 1, 0, 0, 0, 0, time.UTC), Repository: "ONE"}, 58 {ID: "7", IssueID: "2", EventCreatedAt: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), Repository: "ONE"}, 59 {ID: "1", IssueID: "7", EventCreatedAt: time.Date(1998, 1, 1, 0, 0, 0, 0, time.UTC), Repository: "TWO"}, 60 }, 61 2, 62 "ONE", 63 7, 64 }, 65 { 66 []sql.IssueEvent{ 67 {ID: "2", IssueID: "7", EventCreatedAt: time.Date(1999, 1, 1, 0, 0, 0, 0, time.UTC), Repository: "ONE"}, 68 {ID: "7", IssueID: "7", EventCreatedAt: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), Repository: "ONE"}, 69 {ID: "1", IssueID: "7", EventCreatedAt: time.Date(1998, 1, 1, 0, 0, 0, 0, time.UTC), Repository: "TWO"}, 70 }, 71 1, 72 "TWO", 73 7, 74 }, 75 } 76 77 for _, test := range tests { 78 db, err := config.CreateDatabase() 79 if err != nil { 80 t.Fatal("Failed to create database:", err) 81 } 82 83 tx := db.Begin() 84 for _, event := range test.events { 85 tx.Create(&event) 86 } 87 tx.Commit() 88 89 actualLatest, err := findLatestEvent(test.issueID, db, test.repository) 90 if err != nil { 91 t.Error("findLatestEvent failed:", err) 92 } 93 if actualLatest == nil { 94 if test.expectedLatest != 0 { 95 t.Error("Didn't found event, expected:", test.expectedLatest) 96 } 97 } else if *actualLatest != test.expectedLatest { 98 t.Error("Actual:", *actualLatest, 99 "doesn't match expected:", test.expectedLatest) 100 } 101 } 102 } 103 104 func TestUpdateEvents(t *testing.T) { 105 config := sqltest.SQLiteConfig{File: ":memory:"} 106 107 tests := []struct { 108 before []sql.IssueEvent 109 new map[int][]*github.IssueEvent 110 after []sql.IssueEvent 111 repository string 112 issueID int 113 }{ 114 // No new issues 115 { 116 before: []sql.IssueEvent{ 117 *makeIssueEvent(1, 2, "", "Event", "", "", "full/repo", 118 time.Date(2000, time.January, 1, 19, 30, 0, 0, time.UTC)), 119 }, 120 new: map[int][]*github.IssueEvent{}, 121 after: []sql.IssueEvent{ 122 *makeIssueEvent(1, 2, "", "Event", "", "", "full/repo", 123 time.Date(2000, time.January, 1, 19, 30, 0, 0, time.UTC)), 124 }, 125 repository: "FULL/REPO", 126 issueID: 2, 127 }, 128 // New issues 129 { 130 before: []sql.IssueEvent{ 131 *makeIssueEvent(1, 2, "", "Event", "", "", "full/repo", 132 time.Date(2000, time.January, 1, 19, 30, 0, 0, time.UTC)), 133 }, 134 new: map[int][]*github.IssueEvent{ 135 2: { 136 makeGithubIssueEvent(2, "Label", "Event", "Assignee", "Actor", 137 time.Date(2001, time.January, 1, 19, 30, 0, 0, time.UTC)), 138 }, 139 }, 140 after: []sql.IssueEvent{ 141 *makeIssueEvent(1, 2, "", "Event", "", "", "full/repo", 142 time.Date(2000, time.January, 1, 19, 30, 0, 0, time.UTC)), 143 *makeIssueEvent(2, 2, "Label", "Event", "Assignee", "Actor", "full/repo", 144 time.Date(2001, time.January, 1, 19, 30, 0, 0, time.UTC)), 145 }, 146 repository: "FULL/REPO", 147 issueID: 2, 148 }, 149 // New issues + already existing (doesn't update) 150 { 151 before: []sql.IssueEvent{ 152 *makeIssueEvent(1, 2, "", "Event", "", "", "full/repo", 153 time.Date(2000, time.January, 1, 19, 30, 0, 0, time.UTC)), 154 *makeIssueEvent(2, 2, "Label", "Event", "Assignee", "Actor", "full/repo", 155 time.Date(2001, time.January, 1, 19, 30, 0, 0, time.UTC)), 156 }, 157 new: map[int][]*github.IssueEvent{ 158 2: { 159 makeGithubIssueEvent(1, "", "EventNameChanged", "", "", 160 time.Date(2000, time.January, 1, 19, 30, 0, 0, time.UTC)), 161 makeGithubIssueEvent(3, "Label", "Event", "Assignee", "", 162 time.Date(2002, time.January, 1, 19, 30, 0, 0, time.UTC)), 163 }, 164 }, 165 after: []sql.IssueEvent{ 166 *makeIssueEvent(1, 2, "", "Event", "", "", "full/repo", 167 time.Date(2000, time.January, 1, 19, 30, 0, 0, time.UTC)), 168 *makeIssueEvent(2, 2, "Label", "Event", "Assignee", "Actor", "full/repo", 169 time.Date(2001, time.January, 1, 19, 30, 0, 0, time.UTC)), 170 *makeIssueEvent(3, 2, "Label", "Event", "Assignee", "", "full/repo", 171 time.Date(2002, time.January, 1, 19, 30, 0, 0, time.UTC)), 172 }, 173 repository: "FULL/REPO", 174 issueID: 2, 175 }, 176 // Fetch new repository 177 { 178 before: []sql.IssueEvent{ 179 *makeIssueEvent(2, 2, "", "Event", "", "", "full/one", 180 time.Date(2000, time.January, 1, 19, 30, 0, 0, time.UTC)), 181 }, 182 new: map[int][]*github.IssueEvent{ 183 2: { 184 makeGithubIssueEvent(1, "", "EventNameOther", "", "", 185 time.Date(2000, time.January, 1, 19, 30, 0, 0, time.UTC)), 186 }, 187 }, 188 after: []sql.IssueEvent{ 189 *makeIssueEvent(1, 2, "", "EventNameOther", "", "", "full/other", 190 time.Date(2000, time.January, 1, 19, 30, 0, 0, time.UTC)), 191 }, 192 repository: "FULL/OTHER", 193 issueID: 2, 194 }, 195 } 196 197 for _, test := range tests { 198 db, err := config.CreateDatabase() 199 if err != nil { 200 t.Fatal("Failed to create database:", err) 201 } 202 203 for _, event := range test.before { 204 db.Create(&event) 205 } 206 207 UpdateIssueEvents(test.issueID, db, FakeClient{IssueEvents: test.new, Repository: test.repository}) 208 var issues []sql.IssueEvent 209 if err := db.Order("ID").Where("repository = ?", strings.ToLower(test.repository)).Find(&issues).Error; err != nil { 210 t.Fatal(err) 211 } 212 if !reflect.DeepEqual(issues, test.after) { 213 t.Error("Actual:", issues, 214 "doesn't match expected:", test.after) 215 } 216 } 217 }