More testing and Dewpoint added to curweather.go
This commit is contained in:
parent
2614e0a0ac
commit
8a5517ddd9
|
@ -30,9 +30,9 @@ type CurrentWeather struct {
|
||||||
// all values are represented as pointer type returning nil if the data point in question
|
// all values are represented as pointer type returning nil if the data point in question
|
||||||
// is not returned for the requested Station.
|
// is not returned for the requested Station.
|
||||||
type APICurrentWeatherData struct {
|
type APICurrentWeatherData struct {
|
||||||
|
// Dewpoint represents the dewpoint in °C
|
||||||
|
Dewpoint *APIValue `json:"dewpoint,omitempty"`
|
||||||
/*
|
/*
|
||||||
// Dewpoint represents the dewpoint in °C
|
|
||||||
Dewpoint *APIValue `json:"dewpoint,omitempty"`
|
|
||||||
// DewPointMean represents the mean dewpoint in °C
|
// DewPointMean represents the mean dewpoint in °C
|
||||||
DewpointMean *APIValue `json:"dewpointMean,omitempty"`
|
DewpointMean *APIValue `json:"dewpointMean,omitempty"`
|
||||||
// GlobalRadiation10m represents the sum of global radiation over the last
|
// GlobalRadiation10m represents the sum of global radiation over the last
|
||||||
|
@ -119,8 +119,7 @@ func (c *Client) CurrentWeatherByLocation(lo string) (CurrentWeather, error) {
|
||||||
|
|
||||||
// Temperature returns the temperature data point as Temperature.
|
// Temperature returns the temperature data point as Temperature.
|
||||||
// If the data point is not available in the CurrentWeather it will return
|
// If the data point is not available in the CurrentWeather it will return
|
||||||
// Temperature in which the "not available" field will be
|
// Temperature in which the "not available" field will be true.
|
||||||
// true.
|
|
||||||
func (cw CurrentWeather) Temperature() Temperature {
|
func (cw CurrentWeather) Temperature() Temperature {
|
||||||
if cw.Data.Temperature == nil {
|
if cw.Data.Temperature == nil {
|
||||||
return Temperature{na: true}
|
return Temperature{na: true}
|
||||||
|
@ -136,3 +135,22 @@ func (cw CurrentWeather) Temperature() Temperature {
|
||||||
}
|
}
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Dewpoint returns the dewpoint data point as Temperature.
|
||||||
|
// If the data point is not available in the CurrentWeather it will return
|
||||||
|
// Temperature in which the "not available" field will be true.
|
||||||
|
func (cw CurrentWeather) Dewpoint() Temperature {
|
||||||
|
if cw.Data.Dewpoint == nil {
|
||||||
|
return Temperature{na: true}
|
||||||
|
}
|
||||||
|
v := Temperature{
|
||||||
|
dt: cw.Data.Dewpoint.DateTime,
|
||||||
|
n: FieldDewpoint,
|
||||||
|
s: SourceUnknown,
|
||||||
|
v: cw.Data.Dewpoint.Value,
|
||||||
|
}
|
||||||
|
if cw.Data.Dewpoint.Source != nil {
|
||||||
|
v.s = StringToSource(*cw.Data.Dewpoint.Source)
|
||||||
|
}
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
|
@ -101,6 +101,10 @@ func TestClient_CurrentWeatherByLocation_Fail(t *testing.T) {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("CurrentWeatherByCoordinates was supposed to fail, but didn't")
|
t.Errorf("CurrentWeatherByCoordinates was supposed to fail, but didn't")
|
||||||
}
|
}
|
||||||
|
_, err = c.CurrentWeatherByLocation("")
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("CurrentWeatherByCoordinates was supposed to fail, but didn't")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestClient_CurrentWeatherByLocation_Temperature(t *testing.T) {
|
func TestClient_CurrentWeatherByLocation_Temperature(t *testing.T) {
|
||||||
|
@ -158,3 +162,59 @@ func TestClient_CurrentWeatherByLocation_Temperature(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestClient_CurrentWeatherByLocation_Dewpoint(t *testing.T) {
|
||||||
|
tt := []struct {
|
||||||
|
// Location name
|
||||||
|
loc string
|
||||||
|
// Observation dewpoint
|
||||||
|
t *Temperature
|
||||||
|
}{
|
||||||
|
{"Ehrenfeld, Germany", &Temperature{
|
||||||
|
dt: time.Date(2023, 5, 23, 7, 0, 0, 0, time.Local),
|
||||||
|
s: SourceObservation,
|
||||||
|
v: 11.5,
|
||||||
|
}},
|
||||||
|
{"Berlin, Germany", &Temperature{
|
||||||
|
dt: time.Date(2023, 5, 23, 7, 0, 0, 0, time.Local),
|
||||||
|
s: SourceAnalysis,
|
||||||
|
v: 11.0,
|
||||||
|
}},
|
||||||
|
}
|
||||||
|
c := New(withMockAPI())
|
||||||
|
if c == nil {
|
||||||
|
t.Errorf("failed to create new Client, got nil")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for _, tc := range tt {
|
||||||
|
t.Run(tc.loc, func(t *testing.T) {
|
||||||
|
o, err := c.CurrentWeatherByLocation(tc.loc)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("CurrentWeatherByLocation failed: %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if tc.t != nil && tc.t.String() != o.Dewpoint().String() {
|
||||||
|
t.Errorf("CurrentWeatherByLocation failed, expected dewpoint "+
|
||||||
|
"string: %s, got: %s", tc.t.String(), o.Dewpoint())
|
||||||
|
}
|
||||||
|
if tc.t != nil && tc.t.Value() != o.Dewpoint().Value() {
|
||||||
|
t.Errorf("CurrentWeatherByLocation failed, expected dewpoint "+
|
||||||
|
"float: %f, got: %f", tc.t.Value(), o.Dewpoint().Value())
|
||||||
|
}
|
||||||
|
if o.Dewpoint().Source() != tc.t.s {
|
||||||
|
t.Errorf("CurrentWeatherByLocation failed, expected source: %s, but got: %s",
|
||||||
|
tc.t.s, o.Dewpoint().Source())
|
||||||
|
}
|
||||||
|
if tc.t == nil {
|
||||||
|
if o.Dewpoint().IsAvailable() {
|
||||||
|
t.Errorf("CurrentWeatherByLocation failed, expected dewpoint "+
|
||||||
|
"to have no data, but got: %s", o.Dewpoint())
|
||||||
|
}
|
||||||
|
if !math.IsNaN(o.Dewpoint().Value()) {
|
||||||
|
t.Errorf("CurrentWeatherByLocation failed, expected dewpoint "+
|
||||||
|
"to return NaN, but got: %s", o.Dewpoint().String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
56
source_test.go
Normal file
56
source_test.go
Normal file
|
@ -0,0 +1,56 @@
|
||||||
|
// SPDX-FileCopyrightText: 2023 Winni Neessen <wn@neessen.dev>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package meteologix
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSource_String(t *testing.T) {
|
||||||
|
tt := []struct {
|
||||||
|
// Original source
|
||||||
|
os Source
|
||||||
|
// Expected string
|
||||||
|
es string
|
||||||
|
}{
|
||||||
|
{SourceObservation, "Observation"},
|
||||||
|
{SourceAnalysis, "Analysis"},
|
||||||
|
{SourceForecast, "Forecast"},
|
||||||
|
{SourceMixed, "Mixed"},
|
||||||
|
{SourceUnknown, "Unknown"},
|
||||||
|
{999, "Unknown"},
|
||||||
|
}
|
||||||
|
for _, tc := range tt {
|
||||||
|
t.Run(tc.os.String(), func(t *testing.T) {
|
||||||
|
if tc.os.String() != tc.es {
|
||||||
|
t.Errorf("String for Source failed, expected: %s, got: %s",
|
||||||
|
tc.es, tc.os.String())
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStringToSource(t *testing.T) {
|
||||||
|
tt := []struct {
|
||||||
|
// Original string
|
||||||
|
os string
|
||||||
|
// Expected source
|
||||||
|
es Source
|
||||||
|
}{
|
||||||
|
{"Observation", SourceObservation},
|
||||||
|
{"Analysis", SourceAnalysis},
|
||||||
|
{"Forecast", SourceForecast},
|
||||||
|
{"Mixed", SourceMixed},
|
||||||
|
{"Unknown", SourceUnknown},
|
||||||
|
}
|
||||||
|
for _, tc := range tt {
|
||||||
|
t.Run(tc.es.String(), func(t *testing.T) {
|
||||||
|
if r := StringToSource(tc.os); r != tc.es {
|
||||||
|
t.Errorf("StringToSource failed, expected: %s, got: %s",
|
||||||
|
tc.es.String(), r.String())
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue