Skip to content

Commit d5b373a

Browse files
committed
test: add unit tests for tag parsing and splitting functions
1 parent 5a8ad90 commit d5b373a

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package tags
2+
3+
import (
4+
"database/sql"
5+
"strings"
6+
"testing"
7+
8+
"github.com/uptrace/bun"
9+
"github.com/uptrace/bun/dialect/sqlitedialect"
10+
_ "modernc.org/sqlite"
11+
)
12+
13+
func TestSplitOnTopLevelChar_NestedAndSimple(t *testing.T) {
14+
// nested should not split at top-level
15+
in := "a|(b|c)"
16+
parts := splitOnTopLevelChar(in, '|')
17+
if len(parts) != 2 || strings.TrimSpace(parts[0]) != "a" || strings.TrimSpace(parts[1]) != "(b|c)" {
18+
t.Fatalf("unexpected split result for %q: %#v", in, parts)
19+
}
20+
21+
// simple no-op
22+
single := splitOnTopLevelChar("abc", '&')
23+
if len(single) != 1 || single[0] != "abc" {
24+
t.Fatalf("expected single element for simple input, got: %#v", single)
25+
}
26+
27+
// nested with deeper parentheses — expect three top-level parts here
28+
deep := splitOnTopLevelChar("(a&(b|c))|d|e", '|')
29+
if len(deep) != 3 || strings.TrimSpace(deep[0]) != "(a&(b|c))" || strings.TrimSpace(deep[1]) != "d" || strings.TrimSpace(deep[2]) != "e" {
30+
t.Fatalf("unexpected deep split: %#v", deep)
31+
}
32+
}
33+
34+
func TestParseTagMatcher_ValidationOnly_DoesNotPanic(t *testing.T) {
35+
// validation-only path uses a nil QueryBuilder — ensure complex expressions validate
36+
if _, err := parseTagMatcherColumn("(prod|staging)&!dev", nil, true, false, "tags"); err != nil {
37+
t.Fatalf("expected validation to succeed, got: %v", err)
38+
}
39+
}
40+
41+
func TestWildcardsAndEscaping_RendersExpectedSQL(t *testing.T) {
42+
sqldb, err := sql.Open("sqlite", ":memory:")
43+
if err != nil {
44+
t.Fatalf("failed to open sqlite: %v", err)
45+
}
46+
defer sqldb.Close()
47+
48+
bdb := bun.NewDB(sqldb, sqlitedialect.New())
49+
defer bdb.Close()
50+
51+
sel := bdb.NewSelect()
52+
qb := sel.QueryBuilder()
53+
54+
// '*' should become '_' and be embedded in the joined tags pattern
55+
if _, err := parseTagMatcherColumn("pro*", qb, true, false, "tagcol"); err != nil {
56+
t.Fatalf("parseTagMatcherColumn returned error: %v", err)
57+
}
58+
sqlStr := sel.String()
59+
if !strings.Contains(sqlStr, "LIKE") || !strings.Contains(sqlStr, "pro_") {
60+
t.Fatalf("rendered SQL did not contain expected wildcard fragment: %s", sqlStr)
61+
}
62+
}
63+
64+
func TestSplitOnTopLevelChar_NoSplitInsideParens(t *testing.T) {
65+
in := "(a|b)|c"
66+
parts := splitOnTopLevelChar(in, '|')
67+
if len(parts) != 2 || strings.TrimSpace(parts[0]) != "(a|b)" || strings.TrimSpace(parts[1]) != "c" {
68+
t.Fatalf("unexpected split for %q: %#v", in, parts)
69+
}
70+
}

0 commit comments

Comments
 (0)