Skip to content

Commit

Permalink
Tests for spark
Browse files Browse the repository at this point in the history
  • Loading branch information
rafmagana committed Aug 21, 2014
1 parent b910bf6 commit ac5f63e
Show file tree
Hide file tree
Showing 3 changed files with 203 additions and 4 deletions.
4 changes: 4 additions & 0 deletions adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
11 changes: 10 additions & 1 deletion platforms/spark/spark_core_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type SparkCoreAdaptor struct {
gobot.Adaptor
DeviceID string
AccessToken string
APIServer string
}

func NewSparkCoreAdaptor(name string, deviceID string, accessToken string) *SparkCoreAdaptor {
Expand All @@ -23,6 +24,7 @@ func NewSparkCoreAdaptor(name string, deviceID string, accessToken string) *Spar
),
DeviceID: deviceID,
AccessToken: accessToken,
APIServer: "https://api.spark.io",
}
}

Expand Down Expand Up @@ -84,8 +86,15 @@ func (s *SparkCoreAdaptor) DigitalRead(pin string) int {
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 {
Expand Down
192 changes: 189 additions & 3 deletions platforms/spark/spark_core_adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,203 @@ package spark
import (
"github.com/hybridgroup/gobot"
"testing"
"net/http"
"net/http/httptest"
)

func initTestSparkCoreAdaptor() *SparkCoreAdaptor {
return NewSparkCoreAdaptor("bot", "", "")
return NewSparkCoreAdaptor("bot", "myDevice", "token")
}

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()
gobot.Assert(t, a.Finalize(), true)
a := initTestSparkCoreAdaptor()

a.Connect()

gobot.Assert(t, a.Finalize(), true)
gobot.Assert(t, a.Connected(), false)
}

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 TestSparkCoreAdaptorAnalogRead(t *testing.T) {
response := `{"return_value": 5.2}`
params := []string{"A1"}

a := initTestSparkCoreAdaptor()
testServer := getDummyResponseForPathWithParams("/" +a.DeviceID + "/analogread", params, response, t)
defer testServer.Close()

a.setAPIServer(testServer.URL)

gobot.Assert(t, a.AnalogRead("A1"), 5.2)
}

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)
defer testServer.Close()

a.setAPIServer(testServer.URL)

gobot.Assert(t, a.DigitalRead("D7"), 0)
}

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")
}

0 comments on commit ac5f63e

Please sign in to comment.