-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbid_test.go
95 lines (89 loc) · 1.67 KB
/
bid_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
package gortb
import (
"encoding/json"
"testing"
)
func TestBid(t *testing.T) {
tests := []struct {
name string
bid *Bid
wantErr error
}{
{
name: "valid bid",
bid: &Bid{
ID: "test_bid_id",
ImpID: "test_imp_id",
Price: 1.23,
},
wantErr: nil,
},
{
name: "missing id",
bid: &Bid{
ImpID: "test_imp_id",
Price: 1.23,
},
wantErr: ErrMissingBidID,
},
{
name: "missing impression id",
bid: &Bid{
ID: "test_bid_id",
Price: 1.23,
},
wantErr: ErrMissingImpID,
},
{
name: "invalid price",
bid: &Bid{
ID: "test_bid_id",
ImpID: "test_imp_id",
Price: 0,
},
wantErr: ErrInvalidPrice,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.bid.Validate()
if tt.wantErr != nil {
if err == nil {
t.Errorf("Bid.Validate() error = nil, wantErr = %v", tt.wantErr)
return
}
if err.Error() != tt.wantErr.Error() {
t.Errorf("Bid.Validate() error = %v, wantErr = %v", err, tt.wantErr)
}
return
}
if err != nil {
t.Errorf("Bid.Validate() error = %v, wantErr = nil", err)
}
})
}
}
func TestBidWithJSON(t *testing.T) {
jsonData := `{
"id": "test_bid_id",
"impid": "test_imp_id",
"price": 1.23,
"adm": "<ad markup>",
"adid": "ad123",
"adomain": ["advertiser.com"],
"iurl": "https://image.url",
"cid": "campaign123",
"crid": "creative123",
"w": 300,
"h": 250
}`
var bid Bid
err := json.Unmarshal([]byte(jsonData), &bid)
if err != nil {
t.Fatalf("Failed to unmarshal JSON: %v", err)
}
err = bid.Validate()
if err != nil {
t.Errorf("Bid.Validate() error = %v, wantErr = nil", err)
}
}