forked from zemirco/follow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfollow_test.go
More file actions
101 lines (90 loc) · 1.98 KB
/
follow_test.go
File metadata and controls
101 lines (90 loc) · 1.98 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package follow
import (
"bytes"
"fmt"
"net/http"
"testing"
"time"
)
func check(err error) {
if err != nil {
panic(err)
}
}
// create new document
func document(id string) {
url := fmt.Sprintf("%s%s/%s", Url, Database, id)
var data = []byte(`{"key": "value"}`)
req, err := http.NewRequest("PUT", url, bytes.NewBuffer(data))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
check(err)
defer resp.Body.Close()
}
func TestBefore(t *testing.T) {
// create new database
url := fmt.Sprintf("%s%s", Url, Database)
req, err := http.NewRequest("PUT", url, nil)
check(err)
client := &http.Client{}
resp, err := client.Do(req)
check(err)
defer resp.Body.Close()
// create document in database
document("john")
}
func TestChanges(t *testing.T) {
params := QueryParameters{}
changes, err := Changes(params)
if err != nil {
t.Fatal("changes error")
}
if changes.LastSeq != 1 {
t.Fatal("changes LastSeq error")
}
if len(changes.Results) != 1 {
t.Fatal("changes Results error")
}
}
func TestContinuous(t *testing.T) {
params := QueryParameters{}
changes, errors := Continuous(params)
// add document in separate goroutine
go func() {
// wait for for loop to start listening
time.Sleep(100 * time.Millisecond)
document("steve")
}()
// start listening for changes in main goroutine
loop:
for {
select {
// read from channel and test if it has been closed
case change, ok := <-changes:
if ok == false {
t.Fatal("continuous error")
}
if change.Id != "john" {
t.Fatal("continuous error")
}
if change.Id != "steve" {
t.Fatal("continuous error")
}
case err := <-errors:
t.Fatal(err)
case <-time.After(1 * time.Second):
break loop
}
}
}
func TestAfter(t *testing.T) {
// delete database
url := fmt.Sprintf("%s%s", Url, Database)
req, err := http.NewRequest("DELETE", url, nil)
check(err)
client := &http.Client{}
resp, err := client.Do(req)
check(err)
defer resp.Body.Close()
}