-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrun_report_test.go
138 lines (119 loc) · 3.99 KB
/
run_report_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
package ga4data
import (
"context"
"testing"
"github.com/hirokisan/ga4data/ga4datatest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
analyticsdata "google.golang.org/api/analyticsdata/v1beta"
"google.golang.org/api/option"
)
func TestRunReport(t *testing.T) {
ctx := context.Background()
t.Run("no paging", func(t *testing.T) {
wantRequestedCount := 1
resBody := ga4datatest.RunReportResponseBody(
ga4datatest.RunReportResponseBodyWithRowCount(int64(wantRequestedCount) * RunReportMaxRowsLimit),
)
testClient := ga4datatest.NewTestClient(resBody)
service, err := analyticsdata.NewService(ctx, option.WithHTTPClient(testClient))
require.NoError(t, err)
request := CreateRunReportRequest(analyticsdata.DateRange{
StartDate: "2022-10-01",
EndDate: "2022-10-01",
})
resp, err := RunReport(ctx, service, "test", request)
require.NoError(t, err)
assert.Equal(t, wantRequestedCount, resp.RequestedCount)
})
t.Run("paging all", func(t *testing.T) {
wantRequestedCount := 2
resBody := ga4datatest.RunReportResponseBody(
ga4datatest.RunReportResponseBodyWithRowCount(int64(wantRequestedCount) * RunReportMaxRowsLimit),
)
testClient := ga4datatest.NewTestClient(resBody)
service, err := analyticsdata.NewService(ctx, option.WithHTTPClient(testClient))
require.NoError(t, err)
request := CreateRunReportRequest(analyticsdata.DateRange{
StartDate: "2022-10-01",
EndDate: "2022-10-01",
})
resp, err := RunReport(ctx, service, "test", request)
require.NoError(t, err)
assert.Equal(t, wantRequestedCount, resp.RequestedCount)
})
t.Run("quota consumed will be accumulated", func(t *testing.T) {
wantRequestedCount := 2
propertyQuota := analyticsdata.PropertyQuota{
ConcurrentRequests: &analyticsdata.QuotaStatus{
Consumed: 0,
Remaining: 10,
},
PotentiallyThresholdedRequestsPerHour: &analyticsdata.QuotaStatus{
Consumed: 0,
Remaining: 120,
},
ServerErrorsPerProjectPerHour: &analyticsdata.QuotaStatus{
Consumed: 0,
Remaining: 10,
},
TokensPerDay: &analyticsdata.QuotaStatus{
Consumed: 3,
Remaining: 24997,
},
TokensPerHour: &analyticsdata.QuotaStatus{
Consumed: 3,
Remaining: 4997,
},
TokensPerProjectPerHour: &analyticsdata.QuotaStatus{
Consumed: 3,
Remaining: 1247,
},
}
resBody := ga4datatest.RunReportResponseBody(
ga4datatest.RunReportResponseBodyWithRowCount(int64(wantRequestedCount)*RunReportMaxRowsLimit),
ga4datatest.RunReportResponseBodyWithQuota(propertyQuota),
)
testClient := ga4datatest.NewTestClient(resBody)
service, err := analyticsdata.NewService(ctx, option.WithHTTPClient(testClient))
require.NoError(t, err)
request := CreateRunReportRequest(analyticsdata.DateRange{
StartDate: "2022-10-01",
EndDate: "2022-10-01",
},
RunReportRequestWithPropertyQuota(),
)
resp, err := RunReport(ctx, service, "test", request)
require.NoError(t, err)
require.Equal(t, wantRequestedCount, resp.RequestedCount)
assert.Equal(t, propertyQuota.TokensPerDay.Consumed*int64(wantRequestedCount), resp.PropertyQuota.TokensPerDay.Consumed)
assert.Equal(t, propertyQuota.TokensPerHour.Consumed*int64(wantRequestedCount), resp.PropertyQuota.TokensPerHour.Consumed)
assert.Equal(t, propertyQuota.TokensPerProjectPerHour.Consumed*int64(wantRequestedCount), resp.PropertyQuota.TokensPerProjectPerHour.Consumed)
})
}
func TestDimensionIndex(t *testing.T) {
targetColumn := DimensionDate
res := RunReportResponse{
RunReportResponse: &analyticsdata.RunReportResponse{
DimensionHeaders: []*analyticsdata.DimensionHeader{
{
Name: targetColumn,
},
},
},
}
assert.Equal(t, 0, res.DimensionIndex(targetColumn))
}
func TestMetricIndex(t *testing.T) {
targetColumn := MetricSessions
res := RunReportResponse{
RunReportResponse: &analyticsdata.RunReportResponse{
MetricHeaders: []*analyticsdata.MetricHeader{
{
Name: targetColumn,
},
},
},
}
assert.Equal(t, 0, res.MetricIndex(targetColumn))
}