Ryan D McGuire 9077ae354a
All checks were successful
Build and Publish / release (push) Has been skipped
Build and Publish / check-chart (push) Successful in 10s
Build and Publish / helm-release (push) Has been skipped
implement grpc gateway for http api
2025-03-24 16:47:54 -04:00

118 lines
3.7 KiB
Go

package ambientgrpc
import (
"reflect"
"testing"
"time"
"google.golang.org/protobuf/types/known/timestamppb"
"k8s.io/utils/ptr"
pb "gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/api/v1alpha1/weather"
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/ambient/config"
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/weather"
)
var now = time.Now()
var mockUpdate = &weather.WeatherUpdate{
DateUTC: &now,
StationConfig: &config.WeatherStation{
Name: "50W",
Equipment: "WS-5000",
},
StationID: ptr.To("50W"),
StationType: ptr.To("WS-5000"),
TempOutdoorF: ptr.To(97.6),
TempIndoorF: ptr.To(77.6),
HumidityOudoor: ptr.To(50),
HumidityIndoor: ptr.To(50),
WindSpeedMPH: ptr.To(20.5),
WindGustMPH: ptr.To(30.5),
MaxDailyGust: ptr.To(40.0),
WindDir: ptr.To(180),
WindDirAvg10m: ptr.To(180),
UV: nil,
SolarRadiation: ptr.To(9.999),
HourlyRainIn: ptr.To(9.999),
EventRainIn: ptr.To(9.999),
DailyRainIn: ptr.To(9.999),
WeeklyRainIn: ptr.To(9.999),
MonthlyRainIn: ptr.To(9.999),
YearlyRainIn: ptr.To(9.999),
TotalRainIn: ptr.To(9.999),
Batteries: []weather.BatteryStatus{
{Component: "battery1", Status: ptr.To(1)},
{Component: "battery2", Status: ptr.To(1)},
},
BaromRelativeIn: ptr.To(9.999),
BaromAbsoluteIn: ptr.To(9.999),
DewPointF: ptr.To(9.999),
WindChillF: ptr.To(9.999),
TempHumiditySensors: []*weather.TempHumiditySensor{
{Name: "sensor1", TempF: ptr.To(99.999), Humidity: nil},
},
}
func TestUpdateToPbUpdate(t *testing.T) {
type args struct {
u *weather.WeatherUpdate
}
tests := []struct {
name string
args args
want *pb.WeatherUpdate
}{
{
name: "Map Update to PB",
args: args{u: mockUpdate},
want: &pb.WeatherUpdate{
StationName: mockUpdate.StationConfig.Name,
StationType: *mockUpdate.StationType,
StationId: *mockUpdate.StationID,
TempOutdoorF: mockUpdate.TempOutdoorF,
TempIndoorF: mockUpdate.TempIndoorF,
HumidityOutdoor: ptr.To(int32(*mockUpdate.HumidityOudoor)),
HumidityIndoor: ptr.To(int32(*mockUpdate.HumidityIndoor)),
WindSpeedMph: mockUpdate.WindSpeedMPH,
WindGustMph: mockUpdate.WindGustMPH,
MaxDailyGust: mockUpdate.MaxDailyGust,
WindDir: ptr.To(int32(*mockUpdate.WindDir)),
WindDirAvg_10M: ptr.To(int32(*mockUpdate.WindDirAvg10m)),
Uv: nil,
SolarRadiation: mockUpdate.SolarRadiation,
HourlyRainIn: mockUpdate.HourlyRainIn,
EventRainIn: mockUpdate.EventRainIn,
DailyRainIn: mockUpdate.DailyRainIn,
WeeklyRainIn: mockUpdate.WeeklyRainIn,
MonthlyRainIn: mockUpdate.MonthlyRainIn,
YearlyRainIn: mockUpdate.YearlyRainIn,
TotalRainIn: mockUpdate.TotalRainIn,
Batteries: []*pb.BatteryStatus{
{Component: mockUpdate.Batteries[0].Component, Status: ptr.To(int32(*mockUpdate.Batteries[0].Status))},
{Component: mockUpdate.Batteries[1].Component, Status: ptr.To(int32(*mockUpdate.Batteries[1].Status))},
},
BaromRelativeIn: mockUpdate.BaromRelativeIn,
BaromAbsoluteIn: mockUpdate.BaromAbsoluteIn,
DewPointF: mockUpdate.DewPointF,
WindChillF: mockUpdate.WindChillF,
TempHumiditySensors: []*pb.TempHumiditySensor{
{
Name: mockUpdate.TempHumiditySensors[0].Name,
TempF: mockUpdate.TempHumiditySensors[0].TempF,
Humidity: nil,
},
},
UpdateTimestamp: timestamppb.New(now),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := UpdateToPbUpdate(tt.args.u); !reflect.DeepEqual(got, tt.want) {
t.Errorf("UpdateToPbUpdate() = %v, want %v", got, tt.want)
}
})
}
}