|
| 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 batchchanges |
| 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/submitqueue/entity" |
| 24 | + storagemock "github.com/uber/submitqueue/submitqueue/extension/storage/mock" |
| 25 | + "go.uber.org/mock/gomock" |
| 26 | +) |
| 27 | + |
| 28 | +func TestCollect(t *testing.T) { |
| 29 | + ctrl := gomock.NewController(t) |
| 30 | + |
| 31 | + batch := entity.Batch{ID: "q/batch/1", Queue: "q", Contains: []string{"q/1", "q/2"}} |
| 32 | + req1 := entity.Request{ID: "q/1", Change: entity.Change{URIs: []string{"github://o/r/pull/1/a"}}} |
| 33 | + req2 := entity.Request{ID: "q/2", Change: entity.Change{URIs: []string{"github://o/r/pull/2/b"}}} |
| 34 | + rec1 := entity.ChangeRecord{Queue: "q", URI: "github://o/r/pull/1/a", RequestID: "q/1", |
| 35 | + Details: entity.ChangeDetails{ChangedFiles: []entity.ChangedFile{{Path: "f1", LinesAdded: 3}}}} |
| 36 | + // A stray record for the same URI owned by a different request must be skipped. |
| 37 | + recOther := entity.ChangeRecord{Queue: "q", URI: "github://o/r/pull/1/a", RequestID: "q/999"} |
| 38 | + rec2 := entity.ChangeRecord{Queue: "q", URI: "github://o/r/pull/2/b", RequestID: "q/2", |
| 39 | + Details: entity.ChangeDetails{ChangedFiles: []entity.ChangedFile{{Path: "f2", LinesAdded: 5}}}} |
| 40 | + |
| 41 | + reqStore := storagemock.NewMockRequestStore(ctrl) |
| 42 | + reqStore.EXPECT().Get(gomock.Any(), "q/1").Return(req1, nil) |
| 43 | + reqStore.EXPECT().Get(gomock.Any(), "q/2").Return(req2, nil) |
| 44 | + |
| 45 | + changeStore := storagemock.NewMockChangeStore(ctrl) |
| 46 | + changeStore.EXPECT().GetByURI(gomock.Any(), "q", "github://o/r/pull/1/a").Return([]entity.ChangeRecord{recOther, rec1}, nil) |
| 47 | + changeStore.EXPECT().GetByURI(gomock.Any(), "q", "github://o/r/pull/2/b").Return([]entity.ChangeRecord{rec2}, nil) |
| 48 | + |
| 49 | + store := storagemock.NewMockStorage(ctrl) |
| 50 | + store.EXPECT().GetRequestStore().Return(reqStore).AnyTimes() |
| 51 | + store.EXPECT().GetChangeStore().Return(changeStore).AnyTimes() |
| 52 | + |
| 53 | + got, err := Collect(context.Background(), store, batch) |
| 54 | + require.NoError(t, err) |
| 55 | + assert.Equal(t, "q/batch/1", got.BatchID) |
| 56 | + assert.Equal(t, "q", got.Queue) |
| 57 | + require.Len(t, got.Changes, 2) |
| 58 | + assert.Equal(t, "github://o/r/pull/1/a", got.Changes[0].URI) |
| 59 | + assert.Equal(t, "github://o/r/pull/2/b", got.Changes[1].URI) |
| 60 | + assert.Equal(t, 8, got.TotalLinesChanged()) |
| 61 | +} |
0 commit comments