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 58e1c6bbe4e9c77011d4d6ec1a016ea187666acd
parent 2c02aeb7f3b1b2739b686e225150905c01773e68
Author: Jaron Swab <jrswab@gmail.com>
Date:   Sat,  7 Mar 2020 22:57:10 -0500

Moved tests to new directory

For testing the method calls as an import.

Diffstat:
Atests/veterejo_test.go | 76++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mveterejo.go | 6+++---
Dveterejo_test.go | 74--------------------------------------------------------------------------
3 files changed, 79 insertions(+), 77 deletions(-)

diff --git a/tests/veterejo_test.go b/tests/veterejo_test.go @@ -0,0 +1,76 @@ +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) + } + 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 *v.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) + } + }) + } +} diff --git a/veterejo.go b/veterejo.go @@ -7,7 +7,7 @@ import ( ) // weatherData holds the information passed back from the OpenWeather API -type weatherData struct { +type WeatherData struct { Coord struct { Lon float64 `json:"lon"` Lat float64 `json:"lat"` @@ -49,7 +49,7 @@ type weatherData struct { Cod int `json:"cod"` } -func (w *weatherData) GetData(cityID, units, apiID string) error { +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 { @@ -62,6 +62,6 @@ func (w *weatherData) GetData(cityID, units, apiID string) error { } // GetTemp takes the user cityID and API Key to pull the temperature from the OpenWeather API. -func (w *weatherData) GetTemp() (float64, error) { +func (w *WeatherData) GetTemp() (float64, error) { return w.Main.Temp, nil } diff --git a/veterejo_test.go b/veterejo_test.go @@ -1,74 +0,0 @@ -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) - } - }) - } -}