-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
132 lines (110 loc) · 2.34 KB
/
client_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
package sn_test
import (
"bufio"
"fmt"
"log"
"os"
"strings"
"testing"
sn "github.com/ekzyis/snappy"
)
var (
c = testClient()
)
func TestQueryItems(t *testing.T) {
var (
cursor *sn.ItemsCursor
err error
)
if cursor, err = c.Items(nil); err != nil {
t.Error(err)
return
}
if len(cursor.Items) == 0 {
t.Error("items cursor empty")
return
}
}
func TestMutationCreateComment(t *testing.T) {
var (
parentId = 349
text = "test comment"
err error
)
// TODO: return result, invoice, paymentMethod from CreateComment and run assertions on that
if _, err = c.CreateComment(parentId, text); err != nil {
t.Error(err)
return
}
}
func TestMutationPostDiscussion(t *testing.T) {
var (
title = "test discussion"
text = "test discussion text"
sub = "bitcoin"
err error
)
// TODO: return result, invoice, paymentMethod from CreateComment and run assertions on that
if _, err = c.PostDiscussion(title, text, sub); err != nil {
t.Error(err)
return
}
}
func TestMutationPostLink(t *testing.T) {
var (
url = "https://stacker.news"
title = "test discussion"
text = "test discussion text"
sub = "bitcoin"
err error
)
// TODO: return result, invoice, paymentMethod from CreateComment and run assertions on that
if _, err = c.PostLink(url, title, text, sub); err != nil {
t.Error(err)
return
}
}
func testClient() *sn.Client {
loadEnv()
baseUrl, set := os.LookupEnv("TEST_SN_BASE_URL")
if !set {
baseUrl = "http://localhost:3000"
}
log.Printf("baseUrl=%s\n", baseUrl)
apiKey, set := os.LookupEnv("TEST_SN_API_KEY")
if !set {
log.Fatalf("TEST_SN_API_KEY is not set")
}
log.Printf("apiKey=%s\n", apiKey)
return sn.NewClient(
sn.WithBaseUrl(baseUrl),
sn.WithApiKey(apiKey),
)
}
func loadEnv() {
var (
f *os.File
s *bufio.Scanner
err error
)
if f, err = os.Open(".env"); err != nil {
log.Fatalf("error opening .env: %v", err)
}
defer f.Close()
s = bufio.NewScanner(f)
s.Split(bufio.ScanLines)
for s.Scan() {
line := s.Text()
parts := strings.SplitN(line, "=", 2)
// Check if we have exactly 2 parts (key and value)
if len(parts) == 2 {
os.Setenv(parts[0], parts[1])
} else {
log.Fatalf(".env: invalid line: %s\n", line)
}
}
// Check for errors during scanning
if err = s.Err(); err != nil {
fmt.Println("error scanning .env:", err)
}
}