Starts adding tests and github workflow
This commit is contained in:
@@ -23,7 +23,19 @@ type actionTask struct {
|
||||
DueHour int `json:"due_hour"`
|
||||
}
|
||||
|
||||
func HandleHaMessage(w http.ResponseWriter, r *http.Request) {
|
||||
type HaMessageHandler struct {
|
||||
PooRecorderHandler func(message haMessage)
|
||||
LocationRecorderHandler func(message haMessage)
|
||||
TicktickHandler func(message haMessage)
|
||||
}
|
||||
|
||||
var DefaultHandler = HaMessageHandler{
|
||||
PooRecorderHandler: handlePooRecorderMsg,
|
||||
LocationRecorderHandler: handleLocationRecorderMsg,
|
||||
TicktickHandler: handleTicktickMsg,
|
||||
}
|
||||
|
||||
func (handler HaMessageHandler) HandleHaMessage(w http.ResponseWriter, r *http.Request) {
|
||||
var message haMessage
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
decoder.DisallowUnknownFields()
|
||||
@@ -36,13 +48,12 @@ func HandleHaMessage(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
switch message.Target {
|
||||
case "poo_recorder":
|
||||
handlePooRecorderMsg(message)
|
||||
handler.PooRecorderHandler(message)
|
||||
case "location_recorder":
|
||||
handleLocationRecorderMsg(message)
|
||||
handler.LocationRecorderHandler(message)
|
||||
case "ticktick":
|
||||
handleTicktickMsg(message)
|
||||
handler.TicktickHandler(message)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func handlePooRecorderMsg(message haMessage) {
|
||||
@@ -50,7 +61,6 @@ func handlePooRecorderMsg(message haMessage) {
|
||||
case "get_latest":
|
||||
handleGetLatestPoo()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func handleLocationRecorderMsg(message haMessage) {
|
||||
|
||||
65
src/components/homeassistant/homeassistant_test.go
Normal file
65
src/components/homeassistant/homeassistant_test.go
Normal file
@@ -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