-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_test.go
74 lines (60 loc) · 1.83 KB
/
api_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
package main
import (
"io/ioutil"
"log"
"testing"
"github.com/kataras/iris"
"github.com/simplereach/timeutils"
)
func TestTradeGet(t *testing.T) {
m := MgoDB{}
m.Init()
m.C("trades").DropCollection()
defer m.Close()
iris.Default = iris.New()
iris.Config = iris.Default.Config
iris.Config.Tester.ListeningAddr = "localhost:8080"
iris.API("/api/v1/trades", TradeAPI{})
e := iris.Tester(t)
m.C("trades").Insert(NewTrade())
t.Log("GET /api/v1/trades, expecting status 200, array of length 1")
e.GET("/api/v1/trades").Expect().Status(iris.StatusOK).JSON().Array().Length().Equal(1)
m.C("trades").DropCollection()
}
func TestTradePost(t *testing.T) {
m := MgoDB{}
m.Init()
m.C("trades").DropCollection()
defer m.Close()
log.SetOutput(ioutil.Discard)
iris.Default = iris.New()
iris.Config = iris.Default.Config
iris.Config.Tester.ListeningAddr = "localhost:8080"
iris.API("/api/v1/trades", TradeAPI{})
e := iris.Tester(t)
trade := Trade{
UserID: 123,
CurrencyFrom: "EUR",
CurrencyTo: "GBP",
AmountSell: 1000,
AmountBuy: 700,
Rate: 0.7,
TimePlaced: timeutils.Time{},
OriginatingCountry: "IE",
}
t.Log("POST /api/v1/trades, expecting status 201 Created")
e.POST("/api/v1/trades").WithJSON(trade).Expect().Status(iris.StatusCreated)
trade.AmountBuy = 5000
t.Log("POST /api/v1/trades, expecting status 422 Unprocessable Entity")
e.POST("/api/v1/trades").WithJSON(trade).Expect().Status(422)
m.C("trades").DropCollection()
}
func TestTradeIndex(t *testing.T) {
iris.Default = iris.New()
iris.Config = iris.Default.Config
iris.Config.Tester.ListeningAddr = "localhost:8080"
iris.Get("/trades", TradeIndex)
e := iris.Tester(t)
t.Log("Get /trades, expecting status 200 OK")
e.GET("/trades").Expect().Status(iris.StatusOK).ContentType("text/html")
}