Skip to content

Commit

Permalink
fix unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
sjjian committed Dec 3, 2021
1 parent 2a2872d commit 0de6d6e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 57 deletions.
52 changes: 26 additions & 26 deletions sqle/driver/mysql/audit_offline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func TestCheckObjectNameUsingKeywordOffline(t *testing.T) {
"INDEX `show` (v1)"+
")ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT=\"unit test\";",
newTestResult().addResult(rulepkg.DDLCheckObjectNameUsingKeyword, "select, create, show").
addResult(rulepkg.DDLCheckIndexPrefix),
addResult(rulepkg.DDLCheckIndexPrefix, "idx_"),
)
}

Expand All @@ -251,7 +251,7 @@ v1 varchar(255) NOT NULL DEFAULT "unit test" COMMENT "unit test",
v2 varchar(255) NOT NULL DEFAULT "unit test" COMMENT "unit test",
PRIMARY KEY (id)
)ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT="unit test";`, length65),
newTestResult().addResult(rulepkg.DDLCheckObjectNameLength),
newTestResult().addResult(rulepkg.DDLCheckObjectNameLength, 64),
)

runDefaultRulesInspectCase(t, "create_table: columns length > 64", DefaultMysqlInspectOffline(),
Expand All @@ -262,7 +262,7 @@ id bigint unsigned NOT NULL AUTO_INCREMENT COMMENT "unit test",
v2 varchar(255) NOT NULL DEFAULT "unit test" COMMENT "unit test",
PRIMARY KEY (id)
)ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT="unit test";`, length65),
newTestResult().addResult(rulepkg.DDLCheckObjectNameLength),
newTestResult().addResult(rulepkg.DDLCheckObjectNameLength, 64),
)

runDefaultRulesInspectCase(t, "create_table: index length > 64", DefaultMysqlInspectOffline(),
Expand All @@ -274,37 +274,37 @@ v2 varchar(255) NOT NULL DEFAULT "unit test" COMMENT "unit test",
PRIMARY KEY (id),
INDEX idx_%s (v1)
)ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT="unit test";`, length65),
newTestResult().addResult(rulepkg.DDLCheckObjectNameLength),
newTestResult().addResult(rulepkg.DDLCheckObjectNameLength, 64),
)

runDefaultRulesInspectCase(t, "alter_table: table length > 64", DefaultMysqlInspectOffline(),
fmt.Sprintf(`
ALTER TABLE exist_db.exist_tb_1 RENAME %s;`, length65),
newTestResult().addResult(rulepkg.DDLCheckObjectNameLength),
newTestResult().addResult(rulepkg.DDLCheckObjectNameLength, 64),
)

runDefaultRulesInspectCase(t, "alter_table:Add column length > 64", DefaultMysqlInspectOffline(),
fmt.Sprintf(`
ALTER TABLE exist_db.exist_tb_1 ADD COLUMN %s varchar(255) NOT NULL DEFAULT "unit test" COMMENT "unit test";`, length65),
newTestResult().addResult(rulepkg.DDLCheckObjectNameLength),
newTestResult().addResult(rulepkg.DDLCheckObjectNameLength, 64),
)

runDefaultRulesInspectCase(t, "alter_table:change column length > 64", DefaultMysqlInspectOffline(),
fmt.Sprintf(`
ALTER TABLE exist_db.exist_tb_1 CHANGE COLUMN v1 %s varchar(255) NOT NULL DEFAULT "unit test" COMMENT "unit test";`, length65),
newTestResult().addResult(rulepkg.DDLCheckObjectNameLength),
newTestResult().addResult(rulepkg.DDLCheckObjectNameLength, 64),
)

runDefaultRulesInspectCase(t, "alter_table: Add index length > 64", DefaultMysqlInspectOffline(),
fmt.Sprintf(`
ALTER TABLE exist_db.exist_tb_1 ADD index idx_%s (v1);`, length65),
newTestResult().addResult(rulepkg.DDLCheckObjectNameLength),
newTestResult().addResult(rulepkg.DDLCheckObjectNameLength, 64),
)

runDefaultRulesInspectCase(t, "alter_table:rename index length > 64", DefaultMysqlInspectOffline(),
fmt.Sprintf(`
ALTER TABLE exist_db.exist_tb_1 RENAME index idx_1 TO idx_%s;`, length65),
newTestResult().addResult(rulepkg.DDLCheckObjectNameLength),
newTestResult().addResult(rulepkg.DDLCheckObjectNameLength, 64),
)
}

Expand Down Expand Up @@ -438,7 +438,7 @@ INDEX idx_5 (id),
INDEX idx_6 (id)
)ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT="unit test";
`,
newTestResult().addResult(rulepkg.DDLCheckIndexCount),
newTestResult().addResult(rulepkg.DDLCheckIndexCount, 5),
)
}

Expand Down Expand Up @@ -472,7 +472,7 @@ PRIMARY KEY (id),
INDEX idx_1 (id,v1,v2,v3,v4,v5)
)ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT="unit test";
`,
newTestResult().addResult(rulepkg.DDLCheckCompositeIndexMax),
newTestResult().addResult(rulepkg.DDLCheckCompositeIndexMax, 3),
)
}

Expand Down Expand Up @@ -616,21 +616,21 @@ PRIMARY KEY (id),
INDEX index_1 (v1)
)ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT="unit test";
`,
newTestResult().addResult(rulepkg.DDLCheckIndexPrefix),
newTestResult().addResult(rulepkg.DDLCheckIndexPrefix, "idx_"),
)

runDefaultRulesInspectCase(t, "alter_table: index prefix not idx_", DefaultMysqlInspectOffline(),
`
ALTER TABLE exist_db.exist_tb_1 ADD INDEX index_1(v1);
`,
newTestResult().addResult(rulepkg.DDLCheckIndexPrefix),
newTestResult().addResult(rulepkg.DDLCheckIndexPrefix, "idx_"),
)

runDefaultRulesInspectCase(t, "create_index: index prefix not idx_", DefaultMysqlInspectOffline(),
`
CREATE INDEX index_1 ON exist_db.exist_tb_1(v1);
`,
newTestResult().addResult(rulepkg.DDLCheckIndexPrefix),
newTestResult().addResult(rulepkg.DDLCheckIndexPrefix, "idx_"),
)

for _, sql := range []string{
Expand Down Expand Up @@ -658,21 +658,21 @@ PRIMARY KEY (id),
UNIQUE INDEX index_1 (v1)
)ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT="unit test";
`,
newTestResult().addResult(rulepkg.DDLCheckUniqueIndexPrefix),
newTestResult().addResult(rulepkg.DDLCheckUniqueIndexPrefix, "uniq_"),
)

runSingleRuleInspectCase(rule, t, "alter_table: unique index prefix not uniq_", DefaultMysqlInspectOffline(),
`
ALTER TABLE exist_db.exist_tb_1 ADD UNIQUE INDEX index_1(v1);
`,
newTestResult().addResult(rulepkg.DDLCheckUniqueIndexPrefix),
newTestResult().addResult(rulepkg.DDLCheckUniqueIndexPrefix, "uniq_"),
)

runSingleRuleInspectCase(rule, t, "create_index: unique index prefix not uniq_", DefaultMysqlInspectOffline(),
`
CREATE UNIQUE INDEX index_1 ON exist_db.exist_tb_1(v1);
`,
newTestResult().addResult(rulepkg.DDLCheckUniqueIndexPrefix),
newTestResult().addResult(rulepkg.DDLCheckUniqueIndexPrefix, "uniq_"),
)

for _, sql := range []string{
Expand Down Expand Up @@ -890,12 +890,12 @@ insert into exist_db.exist_tb_1 (id,v1,v2) values (?,?,?),(?,?,?);
func TestCheckBatchInsertListsMaxOffline(t *testing.T) {
rule := rulepkg.RuleHandlerMap[rulepkg.DMLCheckBatchInsertListsMax].Rule
// defult 5000, unit testing :4
rule.Value = "4"
rule.Params.SetParamValue(rulepkg.DefaultSingleParamKeyName, "4")
runSingleRuleInspectCase(rule, t, "insert:check batch insert lists max", DefaultMysqlInspectOffline(),
`
insert into exist_db.exist_tb_1 (id,v1,v2) values (1,"1","1"),(2,"2","2"),(3,"3","3"),(4,"4","4"),(5,"5","5");
`,
newTestResult().addResult(rulepkg.DMLCheckBatchInsertListsMax, rule.Value),
newTestResult().addResult(rulepkg.DMLCheckBatchInsertListsMax, 4),
)

runSingleRuleInspectCase(rule, t, "insert: passing the check batch insert lists max", DefaultMysqlInspectOffline(),
Expand All @@ -909,12 +909,12 @@ insert into exist_db.exist_tb_1 (id,v1,v2) values (1,"1","1"),(2,"2","2"),(3,"3"
func TestCheckBatchInsertListsMax_FPOffline(t *testing.T) {
rule := rulepkg.RuleHandlerMap[rulepkg.DMLCheckBatchInsertListsMax].Rule
// defult 5000, unit testing :4
rule.Value = "4"
rule.Params.SetParamValue(rulepkg.DefaultSingleParamKeyName, "4")
runSingleRuleInspectCase(rule, t, "[fp]insert:check batch insert lists max", DefaultMysqlInspectOffline(),
`
insert into exist_db.exist_tb_1 (id,v1,v2) values (?,?,?),(?,?,?),(?,?,?),(?,?,?),(?,?,?);
`,
newTestResult().addResult(rulepkg.DMLCheckBatchInsertListsMax, rule.Value),
newTestResult().addResult(rulepkg.DMLCheckBatchInsertListsMax, 4),
)

runSingleRuleInspectCase(rule, t, "[fp]insert: passing the check batch insert lists max", DefaultMysqlInspectOffline(),
Expand Down Expand Up @@ -1449,7 +1449,7 @@ func TestCheckNeedlessFuncOffline(t *testing.T) {
desc,
DefaultMysqlInspectOffline(),
sql,
newTestResult().addResult(rulepkg.DMLCheckNeedlessFunc))
newTestResult().addResult(rulepkg.DMLCheckNeedlessFunc, "sha(),sqrt(),md5()"))
}

for desc, sql := range map[string]string{
Expand All @@ -1475,7 +1475,7 @@ func TestCheckNeedlessFunc_FPOffline(t *testing.T) {
desc,
DefaultMysqlInspectOffline(),
sql,
newTestResult().addResult(rulepkg.DMLCheckNeedlessFunc))
newTestResult().addResult(rulepkg.DMLCheckNeedlessFunc, "sha(),sqrt(),md5()"))
}

for desc, sql := range map[string]string{
Expand All @@ -1501,7 +1501,7 @@ func TestCheckDatabaseSuffixOffline(t *testing.T) {
desc,
DefaultMysqlInspectOffline(),
sql,
newTestResult().addResult(rulepkg.DDLCheckDatabaseSuffix))
newTestResult().addResult(rulepkg.DDLCheckDatabaseSuffix, "_DB"))
}

for desc, sql := range map[string]string{
Expand Down Expand Up @@ -1690,7 +1690,7 @@ JOIN exist_db.exist_tb_4 ON exist_db.exist_tb_3.id = exist_db.exist_tb_4.id
desc,
inspector,
sql,
newTestResult().addResult(rulepkg.DMLCheckNumberOfJoinTables))
newTestResult().addResult(rulepkg.DMLCheckNumberOfJoinTables, 3))
}

for desc, sql := range map[string]string{
Expand Down Expand Up @@ -1745,7 +1745,7 @@ WHERE exist_db.exist_tb_1.v1 = ? AND exist_db.exist_tb_1.v2 = ?
desc,
inspector,
sql,
newTestResult().addResult(rulepkg.DMLCheckNumberOfJoinTables))
newTestResult().addResult(rulepkg.DMLCheckNumberOfJoinTables, 3))
}

for desc, sql := range map[string]string{
Expand Down
Loading

0 comments on commit 0de6d6e

Please sign in to comment.