github.com/abayer/test-infra@v0.0.5/velodrome/transform/fetcher_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 "os" 21 "testing" 22 "time" 23 24 "k8s.io/test-infra/velodrome/sql" 25 sqltest "k8s.io/test-infra/velodrome/sql/testing" 26 ) 27 28 // Fetch doesn't download too many items, and return the proper date. And only from proper repo 29 30 func TestFetchIssues(t *testing.T) { 31 config := sqltest.SQLiteConfig{File: ":memory:"} 32 db, err := config.CreateDatabase() 33 if err != nil { 34 t.Fatal("Failed to create database:", err) 35 } 36 37 db.Create(&sql.Issue{IssueUpdatedAt: time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC), Repository: "ok"}) 38 db.Create(&sql.Issue{IssueUpdatedAt: time.Date(2000, time.January, 2, 0, 0, 0, 0, time.UTC), Repository: "ok"}) 39 db.Create(&sql.Issue{IssueUpdatedAt: time.Date(2000, time.January, 3, 0, 0, 0, 0, time.UTC), Repository: "ok"}) 40 db.Create(&sql.Issue{IssueUpdatedAt: time.Date(2000, time.January, 4, 0, 0, 0, 0, time.UTC), Repository: "ok"}) 41 db.Create(&sql.Issue{IssueUpdatedAt: time.Date(2000, time.January, 4, 0, 0, 0, 0, time.UTC), Repository: "notok"}) 42 43 fetcher := NewFetcher("ok") 44 fetcher.lastIssue = time.Date(2000, time.January, 2, 0, 0, 0, 0, time.UTC) 45 fetcher.IssuesChannel = make(chan sql.Issue, 10) 46 47 if err := fetcher.fetchRecentIssues(db); err != nil { 48 t.Fatal("Failed to fetch recent issues:", err) 49 } 50 if fetcher.lastIssue != time.Date(2000, time.January, 4, 0, 0, 0, 0, time.UTC) { 51 t.Errorf( 52 "Last issue should be %s, not %s", 53 time.Date(2000, time.January, 4, 0, 0, 0, 0, time.UTC), 54 fetcher.lastIssue, 55 ) 56 } 57 // Last is included in the response set 58 if len(fetcher.IssuesChannel) != 3 { 59 t.Error("Only 3 issues should have been fetched, not ", len(fetcher.IssuesChannel)) 60 } 61 } 62 63 func TestFetchEventsAndComments(t *testing.T) { 64 tests := []struct { 65 events []interface{} 66 lastEvent time.Time 67 lastComment time.Time 68 wantLastEvent time.Time 69 wantLastComment time.Time 70 wantCount int 71 }{ 72 // Mixed events and comments 73 { 74 events: []interface{}{ 75 &sql.IssueEvent{ID: "1", Repository: "ok"}, 76 &sql.IssueEvent{ID: "2", Repository: "ok"}, 77 &sql.IssueEvent{ID: "3", EventCreatedAt: time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC), Repository: "ok"}, 78 &sql.IssueEvent{ID: "4", EventCreatedAt: time.Date(2000, time.January, 3, 0, 0, 0, 0, time.UTC), Repository: "ok"}, 79 &sql.Comment{ID: "1", Repository: "ok"}, 80 &sql.Comment{ID: "2", Repository: "ok"}, 81 &sql.Comment{ID: "3", CommentCreatedAt: time.Date(2000, time.January, 2, 0, 0, 0, 0, time.UTC), Repository: "ok"}, 82 &sql.Comment{ID: "4", CommentCreatedAt: time.Date(2000, time.January, 4, 0, 0, 0, 0, time.UTC), Repository: "ok"}, 83 &sql.Comment{ID: "5", CommentCreatedAt: time.Date(2000, time.January, 5, 0, 0, 0, 0, time.UTC), Repository: "ok"}, 84 &sql.Comment{ID: "6", CommentCreatedAt: time.Date(2000, time.January, 5, 0, 0, 0, 0, time.UTC), Repository: "notok"}, 85 }, 86 lastEvent: time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC), 87 lastComment: time.Date(2000, time.January, 2, 0, 0, 0, 0, time.UTC), 88 wantLastEvent: time.Date(2000, time.January, 3, 0, 0, 0, 0, time.UTC), 89 wantLastComment: time.Date(2000, time.January, 5, 0, 0, 0, 0, time.UTC), 90 wantCount: 3, 91 }, 92 // Only comments 93 { 94 events: []interface{}{ 95 &sql.Comment{ID: "1", Repository: "ok"}, 96 &sql.Comment{ID: "2", Repository: "ok"}, 97 &sql.Comment{ID: "3", CommentCreatedAt: time.Date(2000, time.January, 2, 0, 0, 0, 0, time.UTC), Repository: "ok"}, 98 &sql.Comment{ID: "4", CommentCreatedAt: time.Date(2000, time.January, 4, 0, 0, 0, 0, time.UTC), Repository: "ok"}, 99 &sql.Comment{ID: "5", CommentCreatedAt: time.Date(2000, time.January, 5, 0, 0, 0, 0, time.UTC), Repository: "ok"}, 100 &sql.Comment{ID: "5", CommentCreatedAt: time.Date(2000, time.January, 5, 0, 0, 0, 0, time.UTC), Repository: "notok"}, 101 }, 102 lastEvent: time.Date(1990, time.January, 0, 0, 0, 0, 0, time.UTC), 103 lastComment: time.Date(2000, time.January, 2, 0, 0, 0, 0, time.UTC), 104 wantLastEvent: time.Date(1990, time.January, 0, 0, 0, 0, 0, time.UTC), 105 wantLastComment: time.Date(2000, time.January, 5, 0, 0, 0, 0, time.UTC), 106 wantCount: 2, 107 }, 108 } 109 110 for _, test := range tests { 111 os.Remove("test.db") 112 config := sqltest.SQLiteConfig{File: "test.db"} 113 db, err := config.CreateDatabase() 114 if err != nil { 115 t.Fatal("Failed to create database:", err) 116 } 117 118 for _, event := range test.events { 119 db.Create(event) 120 } 121 122 fetcher := NewFetcher("ok") 123 fetcher.lastEvent = test.lastEvent 124 fetcher.lastComment = test.lastComment 125 fetcher.EventsCommentsChannel = make(chan interface{}, len(test.events)) 126 127 if err := fetcher.fetchRecentEventsAndComments(db); err != nil { 128 t.Fatal("Failed to fetch recent events:", err) 129 } 130 if !fetcher.lastEvent.Equal(test.wantLastEvent) { 131 t.Errorf("LastEvent event should be %s, not %s", test.wantLastEvent, fetcher.lastEvent) 132 } 133 if !fetcher.lastComment.Equal(test.wantLastComment) { 134 t.Errorf("LastComment event should be %s, not %s", test.wantLastComment, fetcher.lastComment) 135 } 136 if len(fetcher.EventsCommentsChannel) != test.wantCount { 137 t.Errorf("%d events should have been fetched, not %d", test.wantCount, len(fetcher.EventsCommentsChannel)) 138 } 139 140 close(fetcher.EventsCommentsChannel) 141 142 lastDate := time.Time{} 143 for item := range fetcher.EventsCommentsChannel { 144 date := time.Time{} 145 switch item := item.(type) { 146 case sql.IssueEvent: 147 date = item.EventCreatedAt 148 case sql.Comment: 149 date = item.CommentCreatedAt 150 default: 151 t.Error("Received item of unknown type:", item) 152 } 153 if date.Before(lastDate) { 154 t.Errorf("Dates are not properly sorted: %v < %v", date, lastDate) 155 } 156 } 157 os.Remove("test.db") 158 } 159 }