github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/pkg/jira/fakejira/fake_test.go (about) 1 /* 2 Copyright 2020 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 fakejira 18 19 import ( 20 "context" 21 "github.com/andygrunwald/go-jira" 22 "github.com/google/go-cmp/cmp" 23 "github.com/google/go-cmp/cmp/cmpopts" 24 "testing" 25 ) 26 27 func TestFakeClient_SearchWithContext(t *testing.T) { 28 s := make(map[SearchRequest]SearchResponse) 29 issueList := []jira.Issue{ 30 { 31 ID: "123", 32 Fields: &jira.IssueFields{Project: jira.Project{Name: "test"}}, 33 }, 34 { 35 ID: "1234", 36 Fields: &jira.IssueFields{Project: jira.Project{Name: "test"}}, 37 }, 38 { 39 ID: "12345", 40 Fields: &jira.IssueFields{Project: jira.Project{Name: "test"}}, 41 }, 42 } 43 searchOptions := &jira.SearchOptions{MaxResults: 50, StartAt: 0} 44 45 s[SearchRequest{query: "project=test", options: searchOptions}] = SearchResponse{ 46 issues: issueList, 47 response: &jira.Response{StartAt: 0, MaxResults: 3, Total: 3}, 48 error: nil, 49 } 50 fakeClient := &FakeClient{SearchResponses: s} 51 52 r, v, err := fakeClient.SearchWithContext(context.Background(), "project=test", searchOptions) 53 if err != nil { 54 t.Fatalf("unexpected error from search: %s", err) 55 } 56 cmpOption := cmpopts.IgnoreUnexported(jira.Date{}) 57 if diff := cmp.Diff(r, issueList, cmpOption); diff != "" { 58 t.Fatalf("incorrect issues from search: %v", diff) 59 } 60 if diff := cmp.Diff(&jira.Response{StartAt: 0, MaxResults: 3, Total: 3}, v, cmpOption); diff != "" { 61 t.Fatalf("incorrect metadata from search: %v", diff) 62 } 63 64 r, v, err = fakeClient.SearchWithContext(context.Background(), "unknown_query=fail", searchOptions) 65 if r != nil { 66 t.Fatalf("expected empty result for an invalid query, but got: %v", r) 67 } 68 if r != nil { 69 t.Fatalf("expected no metadata for an invalid query, but got: %v", v) 70 } 71 if err == nil { 72 t.Fatal("expected invalid query to fail, but got no error") 73 } 74 } 75 76 func TestFakeClient_GetProjectVersions(t *testing.T) { 77 fakeClient := &FakeClient{ 78 ProjectVersions: map[string][]*jira.Version{ 79 "ABC": { 80 { 81 Name: "Version1", 82 }, 83 { 84 Name: "Version2", 85 }, 86 { 87 Name: "Version3", 88 }, 89 }, 90 }, 91 } 92 93 for _, project := range []string{"ABC", "FOO"} { 94 versions, err := fakeClient.GetProjectVersions(project) 95 if len(versions) != len(fakeClient.ProjectVersions[project]) { 96 t.Fatalf("expected: %d results, but got: %d", len(fakeClient.ProjectVersions[project]), len(versions)) 97 } 98 if err != nil { 99 t.Fatalf("Error: %v", err) 100 } 101 } 102 }