Skip to content
Snippets Groups Projects
Commit 49c6e9ae authored by Bjørnar Larsen's avatar Bjørnar Larsen
Browse files

Merge branch 'dev' of git.gvk.idi.ntnu.no:jobnb/cloudproject into dev

parents 0e262086 e532d294
No related branches found
No related tags found
1 merge request!4Finished app
......@@ -53,10 +53,21 @@ func WeatherHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
var result = FinalReport{}
lat := r.FormValue("lat")
long := r.FormValue("long")
result, err := GetSingleForecast(58.8569687273594, 5.85660630854479)
latitude, err := GetFloat(lat)
longitude, err2 := GetFloat(long)
// If input is not valid throw error
if err != nil || err2 != nil || longitude == 0 || latitude == 0 {
http.Error(w, "Latitude/Longitude must be valid floats", http.StatusBadRequest)
return
}
result, err = GetSingleForecast(latitude, longitude)
if err != nil {
fmt.Println("Handler request err: ", err)
http.Error(w, "Could not get forecast", http.StatusInternalServerError)
}
w.Header().Set("content-type", "application/json")
......@@ -71,6 +82,15 @@ func WeatherHandler(w http.ResponseWriter, r *http.Request) {
}
}
// Checks and returns valid float64
func GetFloat(query string) (float64, error) {
result, err := strconv.ParseFloat(query, 64)
if err == nil {
return result, nil
}
return 0, err
}
// ParseWeatherData Parses the response from Metrologisk institutt,
func ParseWeatherData(data []ForecastData) (FinalReport, error) {
var result = FinalReport{}
......@@ -81,7 +101,7 @@ func ParseWeatherData(data []ForecastData) (FinalReport, error) {
// Sort thorugh the response
for i := 0; i < len(data); i++ {
// Format time.Time values for easier to read output
date := data[i].From.Format("02/01/06")
date := data[i].From.Format("01/02/06")
hour := data[i].From.Format("15:04")
// Condition for an object containing temperature data, other field not populated
......
......@@ -4,10 +4,30 @@ import (
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"strconv"
"testing"
)
func TestWeatherHandler(t *testing.T) {
// make new request to status handler
req, err := http.NewRequest("GET", "/weather?lat=some&long=bogus", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
// check if response is 200
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
handler := http.HandlerFunc(WeatherHandler)
handler.ServeHTTP(rr, req)
}
func TestGetSingleForecast(t *testing.T) {
latitude := 58.8569687273594
longitude := 5.85660630854479
......@@ -27,6 +47,7 @@ func TestGetSingleForecast(t *testing.T) {
if err !=nil && err.Error() != expected.Error() {
t.Error(err)
}
}
func TestParseWeatherData(t *testing.T) {
......@@ -89,7 +110,7 @@ func TestParseWeatherData(t *testing.T) {
if len(test.ForecastMap) == 0 {
t.Error("Error in parsing data")
}
if test.ForecastMap["21/11/19: 08:00"] != "3 Celsius" && test.RainMap["21/11/19: 09:00 to 12:00"] != "4.5 mm"{
if test.ForecastMap["11/21/19: 08:00"] != "3 Celsius" && test.RainMap["11/21/19: 09:00 to 12:00"] != "4.5 mm"{
t.Error("Data not present in FinalReport")
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment