Skip to content

Commit

Permalink
Update tests for api and core
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Jun 12, 2014
1 parent d2a5243 commit cf175b9
Show file tree
Hide file tree
Showing 8 changed files with 248 additions and 285 deletions.
3 changes: 0 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ install:
- go get github.com/onsi/gomega
- go get code.google.com/p/go.tools/cmd/cover
- go get github.com/mattn/goveralls
- go get github.com/go-martini/martini
- go get github.com/martini-contrib/auth
- go get github.com/martini-contrib/cors
- go get github.com/hybridgroup/go-ardrone/client
- go get github.com/tarm/goserial
#- go get github.com/hybridgroup/go-sdl2/sdl
Expand Down
20 changes: 0 additions & 20 deletions api/api_suite_test.go

This file was deleted.

265 changes: 137 additions & 128 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,136 +4,145 @@ import (
"bytes"
"encoding/json"
"github.com/hybridgroup/gobot"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"testing"
)

var _ = Describe("API", func() {
var (
m *gobot.Gobot
a *api
)

BeforeEach(func() {
m = gobot.NewGobot()
a = NewAPI(m)
a.start = func(m *api) {}
a.Start()

m.Robots = []*gobot.Robot{
gobot.NewTestRobot("Robot 1"),
gobot.NewTestRobot("Robot 2"),
gobot.NewTestRobot("Robot 3"),
}
})

Context("when valid", func() {
It("should return all robots", func() {
request, _ := http.NewRequest("GET", "/robots", nil)
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)

body, _ := ioutil.ReadAll(response.Body)
var i []map[string]interface{}
json.Unmarshal(body, &i)
Expect(len(i)).To(Equal(3))
})
It("should return robot", func() {
request, _ := http.NewRequest("GET", "/robots/Robot%201", nil)
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)

body, _ := ioutil.ReadAll(response.Body)
var i map[string]interface{}
json.Unmarshal(body, &i)
Expect(i["name"].(string)).To(Equal("Robot 1"))
})
It("should return all robot devices", func() {
request, _ := http.NewRequest("GET", "/robots/Robot%201/devices", nil)
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)

body, _ := ioutil.ReadAll(response.Body)
var i []map[string]interface{}
json.Unmarshal(body, &i)
Expect(len(i)).To(Equal(3))
})
It("should return robot commands", func() {
request, _ := http.NewRequest("GET", "/robots/Robot%201/commands", nil)
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)

body, _ := ioutil.ReadAll(response.Body)
var i []string
json.Unmarshal(body, &i)
Expect(i).To(Equal([]string{"robotTestFunction"}))
})
It("should execute robot command", func() {
request, _ := http.NewRequest("GET", "/robots/Robot%201/commands/robotTestFunction", bytes.NewBufferString(`{"message":"Beep Boop"}`))
request.Header.Add("Content-Type", "application/json")
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)

body, _ := ioutil.ReadAll(response.Body)
var i interface{}
json.Unmarshal(body, &i)
Expect(i).To(Equal("hey Robot 1, Beep Boop"))
})
It("should not execute unknown robot command", func() {
request, _ := http.NewRequest("GET", "/robots/Robot%201/commands/robotTestFuntion1", bytes.NewBufferString(`{"message":"Beep Boop"}`))
request.Header.Add("Content-Type", "application/json")
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)

body, _ := ioutil.ReadAll(response.Body)
var i interface{}
json.Unmarshal(body, &i)
Expect(i).To(Equal("Unknown Command"))
})
It("should return robot device", func() {
request, _ := http.NewRequest("GET", "/robots/Robot%201/devices/Device%201", nil)
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)

body, _ := ioutil.ReadAll(response.Body)
var i map[string]interface{}
json.Unmarshal(body, &i)
Expect(i["name"].(string)).To(Equal("Device 1"))
})
It("should return device commands", func() {
request, _ := http.NewRequest("GET", "/robots/Robot%201/devices/Device%201/commands", nil)
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)

body, _ := ioutil.ReadAll(response.Body)
var i []string
json.Unmarshal(body, &i)
Expect(i).To(Equal([]string{"TestDriverCommand", "DriverCommand"}))
})
It("should execute device command", func() {
request, _ := http.NewRequest("GET", "/robots/Robot%201/devices/Device%201/commands/TestDriverCommand", bytes.NewBufferString(`{"name":"human"}`))
request.Header.Add("Content-Type", "application/json")
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)

body, _ := ioutil.ReadAll(response.Body)
var i interface{}
json.Unmarshal(body, &i)
Expect(i).To(Equal("hello human"))
})
It("should not execute unknown device command", func() {
request, _ := http.NewRequest("GET", "/robots/Robot%201/devices/Device%201/commands/DriverCommand1", bytes.NewBufferString(`{"name":"human"}`))
request.Header.Add("Content-Type", "application/json")
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)

body, _ := ioutil.ReadAll(response.Body)
var i interface{}
json.Unmarshal(body, &i)
Expect(i).To(Equal("Unknown Command"))
})
})
})
type null struct{}

func (null) Write(p []byte) (int, error) {
return len(p), nil
}

var m *gobot.Gobot
var a *api

func init() {
log.SetOutput(new(null))
m = gobot.NewGobot()
a = NewAPI(m)
a.start = func(m *api) {}
a.Start()

m.Robots = []*gobot.Robot{
gobot.NewTestRobot("Robot 1"),
gobot.NewTestRobot("Robot 2"),
gobot.NewTestRobot("Robot 3"),
}
}

func TestRobots(t *testing.T) {
request, _ := http.NewRequest("GET", "/robots", nil)
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)

body, _ := ioutil.ReadAll(response.Body)
var i []map[string]interface{}
json.Unmarshal(body, &i)
gobot.Expect(t, len(i), 3)
}

func TestRobot(t *testing.T) {
request, _ := http.NewRequest("GET", "/robots/Robot%201", nil)
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)

body, _ := ioutil.ReadAll(response.Body)
var i map[string]interface{}
json.Unmarshal(body, &i)
gobot.Expect(t, i["name"].(string), "Robot 1")
}

func TestRobotDevices(t *testing.T) {
request, _ := http.NewRequest("GET", "/robots/Robot%201/devices", nil)
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)

body, _ := ioutil.ReadAll(response.Body)
var i []map[string]interface{}
json.Unmarshal(body, &i)
gobot.Expect(t, len(i), 3)
}

func TestRobotCommands(t *testing.T) {
request, _ := http.NewRequest("GET", "/robots/Robot%201/commands", nil)
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)

body, _ := ioutil.ReadAll(response.Body)
var i []string
json.Unmarshal(body, &i)
gobot.Expect(t, i, []string{"robotTestFunction"})
}
func TestExecuteRobotCommand(t *testing.T) {
request, _ := http.NewRequest("GET", "/robots/Robot%201/commands/robotTestFunction", bytes.NewBufferString(`{"message":"Beep Boop"}`))
request.Header.Add("Content-Type", "application/json")
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)

body, _ := ioutil.ReadAll(response.Body)
var i interface{}
json.Unmarshal(body, &i)
gobot.Expect(t, i, "hey Robot 1, Beep Boop")
}

func TestUnknownRobotCommand(t *testing.T) {
request, _ := http.NewRequest("GET", "/robots/Robot%201/commands/robotTestFuntion1", bytes.NewBufferString(`{"message":"Beep Boop"}`))
request.Header.Add("Content-Type", "application/json")
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)

body, _ := ioutil.ReadAll(response.Body)
var i interface{}
json.Unmarshal(body, &i)
gobot.Expect(t, i, "Unknown Command")
}

func TestRobotDevice(t *testing.T) {
request, _ := http.NewRequest("GET", "/robots/Robot%201/devices/Device%201", nil)
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)

body, _ := ioutil.ReadAll(response.Body)
var i map[string]interface{}
json.Unmarshal(body, &i)
gobot.Expect(t, i["name"].(string), "Device 1")
}

func TestRobotDeviceCommands(t *testing.T) {
request, _ := http.NewRequest("GET", "/robots/Robot%201/devices/Device%201/commands", nil)
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)

body, _ := ioutil.ReadAll(response.Body)
var i []string
json.Unmarshal(body, &i)
gobot.Expect(t, i, []string{"TestDriverCommand", "DriverCommand"})
}

func TestExecuteRobotDeviceCommand(t *testing.T) {
request, _ := http.NewRequest("GET", "/robots/Robot%201/devices/Device%201/commands/TestDriverCommand", bytes.NewBufferString(`{"name":"human"}`))
request.Header.Add("Content-Type", "application/json")
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)

body, _ := ioutil.ReadAll(response.Body)
var i interface{}
json.Unmarshal(body, &i)
gobot.Expect(t, i, "hello human")
}

func TestUnknownRobotDeviceCommand(t *testing.T) {
request, _ := http.NewRequest("GET", "/robots/Robot%201/devices/Device%201/commands/DriverCommand1", bytes.NewBufferString(`{"name":"human"}`))
request.Header.Add("Content-Type", "application/json")
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)

body, _ := ioutil.ReadAll(response.Body)
var i interface{}
json.Unmarshal(body, &i)
gobot.Expect(t, i, "Unknown Command")
}
14 changes: 0 additions & 14 deletions gobot_suite_test.go

This file was deleted.

Loading

0 comments on commit cf175b9

Please sign in to comment.