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 2c02aeb7f3b1b2739b686e225150905c01773e68
Author: Jaron Swab <jrswab@gmail.com>
Date:   Sat,  7 Mar 2020 22:47:56 -0500

Created GetDatad and GetTemp functions

Tests added for GetTemp.

Diffstat:
Aveterejo.go | 67+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aveterejo_test.go | 74++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 141 insertions(+), 0 deletions(-)

diff --git a/veterejo.go b/veterejo.go @@ -0,0 +1,67 @@ +package veterejo + +import ( + "encoding/json" + "fmt" + "net/http" +) + +// weatherData holds the information passed back from the OpenWeather API +type weatherData struct { + Coord struct { + Lon float64 `json:"lon"` + Lat float64 `json:"lat"` + } `json:"coord"` + Weather []struct { + ID int `json:"id"` + Main string `json:"main"` + Description string `json:"description"` + Icon string `json:"icon"` + } `json:"weather"` + Base string `json:"base"` + Main struct { + Temp float64 `json:"temp"` + FeelsLike float64 `json:"feels_like"` + TempMin float64 `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"` + Deg int `json:"deg"` + } `json:"wind"` + Clouds struct { + All int `json:"all"` + } `json:"clouds"` + Dt int `json:"dt"` + Sys struct { + Type int `json:"type"` + ID int `json:"id"` + Country string `json:"country"` + Sunrise int `json:"sunrise"` + Sunset int `json:"sunset"` + } `json:"sys"` + Timezone int `json:"timezone"` + ID int `json:"id"` + Name string `json:"name"` + Cod int `json:"cod"` +} + +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) + if err != nil { + return err + } + + defer resp.Body.Close() + json.NewDecoder(resp.Body).Decode(&w) + 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 +} diff --git a/veterejo_test.go b/veterejo_test.go @@ -0,0 +1,74 @@ +package veterejo + +import ( + "encoding/json" + "fmt" + "log" + "net/http" + "net/http/httptest" + "testing" +) + +func runTestServer(unit string) *weatherData { + w := new(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) + } + 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) { + tests := []struct { + name string + data *weatherData + want float64 + wantErr bool + }{ + // 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 (metric)", + data: runTestServer("metric"), + want: -0.73, + wantErr: false, + }, + } + 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 + } + if got != tt.want { + t.Errorf("GetTemp() = %v, want %v", got, tt.want) + } + }) + } +}