-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobservation_test.go
61 lines (55 loc) · 1.9 KB
/
observation_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package twweather
import (
"encoding/xml"
"testing"
"github.com/MinecraftXwinP/twweather/testutil"
"github.com/stretchr/testify/assert"
)
var (
sampleXML []byte
locationXML []byte
)
func TestMain(m *testing.M) {
sampleXML = testutil.Load("sample.xml")
locationXML = testutil.Load("location.xml")
m.Run()
}
func TestParseObservation(t *testing.T) {
observation := new(Observation)
xml.Unmarshal(locationXML, observation)
assert.Equal(t, float64(227), observation.WeatherElements["ELEV"])
assert.Equal(t, float64(56), observation.WeatherElements["WDIR"])
assert.Equal(t, float64(1.9), observation.WeatherElements["WDSD"])
assert.Equal(t, float64(26.6), observation.WeatherElements["TEMP"])
assert.Equal(t, float64(0.79), observation.WeatherElements["HUMD"])
assert.Equal(t, float64(989.1), observation.WeatherElements["PRES"])
assert.Equal(t, float64(-99), observation.WeatherElements["SUN"])
assert.Equal(t, float64(0.0), observation.WeatherElements["H_24R"])
assert.Equal(t, float64(-99), observation.WeatherElements["H_FX"])
assert.Equal(t, float64(-99), observation.WeatherElements["H_XD"])
assert.Equal(t, "新竹縣", observation.CityName)
assert.Equal(t, 10, observation.CitySN)
assert.Equal(t, "橫山鄉", observation.TownName)
assert.Equal(t, 78, observation.TownSN)
}
func TestObservationQueries(t *testing.T) {
ob := new(Observation)
xml.Unmarshal(locationXML, ob)
temp, err := ob.GetTemperature(true)
assert.NoError(t, err)
assert.Equal(t, float64(26.6), temp)
temp, err = ob.GetTemperature(false)
assert.NoError(t, err)
assert.Equal(t, float64(79.88), temp)
_, err = ob.GetSunHours()
assert.Error(t, err)
rainfail, err := ob.GetDailyRainfall()
assert.NoError(t, err)
assert.Equal(t, float64(0.0), rainfail)
humd, err := ob.GetHumidity()
assert.NoError(t, err)
assert.Equal(t, 79, humd)
pres, err := ob.GetPressure()
assert.NoError(t, err)
assert.Equal(t, float64(989.1), pres)
}