Starts adding tests and github workflow
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
package homeassistant
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand/v2"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
type MockedHaHandler struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
func (m *MockedHaHandler) handlePooRecorderMsg(message haMessage) {
|
||||
m.Called(message)
|
||||
}
|
||||
|
||||
func (m *MockedHaHandler) handleLocationRecorderMsg(message haMessage) {
|
||||
m.Called(message)
|
||||
}
|
||||
|
||||
func (m *MockedHaHandler) handleTicktickMsg(message haMessage) {
|
||||
m.Called(message)
|
||||
}
|
||||
|
||||
func TestHandleHaMessagePooRecorder(t *testing.T) {
|
||||
requestBody := `{"target": "poo_recorder", "action": "get_latest", "content": ""}`
|
||||
req := httptest.NewRequest(http.MethodPost, "/homeassistant/publish", strings.NewReader(requestBody))
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
testObj := new(MockedHaHandler)
|
||||
mockedHandler := HaMessageHandler{
|
||||
PooRecorderHandler: testObj.handlePooRecorderMsg,
|
||||
LocationRecorderHandler: testObj.handleLocationRecorderMsg,
|
||||
TicktickHandler: testObj.handleTicktickMsg,
|
||||
}
|
||||
testObj.On("handlePooRecorderMsg", haMessage{Target: "poo_recorder", Action: "get_latest"}).Return()
|
||||
mockedHandler.HandleHaMessage(w, req)
|
||||
testObj.AssertCalled(t, "handlePooRecorderMsg", haMessage{Target: "poo_recorder", Action: "get_latest"})
|
||||
}
|
||||
|
||||
func TestHandleHaMessageLocationRecorder(t *testing.T) {
|
||||
expectedLatitude := rand.Float64() * 359
|
||||
expectedLongitude := rand.Float64() * 359
|
||||
expectedAltitude := rand.Float64() * 10000
|
||||
requestBody := fmt.Sprintf(`{"target": "location_recorder", "action": "record",
|
||||
"content": "{'Person': 'Tom', 'latitude': %f, 'longitude': %f, 'altitude': %f}"}`, expectedLatitude, expectedLongitude, expectedAltitude)
|
||||
req := httptest.NewRequest(http.MethodPost, "/homeassistant/publish", strings.NewReader(requestBody))
|
||||
w := httptest.NewRecorder()
|
||||
testobj := new(MockedHaHandler)
|
||||
mockedHandler := HaMessageHandler{
|
||||
PooRecorderHandler: testobj.handlePooRecorderMsg,
|
||||
LocationRecorderHandler: testobj.handleLocationRecorderMsg,
|
||||
TicktickHandler: testobj.handleTicktickMsg,
|
||||
}
|
||||
testobj.On("handleLocationRecorderMsg", haMessage{Target: "location_recorder", Action: "record",
|
||||
Content: fmt.Sprintf("{'Person': 'Tom', 'latitude': %f, 'longitude': %f, 'altitude': %f}", expectedLatitude, expectedLongitude, expectedAltitude)}).Return()
|
||||
mockedHandler.HandleHaMessage(w, req)
|
||||
testobj.AssertCalled(t, "handleLocationRecorderMsg", haMessage{Target: "location_recorder", Action: "record",
|
||||
Content: fmt.Sprintf("{'Person': 'Tom', 'latitude': %f, 'longitude': %f, 'altitude': %f}", expectedLatitude, expectedLongitude, expectedAltitude)})
|
||||
}
|
||||
Reference in New Issue
Block a user