-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathclassifier_test.go
102 lines (73 loc) · 2.7 KB
/
classifier_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
package elasticthought
import (
"encoding/json"
"fmt"
"strings"
"sync"
"testing"
"github.com/couchbaselabs/go.assert"
"github.com/tleyden/fakehttp"
)
// starting port to use for fakehttp servers
var port = 4444
// since I'm not sure if NextPort() can be called by multiple threads,
// protect it with a mutex
var portMutex = &sync.Mutex{}
// the fakehttp library doesn't provide an easy way to shutdown an http
// server, so as a workaround, run each fake http server on a unique port
// for each test.
func NextPort() int {
portMutex.Lock()
defer portMutex.Unlock()
port2use := port
port += 1
return port2use
}
func TestInsertClassifier(t *testing.T) {
testServer := fakehttp.NewHTTPServerWithPort(NextPort())
testServer.Start()
// response when go-couch tries to see that the server is up
testServer.Response(200, jsonHeaders(), `{"version": "fake"}`)
// response when go-couch check is db exists
testServer.Response(200, jsonHeaders(), `{"db_name": "db"}`)
// insert succeeds
testServer.Response(200, jsonHeaders(), `{"id": "classifier", "rev": "bar", "ok": true}`)
configuration := NewDefaultConfiguration()
configuration.DbUrl = fmt.Sprintf("%v/db", testServer.URL)
classifier := NewClassifier(*configuration)
classifier.SpecificationUrl = "http://s3.com/proto.txt"
classifier.TrainingJobID = "123"
err := classifier.Insert()
assert.True(t, err == nil)
assert.Equals(t, "classifier", classifier.Id)
assert.Equals(t, "bar", classifier.Revision)
}
func TestSetSpecificationUrl(t *testing.T) {
testServer := fakehttp.NewHTTPServerWithPort(NextPort())
testServer.Start()
// response when go-couch tries to see that the server is up
testServer.Response(200, jsonHeaders(), `{"version": "fake"}`)
// response when go-couch check is db exists
testServer.Response(200, jsonHeaders(), `{"db_name": "db"}`)
// update succeeds
testServer.Response(200, jsonHeaders(), `{"id": "classifier", "rev": "bar"}`)
configuration := NewDefaultConfiguration()
configuration.DbUrl = fmt.Sprintf("%v/db", testServer.URL)
classifier := NewClassifier(*configuration)
classifier.Id = "classifier"
classifier.Revision = "foo"
err := classifier.SetSpecificationUrl("whatever")
assert.True(t, err == nil)
// make assertions about outgoing request
for _, savedReq := range testServer.SavedRequests {
path := savedReq.Request.URL.Path
if strings.HasSuffix(path, "db/classifier") {
var requestDictionary map[string]interface{}
err := json.Unmarshal(savedReq.Data, &requestDictionary)
assert.True(t, err == nil)
assert.Equals(t, requestDictionary["_id"], "classifier")
assert.Equals(t, requestDictionary["_rev"], "foo")
assert.Equals(t, requestDictionary["specification-url"], "whatever")
}
}
}