-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathomtr_test.go
100 lines (85 loc) · 1.78 KB
/
omtr_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
package omtr
import (
"encoding/json"
"fmt"
"os"
"testing"
"time"
)
func OmClient() *OmnitureClient {
return New(os.Getenv("OM_USERNAME"), os.Getenv("OM_SECRET"))
}
func SampleQuery() *ReportQuery {
return &ReportQuery{
ReportDescription: &Description{
ReportSuiteID: "cnn-adbp-domestic",
DateGranularity: "day",
Date: "2014-05-02",
Metrics: []*Metric{
&Metric{"pageviews"},
&Metric{"event32"},
&Metric{"event1"},
},
},
}
}
func TestJsonNumberAsInt(t *testing.T) {
var qr queueReport_response
raw := `{"reportID": 42}`
err := json.Unmarshal([]byte(raw), &qr)
if err != nil {
t.Error(err)
}
}
func TestJsonNumberAsString(t *testing.T) {
var qr queueReport_response
raw := `{"reportID": "42"}`
err := json.Unmarshal([]byte(raw), &qr)
if err != nil {
t.Error(err)
}
}
func TestJsonGetError(t *testing.T) {
var ge getError
raw := `{"error":"report_not_ready","error_description":"Report not ready","error_uri":null}`
err := json.Unmarshal([]byte(raw), &ge)
if err != nil {
t.Error(err)
}
}
func TestQueueReport(t *testing.T) {
omcl := OmClient()
resp, err := omcl.QueueReport(SampleQuery())
if err != nil {
t.Error(err)
}
fmt.Printf("response: %d\n", resp)
}
func TestReport(t *testing.T) {
omcl := OmClient()
c := make(chan *ReportResponse, 2)
reportId, err := omcl.Report(SampleQuery(), func(response *ReportResponse, err error) {
if err != nil {
t.Error(err)
c <- nil
} else {
c <- response
}
})
if err != nil {
t.Error(err)
}
fmt.Printf("Submitted report, reportId is %d\n", reportId)
go func() {
time.Sleep(30 * time.Second)
c <- nil
}()
for {
data := <-c
if data == nil {
t.Errorf("Timed out waiting for report %d", reportId)
}
fmt.Printf("received data: %s\n", data)
return
}
}