-
Notifications
You must be signed in to change notification settings - Fork 231
Expand file tree
/
Copy pathvariant_path_test.go
More file actions
122 lines (115 loc) · 3.99 KB
/
Copy pathvariant_path_test.go
File metadata and controls
122 lines (115 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package iceberg
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNormalizeVariantPathEscaping(t *testing.T) {
for _, tt := range []struct {
name string
fields []string
want string
}{
{"root", nil, "$"},
{"plain", []string{"event_type"}, "$['event_type']"},
{"dotted name kept literal", []string{"user.name"}, "$['user.name']"},
{"nested", []string{"location", "latitude"}, "$['location']['latitude']"},
{"single quote escaped", []string{"o'brien"}, `$['o\'brien']`},
{"backslash escaped", []string{`a\b`}, `$['a\\b']`},
{"newline escaped", []string{"a\nb"}, `$['a\nb']`},
{"other control char hex-escaped", []string{"a\x01b"}, "$['a\\u0001b']"},
} {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, NormalizeVariantPath(tt.fields))
})
}
}
func TestParseVariantPath(t *testing.T) {
for _, tt := range []struct {
name string
path string
want []string
}{
{"root", "$", []string{}},
{"single member", "$.event_id", []string{"event_id"}},
{"nested members", "$.location.latitude", []string{"location", "latitude"}},
{"underscore and digits", "$._a1.b2", []string{"_a1", "b2"}},
{"non-ascii first char", "$.naïve", []string{"naïve"}},
{"bracket notation", "$['event_id']", []string{"event_id"}},
{"bracket nested", "$['location']['latitude']", []string{"location", "latitude"}},
{"bracket dotted name", "$['user.name']", []string{"user.name"}},
{"bracket leading digit", "$['1abc']", []string{"1abc"}},
{"bracket escaped quote", `$['o\'brien']`, []string{"o'brien"}},
{"bracket escaped backslash", `$['a\\b']`, []string{`a\b`}},
{"bracket star is literal", "$['a*b']", []string{"a*b"}},
{"mixed dot and bracket", "$['a'].b", []string{"a", "b"}},
{"bracket unicode BMP escape", "$['\\u00e9']", []string{"é"}},
{"bracket surrogate pair escape", "$['\\uD840\\uDC00']", []string{"\U00020000"}},
} {
t.Run(tt.name, func(t *testing.T) {
got, err := parseVariantPath(tt.path)
require.NoError(t, err)
assert.Equal(t, tt.want, got)
})
}
}
// TestParseVariantPathRoundTrip proves NormalizeVariantPath output parses back to the same members.
func TestParseVariantPathRoundTrip(t *testing.T) {
for _, fields := range [][]string{
{"event_id"},
{"location", "latitude"},
{"user.name"},
{"o'brien"},
{`a\b`},
{"a\nb"},
{"a\x01b"},
{"a*b"},
{"1abc"},
{""},
} {
got, err := parseVariantPath(NormalizeVariantPath(fields))
require.NoError(t, err)
assert.Equal(t, fields, got)
}
}
func TestParseVariantPathRejects(t *testing.T) {
for _, tt := range []struct {
name string
path string
}{
{"wildcard", "$.*"},
{"recursive descent", "$..event_id"},
{"missing root", "event_id"},
{"leading digit member", "$.1abc"},
{"empty member", "$."},
{"array index", "$[0]"},
{"bracket wildcard", "$[*]"},
{"unquoted bracket", "$[event_id]"},
{"unterminated bracket", "$['event_id"},
{"missing close bracket", "$['event_id'"},
{"lone high surrogate", "$['\\uD840']"},
{"lone low surrogate", "$['\\uDC00']"},
{"unpaired high surrogate", "$['\\uD840x']"},
} {
t.Run(tt.name, func(t *testing.T) {
_, err := parseVariantPath(tt.path)
require.ErrorIs(t, err, ErrInvalidArgument)
})
}
}