-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_data_test.go
115 lines (89 loc) · 2.78 KB
/
user_data_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
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"reflect"
"testing"
)
func TestGetsTwitterClient(t *testing.T) {
const consumerKey = "myKey"
const consumerSecret = "mySecret"
client := getTwitterOauth2Client(consumerKey, consumerSecret)
if "*http.Client" != fmt.Sprintf("%T", client) {
t.Errorf("getTwitterOauth2Client(%s, %s) returned %T want *http.Client", consumerKey, consumerSecret, client)
}
if "*oauth2.Transport" != fmt.Sprintf("%T", client.Transport) {
t.Errorf("getTwitterOauth2Client(%s, %s) returned Transport %T want *oauth2.Transport", consumerKey, consumerSecret, client.Transport)
}
}
func TestFetchIdForUsername(t *testing.T) {
_, err := fetchIdForUsername(&http.Client{}, "")
if err == nil {
t.Errorf("fetchIdForUsername(client, username) succeess want error")
}
if "*errors.errorString" != fmt.Sprintf("%T", err) {
t.Errorf("fetchIdForUsername(client, username) returned error %T want *errors.errorString", err)
}
}
func TestFetchesUserData(t *testing.T) {
const userId = "123"
client := &http.Client{}
_, err := fetchUserData(client, userId)
if err == nil {
t.Errorf("fetchUserData(client, userId) succeess want error")
}
if _, ok := err.(*json.UnmarshalTypeError); !ok {
t.Errorf("fetchUserData(client, userId) returned error %T want *json.UnmarshalTypeError", err)
}
}
func TestGetsTextLines(t *testing.T) {
var metrics map[string]string
var lines []string
var expected []string
metrics = map[string]string{
"following_count": "5",
"followers_count": "10",
"tweet_count": "101",
"listed_count": "2",
"location": "London, UK",
}
lines = GetTextLines(metrics, "")
lines = lines[:len(lines)-1]
expected = []string{
"5 Following",
"10 Followers",
"101 Tweets",
"In 2 lists",
"Located in London, UK",
}
if !reflect.DeepEqual(lines, expected) {
t.Errorf("GetTextLines(%#v) returned %#v want %#v", metrics, lines, expected)
}
delete(metrics, "location")
lines = GetTextLines(metrics, "Promoted string")
lines = append(lines[:len(lines)-2], lines[len(lines)-1])
expected = []string{
"5 Following",
"10 Followers",
"101 Tweets",
"In 2 lists",
"Promoted string",
}
if !reflect.DeepEqual(lines, expected) {
t.Errorf("GetTextLines(%#v) returned %#v want %#v", metrics, lines, expected)
}
}
func TestGetsTwitterUserData(t *testing.T) {
const consumerKey = "asff"
const consumerSecret = "afas"
const username = "bob"
_, err := GetTwitterUserData(consumerKey, consumerSecret, username)
if err == nil {
t.Errorf("GetTwitterUserData(%s, %s, %s) returned success want error", consumerKey, consumerSecret, username)
}
if _, ok := err.(*url.Error); !ok {
t.Errorf("GetTwitterUserData(%s, %s, %s) returned %T as error want *url.Error", consumerKey, consumerSecret, username, err)
}
}