-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathannotations_test.go
209 lines (187 loc) · 5.55 KB
/
annotations_test.go
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright (c) Liam Stanley <liam@liam.sh>. All rights reserved. Use of
// this source code is governed by the MIT license that can be found in
// the LICENSE file.
package entrest
import (
"testing"
"entgo.io/ent/entc/gen"
"github.com/ogen-go/ogen"
"github.com/stretchr/testify/assert"
)
func TestGetAnnotation(t *testing.T) {
// True.
assert.Equal(
t,
ptr(true),
GetAnnotation(&gen.Type{Annotations: map[string]any{
Annotation{}.Name(): WithPagination(true),
}}).Pagination,
)
// False.
assert.Equal(
t,
ptr(false),
GetAnnotation(&gen.Type{Annotations: map[string]any{
Annotation{}.Name(): WithPagination(false),
}}).Pagination,
)
// Unspecified.
var ptrBoolNil *bool
assert.Equal(
t,
ptrBoolNil,
GetAnnotation(&gen.Type{Annotations: map[string]any{
Annotation{}.Name(): Annotation{},
}}).Pagination,
)
// Test fields.
assert.True(
t,
GetAnnotation(&gen.Field{Annotations: map[string]any{
Annotation{}.Name(): WithSortable(true),
}}).Sortable,
)
// Test edges.
assert.Equal(
t,
ptr(true),
GetAnnotation(&gen.Edge{Annotations: map[string]any{
Annotation{}.Name(): WithEagerLoad(true),
}}).EagerLoad,
)
}
func TestValidateAnnotation(t *testing.T) {
tests := []struct {
name string
value *gen.Type
wantErr bool
}{
{
name: "no-annotation",
value: &gen.Type{Annotations: map[string]any{}},
},
{
name: "valid-annotation",
value: &gen.Type{Annotations: map[string]any{
Annotation{}.Name(): WithPagination(true), // Type's should support pagination.
}},
wantErr: false,
},
{
name: "invalid-annotation-type-with-edge",
value: &gen.Type{Annotations: map[string]any{
Annotation{}.Name(): WithEagerLoad(false), // Only edges support eager loading.
}},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateAnnotations(tt.value)
assert.Equal(t, tt.wantErr, err != nil)
})
}
}
func TestAnnotation_Merge(t *testing.T) {
tests := []struct {
name string
annotations []Annotation
want Annotation
}{
{
name: "no-annotations",
annotations: []Annotation{
{},
{},
},
want: Annotation{},
},
{
name: "overlap-single",
annotations: []Annotation{
WithPagination(true),
WithPagination(false),
},
want: Annotation{
Pagination: ptr(false),
},
},
{
name: "overlap-multiple",
annotations: []Annotation{
WithPagination(false),
WithDescription("foo"),
WithPagination(true),
WithDescription("bar"),
},
want: Annotation{
Pagination: ptr(true),
Description: "bar",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var out Annotation
for _, a := range tt.annotations {
out, _ = out.Merge(a).(Annotation)
}
assert.Equal(t, tt.want, out)
})
}
}
func TestAnnotation_AdditionalTags(t *testing.T) {
t.Parallel()
r := mustBuildSpec(t, &Config{
PreGenerateHook: func(g *gen.Graph, _ *ogen.Spec) error {
injectAnnotations(t, g, "Pet", WithAdditionalTags("Foo"))
injectAnnotations(t, g, "Pet.categories", WithAdditionalTags("Bar"))
return nil
},
})
assert.Contains(t, r.json(`$.paths./pets.get.tags`), "Foo")
assert.Contains(t, r.json(`$.paths./pets.post.tags`), "Foo")
assert.Contains(t, r.json(`$.paths./pets/{petID}.get.tags`), "Foo")
assert.Contains(t, r.json(`$.paths./pets/{petID}.patch.tags`), "Foo")
assert.Contains(t, r.json(`$.paths./pets/{petID}.delete.tags`), "Foo")
assert.Contains(t, r.json(`$.paths./pets/{petID}/categories.get.tags`), "Bar")
}
func TestAnnotation_Tags(t *testing.T) {
t.Parallel()
r := mustBuildSpec(t, &Config{
PreGenerateHook: func(g *gen.Graph, _ *ogen.Spec) error {
injectAnnotations(t, g, "Pet", WithTags("Foo"))
injectAnnotations(t, g, "Pet.categories", WithTags("Bar"))
return nil
},
})
assert.Equal(t, "Foo", r.json(`$.paths./pets.get.tags.*`))
assert.Equal(t, "Foo", r.json(`$.paths./pets.post.tags.*`))
assert.Equal(t, "Foo", r.json(`$.paths./pets/{petID}.get.tags.*`))
assert.Equal(t, "Foo", r.json(`$.paths./pets/{petID}.patch.tags.*`))
assert.Equal(t, "Foo", r.json(`$.paths./pets/{petID}.delete.tags.*`))
assert.Equal(t, "Bar", r.json(`$.paths./pets/{petID}/categories.get.tags.*`))
}
func TestAnnotation_EdgeUpdateBulk(t *testing.T) {
t.Parallel()
r := mustBuildSpec(t, &Config{
PreGenerateHook: func(g *gen.Graph, _ *ogen.Spec) error {
injectAnnotations(t, g, "Pet.categories", WithEdgeUpdateBulk(true))
return nil
},
})
// Ensure create and update on categories have the bulk field in the right place.
assert.NotNil(t, r.json(`$.components.schemas.PetCreate.properties.categories`))
assert.Nil(t, r.json(`$.components.schemas.PetCreate.properties.add_categories`))
assert.Nil(t, r.json(`$.components.schemas.PetCreate.properties.remove_categories`))
assert.NotNil(t, r.json(`$.components.schemas.PetUpdate.properties.categories`))
assert.NotNil(t, r.json(`$.components.schemas.PetUpdate.properties.add_categories`))
assert.NotNil(t, r.json(`$.components.schemas.PetUpdate.properties.remove_categories`))
// And ensure it's not on others.
assert.NotNil(t, r.json(`$.components.schemas.PetCreate.properties.friends`))
assert.Nil(t, r.json(`$.components.schemas.PetCreate.properties.add_friends`))
assert.Nil(t, r.json(`$.components.schemas.PetCreate.properties.remove_friends`))
assert.Nil(t, r.json(`$.components.schemas.PetUpdate.properties.friends`))
assert.NotNil(t, r.json(`$.components.schemas.PetUpdate.properties.add_friends`))
assert.NotNil(t, r.json(`$.components.schemas.PetUpdate.properties.remove_friends`))
}