veterejo

Unnamed repository; edit this file 'description' to name the repository.
git clone git://git.swab.dev/veterejo.git
Log | Files | Refs | README | LICENSE

commit 4d7c25fadcde65e600b840a8a7fdbe0765a9c88b
parent 58e1c6bbe4e9c77011d4d6ec1a016ea187666acd
Author: Jaron Swab <jrswab@gmail.com>
Date:   Sun,  8 Mar 2020 11:53:09 -0400

Added helpers.go to lessen the setup in the test

Diffstat:
Atests/helpers.go | 129+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mtests/veterejo_test.go | 87++++++++++++++++++++++++++++++++++---------------------------------------------
Mveterejo.go | 29++++++++++++++++++++---------
3 files changed, 186 insertions(+), 59 deletions(-)

diff --git a/tests/helpers.go b/tests/helpers.go @@ -0,0 +1,129 @@ +package veterejo + +import ( + "encoding/json" + "fmt" + "log" + "net/http" + "net/http/httptest" + + v "github.com/jrswab/veterejo" +) + +// RunTestServer creates a temporary webserver to serve the test data defined below. +func RunTestServer(unit string) *v.WeatherData { + w := new(v.WeatherData) + imperialJSON := getImperialTestData() + metricJSON := getMetricTestData() + + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if unit == "imperial" { + fmt.Fprintln(w, imperialJSON) + } + if unit == "metric" { + fmt.Fprintln(w, metricJSON) + } + })) + defer ts.Close() + + res, err := http.Get(ts.URL) + if err != nil { + log.Fatal(err) + } + defer res.Body.Close() + + json.NewDecoder(res.Body).Decode(&w) + return w +} + +func getImperialTestData() string { + return `{ + "coord": { + "lon": -79.96, + "lat": 40.56 + }, + "weather": [ + { + "id": 800, + "main": "Clear", + "description": "clear sky", + "icon": "01n" + } + ], + "base": "stations", + "main": { + "temp": 30.9, + "feels_like": 24.13, + "temp_min": 26.01, + "temp_max": 36, + "pressure": 1031, + "humidity": 68 + }, + "visibility": 16093, + "wind": { + "speed": 3.42, + "deg": 239 + }, + "clouds": { + "all": 1 + }, + "dt": 1583637756, + "sys": { + "type": 1, + "id": 3511, + "country": "US", + "sunrise": 1583581421, + "sunset": 1583623083 + }, + "timezone": -18000, + "id": 5178165, + "name": "Allison Park", + "cod": 200 + }` +} + +func getMetricTestData() string { + return `{ + "coord": { + "lon": -79.96, + "lat": 40.56 + }, + "weather": [ + { + "id": 800, + "main": "Clear", + "description": "clear sky", + "icon": "01n" + } + ], + "base": "stations", + "main": { + "temp": -0.73, + "feels_like": -4.73, + "temp_min": -3.33, + "temp_max": 2, + "pressure": 1031, + "humidity": 55 + }, + "visibility": 16093, + "wind": { + "speed": 1.5, + "deg": 200 + }, + "clouds": { + "all": 1 + }, + "dt": 1583638861, + "sys": { + "type": 1, + "id": 3510, + "country": "US", + "sunrise": 1583581421, + "sunset": 1583623083 + }, + "timezone": -18000, + "id": 5178165, + "name": "Allison Park", + "cod": 200 + }` +} diff --git a/tests/veterejo_test.go b/tests/veterejo_test.go @@ -1,75 +1,62 @@ package veterejo import ( - "encoding/json" - "fmt" - "log" - "net/http" - "net/http/httptest" "testing" v "github.com/jrswab/veterejo" ) -func runTestServer(unit string) *v.WeatherData { - w := new(v.WeatherData) - imperialJSON := `{"coord":{"lon":-79.96,"lat":40.56},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"base":"stations","main":{"temp":30.9,"feels_like":24.13,"temp_min":26.01,"temp_max":36,"pressure":1031,"humidity":68},"visibility":16093,"wind":{"speed":3.42,"deg":239},"clouds":{"all":1},"dt":1583637756,"sys":{"type":1,"id":3511,"country":"US","sunrise":1583581421,"sunset":1583623083},"timezone":-18000,"id":5178165,"name":"Allison Park","cod":200}` - metricJSON := `{"coord":{"lon":-79.96,"lat":40.56},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"base":"stations","main":{"temp":-0.73,"feels_like":-4.73,"temp_min":-3.33,"temp_max":2,"pressure":1031,"humidity":55},"visibility":16093,"wind":{"speed":1.5,"deg":200},"clouds":{"all":1},"dt":1583638861,"sys":{"type":1,"id":3510,"country":"US","sunrise":1583581421,"sunset":1583623083},"timezone":-18000,"id":5178165,"name":"Allison Park","cod":200}` - - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if unit == "imperial" { - fmt.Fprintln(w, imperialJSON) - } - if unit == "metric" { - fmt.Fprintln(w, metricJSON) - } - })) - defer ts.Close() - - res, err := http.Get(ts.URL) - if err != nil { - log.Fatal(err) +func TestGetMaxTemp(t *testing.T) { + tests := []struct { + name string + data *v.WeatherData + want int + }{ + // TODO: Add test cases. + { + name: "Temperature returns integer on a correct call to the api (imperial)", + data: RunTestServer("imperial"), + want: 36, + }, + { + name: "Temperature returns integer on a correct call to the api (metric)", + data: RunTestServer("metric"), + want: 2, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := tt.data.GetMaxTemp() + if got != tt.want { + t.Errorf("GetMaxTemp() = %v, want %v", got, tt.want) + } + }) } - defer res.Body.Close() - //data, err := ioutil.ReadAll(res.Body) - //if err != nil { - // log.Fatal(err) - //} - - json.NewDecoder(res.Body).Decode(&w) - return w } -func TestGetTemp(t *testing.T) { +func TestGetMinTemp(t *testing.T) { tests := []struct { - name string - data *v.WeatherData - want float64 - wantErr bool + name string + data *v.WeatherData + want float32 }{ // TODO: Add test cases. { - name: "Temperature returns float on a correct call to the api (imperial)", - data: runTestServer("imperial"), - want: 30.9, - wantErr: false, + name: "Temperature returns float on a correct call to the api (imperial)", + data: RunTestServer("imperial"), + want: 26.01, }, { - name: "Temperature returns float on a correct call to the api (metric)", - data: runTestServer("metric"), - want: -0.73, - wantErr: false, + name: "Temperature returns float on a correct call to the api (metric)", + data: RunTestServer("metric"), + want: -3.33, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got, err := tt.data.GetTemp() - if (err != nil) != tt.wantErr { - t.Errorf("GetTemp() error = %v, wantErr %v", err, tt.wantErr) - return - } + got := tt.data.GetMinTemp() if got != tt.want { - t.Errorf("GetTemp() = %v, want %v", got, tt.want) + t.Errorf("GetMinTemp() = %v, want %v", got, tt.want) } }) } diff --git a/veterejo.go b/veterejo.go @@ -9,8 +9,8 @@ import ( // weatherData holds the information passed back from the OpenWeather API type WeatherData struct { Coord struct { - Lon float64 `json:"lon"` - Lat float64 `json:"lat"` + Lon float32 `json:"lon"` + Lat float32 `json:"lat"` } `json:"coord"` Weather []struct { ID int `json:"id"` @@ -20,16 +20,16 @@ type WeatherData struct { } `json:"weather"` Base string `json:"base"` Main struct { - Temp float64 `json:"temp"` - FeelsLike float64 `json:"feels_like"` - TempMin float64 `json:"temp_min"` + Temp float32 `json:"temp"` + FeelsLike float32 `json:"feels_like"` + TempMin float32 `json:"temp_min"` TempMax int `json:"temp_max"` Pressure int `json:"pressure"` Humidity int `json:"humidity"` } `json:"main"` Visibility int `json:"visibility"` Wind struct { - Speed float64 `json:"speed"` + Speed float32 `json:"speed"` Deg int `json:"deg"` } `json:"wind"` Clouds struct { @@ -49,6 +49,7 @@ type WeatherData struct { Cod int `json:"cod"` } +// GetData calls the OpenWeatherMap API and adds the date to the struct. func (w *WeatherData) GetData(cityID, units, apiID string) error { url := fmt.Sprintf("https://api.openweathermap.org/data/2.5/weather/?id=%s&units=%s&appid=%s", cityID, units, apiID) resp, err := http.Get(url) @@ -61,7 +62,17 @@ func (w *WeatherData) GetData(cityID, units, apiID string) error { return nil } -// GetTemp takes the user cityID and API Key to pull the temperature from the OpenWeather API. -func (w *WeatherData) GetTemp() (float64, error) { - return w.Main.Temp, nil +// GetTemp returns the current temperature that GetData gets in the call. +func (w *WeatherData) GetTemp() float32 { + return w.Main.Temp +} + +// GetMaxTemp returns the current high temperature that GetData gets in the call. +func (w *WeatherData) GetMaxTemp() int { + return w.Main.TempMax +} + +// GetMinTemp returns the current high temperature that GetData gets in the call. +func (w *WeatherData) GetMinTemp() float32 { + return w.Main.TempMin }