Skip to content

Commit

Permalink
Use same pkg for tests (#21)
Browse files Browse the repository at this point in the history
* Use same pkg for tests
* Additional unit tests
  • Loading branch information
Amir Mofasser authored May 4, 2020
1 parent 7f3dc1c commit b4a8f71
Show file tree
Hide file tree
Showing 13 changed files with 590 additions and 205 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
sudo: false
os: linux
dist: xenial
language: go
go:
Expand Down
70 changes: 64 additions & 6 deletions bridge_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package huego_test
package huego

import (
"context"
"fmt"
"github.com/amimof/huego"
"github.com/jarcoal/httpmock"
"strings"
"testing"
)

func ExampleBridge_CreateUser() {
bridge, _ := huego.Discover()
bridge, _ := Discover()
user, err := bridge.CreateUser("my awesome hue app") // Link button needs to be pressed
if err != nil {
fmt.Printf("Error creating user: %s", err.Error())
Expand All @@ -19,7 +20,7 @@ func ExampleBridge_CreateUser() {
}

func TestLogin(t *testing.T) {
b := huego.New(hostname, username)
b := New(hostname, username)
c, err := b.GetConfig()
if err != nil {
t.Fatal(err)
Expand All @@ -29,7 +30,7 @@ func TestLogin(t *testing.T) {
}

func TestLoginUnauthorized(t *testing.T) {
b := huego.New(hostname, "")
b := New(hostname, "")
b = b.Login("invalid_password")
_, err := b.GetLights()
if err != nil {
Expand All @@ -42,7 +43,7 @@ func TestLoginUnauthorized(t *testing.T) {
}

func TestUpdateBridgeConfig(t *testing.T) {
b := huego.New(hostname, username)
b := New(hostname, username)
c, err := b.GetConfig()
if err != nil {
t.Fatal(err)
Expand All @@ -52,3 +53,60 @@ func TestUpdateBridgeConfig(t *testing.T) {
t.Fatal(err)
}
}

func TestUpdateBridgeConfigError(t *testing.T) {
b := New(badHostname, username)
_, err := b.GetConfig()
if err == nil {
t.Fatal("Expected error not to be nil")
}
}

func TestBridge_getAPIPathError(t *testing.T) {
b := New("invalid hostname", "")
expected := "parse http://invalid hostname: invalid character \" \" in host name"
_, err := b.getAPIPath("/")
if err.Error() != expected {
t.Fatalf("Expected error %s but got %s", expected, err.Error())
}
}

func TestBridge_getError(t *testing.T) {
httpmock.Deactivate()
defer httpmock.Activate()
expected := "Get invalid%20hostname: unsupported protocol scheme \"\""
_, err := get(context.Background(), "invalid hostname")
if err.Error() != expected {
t.Fatalf("Expected error %s but got %s", expected, err.Error())
}
}

func TestBridge_putError(t *testing.T) {
httpmock.Deactivate()
defer httpmock.Activate()
expected := "Put invalid%20hostname: unsupported protocol scheme \"\""
_, err := put(context.Background(), "invalid hostname", []byte("huego"))
if err.Error() != expected {
t.Fatalf("Expected error %s but got %s", expected, err.Error())
}
}

func TestBridge_postError(t *testing.T) {
httpmock.Deactivate()
defer httpmock.Activate()
expected := "Post invalid%20hostname: unsupported protocol scheme \"\""
_, err := post(context.Background(), "invalid hostname", []byte("huego"))
if err.Error() != expected {
t.Fatalf("Expected error %s but got %s", expected, err.Error())
}
}

func TestBridge_deleteError(t *testing.T) {
httpmock.Deactivate()
defer httpmock.Activate()
expected := "Delete invalid%20hostname: unsupported protocol scheme \"\""
_, err := delete(context.Background(), "invalid hostname")
if err.Error() != expected {
t.Fatalf("Expected error %s but got %s", expected, err.Error())
}
}
14 changes: 10 additions & 4 deletions capabilities_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package huego_test
package huego

import (
"testing"

"github.com/amimof/huego"
)

func TestGetCapabilities(t *testing.T) {
b := huego.New(hostname, username)
b := New(hostname, username)
c, err := b.GetCapabilities()
if err != nil {
t.Fatal(c)
Expand All @@ -30,3 +28,11 @@ func TestGetCapabilities(t *testing.T) {
t.Log(" Streaming")
t.Logf(" Available: %d", c.Streaming.Available)
}

func TestGetCapabilitiesError(t *testing.T) {
b := New(badHostname, username)
_, err := b.GetCapabilities()
if err == nil {
t.Fatal("Expected error not to be nil")
}
}
50 changes: 40 additions & 10 deletions config_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package huego_test
package huego

import (
"testing"

"github.com/amimof/huego"
"github.com/stretchr/testify/assert"
"testing"
)

func TestGetConfig(t *testing.T) {
b := huego.New(hostname, username)
b := New(hostname, username)
config, err := b.GetConfig()
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -65,8 +63,16 @@ func TestGetConfig(t *testing.T) {
t.Logf("StarterKitID: %s", config.StarterKitID)
}

func TestGetConfigError(t *testing.T) {
b := New(badHostname, username)
_, err := b.GetConfig()
if err == nil {
t.Fatal("Expected error not to be nil")
}
}

func TestCreateUser(t *testing.T) {
b := huego.New(hostname, "")
b := New(hostname, "")
u, err := b.CreateUser("github.com/amimof/huego#go test")
if err != nil {
t.Fatal(err)
Expand All @@ -75,8 +81,16 @@ func TestCreateUser(t *testing.T) {
}
}

func TestCreateUserError(t *testing.T) {
b := New(badHostname, username)
_, err := b.CreateUser("github.com/amimof/huego#go test")
if err == nil {
t.Fatal("Expected error not to be nil")
}
}

func TestGetUsers(t *testing.T) {
b := huego.New(hostname, username)
b := New(hostname, username)
users, err := b.GetUsers()
if err != nil {
t.Fatal(err)
Expand All @@ -88,7 +102,7 @@ func TestGetUsers(t *testing.T) {
t.Logf(" CreateDate: %s", u.CreateDate)
t.Logf(" LastUseDate: %s", u.LastUseDate)
}
contains := func(name string, ss []huego.Whitelist) bool {
contains := func(name string, ss []Whitelist) bool {
for _, s := range ss {
if s.Name == name {
return true
Expand All @@ -101,8 +115,16 @@ func TestGetUsers(t *testing.T) {
assert.True(t, contains("MyApplication", users))
}

func TestGetUsersError(t *testing.T) {
b := New(badHostname, username)
_, err := b.GetUsers()
if err == nil {
t.Fatal("Expected error not to be nil")
}
}

func TestDeleteUser(t *testing.T) {
b := huego.New(hostname, username)
b := New(hostname, username)
err := b.DeleteUser("ffffffffe0341b1b376a2389376a2389")
if err != nil {
t.Fatal(err)
Expand All @@ -111,9 +133,17 @@ func TestDeleteUser(t *testing.T) {
}

func TestGetFullState(t *testing.T) {
b := huego.New(hostname, username)
b := New(hostname, username)
_, err := b.GetFullState()
if err != nil {
t.Fatal(err)
}
}

func TestGetFullStateError(t *testing.T) {
b := New(badHostname, username)
_, err := b.GetFullState()
if err == nil {
t.Fatal("Expected error not to be nil")
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/amimof/huego

go 1.12
go 1.13

require (
github.com/jarcoal/httpmock v1.0.4
Expand Down
Loading

0 comments on commit b4a8f71

Please sign in to comment.