|
| 1 | +/* |
| 2 | +Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +contributor license agreements. See the NOTICE file distributed with |
| 4 | +this work for additional information regarding copyright ownership. |
| 5 | +The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +(the "License"); you may not use this file except in compliance with |
| 7 | +the License. You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +*/ |
| 17 | + |
| 18 | +package migrationscripts |
| 19 | + |
| 20 | +import ( |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/apache/incubator-devlake/core/context" |
| 24 | + "github.com/apache/incubator-devlake/core/dal" |
| 25 | + "github.com/apache/incubator-devlake/core/errors" |
| 26 | +) |
| 27 | + |
| 28 | +type modifyColumnTypeCall struct { |
| 29 | + tableName string |
| 30 | + columnName string |
| 31 | + columnType string |
| 32 | +} |
| 33 | + |
| 34 | +type recordingDal struct { |
| 35 | + dal.Dal |
| 36 | + call *modifyColumnTypeCall |
| 37 | +} |
| 38 | + |
| 39 | +func (d *recordingDal) ModifyColumnType(tableName string, columnName string, columnType string) errors.Error { |
| 40 | + d.call = &modifyColumnTypeCall{tableName, columnName, columnType} |
| 41 | + return nil |
| 42 | +} |
| 43 | + |
| 44 | +type basicResWithDal struct { |
| 45 | + context.BasicRes |
| 46 | + database dal.Dal |
| 47 | +} |
| 48 | + |
| 49 | +func (r *basicResWithDal) GetDal() dal.Dal { |
| 50 | + return r.database |
| 51 | +} |
| 52 | + |
| 53 | +func TestChangeIssueComponentToText(t *testing.T) { |
| 54 | + database := new(recordingDal) |
| 55 | + script := new(changeIssueComponentToText) |
| 56 | + |
| 57 | + if err := script.Up(&basicResWithDal{database: database}); err != nil { |
| 58 | + t.Fatalf("migration failed: %v", err) |
| 59 | + } |
| 60 | + |
| 61 | + want := &modifyColumnTypeCall{"issues", "component", "text"} |
| 62 | + if database.call == nil || *database.call != *want { |
| 63 | + t.Fatalf("ModifyColumnType call = %#v, want %#v", database.call, want) |
| 64 | + } |
| 65 | + if script.Version() != 20260629120000 { |
| 66 | + t.Fatalf("Version() = %d, want 20260629120000", script.Version()) |
| 67 | + } |
| 68 | + if script.Name() != "change issues.component type to text" { |
| 69 | + t.Fatalf("Name() = %q, want %q", script.Name(), "change issues.component type to text") |
| 70 | + } |
| 71 | + |
| 72 | + found := false |
| 73 | + for _, registeredScript := range All() { |
| 74 | + if registeredScript.Version() == script.Version() { |
| 75 | + found = true |
| 76 | + break |
| 77 | + } |
| 78 | + } |
| 79 | + if !found { |
| 80 | + t.Fatalf("migration version %d is not registered", script.Version()) |
| 81 | + } |
| 82 | +} |
0 commit comments