-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: add tests for rest services and states (#34)
- Loading branch information
Showing
5 changed files
with
5,185 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.