Debian Codegraph @

radix/path_tree_test.go

raw
1 // Copyright 2025 Princess B33f Heavy Industries / Dave Shanley
2 // SPDX-License-Identifier: MIT
3
4 package radix
5
6 import (
7 "testing"
8
9 "github.com/pb33f/libopenapi"
10 v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
11 "github.com/stretchr/testify/assert"
12 "github.com/stretchr/testify/require"
13 )
14
15 func TestNewPathTree(t *testing.T) {
16 tree := NewPathTree()
17 require.NotNil(t, tree)
18 assert.Equal(t, 0, tree.Size())
19 }
20
21 func TestPathTree_ImplementsPathLookup(t *testing.T) {
22 // Compile-time check that PathTree implements PathLookup
23 var _ PathLookup = (*PathTree)(nil)
24 }
25
26 func TestPathTree_Insert_Lookup(t *testing.T) {
27 spec := `openapi: 3.1.0
28 info:
29 title: Test API
30 version: 1.0.0
31 paths:
32 /users:
33 get:
34 responses:
35 '200':
36 description: OK
37 `
38 doc, err := libopenapi.NewDocument([]byte(spec))
39 require.NoError(t, err)
40
41 model, errs := doc.BuildV3Model()
42 require.Empty(t, errs)
43
44 pair := model.Model.Paths.PathItems.First()
45 require.NotNil(t, pair)
46
47 tree := NewPathTree()
48 tree.Insert("/users", pair.Value())
49
50 pathItem, path, found := tree.Lookup("/users")
51 assert.True(t, found)
52 assert.Equal(t, "/users", path)
53 assert.NotNil(t, pathItem)
54 assert.NotNil(t, pathItem.Get)
55 }
56
57 func TestPathTree_Walk(t *testing.T) {
58 spec := `openapi: 3.1.0
59 info:
60 title: Test API
61 version: 1.0.0
62 paths:
63 /users:
64 get:
65 responses:
66 '200':
67 description: OK
68 /posts:
69 get:
70 responses:
71 '200':
72 description: OK
73 `
74 doc, err := libopenapi.NewDocument([]byte(spec))
75 require.NoError(t, err)
76
77 model, errs := doc.BuildV3Model()
78 require.Empty(t, errs)
79
80 tree := BuildPathTree(&model.Model)
81 assert.Equal(t, 2, tree.Size())
82
83 var paths []string
84 tree.Walk(func(path string, pathItem *v3.PathItem) bool {
85 paths = append(paths, path)
86 assert.NotNil(t, pathItem)
87 return true
88 })
89 assert.Len(t, paths, 2)
90 }
91
92 func TestBuildPathTree(t *testing.T) {
93 spec := `openapi: 3.1.0
94 info:
95 title: Test API
96 version: 1.0.0
97 paths:
98 /users:
99 get:
100 responses:
101 '200':
102 description: OK
103 /users/{id}:
104 get:
105 responses:
106 '200':
107 description: OK
108 /posts:
109 post:
110 responses:
111 '201':
112 description: Created
113 `
114 doc, err := libopenapi.NewDocument([]byte(spec))
115 require.NoError(t, err)
116
117 model, errs := doc.BuildV3Model()
118 require.Empty(t, errs)
119
120 tree := BuildPathTree(&model.Model)
121
122 assert.Equal(t, 3, tree.Size())
123
124 // Test lookups
125 pathItem, path, found := tree.Lookup("/users")
126 assert.True(t, found)
127 assert.Equal(t, "/users", path)
128 assert.NotNil(t, pathItem.Get)
129
130 pathItem, path, found = tree.Lookup("/users/123")
131 assert.True(t, found)
132 assert.Equal(t, "/users/{id}", path)
133 assert.NotNil(t, pathItem.Get)
134
135 pathItem, path, found = tree.Lookup("/posts")
136 assert.True(t, found)
137 assert.Equal(t, "/posts", path)
138 assert.NotNil(t, pathItem.Post)
139 }
140
141 func TestBuildPathTree_NilDocument(t *testing.T) {
142 tree := BuildPathTree(nil)
143 require.NotNil(t, tree)
144 assert.Equal(t, 0, tree.Size())
145 }
146
147 func TestBuildPathTree_NilPaths(t *testing.T) {
148 doc := &v3.Document{}
149 tree := BuildPathTree(doc)
150 require.NotNil(t, tree)
151 assert.Equal(t, 0, tree.Size())
152 }
153
154 func TestPathTree_LiteralOverParam(t *testing.T) {
155 spec := `openapi: 3.1.0
156 info:
157 title: Test API
158 version: 1.0.0
159 paths:
160 /users/{id}:
161 get:
162 operationId: getUserById
163 responses:
164 '200':
165 description: OK
166 /users/admin:
167 get:
168 operationId: getAdmin
169 responses:
170 '200':
171 description: OK
172 `
173 doc, err := libopenapi.NewDocument([]byte(spec))
174 require.NoError(t, err)
175
176 model, errs := doc.BuildV3Model()
177 require.Empty(t, errs)
178
179 tree := BuildPathTree(&model.Model)
180
181 // Literal should win
182 pathItem, path, found := tree.Lookup("/users/admin")
183 assert.True(t, found)
184 assert.Equal(t, "/users/admin", path)
185 assert.Equal(t, "getAdmin", pathItem.Get.OperationId)
186
187 // Param should match other values
188 pathItem, path, found = tree.Lookup("/users/123")
189 assert.True(t, found)
190 assert.Equal(t, "/users/{id}", path)
191 assert.Equal(t, "getUserById", pathItem.Get.OperationId)
192 }
193
194 // Benchmark
195
196 func BenchmarkPathTree_Lookup(b *testing.B) {
197 spec := `openapi: 3.1.0
198 info:
199 title: Test API
200 version: 1.0.0
201 paths:
202 /api/v3/ad_accounts:
203 get:
204 responses:
205 '200':
206 description: OK
207 /api/v3/ad_accounts/{id}:
208 get:
209 responses:
210 '200':
211 description: OK
212 /api/v3/ad_accounts/{id}/campaigns:
213 get:
214 responses:
215 '200':
216 description: OK
217 /api/v3/ad_accounts/{id}/campaigns/{campaign_id}:
218 get:
219 responses:
220 '200':
221 description: OK
222 `
223 doc, err := libopenapi.NewDocument([]byte(spec))
224 if err != nil {
225 b.Fatal(err)
226 }
227
228 model, modelErr := doc.BuildV3Model()
229 if modelErr != nil {
230 b.Fatal(modelErr)
231 }
232
233 tree := BuildPathTree(&model.Model)
234
235 b.ResetTimer()
236 b.ReportAllocs()
237
238 for i := 0; i < b.N; i++ {
239 tree.Lookup("/api/v3/ad_accounts/acc123/campaigns/camp456")
240 }
241 }