|
| 1 | +// Copyright (c) 2026 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 mysql |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "database/sql" |
| 20 | + "fmt" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/DATA-DOG/go-sqlmock" |
| 24 | + "github.com/go-sql-driver/mysql" |
| 25 | + "github.com/stretchr/testify/assert" |
| 26 | + "github.com/stretchr/testify/require" |
| 27 | + |
| 28 | + "github.com/uber/submitqueue/submitqueue/extension/conflict/tango" |
| 29 | + "github.com/uber/submitqueue/submitqueue/extension/storage" |
| 30 | +) |
| 31 | + |
| 32 | +func setupDictionaryStoreTest(t *testing.T) (*sql.DB, sqlmock.Sqlmock, tango.DictionaryStore) { |
| 33 | + t.Helper() |
| 34 | + |
| 35 | + db, mock, err := sqlmock.New() |
| 36 | + require.NoError(t, err) |
| 37 | + |
| 38 | + return db, mock, NewDictionaryStore(db, testMetrics(), "monorepo") |
| 39 | +} |
| 40 | + |
| 41 | +func TestTargetDictionaryStore_Create(t *testing.T) { |
| 42 | + label := tango.TargetLabel{Queue: "monorepo", ID: 42, Label: "//app/server:lib", CreatedAt: 1700000000000} |
| 43 | + mismatched := label |
| 44 | + mismatched.Queue = "other-queue" |
| 45 | + |
| 46 | + tests := []struct { |
| 47 | + name string |
| 48 | + label tango.TargetLabel |
| 49 | + setup func(mock sqlmock.Sqlmock) |
| 50 | + wantErr bool |
| 51 | + wantErrIs error |
| 52 | + }{ |
| 53 | + { |
| 54 | + name: "success", |
| 55 | + label: label, |
| 56 | + setup: func(mock sqlmock.Sqlmock) { |
| 57 | + mock.ExpectExec("INSERT INTO target_dictionary"). |
| 58 | + WithArgs("monorepo", uint64(42), label.Label, label.CreatedAt). |
| 59 | + WillReturnResult(sqlmock.NewResult(1, 1)) |
| 60 | + }, |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "id already recorded", |
| 64 | + label: label, |
| 65 | + setup: func(mock sqlmock.Sqlmock) { |
| 66 | + mock.ExpectExec("INSERT INTO target_dictionary"). |
| 67 | + WillReturnError(&mysql.MySQLError{Number: mysqlErrDuplicateEntry}) |
| 68 | + }, |
| 69 | + wantErr: true, |
| 70 | + wantErrIs: storage.ErrAlreadyExists, |
| 71 | + }, |
| 72 | + { |
| 73 | + name: "insert error", |
| 74 | + label: label, |
| 75 | + setup: func(mock sqlmock.Sqlmock) { |
| 76 | + mock.ExpectExec("INSERT INTO target_dictionary"). |
| 77 | + WillReturnError(fmt.Errorf("data too long for column label")) |
| 78 | + }, |
| 79 | + wantErr: true, |
| 80 | + }, |
| 81 | + { |
| 82 | + name: "queue differs from the bound queue", |
| 83 | + label: mismatched, |
| 84 | + setup: func(mock sqlmock.Sqlmock) {}, |
| 85 | + wantErr: true, |
| 86 | + }, |
| 87 | + } |
| 88 | + for _, tt := range tests { |
| 89 | + t.Run(tt.name, func(t *testing.T) { |
| 90 | + db, mock, store := setupDictionaryStoreTest(t) |
| 91 | + defer db.Close() |
| 92 | + tt.setup(mock) |
| 93 | + |
| 94 | + err := store.Create(context.Background(), tt.label) |
| 95 | + |
| 96 | + if tt.wantErr { |
| 97 | + require.Error(t, err) |
| 98 | + if tt.wantErrIs != nil { |
| 99 | + assert.ErrorIs(t, err, tt.wantErrIs) |
| 100 | + } |
| 101 | + } else { |
| 102 | + require.NoError(t, err) |
| 103 | + } |
| 104 | + assert.NoError(t, mock.ExpectationsWereMet()) |
| 105 | + }) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func TestTargetDictionaryStore_Get(t *testing.T) { |
| 110 | + columns := []string{"label", "created_at"} |
| 111 | + |
| 112 | + tests := []struct { |
| 113 | + name string |
| 114 | + setup func(mock sqlmock.Sqlmock) |
| 115 | + want tango.TargetLabel |
| 116 | + wantFound bool |
| 117 | + wantErr bool |
| 118 | + }{ |
| 119 | + { |
| 120 | + name: "found", |
| 121 | + setup: func(mock sqlmock.Sqlmock) { |
| 122 | + mock.ExpectQuery("SELECT label, created_at FROM target_dictionary"). |
| 123 | + WithArgs("monorepo", uint64(42)). |
| 124 | + WillReturnRows(sqlmock.NewRows(columns).AddRow("//app:lib", int64(10))) |
| 125 | + }, |
| 126 | + want: tango.TargetLabel{Queue: "monorepo", ID: 42, Label: "//app:lib", CreatedAt: 10}, |
| 127 | + wantFound: true, |
| 128 | + }, |
| 129 | + { |
| 130 | + name: "none recorded", |
| 131 | + setup: func(mock sqlmock.Sqlmock) { |
| 132 | + mock.ExpectQuery("SELECT label, created_at FROM target_dictionary"). |
| 133 | + WithArgs("monorepo", uint64(42)). |
| 134 | + WillReturnRows(sqlmock.NewRows(columns)) |
| 135 | + }, |
| 136 | + }, |
| 137 | + { |
| 138 | + name: "query error", |
| 139 | + setup: func(mock sqlmock.Sqlmock) { |
| 140 | + mock.ExpectQuery("SELECT label, created_at FROM target_dictionary"). |
| 141 | + WillReturnError(fmt.Errorf("connection reset")) |
| 142 | + }, |
| 143 | + wantErr: true, |
| 144 | + }, |
| 145 | + } |
| 146 | + for _, tt := range tests { |
| 147 | + t.Run(tt.name, func(t *testing.T) { |
| 148 | + db, mock, store := setupDictionaryStoreTest(t) |
| 149 | + defer db.Close() |
| 150 | + tt.setup(mock) |
| 151 | + |
| 152 | + got, found, err := store.Get(context.Background(), 42) |
| 153 | + |
| 154 | + if tt.wantErr { |
| 155 | + require.Error(t, err) |
| 156 | + } else { |
| 157 | + require.NoError(t, err) |
| 158 | + assert.Equal(t, tt.wantFound, found) |
| 159 | + assert.Equal(t, tt.want, got) |
| 160 | + } |
| 161 | + assert.NoError(t, mock.ExpectationsWereMet()) |
| 162 | + }) |
| 163 | + } |
| 164 | +} |
0 commit comments