(t *testing.T)
| 31 | } |
| 32 | |
| 33 | func TestWalkThrough(t *testing.T) { |
| 34 | originDatabase := &storepb.DatabaseSchemaMetadata{ |
| 35 | Name: "test", |
| 36 | } |
| 37 | |
| 38 | tests := []testData{} |
| 39 | filepath := filepath.Join("testdata", "walk_through.yaml") |
| 40 | yamlFile, err := os.Open(filepath) |
| 41 | require.NoError(t, err) |
| 42 | defer yamlFile.Close() |
| 43 | |
| 44 | byteValue, err := io.ReadAll(yamlFile) |
| 45 | require.NoError(t, err) |
| 46 | err = yaml.Unmarshal(byteValue, &tests) |
| 47 | require.NoError(t, err) |
| 48 | sm := sheet.NewManager() |
| 49 | |
| 50 | for _, test := range tests { |
| 51 | // Make a deep copy to avoid mutation across tests |
| 52 | protoData, ok := proto.Clone(originDatabase).(*storepb.DatabaseSchemaMetadata) |
| 53 | require.True(t, ok) |
| 54 | |
| 55 | // Create DatabaseMetadata for walk-through |
| 56 | state := model.NewDatabaseMetadata(protoData, nil, nil, storepb.Engine_MYSQL, !test.IgnoreCaseSensitive) |
| 57 | |
| 58 | stmts, _ := sm.GetStatementsForChecks(storepb.Engine_MYSQL, test.Statement) |
| 59 | asts := base.ExtractASTs(stmts) |
| 60 | advice := WalkThroughOmni(schema.WalkThroughContext{RawSQL: test.Statement}, state, asts) |
| 61 | if advice != nil { |
| 62 | // Compare the advice fields |
| 63 | require.NotNil(t, test.Advice, "unexpected advice for statement %q: %+v", test.Statement, advice) |
| 64 | require.Equal(t, test.Advice.Code, advice.Code, "statement %q advice %+v", test.Statement, advice) |
| 65 | require.Equal(t, test.Advice.Content, advice.Content, "statement %q advice %+v", test.Statement, advice) |
| 66 | continue |
| 67 | } |
| 68 | |
| 69 | // Skip comparison if want is empty (error cases) |
| 70 | if test.Want == "" { |
| 71 | continue |
| 72 | } |
| 73 | |
| 74 | want := &storepb.DatabaseSchemaMetadata{} |
| 75 | err = common.ProtojsonUnmarshaler.Unmarshal([]byte(test.Want), want) |
| 76 | require.NoError(t, err) |
| 77 | result := state.GetProto() |
| 78 | diff := cmp.Diff(want, result, protocmp.Transform(), |
| 79 | protocmp.SortRepeatedFields(&storepb.DatabaseSchemaMetadata{}, "schemas"), |
| 80 | protocmp.SortRepeatedFields(&storepb.SchemaMetadata{}, "tables", "views"), |
| 81 | protocmp.SortRepeatedFields(&storepb.TableMetadata{}, "indexes", "columns"), |
| 82 | ) |
| 83 | require.Empty(t, diff, "statement %q", test.Statement) |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | func TestWalkThroughOmniCreateTableIfNotExistsCTASExistingTable(t *testing.T) { |
| 88 | originDatabase := &storepb.DatabaseSchemaMetadata{ |
nothing calls this directly
no test coverage detected