-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathannotations_test.go
More file actions
211 lines (177 loc) · 4.99 KB
/
annotations_test.go
File metadata and controls
211 lines (177 loc) · 4.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
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
210
211
package traverse
import (
"encoding/json"
"testing"
)
type annotationProduct struct {
ID int `json:"ID"`
Name string `json:"Name"`
Price float64 `json:"Price"`
}
func TestDecodeAnnotated_BasicEntity(t *testing.T) {
data := []byte(`{
"ID": 1,
"Name": "Widget",
"Price": 9.99,
"@odata.etag": "W/\"abc123\"",
"@odata.count": 42
}`)
result, err := DecodeAnnotated[annotationProduct](data)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result.Entity.ID != 1 {
t.Errorf("expected ID=1, got %d", result.Entity.ID)
}
if result.Entity.Name != "Widget" {
t.Errorf("expected Name=Widget, got %q", result.Entity.Name)
}
if result.Entity.Price != 9.99 {
t.Errorf("expected Price=9.99, got %f", result.Entity.Price)
}
if len(result.Annotations) != 2 {
t.Errorf("expected 2 annotations, got %d", len(result.Annotations))
}
}
func TestDecodeAnnotated_AnnotationsNotInEntity(t *testing.T) {
data := []byte(`{
"ID": 2,
"Name": "Gadget",
"Price": 19.99,
"@odata.etag": "W/\"xyz\"",
"@Custom.Score": 99.5
}`)
result, err := DecodeAnnotated[annotationProduct](data)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
_, ok := result.Annotations["@odata.etag"]
if !ok {
t.Error("expected @odata.etag annotation")
}
_, ok = result.Annotations["@Custom.Score"]
if !ok {
t.Error("expected @Custom.Score annotation")
}
// Entity fields should not bleed into annotations
_, ok = result.Annotations["ID"]
if ok {
t.Error("ID should not be in annotations")
}
}
func TestDecodeAnnotated_NoAnnotations(t *testing.T) {
data := []byte(`{"ID": 3, "Name": "Plain", "Price": 1.0}`)
result, err := DecodeAnnotated[annotationProduct](data)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(result.Annotations) != 0 {
t.Errorf("expected 0 annotations, got %d", len(result.Annotations))
}
if result.Entity.Name != "Plain" {
t.Errorf("expected Name=Plain, got %q", result.Entity.Name)
}
}
func TestGetAnnotation_String(t *testing.T) {
data := []byte(`{
"ID": 1,
"Name": "Alice",
"Price": 5.0,
"@odata.etag": "W/\"abc123\""
}`)
result, err := DecodeAnnotated[annotationProduct](data)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
var etag string
if err := result.GetAnnotation("@odata.etag", &etag); err != nil {
t.Fatalf("GetAnnotation failed: %v", err)
}
if etag != `W/"abc123"` {
t.Errorf("expected etag W/\"abc123\", got %q", etag)
}
}
func TestGetAnnotation_Float(t *testing.T) {
data := []byte(`{"ID": 1, "Name": "X", "Price": 1.0, "@Custom.Score": 99.5}`)
result, err := DecodeAnnotated[annotationProduct](data)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
var score float64
if err := result.GetAnnotation("@Custom.Score", &score); err != nil {
t.Fatalf("GetAnnotation failed: %v", err)
}
if score != 99.5 {
t.Errorf("expected score 99.5, got %f", score)
}
}
func TestGetAnnotation_Int(t *testing.T) {
data := []byte(`{"ID": 1, "Name": "X", "Price": 1.0, "@odata.count": 42}`)
result, err := DecodeAnnotated[annotationProduct](data)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
var count int
if err := result.GetAnnotation("@odata.count", &count); err != nil {
t.Fatalf("GetAnnotation failed: %v", err)
}
if count != 42 {
t.Errorf("expected count 42, got %d", count)
}
}
func TestGetAnnotation_Missing(t *testing.T) {
data := []byte(`{"ID": 1, "Name": "X", "Price": 1.0}`)
result, err := DecodeAnnotated[annotationProduct](data)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
var val string
err = result.GetAnnotation("@odata.etag", &val)
if err == nil {
t.Error("expected error for missing annotation, got nil")
}
}
func TestGetAnnotation_RawJSON(t *testing.T) {
data := []byte(`{"ID": 1, "Name": "X", "Price": 1.0, "@meta.tags": ["a","b"]}`)
result, err := DecodeAnnotated[annotationProduct](data)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
var tags []string
if err := result.GetAnnotation("@meta.tags", &tags); err != nil {
t.Fatalf("GetAnnotation failed: %v", err)
}
if len(tags) != 2 || tags[0] != "a" || tags[1] != "b" {
t.Errorf("unexpected tags: %v", tags)
}
}
func TestDecodeAnnotated_InvalidJSON(t *testing.T) {
_, err := DecodeAnnotated[annotationProduct]([]byte(`{not valid json`))
if err == nil {
t.Error("expected error for invalid JSON")
}
}
func TestDecodeAnnotated_AnnotationKeys(t *testing.T) {
data := []byte(`{
"ID": 5,
"Name": "Test",
"Price": 3.14,
"@odata.context": "https://example.com/$metadata#Products",
"@odata.etag": "W/\"v1\"",
"@ns.term": true
}`)
result, err := DecodeAnnotated[annotationProduct](data)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
expected := []string{"@odata.context", "@odata.etag", "@ns.term"}
for _, key := range expected {
if _, ok := result.Annotations[key]; !ok {
t.Errorf("missing annotation %q", key)
}
}
var raw json.RawMessage
if err := result.GetAnnotation("@ns.term", &raw); err != nil {
t.Fatalf("GetAnnotation @ns.term failed: %v", err)
}
}