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 9e6b3df6c78a013fd4a0deac2b8d6dc731ef3204
parent c3a61e9a2510f095442c979b2bc4f00f4f6eb0d6
Author: Jaron Swab <jrswab@gmail.com>
Date:   Sun,  8 Mar 2020 13:49:50 -0400

Added URL parsing method

Also added tests for GetData and the new url parsing method

Diffstat:
Mtests/veterejo_test.go | 86+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mveterejo.go | 24+++++++++++++++++++++---
2 files changed, 107 insertions(+), 3 deletions(-)

diff --git a/tests/veterejo_test.go b/tests/veterejo_test.go @@ -1,11 +1,97 @@ package veterejo import ( + "fmt" + "net/http" + "net/http/httptest" "testing" v "github.com/jrswab/veterejo" ) +func TestMakeURL(t *testing.T) { + type args struct { + cityID string + units string + apiID string + } + tests := []struct { + name string + args args + want string + }{ + // TODO: Add test cases. + { + name: "Test with all args", + args: args{ + cityID: "0000", + units: "metric", + apiID: "0x0p0q", + }, + want: "https://api.openweathermap.org/data/2.5/weather/?id=0000&units=metric&appid=0x0p0q", + }, + { + name: "Test without CityID", + args: args{ + cityID: "", + units: "metric", + apiID: "0x0p0q", + }, + want: "https://api.openweathermap.org/data/2.5/weather/?units=metric&appid=0x0p0q", + }, + { + name: "Test without unitis", + args: args{ + cityID: "0000", + units: "", + apiID: "0x0p0q", + }, + want: "https://api.openweathermap.org/data/2.5/weather/?id=0000&appid=0x0p0q", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := v.MakeURL(tt.args.cityID, tt.args.units, tt.args.apiID); got != tt.want { + t.Errorf("MakeURL() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestGetData(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintln(w, `{}`) + })) + defer ts.Close() + fakeURL := "" + + tests := []struct { + name string + url string + data *v.WeatherData + wantErr bool + }{ + // TODO: Add test cases. + { + name: "Test with valid url", + url: ts.URL, + wantErr: false, + }, + { + name: "Test with invalid url", + url: fakeURL, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := tt.data.GetData(tt.url); (err != nil) != tt.wantErr { + t.Errorf("WeatherData.GetData() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + func TestGetMaxTemp(t *testing.T) { tests := []struct { name string diff --git a/veterejo.go b/veterejo.go @@ -49,16 +49,29 @@ type WeatherData struct { Cod int `json:"cod"` } +// MakeURL formats the URL for the Open Weather Map API call +func MakeURL(cityID, units, apiID string) string { + if cityID == "" { + return fmt.Sprintf("https://api.openweathermap.org/data/2.5/weather/?units=%s&appid=%s", units, apiID) + } + if units == "" { + return fmt.Sprintf("https://api.openweathermap.org/data/2.5/weather/?id=%s&appid=%s", cityID, apiID) + } + return fmt.Sprintf("https://api.openweathermap.org/data/2.5/weather/?id=%s&units=%s&appid=%s", cityID, units, apiID) +} + // 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) +func (w *WeatherData) GetData(url string) error { resp, err := http.Get(url) if err != nil { return err } defer resp.Body.Close() - json.NewDecoder(resp.Body).Decode(&w) + err = json.NewDecoder(resp.Body).Decode(&w) + if err != nil { + return err + } return nil } @@ -96,3 +109,8 @@ func (w *WeatherData) GetHumidity() int { func (w *WeatherData) GetCoords() string { return fmt.Sprintf("%.2f, %.2f", w.Coord.Lat, w.Coord.Lon) } + +// GetVisibility returns a visiblity meter integer +func (w *WeatherData) GetVisibility() int { + return w.Visibility +}