Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
cloudProject
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Job Bahner
cloudProject
Commits
49c6e9ae
Commit
49c6e9ae
authored
5 years ago
by
Bjørnar Larsen
Browse files
Options
Downloads
Plain Diff
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
!4
Finished app
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
weather.go
+23
-3
23 additions, 3 deletions
weather.go
weather_test.go
+22
-1
22 additions, 1 deletion
weather_test.go
with
45 additions
and
4 deletions
weather.go
+
23
−
3
View file @
49c6e9ae
...
...
@@ -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: "
,
er
r
)
http
.
Error
(
w
,
"Could not get forecast"
,
http
.
StatusInternalServerErro
r
)
}
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
(
"0
2
/0
1
/06"
)
date
:=
data
[
i
]
.
From
.
Format
(
"0
1
/0
2
/06"
)
hour
:=
data
[
i
]
.
From
.
Format
(
"15:04"
)
// Condition for an object containing temperature data, other field not populated
...
...
This diff is collapsed.
Click to expand it.
weather_test.go
+
22
−
1
View file @
49c6e9ae
...
...
@@ -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
[
"
2
1/
1
1/19: 08:00"
]
!=
"3 Celsius"
&&
test
.
RainMap
[
"
2
1/
1
1/19: 09:00 to 12:00"
]
!=
"4.5 mm"
{
if
test
.
ForecastMap
[
"
1
1/
2
1/19: 08:00"
]
!=
"3 Celsius"
&&
test
.
RainMap
[
"
1
1/
2
1/19: 09:00 to 12:00"
]
!=
"4.5 mm"
{
t
.
Error
(
"Data not present in FinalReport"
)
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment