Skip to content

Commit

Permalink
fix ut
Browse files Browse the repository at this point in the history
Signed-off-by: wang yan <[email protected]>
  • Loading branch information
wy65701436 committed Apr 8, 2024
1 parent f42c610 commit f42c204
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/pkg/task/dao/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,9 @@ func buildInClauseSQLForExtraAttrs(jsonbStrus []jsonbStru) (string, []interface{
sql := "select id from execution where"

for i, jsonbStr := range jsonbStrus {
if jsonbStr.key == "" || jsonbStr.value == "" {
return "", nil
}
keys := strings.Split(strings.TrimPrefix(jsonbStr.key, jsonbStr.keyPrefix), ".")
if len(keys) == 1 {
if i == 0 {
Expand All @@ -442,7 +445,6 @@ func buildInClauseSQLForExtraAttrs(jsonbStrus []jsonbStru) (string, []interface{
args = append(args, item)
}
args = append(args, jsonbStr.value)

}

return fmt.Sprintf("%s %s", sql, cond), args
Expand Down
32 changes: 23 additions & 9 deletions src/pkg/task/dao/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,22 +395,36 @@ func TestExecutionDAOSuite(t *testing.T) {
}

func Test_buildInClauseSQLForExtraAttrs(t *testing.T) {
type args struct {
keys []string
}
tests := []struct {
name string
args args
args []jsonbStru
want string
}{
{"extra_attrs.", args{[]string{}}, ""},
{"extra_attrs.id", args{[]string{"id"}}, "select id from execution where extra_attrs->>?=?"},
{"extra_attrs.artifact.digest", args{[]string{"artifact", "digest"}}, "select id from execution where extra_attrs->?->>?=?"},
{"extra_attrs.a.b.c", args{[]string{"a", "b", "c"}}, "select id from execution where extra_attrs->?->?->>?=?"},
{"extra_attrs.", []jsonbStru{}, ""},
{"extra_attrs.", []jsonbStru{{}}, ""},
{"extra_attrs.id", []jsonbStru{{
keyPrefix: "extra_attrs.",
key: "extra_attrs.id",
value: "1",
}}, "select id from execution where extra_attrs->>?=?"},
{"extra_attrs.artifact.digest", []jsonbStru{{
keyPrefix: "extra_attrs.",
key: "extra_attrs.artifact.digest",
value: "sha256:1234",
}}, "select id from execution where extra_attrs->?->>?=?"},
{"extra_attrs.a.b.c", []jsonbStru{{
keyPrefix: "extra_attrs.",
key: "extra_attrs.a.b.c",
value: "test_value_1",
}, {
keyPrefix: "extra_attrs.",
key: "extra_attrs.d.e",
value: "test_value_2",
}}, "select id from execution where extra_attrs->?->?->>?=? and extra_attrs->?->>?=?"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := buildInClauseSQLForExtraAttrs(tt.args.keys); got != tt.want {
if got, _ := buildInClauseSQLForExtraAttrs(tt.args); got != tt.want {
t.Errorf("buildInClauseSQLForExtraAttrs() = %v, want %v", got, tt.want)
}
})
Expand Down

0 comments on commit f42c204

Please sign in to comment.