|
| 1 | +package bitbucket |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "slices" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/KubeRocketCI/gitfusion/pkg/xiter" |
| 9 | + bitbucketcl "github.com/ktrysmt/go-bitbucket" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | +) |
| 12 | + |
| 13 | +// helper to create a bitbucketcl.RepositoryBranch with a given name |
| 14 | +func branch(name string) bitbucketcl.RepositoryBranch { |
| 15 | + return bitbucketcl.RepositoryBranch{Name: name} |
| 16 | +} |
| 17 | + |
| 18 | +// helper to create a *bitbucketcl.RepositoryBranches response |
| 19 | +func branchesResponse(branches []bitbucketcl.RepositoryBranch, next string, page int) *bitbucketcl.RepositoryBranches { |
| 20 | + return &bitbucketcl.RepositoryBranches{ |
| 21 | + Branches: branches, |
| 22 | + Next: next, |
| 23 | + Page: page, |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +func TestScanBitbucketBranches(t *testing.T) { |
| 28 | + tests := []struct { |
| 29 | + name string |
| 30 | + fetchPage func(rbo *bitbucketcl.RepositoryBranchOptions) (*bitbucketcl.RepositoryBranches, error) |
| 31 | + rbo *bitbucketcl.RepositoryBranchOptions |
| 32 | + want []*bitbucketcl.RepositoryBranch |
| 33 | + wantErr assert.ErrorAssertionFunc |
| 34 | + }{ |
| 35 | + { |
| 36 | + name: "single page", |
| 37 | + fetchPage: func(rbo *bitbucketcl.RepositoryBranchOptions) (*bitbucketcl.RepositoryBranches, error) { |
| 38 | + return branchesResponse([]bitbucketcl.RepositoryBranch{ |
| 39 | + branch("main"), |
| 40 | + branch("develop"), |
| 41 | + }, "", 1), nil |
| 42 | + }, |
| 43 | + rbo: &bitbucketcl.RepositoryBranchOptions{}, |
| 44 | + want: []*bitbucketcl.RepositoryBranch{ |
| 45 | + {Name: "main"}, |
| 46 | + {Name: "develop"}, |
| 47 | + }, |
| 48 | + wantErr: assert.NoError, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "multiple pages", |
| 52 | + fetchPage: func() func(rbo *bitbucketcl.RepositoryBranchOptions) (*bitbucketcl.RepositoryBranches, error) { |
| 53 | + pages := map[int]*bitbucketcl.RepositoryBranches{ |
| 54 | + 1: branchesResponse([]bitbucketcl.RepositoryBranch{branch("main")}, "next", 1), |
| 55 | + 2: branchesResponse([]bitbucketcl.RepositoryBranch{branch("develop")}, "", 2), |
| 56 | + } |
| 57 | + return func(rbo *bitbucketcl.RepositoryBranchOptions) (*bitbucketcl.RepositoryBranches, error) { |
| 58 | + if rbo.PageNum == 0 { |
| 59 | + rbo.PageNum = 1 |
| 60 | + } |
| 61 | + if resp, ok := pages[rbo.PageNum]; ok { |
| 62 | + return resp, nil |
| 63 | + } |
| 64 | + return branchesResponse([]bitbucketcl.RepositoryBranch{}, "", rbo.PageNum), nil |
| 65 | + } |
| 66 | + }(), |
| 67 | + rbo: &bitbucketcl.RepositoryBranchOptions{}, |
| 68 | + want: []*bitbucketcl.RepositoryBranch{ |
| 69 | + {Name: "main"}, |
| 70 | + {Name: "develop"}, |
| 71 | + }, |
| 72 | + wantErr: assert.NoError, |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "error on first page", |
| 76 | + fetchPage: func(rbo *bitbucketcl.RepositoryBranchOptions) (*bitbucketcl.RepositoryBranches, error) { |
| 77 | + return nil, errors.New("fetch failed") |
| 78 | + }, |
| 79 | + rbo: &bitbucketcl.RepositoryBranchOptions{}, |
| 80 | + want: nil, |
| 81 | + wantErr: assert.Error, |
| 82 | + }, |
| 83 | + { |
| 84 | + name: "error on second page", |
| 85 | + fetchPage: func() func(rbo *bitbucketcl.RepositoryBranchOptions) (*bitbucketcl.RepositoryBranches, error) { |
| 86 | + calls := 0 |
| 87 | + return func(rbo *bitbucketcl.RepositoryBranchOptions) (*bitbucketcl.RepositoryBranches, error) { |
| 88 | + calls++ |
| 89 | + if calls == 1 { |
| 90 | + return branchesResponse([]bitbucketcl.RepositoryBranch{branch("main")}, "next", 1), nil |
| 91 | + } |
| 92 | + return nil, errors.New("fetch failed on second page") |
| 93 | + } |
| 94 | + }(), |
| 95 | + rbo: &bitbucketcl.RepositoryBranchOptions{}, |
| 96 | + want: []*bitbucketcl.RepositoryBranch{ |
| 97 | + {Name: "main"}, |
| 98 | + }, |
| 99 | + wantErr: assert.Error, |
| 100 | + }, |
| 101 | + { |
| 102 | + name: "empty response", |
| 103 | + fetchPage: func(rbo *bitbucketcl.RepositoryBranchOptions) (*bitbucketcl.RepositoryBranches, error) { |
| 104 | + return branchesResponse([]bitbucketcl.RepositoryBranch{}, "", 1), nil |
| 105 | + }, |
| 106 | + rbo: &bitbucketcl.RepositoryBranchOptions{}, |
| 107 | + want: []*bitbucketcl.RepositoryBranch{}, |
| 108 | + wantErr: assert.NoError, |
| 109 | + }, |
| 110 | + } |
| 111 | + |
| 112 | + for _, tt := range tests { |
| 113 | + t.Run(tt.name, func(t *testing.T) { |
| 114 | + got, err := xiter.CollectFromScan(ScanBitbucketBranches(tt.fetchPage, tt.rbo)) |
| 115 | + |
| 116 | + // Compare the results using slices.EqualFunc for proper comparison |
| 117 | + assert.True(t, slices.EqualFunc(got, tt.want, func(a, b *bitbucketcl.RepositoryBranch) bool { |
| 118 | + return a.Name == b.Name |
| 119 | + })) |
| 120 | + |
| 121 | + tt.wantErr(t, err) |
| 122 | + }) |
| 123 | + } |
| 124 | +} |
0 commit comments