Merge pull request #95 from hybridgroup/148-spark-tests
Tests for spark
This commit is contained in:
commit
045229029f
|
@ -9,6 +9,10 @@ func TestAdaptor(t *testing.T) {
|
|||
Assert(t, a.Port(), "/dev/null1")
|
||||
a.SetName("myAdaptor")
|
||||
Assert(t, a.Name(), "myAdaptor")
|
||||
|
||||
a.SetConnected(true)
|
||||
Assert(t, a.Connected(), true)
|
||||
|
||||
a.SetConnected(false)
|
||||
Assert(t, a.Connected(), false)
|
||||
}
|
||||
|
|
|
@ -13,6 +13,7 @@ type SparkCoreAdaptor struct {
|
|||
gobot.Adaptor
|
||||
DeviceID string
|
||||
AccessToken string
|
||||
APIServer string
|
||||
}
|
||||
|
||||
func NewSparkCoreAdaptor(name string, deviceID string, accessToken string) *SparkCoreAdaptor {
|
||||
|
@ -23,6 +24,7 @@ func NewSparkCoreAdaptor(name string, deviceID string, accessToken string) *Spar
|
|||
),
|
||||
DeviceID: deviceID,
|
||||
AccessToken: accessToken,
|
||||
APIServer: "https://api.spark.io",
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -41,11 +43,14 @@ func (s *SparkCoreAdaptor) AnalogRead(pin string) float64 {
|
|||
"params": {pin},
|
||||
"access_token": {s.AccessToken},
|
||||
}
|
||||
|
||||
url := fmt.Sprintf("%v/analogread", s.deviceURL())
|
||||
resp := s.postToSpark(url, params)
|
||||
if resp != nil {
|
||||
|
||||
resp, err := s.postToSpark(url, params)
|
||||
if err == nil {
|
||||
return resp["return_value"].(float64)
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
|
@ -77,15 +82,22 @@ func (s *SparkCoreAdaptor) DigitalRead(pin string) int {
|
|||
"access_token": {s.AccessToken},
|
||||
}
|
||||
url := fmt.Sprintf("%v/digitalread", s.deviceURL())
|
||||
resp := s.postToSpark(url, params)
|
||||
if resp != nil {
|
||||
resp, err := s.postToSpark(url, params)
|
||||
if err == nil {
|
||||
return int(resp["return_value"].(float64))
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
func (s *SparkCoreAdaptor) setAPIServer(server string) {
|
||||
s.APIServer = server
|
||||
}
|
||||
|
||||
func (s *SparkCoreAdaptor) deviceURL() string {
|
||||
return fmt.Sprintf("https://api.spark.io/v1/devices/%v", s.DeviceID)
|
||||
if len(s.APIServer) <= 0 {
|
||||
s.setAPIServer("https://api.spark.io")
|
||||
}
|
||||
return fmt.Sprintf("%v/v1/devices/%v", s.APIServer, s.DeviceID)
|
||||
}
|
||||
|
||||
func (s *SparkCoreAdaptor) pinLevel(level byte) string {
|
||||
|
@ -95,22 +107,27 @@ func (s *SparkCoreAdaptor) pinLevel(level byte) string {
|
|||
return "LOW"
|
||||
}
|
||||
|
||||
func (s *SparkCoreAdaptor) postToSpark(url string, params url.Values) map[string]interface{} {
|
||||
func (s *SparkCoreAdaptor) postToSpark(url string, params url.Values) (m map[string]interface{}, err error) {
|
||||
resp, err := http.PostForm(url, params)
|
||||
if err != nil {
|
||||
fmt.Println(s.Name, "Error writing to spark device", err)
|
||||
return nil
|
||||
return
|
||||
}
|
||||
m := make(map[string]interface{})
|
||||
|
||||
buf, err := ioutil.ReadAll(resp.Body)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(s.Name, "Error reading response body", err)
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
json.Unmarshal(buf, &m)
|
||||
|
||||
if resp.Status != "200 OK" {
|
||||
fmt.Println(s.Name, "Error: ", m["error"])
|
||||
return nil
|
||||
err = fmt.Errorf("%q was not found", url)
|
||||
return
|
||||
}
|
||||
return m
|
||||
|
||||
return
|
||||
}
|
||||
|
|
|
@ -2,18 +2,255 @@ package spark
|
|||
|
||||
import (
|
||||
"github.com/hybridgroup/gobot"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// HELPERS
|
||||
|
||||
func createTestServer(handler func(w http.ResponseWriter, r *http.Request)) *httptest.Server {
|
||||
return httptest.NewServer(http.HandlerFunc(handler))
|
||||
}
|
||||
|
||||
func getDummyResponseForPath(path string, dummy_response string, t *testing.T) *httptest.Server {
|
||||
dummy_data := []byte(dummy_response)
|
||||
|
||||
return createTestServer(func(w http.ResponseWriter, r *http.Request) {
|
||||
actualPath := "/v1/devices" + path
|
||||
if r.URL.Path != actualPath {
|
||||
t.Errorf("Path doesn't match, expected %#v, got %#v", actualPath, r.URL.Path)
|
||||
}
|
||||
w.Write(dummy_data)
|
||||
})
|
||||
}
|
||||
|
||||
func getDummyResponseForPathWithParams(path string, params []string, dummy_response string, t *testing.T) *httptest.Server {
|
||||
dummy_data := []byte(dummy_response)
|
||||
|
||||
return createTestServer(func(w http.ResponseWriter, r *http.Request) {
|
||||
actualPath := "/v1/devices" + path
|
||||
if r.URL.Path != actualPath {
|
||||
t.Errorf("Path doesn't match, expected %#v, got %#v", actualPath, r.URL.Path)
|
||||
}
|
||||
|
||||
r.ParseForm()
|
||||
|
||||
for key, value := range params {
|
||||
if r.Form["params"][key] != value {
|
||||
t.Error("Expected param to be " + r.Form["params"][key] + " but was " + value)
|
||||
}
|
||||
}
|
||||
w.Write(dummy_data)
|
||||
})
|
||||
}
|
||||
|
||||
func initTestSparkCoreAdaptor() *SparkCoreAdaptor {
|
||||
return NewSparkCoreAdaptor("bot", "", "")
|
||||
return NewSparkCoreAdaptor("bot", "myDevice", "token")
|
||||
}
|
||||
|
||||
// TESTS
|
||||
|
||||
func TestSparkCoreAdaptor(t *testing.T) {
|
||||
// does it implements AdaptorInterface?
|
||||
var _ gobot.AdaptorInterface = (*SparkCoreAdaptor)(nil)
|
||||
|
||||
//does it embed gobot.Adaptor?
|
||||
var a interface{} = initTestSparkCoreAdaptor().Adaptor
|
||||
_, ok := a.(gobot.Adaptor)
|
||||
if !ok {
|
||||
t.Errorf("SparkCoreAdaptor{}.Adaptor should be a gobot.Adaptor")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewSparkCoreAdaptor(t *testing.T) {
|
||||
// does it return a pointer to an instance of SparkCoreAdaptor?
|
||||
var a interface{} = initTestSparkCoreAdaptor()
|
||||
spark, ok := a.(*SparkCoreAdaptor)
|
||||
if !ok {
|
||||
t.Errorf("NewSparkCoreAdaptor() should have returned a *SparkCoreAdaptor")
|
||||
}
|
||||
|
||||
gobot.Assert(t, spark.APIServer, "https://api.spark.io")
|
||||
}
|
||||
|
||||
func TestSparkCoreAdaptorConnect(t *testing.T) {
|
||||
a := initTestSparkCoreAdaptor()
|
||||
gobot.Assert(t, a.Connect(), true)
|
||||
|
||||
a.SetConnected(false)
|
||||
|
||||
gobot.Assert(t, a.Connect(), true)
|
||||
gobot.Assert(t, a.Connected(), true)
|
||||
}
|
||||
|
||||
func TestSparkCoreAdaptorFinalize(t *testing.T) {
|
||||
a := initTestSparkCoreAdaptor()
|
||||
|
||||
a.Connect()
|
||||
|
||||
gobot.Assert(t, a.Finalize(), true)
|
||||
gobot.Assert(t, a.Connected(), false)
|
||||
}
|
||||
|
||||
func TestSparkCoreAdaptorAnalogRead(t *testing.T) {
|
||||
// When no error
|
||||
response := `{"return_value": 5.2}`
|
||||
params := []string{"A1"}
|
||||
|
||||
a := initTestSparkCoreAdaptor()
|
||||
testServer := getDummyResponseForPathWithParams("/"+a.DeviceID+"/analogread", params, response, t)
|
||||
|
||||
a.setAPIServer(testServer.URL)
|
||||
|
||||
gobot.Assert(t, a.AnalogRead("A1"), 5.2)
|
||||
|
||||
testServer.Close()
|
||||
|
||||
// When error
|
||||
testServer = createTestServer(func(w http.ResponseWriter, r *http.Request) {
|
||||
http.NotFound(w, r)
|
||||
})
|
||||
defer testServer.Close()
|
||||
|
||||
gobot.Assert(t, a.AnalogRead("A1"), float64(0))
|
||||
|
||||
}
|
||||
|
||||
func TestSparkCoreAdaptorPwmWrite(t *testing.T) {
|
||||
response := `{}`
|
||||
params := []string{"A1,1"}
|
||||
|
||||
a := initTestSparkCoreAdaptor()
|
||||
testServer := getDummyResponseForPathWithParams("/"+a.DeviceID+"/analogwrite", params, response, t)
|
||||
defer testServer.Close()
|
||||
|
||||
a.setAPIServer(testServer.URL)
|
||||
a.PwmWrite("A1", 1)
|
||||
}
|
||||
|
||||
func TestSparkCoreAdaptorAnalogWrite(t *testing.T) {
|
||||
response := `{}`
|
||||
params := []string{"A1,1"}
|
||||
|
||||
a := initTestSparkCoreAdaptor()
|
||||
testServer := getDummyResponseForPathWithParams("/"+a.DeviceID+"/analogwrite", params, response, t)
|
||||
defer testServer.Close()
|
||||
|
||||
a.setAPIServer(testServer.URL)
|
||||
a.AnalogWrite("A1", 1)
|
||||
}
|
||||
|
||||
func TestSparkCoreAdaptorDigitalWrite(t *testing.T) {
|
||||
// When HIGH
|
||||
response := `{}`
|
||||
params := []string{"D7,HIGH"}
|
||||
|
||||
a := initTestSparkCoreAdaptor()
|
||||
testServer := getDummyResponseForPathWithParams("/"+a.DeviceID+"/digitalwrite", params, response, t)
|
||||
|
||||
a.setAPIServer(testServer.URL)
|
||||
a.DigitalWrite("D7", 1)
|
||||
|
||||
testServer.Close()
|
||||
// When LOW
|
||||
params = []string{"D7,LOW"}
|
||||
|
||||
testServer = getDummyResponseForPathWithParams("/"+a.DeviceID+"/digitalwrite", params, response, t)
|
||||
defer testServer.Close()
|
||||
|
||||
a.setAPIServer(testServer.URL)
|
||||
a.DigitalWrite("D7", 0)
|
||||
}
|
||||
|
||||
func TestSparkCoreAdaptorDigitalRead(t *testing.T) {
|
||||
// When HIGH
|
||||
response := `{"return_value": 1}`
|
||||
params := []string{"D7"}
|
||||
|
||||
a := initTestSparkCoreAdaptor()
|
||||
testServer := getDummyResponseForPathWithParams("/"+a.DeviceID+"/digitalread", params, response, t)
|
||||
|
||||
a.setAPIServer(testServer.URL)
|
||||
|
||||
gobot.Assert(t, a.DigitalRead("D7"), 1)
|
||||
testServer.Close()
|
||||
|
||||
// When LOW
|
||||
response = `{"return_value": 0}`
|
||||
|
||||
testServer = getDummyResponseForPathWithParams("/"+a.DeviceID+"/digitalread", params, response, t)
|
||||
|
||||
a.setAPIServer(testServer.URL)
|
||||
|
||||
gobot.Assert(t, a.DigitalRead("D7"), 0)
|
||||
|
||||
testServer.Close()
|
||||
|
||||
// When error
|
||||
testServer = createTestServer(func(w http.ResponseWriter, r *http.Request) {
|
||||
http.NotFound(w, r)
|
||||
})
|
||||
defer testServer.Close()
|
||||
|
||||
gobot.Assert(t, a.DigitalRead("D7"), -1)
|
||||
}
|
||||
|
||||
func TestSparkCoreAdaptorSetAPIServer(t *testing.T) {
|
||||
|
||||
a := initTestSparkCoreAdaptor()
|
||||
apiServer := "new_api_server"
|
||||
gobot.Refute(t, a.APIServer, apiServer)
|
||||
|
||||
a.setAPIServer(apiServer)
|
||||
gobot.Assert(t, a.APIServer, apiServer)
|
||||
}
|
||||
|
||||
func TestSparkCoreAdaptorDeviceURL(t *testing.T) {
|
||||
// When APIServer is set
|
||||
a := initTestSparkCoreAdaptor()
|
||||
a.setAPIServer("http://server")
|
||||
a.DeviceID = "devID"
|
||||
gobot.Assert(t, a.deviceURL(), "http://server/v1/devices/devID")
|
||||
|
||||
//When APIServer is not set
|
||||
a = &SparkCoreAdaptor{Adaptor: gobot.Adaptor{}, DeviceID: "myDevice", AccessToken: "token"}
|
||||
|
||||
gobot.Assert(t, a.deviceURL(), "https://api.spark.io/v1/devices/myDevice")
|
||||
}
|
||||
|
||||
func TestSparkCoreAdaptorPinLevel(t *testing.T) {
|
||||
|
||||
a := initTestSparkCoreAdaptor()
|
||||
|
||||
gobot.Assert(t, a.pinLevel(1), "HIGH")
|
||||
gobot.Assert(t, a.pinLevel(0), "LOW")
|
||||
gobot.Assert(t, a.pinLevel(5), "LOW")
|
||||
}
|
||||
|
||||
func TestSparkCoreAdaptorPostToSpark(t *testing.T) {
|
||||
|
||||
a := initTestSparkCoreAdaptor()
|
||||
|
||||
// When error on request
|
||||
resp, err := a.postToSpark("http://invalid%20host.com", url.Values{})
|
||||
if err == nil {
|
||||
t.Errorf("postToSpark() should return an error when request was unsuccessful but returned", resp)
|
||||
}
|
||||
|
||||
// When error reading body
|
||||
// Pending
|
||||
|
||||
// When response.Status is not 200
|
||||
testServer := createTestServer(func(w http.ResponseWriter, r *http.Request) {
|
||||
http.NotFound(w, r)
|
||||
})
|
||||
defer testServer.Close()
|
||||
|
||||
resp, err = a.postToSpark(testServer.URL+"/existent", url.Values{})
|
||||
if err == nil {
|
||||
t.Errorf("postToSpark() should return an error when status is not 200 but returned", resp)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue