Initial MVP commit
All checks were successful
ci/woodpecker/push/build-image Pipeline was successful
All checks were successful
ci/woodpecker/push/build-image Pipeline was successful
This commit is contained in:
35
internal/application/units/converter.go
Normal file
35
internal/application/units/converter.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package units
|
||||
|
||||
import (
|
||||
"math"
|
||||
|
||||
"gitea.maximumdirect.net/ejr/weatherapi/internal/platform/constants"
|
||||
)
|
||||
|
||||
type Converter interface {
|
||||
TemperatureCToOutput(celsius *float64) *float64
|
||||
SpeedKmhToOutput(kmh *float64) *float64
|
||||
}
|
||||
|
||||
type USConverter struct{}
|
||||
|
||||
func (USConverter) TemperatureCToOutput(celsius *float64) *float64 {
|
||||
if celsius == nil {
|
||||
return nil
|
||||
}
|
||||
f := round(constants.CelsiusToFahrenheit(*celsius), constants.TempFahrenheitPrecision)
|
||||
return &f
|
||||
}
|
||||
|
||||
func (USConverter) SpeedKmhToOutput(kmh *float64) *float64 {
|
||||
if kmh == nil {
|
||||
return nil
|
||||
}
|
||||
mph := round(constants.KmhToMph(*kmh), constants.WindMphPrecision)
|
||||
return &mph
|
||||
}
|
||||
|
||||
func round(v float64, precision int) float64 {
|
||||
pow := math.Pow(10, float64(precision))
|
||||
return math.Round(v*pow) / pow
|
||||
}
|
||||
43
internal/application/units/converter_test.go
Normal file
43
internal/application/units/converter_test.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package units
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestUSConverterTemperatureCToOutput(t *testing.T) {
|
||||
c := USConverter{}
|
||||
|
||||
if got := c.TemperatureCToOutput(nil); got != nil {
|
||||
t.Fatalf("expected nil for nil input")
|
||||
}
|
||||
|
||||
zeroC := 0.0
|
||||
got := c.TemperatureCToOutput(&zeroC)
|
||||
if got == nil || *got != 32.0 {
|
||||
t.Fatalf("expected 32.0F, got %v", got)
|
||||
}
|
||||
|
||||
v := 20.56 // 69.008F -> 69.0 at precision 1
|
||||
got = c.TemperatureCToOutput(&v)
|
||||
if got == nil || *got != 69.0 {
|
||||
t.Fatalf("expected 69.0F, got %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUSConverterSpeedKmhToOutput(t *testing.T) {
|
||||
c := USConverter{}
|
||||
|
||||
if got := c.SpeedKmhToOutput(nil); got != nil {
|
||||
t.Fatalf("expected nil for nil input")
|
||||
}
|
||||
|
||||
v := 10.0 // 6.21371 mph -> 6.2 at precision 1
|
||||
got := c.SpeedKmhToOutput(&v)
|
||||
if got == nil || *got != 6.2 {
|
||||
t.Fatalf("expected 6.2 mph, got %v", got)
|
||||
}
|
||||
|
||||
exact := 16.09344 // exact 10 mph
|
||||
got = c.SpeedKmhToOutput(&exact)
|
||||
if got == nil || *got != 10.0 {
|
||||
t.Fatalf("expected 10.0 mph, got %v", got)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user