This repository has been archived by the owner on Jul 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
135 lines (114 loc) · 2.93 KB
/
main_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
package main
import (
"errors"
"fmt"
"io/ioutil"
"os"
"reflect"
"testing"
"github.com/dghubble/go-twitter/twitter"
homedir "github.com/mitchellh/go-homedir"
gock "gopkg.in/h2non/gock.v1"
)
type Test struct {
expected interface{}
actual interface{}
}
func (test Test) Compare(t *testing.T) {
if test.expected != test.actual {
t.Errorf(`Expected "%v" but got "%v"`, test.expected, test.actual)
}
}
func (test Test) DeepEqual(t *testing.T) {
if !reflect.DeepEqual(test.expected, test.actual) {
t.Errorf(`Expected "%v" but got "%v"`, test.expected, test.actual)
}
}
func setup(t *testing.T) {
rc, _ := homedir.Expand("~/.avaiabilitybotrc")
os.Remove(rc)
if _, err := os.Stat(rc); err == nil {
t.Fatalf("File exists at %v", rc)
}
}
func readrc() string {
rc, _ := homedir.Expand("~/.avaiabilitybotrc")
b, _ := ioutil.ReadFile(rc)
return string(b)
}
func TestNoArg(t *testing.T) {
setup(t)
statuses := []string{}
setUpdateStatusFn(func(c *twitter.Client, status string) error {
statuses = append(statuses, status)
return nil
})
os.Args = []string{"app"}
err := run()
if err != nil {
t.Errorf("Expected nil but got %v", err)
}
Test{[]string{}, statuses}.DeepEqual(t)
}
func TestAvailable(t *testing.T) {
defer gock.Off()
setup(t)
statuses := []string{}
setUpdateStatusFn(func(c *twitter.Client, status string) error {
statuses = append(statuses, status)
return nil
})
os.Args = []string{"app", "MMEF2J/A"}
gock.New("http://www.apple.com").
Get("/jp/shop/retail/pickup-message").
Reply(200).
File("_fixtures/available.json")
err := run()
if err != nil {
t.Errorf("Expected nil but got %v", err)
}
Test{
[]string{
"銀座店に AirPods の在庫が追加されました",
"名古屋栄店に AirPods の在庫が追加されました",
},
statuses,
}.DeepEqual(t)
Test{`[{"Name":"銀座","Product":"AirPods"},{"Name":"名古屋栄","Product":"AirPods"}]`, readrc()}.Compare(t)
}
func TestTweetError(t *testing.T) {
defer gock.Off()
setup(t)
setUpdateStatusFn(func(c *twitter.Client, status string) error {
return fmt.Errorf("omg %v", status)
})
os.Args = []string{"app", "MMEF2J/A"}
gock.New("http://www.apple.com").
Get("/jp/shop/retail/pickup-message").
Reply(200).
File("_fixtures/available.json")
err := run()
Test{"Tweet Error", err.Error()}.Compare(t)
Test{"", readrc()}.Compare(t)
}
func TestJSONError(t *testing.T) {
defer gock.Off()
setup(t)
statuses := []string{}
setUpdateStatusFn(func(c *twitter.Client, status string) error {
statuses = append(statuses, status)
return nil
})
os.Args = []string{"app", "MMEF2J/A"}
statuses = []string{}
gock.New("http://www.apple.com").
Get("/jp/shop/retail/pickup-message").
ReplyError(errors.New("omg"))
err := run()
Test{"Get http://www.apple.com/jp/shop/retail/pickup-message?location=100-0001&parts.0=MMEF2J%2FA: omg", err.Error()}.Compare(t)
Test{
[]string{},
statuses,
}.DeepEqual(t)
Test{"", readrc()}.Compare(t)
}