-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnative_test.go
76 lines (70 loc) · 1.41 KB
/
native_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
package gortb
import (
"testing"
"encoding/json"
)
func TestNative(t *testing.T) {
tests := []struct {
name string
native *Native
wantErr error
}{
{
name: "valid native",
native: &Native{
Request: "native_request_string",
Ver: "1.0",
API: []int{1, 2},
BAttr: []int{1, 2, 3},
},
wantErr: nil,
},
{
name: "missing request",
native: &Native{
Ver: "1.0",
API: []int{1, 2},
BAttr: []int{1, 2, 3},
},
wantErr: ErrMissingRequest,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.native.Validate()
if tt.wantErr != nil {
if err == nil {
t.Errorf("Native.Validate() error = nil, wantErr = %v", tt.wantErr)
return
}
if err.Error() != tt.wantErr.Error() {
t.Errorf("Native.Validate() error = %v, wantErr = %v", err, tt.wantErr)
}
return
}
if err != nil {
t.Errorf("Native.Validate() error = %v, wantErr = nil", err)
}
})
}
}
func TestNativeWithJSON(t *testing.T) {
jsonData := `{
"request": "native_request_string",
"ver": "1.0",
"api": [1, 2],
"battr": [1, 2, 3],
"ext": {
"custom_field": "value"
}
}`
var native Native
err := json.Unmarshal([]byte(jsonData), &native)
if err != nil {
t.Fatalf("Failed to unmarshal JSON: %v", err)
}
err = native.Validate()
if err != nil {
t.Errorf("Native.Validate() error = %v, wantErr = nil", err)
}
}