Skip to content

Commit

Permalink
tests: add tests for rest services and states (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
xx4h authored Oct 12, 2024
1 parent 6854fec commit 40ee3b0
Show file tree
Hide file tree
Showing 5 changed files with 5,185 additions and 0 deletions.
38 changes: 38 additions & 0 deletions pkg/rest/rest_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2024 Fabian `xx4h` Sylvester
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package rest

import (
"fmt"
"net/http"
"net/http/httptest"
"os"
"testing"
)

func mockServerGetDataFromFile(t testing.TB, testdatafile string) *httptest.Server {
t.Helper()
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
data, err := os.ReadFile(fmt.Sprintf("testdata/%s", testdatafile))
if err != nil {
t.Errorf("Error reading file: %v", err)
}
if _, err := w.Write(data); err != nil {
t.Errorf("Error writing data: %v", err)
}
}))
return mockServer
}
46 changes: 46 additions & 0 deletions pkg/rest/services_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2024 Fabian `xx4h` Sylvester
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package rest

import (
"reflect"
"testing"
)

const (
serviceCount = 47
)

func Test_GetServices(t *testing.T) {
ms := mockServerGetDataFromFile(t, "services.json")
h := &Hass{
APIURL: ms.URL,
Token: "test_token",
}
defer ms.Close()
s, err := h.GetServices()
if err != nil {
t.Errorf("Error getting services: %v", err)
}
st := reflect.TypeOf(s)
wt := reflect.TypeOf([]HassService{})
if st != wt {
t.Errorf("got %s, want %s", st, wt)
}
cs := len(s)
if cs != serviceCount {
t.Errorf("got %d, want %d", cs, serviceCount)
}
}
46 changes: 46 additions & 0 deletions pkg/rest/states_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2024 Fabian `xx4h` Sylvester
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package rest

import (
"reflect"
"testing"
)

const (
statesCount = 10
)

func Test_GetStates(t *testing.T) {
ms := mockServerGetDataFromFile(t, "states.json")
h := &Hass{
APIURL: ms.URL,
Token: "test_token",
}
defer ms.Close()
s, err := h.GetStates()
if err != nil {
t.Errorf("Error getting states: %v", err)
}
st := reflect.TypeOf(s)
wt := reflect.TypeOf([]HassState{})
if st != wt {
t.Errorf("got %s, want %s", st, wt)
}
cs := len(s)
if cs != statesCount {
t.Errorf("got %d, want %d", cs, statesCount)
}
}
Loading

0 comments on commit 40ee3b0

Please sign in to comment.