forked from wit-ai/wit-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapps_test.go
112 lines (94 loc) · 3.11 KB
/
apps_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
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
package witai
import (
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestGetApps(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
res.Write([]byte(`[{"name": "app1"}, {"name":"app2"}]`))
}))
defer func() { testServer.Close() }()
c := NewClient(unitTestToken)
c.APIBase = testServer.URL
apps, _ := c.GetApps(1, 0)
if len(apps) != 2 {
t.Fatalf("expected 2 apps, got: %v", apps)
}
}
func TestGetApp(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
res.Write([]byte(`{"name": "alarm-clock","training_status":"done","will_train_at":"2018-07-29T18:17:34-0700","last_training_duration_secs":42,"last_trained_at":"2018-07-29T18:16:34-0700"}`))
}))
defer func() { testServer.Close() }()
c := NewClient(unitTestToken)
c.APIBase = testServer.URL
app, err := c.GetApp("my-id")
if err != nil {
t.Fatalf("not expected err, got: %s", err.Error())
}
if app.Name != "alarm-clock" {
t.Fatalf("expected alarm-clock, got: %v", app)
}
if app.TrainingStatus != Done {
t.Fatalf("expected Done, got: %v", app)
}
expectedNextTrainTime, _ := time.Parse(WitTimeFormat, "2018-07-29T18:17:34-0700")
if !app.WillTrainAt.Time.Equal(expectedNextTrainTime) {
t.Fatalf("expected %v got: %v", app.WillTrainAt, expectedNextTrainTime)
}
if app.LastTrainingDurationSecs != 42 {
t.Fatalf("Expected 42 got %v", app.LastTrainingDurationSecs)
}
expectedLastTrainTime, _ := time.Parse(WitTimeFormat, "2018-07-29T18:16:34-0700")
if !app.LastTrainedAt.Time.Equal(expectedLastTrainTime) {
t.Fatalf("expected %v got: %v", app.WillTrainAt, expectedLastTrainTime)
}
}
func TestCreateApp(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
res.Write([]byte(`{"app_id": "ai-dee"}`))
}))
defer func() { testServer.Close() }()
c := NewClient(unitTestToken)
c.APIBase = testServer.URL
app, err := c.CreateApp(App{
Name: "app",
})
if err != nil {
t.Fatalf("nil error expected, got %v", err)
}
if app.AppID != "ai-dee" {
t.Fatalf("id=ai-dee expected, got: %s", app.AppID)
}
}
func TestDeleteApp(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
res.Write([]byte(`{}`))
}))
defer func() { testServer.Close() }()
c := NewClient(unitTestToken)
c.APIBase = testServer.URL
if err := c.DeleteApp("appid"); err != nil {
t.Fatalf("expected nil error, got: %v", err)
}
}
func TestUpdateApp(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
res.Write([]byte(`{"description": "updated"}`))
}))
defer func() { testServer.Close() }()
c := NewClient(unitTestToken)
c.APIBase = testServer.URL
app, err := c.UpdateApp("appid", App{
Description: "new desc",
})
if err != nil {
t.Fatalf("err=nil expected, got: %v", err)
}
if app.Description != "updated" {
t.Fatalf("description=updated expected, got: %s", app.Lang)
}
}