MCPcopy Index your code
hub / github.com/bytebase/bytebase / RunSQLReviewRuleTest

Function RunSQLReviewRuleTest

backend/plugin/advisor/utils_for_tests.go:342–460  ·  view source on GitHub ↗

RunSQLReviewRuleTest helps to test the SQL review rule. The rule parameter should be a complete rule with Type, Level, and Payload already set.

(t *testing.T, rule *storepb.SQLReviewRule, dbType storepb.Engine, record bool)

Source from the content-addressed store, hash-verified

340// RunSQLReviewRuleTest helps to test the SQL review rule.
341// The rule parameter should be a complete rule with Type, Level, and Payload already set.
342func RunSQLReviewRuleTest(t *testing.T, rule *storepb.SQLReviewRule, dbType storepb.Engine, record bool) {
343 require.NotNil(t, rule, "rule must not be nil")
344 require.NotEqual(t, storepb.SQLReviewRule_TYPE_UNSPECIFIED, rule.Type, "rule type must be specified")
345
346 var tests []TestCase
347
348 fileName := strings.Map(func(r rune) rune {
349 switch r {
350 case '.', '-':
351 return '_'
352 default:
353 return r
354 }
355 }, strings.ToLower(rule.Type.String()))
356 filepath := filepath.Join("test", fileName+".yaml")
357 yamlFile, err := os.Open(filepath)
358 require.NoError(t, err)
359 defer yamlFile.Close()
360
361 byteValue, err := io.ReadAll(yamlFile)
362 require.NoError(t, err)
363 err = yaml.Unmarshal(byteValue, &tests)
364 require.NoError(t, err, rule.Type)
365
366 sm := sheet.NewManager()
367 for i, tc := range tests {
368 // Set metadata and database-specific settings based on engine type
369 var schemaMetadata *storepb.DatabaseSchemaMetadata
370 curDB := "TEST_DB"
371 isCaseSensitive := false
372
373 switch dbType {
374 case storepb.Engine_POSTGRES:
375 schemaMetadata = MockPostgreSQLDatabase
376 isCaseSensitive = true
377 case storepb.Engine_MSSQL:
378 schemaMetadata = MockMSSQLDatabase
379 curDB = "master"
380 case storepb.Engine_MYSQL:
381 schemaMetadata = MockMySQLDatabase
382 case storepb.Engine_ORACLE:
383 schemaMetadata = MockOracleDatabase
384 curDB = "TEST_DB"
385 // Match production: store.IsObjectCaseSensitive(Oracle) returns
386 // true via the default branch in backend/store/instance.go. Test
387 // fixtures use upper-case identifiers like the real Oracle sync.
388 isCaseSensitive = true
389 default:
390 // Fallback to MySQL for engines without specific mock
391 schemaMetadata = MockMySQLDatabase
392 }
393
394 // Create OriginalMetadata as DatabaseMetadata (read-only)
395 // Clone to avoid mutations affecting future test cases
396 metadata, ok := proto.Clone(schemaMetadata).(*storepb.DatabaseSchemaMetadata)
397 require.True(t, ok, "failed to clone metadata")
398 originalMetadata := model.NewDatabaseMetadata(metadata, nil, nil, dbType, isCaseSensitive)
399

Callers 6

TestOracleRulesFunction · 0.92
TestMSSQLRulesFunction · 0.92
TestMySQLRulesFunction · 0.92
TestPostgreSQLRulesFunction · 0.92
TestTiDBRulesFunction · 0.92
TestSnowflakeRulesFunction · 0.92

Calls 10

NewManagerFunction · 0.92
NewDatabaseMetadataFunction · 0.92
RecordFunction · 0.92
SQLReviewCheckFunction · 0.85
JoinMethod · 0.80
UnmarshalMethod · 0.80
StringMethod · 0.65
OpenMethod · 0.65
CloseMethod · 0.65
GetStartPositionMethod · 0.45

Tested by 6

TestOracleRulesFunction · 0.74
TestMSSQLRulesFunction · 0.74
TestMySQLRulesFunction · 0.74
TestPostgreSQLRulesFunction · 0.74
TestTiDBRulesFunction · 0.74
TestSnowflakeRulesFunction · 0.74