-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclient_test.go
More file actions
86 lines (73 loc) · 3.03 KB
/
client_test.go
File metadata and controls
86 lines (73 loc) · 3.03 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
package seomoz
import (
"errors"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
type mockMozAPI struct {
metrics map[string]*URLMetrics
err error
}
func (m *mockMozAPI) GetURLMetrics(link string, cols int) (*URLMetrics, error) {
return m.metrics[link], m.err
}
func (m *mockMozAPI) GetBatchURLMetrics(urls []string, cols int) (map[string]*URLMetrics, error) {
if m.err != nil {
return nil, m.err
}
results := map[string]*URLMetrics{}
for _, link := range urls {
results[link] = m.metrics[link]
}
return results, nil
}
func TestEnvClient(t *testing.T) {
os.Setenv("SEOMOZ_ACCESS_ID", "my_id")
os.Setenv("SEOMOZ_SECRET_KEY", "my_secret")
client := NewEnvClient()
moz := client.moz.(*MOZ)
assert.Equal(t, moz.AccessID, "my_id")
assert.Equal(t, moz.SecretKey, "my_secret")
}
func TestClientGetSingleMetrics(t *testing.T) {
expectedMetrics := &URLMetrics{DomainAuthority: 45, PageAuthority: 68, Links: 123, URL: "https://example.com"}
mockMoz := &mockMozAPI{metrics: map[string]*URLMetrics{expectedMetrics.URL: expectedMetrics}}
client := &Client{moz: mockMoz}
m, err := client.GetURLMetrics("https://example.com", 0)
assert.Nil(t, err)
assert.Equal(t, expectedMetrics, m)
client = &Client{moz: &mockMozAPI{err: errors.New("mock error")}}
m, err = client.GetURLMetrics("https://example.com", 0)
assert.Nil(t, m)
assert.NotNil(t, err)
}
func TestClientGetBatchMetrics(t *testing.T) {
expectedMetrics := &URLMetrics{DomainAuthority: 45, PageAuthority: 68, Links: 123, URL: "https://example.com"}
mockMoz := &mockMozAPI{metrics: map[string]*URLMetrics{expectedMetrics.URL: expectedMetrics}}
client := &Client{moz: mockMoz}
m, err := client.GetBatchURLMetrics([]string{"https://example.com"}, 0)
assert.Nil(t, err)
assert.Equal(t, mockMoz.metrics, m)
client = &Client{moz: &mockMozAPI{err: errors.New("mock error")}}
m, err = client.GetBatchURLMetrics([]string{"https://example.com"}, 0)
assert.Nil(t, m)
assert.NotNil(t, err)
}
func TestClientGetBulkMetrics(t *testing.T) {
expectedMetrics := map[string]*URLMetrics{
"https://example.com/1": {DomainAuthority: 45, PageAuthority: 68, Links: 123, URL: "https://example.com/1"},
"https://example.com/2": {DomainAuthority: 45, PageAuthority: 68, Links: 123, URL: "https://example.com/2"},
"https://example.com/3": {DomainAuthority: 45, PageAuthority: 68, Links: 123, URL: "https://example.com/3"},
"https://example.com/4": {DomainAuthority: 45, PageAuthority: 68, Links: 123, URL: "https://example.com/4"},
}
mockMoz := &mockMozAPI{metrics: expectedMetrics}
client := &Client{moz: mockMoz, MaxBatchURLs: 2}
m, err := client.GetBulkURLMetrics([]string{"https://example.com/1", "https://example.com/2", "https://example.com/3", "https://example.com/4"}, 0)
assert.Nil(t, err)
assert.Equal(t, mockMoz.metrics, m)
client = &Client{moz: &mockMozAPI{err: errors.New("mock error")}, MaxBatchURLs: 0}
m, err = client.GetBulkURLMetrics([]string{"https://example.com/1", "https://example.com/2", "https://example.com/3", "https://example.com/4"}, 0)
assert.Nil(t, m)
assert.NotNil(t, err)
}