MCPcopy
hub / github.com/XiaoMi/Gaea / checkRandomSQLs

Function checkRandomSQLs

proxy/plan/plan_test.go:225–274  ·  view source on GitHub ↗

用来判断全局表生成所有的DB和SQL是否和randomSqls对应,actual 没有多db 的情况,例如:"slice0": {"db0": {"sql1", "sql2"}, "db1": {"sql1"}}

(randomSqls []map[string]map[string][]string, actual map[string]map[string][]string)

Source from the content-addressed store, hash-verified

223
224// 用来判断全局表生成所有的DB和SQL是否和randomSqls对应,actual 没有多db 的情况,例如:"slice0": {"db0": {"sql1", "sql2"}, "db1": {"sql1"}}
225func checkRandomSQLs(randomSqls []map[string]map[string][]string, actual map[string]map[string][]string) bool {
226 // sliceMatch 检查两个切片是否包含相同的元素,不考虑顺序
227 sliceMatch := func(a, b []string) bool {
228 if len(a) != len(b) {
229 return false
230 }
231 count := make(map[string]int)
232 for _, v := range a {
233 count[v]++
234 }
235 for _, v := range b {
236 if count[v] == 0 {
237 return false
238 }
239 count[v]--
240 }
241 return true
242 }
243 // 遍历 actual 中的每个 slice 和其对应的 db-SQL 映射
244 for slice, actualDbMap := range actual {
245 foundSliceMatch := false
246 for _, randomSQLMap := range randomSqls {
247 if randomDbMap, ok := randomSQLMap[slice]; ok {
248 // 检查当前 slice 的所有 db 和 SQL 是否匹配
249 allDbSQLMatch := true
250 for db, actualSQLs := range actualDbMap {
251 if expectedSQLs, ok := randomDbMap[db]; ok {
252 if !sliceMatch(expectedSQLs, actualSQLs) {
253 allDbSQLMatch = false
254 break
255 }
256 } else {
257 // 如果 actual 中的 db 在 randomDbMap 中不存在
258 allDbSQLMatch = false
259 break
260 }
261 }
262 if allDbSQLMatch {
263 foundSliceMatch = true
264 break
265 }
266 }
267 }
268 if !foundSliceMatch {
269 return false
270 }
271 }
272 // 如果所有检查都通过,则返回 true
273 return true
274}
275
276func createNamespace(nsStr string) (*models.Namespace, error) {
277 ns := &models.Namespace{}

Callers 2

getTestFuncFunction · 0.85
TestCheckRandomSQLsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected