-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandler_test.go
66 lines (56 loc) · 2.04 KB
/
handler_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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package natsdb
import (
"os"
"testing"
"time"
"github.com/nats-io/go-nats"
. "github.com/smartystreets/goconvey/convey"
)
func TestGetHandler(t *testing.T) {
var natsURL = os.Getenv("NATS_URI")
n, _ := nats.Connect(natsURL)
handler := Handler{
NotFoundErrorMessage: []byte(`{"error":"not found"}`),
UnexpectedErrorMessage: []byte(`{"error":"unexpected"}`),
DeletedMessage: []byte(`{"deleted"}`),
Nats: n,
NewModel: func() Model {
return &Entity{}
},
}
Convey("Scenario: getting a client", t, func() {
n.Subscribe("client.get", handler.Get)
Convey("Given the client does not exist on the database", func() {
msg, err := n.Request("client.get", []byte(`{"id":"32"}`), time.Second)
So(string(msg.Data), ShouldEqual, string(`{"id":0,"name":""}`))
So(err, ShouldEqual, nil)
})
})
Convey("Scenario: deleting a client", t, func() {
n.Subscribe("client.del", handler.Del)
Convey("Given the client does not exist on the database", func() {
msg, err := n.Request("client.del", []byte(`{"id":"32"}`), time.Second)
So(string(msg.Data), ShouldEqual, string(handler.DeletedMessage))
So(err, ShouldEqual, nil)
})
})
Convey("Scenario: setting a client", t, func() {
n.Subscribe("client.set", handler.Set)
Convey("Given the client does not exist on the database", func() {
msg, err := n.Request("client.set", []byte(`{"id":"32"}`), time.Second)
So(string(msg.Data), ShouldEqual, string(`{"id":0,"name":""}`))
So(err, ShouldEqual, nil)
})
})
Convey("Scenario: setting a client", t, func() {
n.Subscribe("client.find", handler.Set)
Convey("Given the client does not exist on the database", func() {
msg, err := n.Request("client.find", []byte(`{"id":"32"}`), time.Second)
So(string(msg.Data), ShouldEqual, string(`{"id":0,"name":""}`))
So(err, ShouldEqual, nil)
})
})
}