(t *testing.T)
| 147 | } |
| 148 | |
| 149 | func TestFindCachedCollectionByNameOrId(t *testing.T) { |
| 150 | t.Parallel() |
| 151 | |
| 152 | app, _ := tests.NewTestApp() |
| 153 | defer app.Cleanup() |
| 154 | |
| 155 | totalQueries := 0 |
| 156 | app.ConcurrentDB().(*dbx.DB).QueryLogFunc = func(ctx context.Context, t time.Duration, sql string, rows *sql.Rows, err error) { |
| 157 | totalQueries++ |
| 158 | } |
| 159 | |
| 160 | run := func(withCache bool) { |
| 161 | scenarios := []struct { |
| 162 | nameOrId string |
| 163 | expectError bool |
| 164 | }{ |
| 165 | {"", true}, |
| 166 | {"missing", true}, |
| 167 | {"wsmn24bux7wo113", false}, |
| 168 | {"demo1", false}, |
| 169 | {"DEMO1", false}, // case insensitive |
| 170 | } |
| 171 | |
| 172 | var expectedTotalQueries int |
| 173 | |
| 174 | if withCache { |
| 175 | err := app.ReloadCachedCollections() |
| 176 | if err != nil { |
| 177 | t.Fatal(err) |
| 178 | } |
| 179 | } else { |
| 180 | app.Store().Reset(nil) |
| 181 | expectedTotalQueries = len(scenarios) |
| 182 | } |
| 183 | |
| 184 | totalQueries = 0 |
| 185 | |
| 186 | for i, s := range scenarios { |
| 187 | t.Run(fmt.Sprintf("%d_%s", i, s.nameOrId), func(t *testing.T) { |
| 188 | model, err := app.FindCachedCollectionByNameOrId(s.nameOrId) |
| 189 | |
| 190 | hasErr := err != nil |
| 191 | if hasErr != s.expectError { |
| 192 | t.Fatalf("Expected hasErr to be %v, got %v (%v)", s.expectError, hasErr, err) |
| 193 | } |
| 194 | |
| 195 | if model != nil && model.Id != s.nameOrId && !strings.EqualFold(model.Name, s.nameOrId) { |
| 196 | t.Fatalf("Expected model with identifier %s, got %v", s.nameOrId, model) |
| 197 | } |
| 198 | }) |
| 199 | } |
| 200 | |
| 201 | if totalQueries != expectedTotalQueries { |
| 202 | t.Fatalf("Expected %d totalQueries, got %d", expectedTotalQueries, totalQueries) |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | run(true) |
nothing calls this directly
no test coverage detected
searching dependent graphs…