|
| 1 | +// Copyright (c) 2025 Uber Technologies, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package fake |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/stretchr/testify/assert" |
| 22 | + "github.com/stretchr/testify/require" |
| 23 | + "github.com/uber/submitqueue/stovepipe/extension/sourcecontrol" |
| 24 | +) |
| 25 | + |
| 26 | +func TestNew_ImplementsInterface(t *testing.T) { |
| 27 | + var _ sourcecontrol.SourceControl = New(nil) |
| 28 | +} |
| 29 | + |
| 30 | +// history is ordered newest-first: c is the latest, a is the oldest ancestor. |
| 31 | +var history = []string{"git://repo/ref/c", "git://repo/ref/b", "git://repo/ref/a"} |
| 32 | + |
| 33 | +func TestLatest(t *testing.T) { |
| 34 | + tests := []struct { |
| 35 | + name string |
| 36 | + history []string |
| 37 | + want string |
| 38 | + wantErr bool |
| 39 | + }{ |
| 40 | + {name: "newest first", history: history, want: "git://repo/ref/c"}, |
| 41 | + {name: "empty history", history: nil, wantErr: true}, |
| 42 | + } |
| 43 | + for _, tt := range tests { |
| 44 | + t.Run(tt.name, func(t *testing.T) { |
| 45 | + got, err := New(tt.history).Latest(context.Background()) |
| 46 | + if tt.wantErr { |
| 47 | + require.ErrorIs(t, err, sourcecontrol.ErrNotFound) |
| 48 | + return |
| 49 | + } |
| 50 | + require.NoError(t, err) |
| 51 | + assert.Equal(t, tt.want, got) |
| 52 | + }) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func TestIsAncestor(t *testing.T) { |
| 57 | + tests := []struct { |
| 58 | + name string |
| 59 | + ancestor string |
| 60 | + descendant string |
| 61 | + want bool |
| 62 | + wantErr bool |
| 63 | + }{ |
| 64 | + {name: "older is ancestor of newer", ancestor: "git://repo/ref/a", descendant: "git://repo/ref/c", want: true}, |
| 65 | + {name: "newer is not ancestor of older", ancestor: "git://repo/ref/c", descendant: "git://repo/ref/a", want: false}, |
| 66 | + {name: "equal is ancestor of itself", ancestor: "git://repo/ref/b", descendant: "git://repo/ref/b", want: true}, |
| 67 | + {name: "unknown ancestor", ancestor: "git://repo/ref/x", descendant: "git://repo/ref/a", wantErr: true}, |
| 68 | + {name: "unknown descendant", ancestor: "git://repo/ref/a", descendant: "git://repo/ref/x", wantErr: true}, |
| 69 | + } |
| 70 | + for _, tt := range tests { |
| 71 | + t.Run(tt.name, func(t *testing.T) { |
| 72 | + got, err := New(history).IsAncestor(context.Background(), tt.ancestor, tt.descendant) |
| 73 | + if tt.wantErr { |
| 74 | + require.ErrorIs(t, err, sourcecontrol.ErrNotFound) |
| 75 | + return |
| 76 | + } |
| 77 | + require.NoError(t, err) |
| 78 | + assert.Equal(t, tt.want, got) |
| 79 | + }) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func TestHistory(t *testing.T) { |
| 84 | + tests := []struct { |
| 85 | + name string |
| 86 | + cursor string |
| 87 | + limit int |
| 88 | + wantItems []string |
| 89 | + wantCursor string |
| 90 | + wantErr bool |
| 91 | + }{ |
| 92 | + { |
| 93 | + name: "first page with next cursor", |
| 94 | + cursor: "", |
| 95 | + limit: 2, |
| 96 | + wantItems: []string{"git://repo/ref/c", "git://repo/ref/b"}, |
| 97 | + wantCursor: "git://repo/ref/a", |
| 98 | + }, |
| 99 | + { |
| 100 | + name: "second page reaches end", |
| 101 | + cursor: "git://repo/ref/a", |
| 102 | + limit: 2, |
| 103 | + wantItems: []string{"git://repo/ref/a"}, |
| 104 | + wantCursor: "", |
| 105 | + }, |
| 106 | + { |
| 107 | + name: "limit larger than remaining returns rest", |
| 108 | + cursor: "", |
| 109 | + limit: 10, |
| 110 | + wantItems: history, |
| 111 | + wantCursor: "", |
| 112 | + }, |
| 113 | + { |
| 114 | + name: "zero limit returns rest in one page", |
| 115 | + cursor: "", |
| 116 | + limit: 0, |
| 117 | + wantItems: history, |
| 118 | + wantCursor: "", |
| 119 | + }, |
| 120 | + { |
| 121 | + name: "unknown cursor", |
| 122 | + cursor: "git://repo/ref/x", |
| 123 | + limit: 2, |
| 124 | + wantErr: true, |
| 125 | + }, |
| 126 | + } |
| 127 | + for _, tt := range tests { |
| 128 | + t.Run(tt.name, func(t *testing.T) { |
| 129 | + got, err := New(history).History(context.Background(), tt.cursor, tt.limit) |
| 130 | + if tt.wantErr { |
| 131 | + require.ErrorIs(t, err, sourcecontrol.ErrNotFound) |
| 132 | + return |
| 133 | + } |
| 134 | + require.NoError(t, err) |
| 135 | + assert.Equal(t, tt.wantItems, got.Items) |
| 136 | + assert.Equal(t, tt.wantCursor, got.NextCursor) |
| 137 | + }) |
| 138 | + } |
| 139 | +} |
0 commit comments