mirror of
https://github.com/rancher/steve.git
synced 2025-09-11 20:29:52 +00:00
Fix new projectsornamespaces negative tests. (#769)
Remember to select objects that don't have the project label assigned to them when doing a negative test.
This commit is contained in:
@@ -1253,24 +1253,27 @@ func (l *ListOptionIndexer) getProjectsOrNamespacesFieldFilter(filter sqltypes.F
|
|||||||
func (l *ListOptionIndexer) getProjectsOrNamespacesLabelFilter(index int, filter sqltypes.Filter, dbName string) (string, []any, error) {
|
func (l *ListOptionIndexer) getProjectsOrNamespacesLabelFilter(index int, filter sqltypes.Filter, dbName string) (string, []any, error) {
|
||||||
opString := ""
|
opString := ""
|
||||||
labelName := filter.Field[2]
|
labelName := filter.Field[2]
|
||||||
|
target := "()"
|
||||||
|
if len(filter.Matches) > 0 {
|
||||||
|
target = fmt.Sprintf("(?%s)", strings.Repeat(", ?", len(filter.Matches)-1))
|
||||||
|
}
|
||||||
|
matches := make([]any, len(filter.Matches)+1)
|
||||||
|
matches[0] = labelName
|
||||||
|
for i, match := range filter.Matches {
|
||||||
|
matches[i+1] = match
|
||||||
|
}
|
||||||
switch filter.Op {
|
switch filter.Op {
|
||||||
case sqltypes.In:
|
case sqltypes.In:
|
||||||
fallthrough
|
clause := fmt.Sprintf(`lt%d.label = ? AND lt%d.value IN %s`, index, index, target)
|
||||||
|
return clause, matches, nil
|
||||||
case sqltypes.NotIn:
|
case sqltypes.NotIn:
|
||||||
opString = "IN"
|
clause1 := fmt.Sprintf(`(lt%d.label = ? AND lt%d.value NOT IN %s)`, index, index, target)
|
||||||
if filter.Op == sqltypes.NotIn {
|
clause2 := fmt.Sprintf(`(o.key NOT IN (SELECT o1.key FROM "%s" o1
|
||||||
opString = "NOT IN"
|
JOIN "%s_fields" f1 ON o1.key = f1.key
|
||||||
}
|
LEFT OUTER JOIN "%s_labels" lt%di1 ON o1.key = lt%di1.key
|
||||||
target := "()"
|
WHERE lt%di1.label = ?))`, dbName, dbName, dbName, index, index, index)
|
||||||
if len(filter.Matches) > 0 {
|
matches = append(matches, labelName)
|
||||||
target = fmt.Sprintf("(?%s)", strings.Repeat(", ?", len(filter.Matches)-1))
|
clause := fmt.Sprintf("%s OR %s", clause1, clause2)
|
||||||
}
|
|
||||||
clause := fmt.Sprintf(`lt%d.label = ? AND lt%d.value %s %s`, index, index, opString, target)
|
|
||||||
matches := make([]any, len(filter.Matches)+1)
|
|
||||||
matches[0] = labelName
|
|
||||||
for i, match := range filter.Matches {
|
|
||||||
matches[i+1] = match
|
|
||||||
}
|
|
||||||
return clause, matches, nil
|
return clause, matches, nil
|
||||||
}
|
}
|
||||||
return "", nil, fmt.Errorf("unrecognized operator: %s", opString)
|
return "", nil, fmt.Errorf("unrecognized operator: %s", opString)
|
||||||
|
@@ -1624,9 +1624,12 @@ func TestConstructQuery(t *testing.T) {
|
|||||||
JOIN "_v1_Namespace_fields" nsf ON f."metadata.namespace" = nsf."metadata.name"
|
JOIN "_v1_Namespace_fields" nsf ON f."metadata.namespace" = nsf."metadata.name"
|
||||||
LEFT OUTER JOIN "_v1_Namespace_labels" lt1 ON nsf.key = lt1.key
|
LEFT OUTER JOIN "_v1_Namespace_labels" lt1 ON nsf.key = lt1.key
|
||||||
WHERE
|
WHERE
|
||||||
((nsf."metadata.name" NOT IN (?)) AND (lt1.label = ? AND lt1.value NOT IN (?)))
|
((nsf."metadata.name" NOT IN (?)) AND ((lt1.label = ? AND lt1.value NOT IN (?)) OR (o.key NOT IN (SELECT o1.key FROM "something" o1
|
||||||
|
JOIN "something_fields" f1 ON o1.key = f1.key
|
||||||
|
LEFT OUTER JOIN "something_labels" lt1i1 ON o1.key = lt1i1.key
|
||||||
|
WHERE lt1i1.label = ?))))
|
||||||
ORDER BY f."metadata.name" ASC `,
|
ORDER BY f."metadata.name" ASC `,
|
||||||
expectedStmtArgs: []any{"some_namespace", "field.cattle.io/projectId", "some_namespace"},
|
expectedStmtArgs: []any{"some_namespace", "field.cattle.io/projectId", "some_namespace", "field.cattle.io/projectId"},
|
||||||
expectedErr: nil,
|
expectedErr: nil,
|
||||||
})
|
})
|
||||||
tests = append(tests, testCase{
|
tests = append(tests, testCase{
|
||||||
@@ -1655,9 +1658,12 @@ func TestConstructQuery(t *testing.T) {
|
|||||||
JOIN "_v1_Namespace_fields" nsf ON f."metadata.namespace" = nsf."metadata.name"
|
JOIN "_v1_Namespace_fields" nsf ON f."metadata.namespace" = nsf."metadata.name"
|
||||||
LEFT OUTER JOIN "_v1_Namespace_labels" lt1 ON nsf.key = lt1.key
|
LEFT OUTER JOIN "_v1_Namespace_labels" lt1 ON nsf.key = lt1.key
|
||||||
WHERE
|
WHERE
|
||||||
((nsf."metadata.name" NOT IN (?, ?)) AND (lt1.label = ? AND lt1.value NOT IN (?, ?)))
|
((nsf."metadata.name" NOT IN (?, ?)) AND ((lt1.label = ? AND lt1.value NOT IN (?, ?)) OR (o.key NOT IN (SELECT o1.key FROM "something" o1
|
||||||
|
JOIN "something_fields" f1 ON o1.key = f1.key
|
||||||
|
LEFT OUTER JOIN "something_labels" lt1i1 ON o1.key = lt1i1.key
|
||||||
|
WHERE lt1i1.label = ?))))
|
||||||
ORDER BY f."metadata.name" ASC `,
|
ORDER BY f."metadata.name" ASC `,
|
||||||
expectedStmtArgs: []any{"some_namespace", "p-example", "field.cattle.io/projectId", "some_namespace", "p-example"},
|
expectedStmtArgs: []any{"some_namespace", "p-example", "field.cattle.io/projectId", "some_namespace", "p-example", "field.cattle.io/projectId"},
|
||||||
expectedErr: nil,
|
expectedErr: nil,
|
||||||
})
|
})
|
||||||
tests = append(tests, testCase{
|
tests = append(tests, testCase{
|
||||||
|
Reference in New Issue
Block a user