Skip to content
This repository was archived by the owner on Nov 16, 2023. It is now read-only.

Commit f83e99f

Browse files
author
David Tesar
authored
Separate trips unit / integration tests (#63)
* Add go.mod deps file * ignore go.sum lockfile * Extract health route test * refactor test framework * unexport LogError LogMessage * Add testing instructions to readme * Migrate docker container build to golang 1.11.0
1 parent c0e2ad9 commit f83e99f

13 files changed

Lines changed: 395 additions & 342 deletions

apis/trips/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ launch.json
1919
.vscode/
2020
vendor/
2121
glide.lock
22+
go.sum

apis/trips/Dockerfile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
FROM golang:1.10.3 AS gobuild
1+
FROM golang:1.11.0 AS gobuild
22

33
WORKDIR /go/src/github.com/Azure-Samples/openhack-devops-team/apis/trips
44

5-
RUN curl https://glide.sh/get | sh
6-
75
COPY . .
86

9-
RUN glide install --skip-test
7+
ENV GO111MODULE=on
8+
9+
RUN go mod vendor
1010

1111
RUN CGO_ENABLED=0 GOOS=linux go build -o main .
1212

apis/trips/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,18 @@ To run the server, follow these simple steps:
1414
go run main.go
1515
```
1616

17+
## Testing
18+
19+
To run unit tests, execute:
20+
21+
```shell
22+
go test ./test
23+
```
24+
25+
To run integration tests, execute:
26+
27+
```shell
28+
go test ./test/integration
29+
```
30+
31+
> Note: this requires an actual database connection, so the required ENV variables need to be present.

apis/trips/go.mod

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module github.com/Azure-Samples/openhack-devops-team/apis/trips
2+
3+
require (
4+
cloud.google.com/go v0.0.0-20180627205256-45528feae6df
5+
github.com/codemodus/swagui v0.1.0
6+
github.com/davecgh/go-spew v1.1.1 // indirect
7+
github.com/denisenkom/go-mssqldb v0.0.0-20180625034930-3724b4745ca9
8+
github.com/gorilla/context v1.1.1
9+
github.com/gorilla/mux v1.6.2
10+
github.com/pmezard/go-difflib v1.0.0 // indirect
11+
github.com/stretchr/objx v0.1.1 // indirect
12+
github.com/stretchr/testify v1.2.2
13+
golang.org/x/crypto v0.0.0-20180621125126-a49355c7e3f8
14+
)

apis/trips/test/api_test.go

Lines changed: 0 additions & 49 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package tripsgo
2+
3+
import (
4+
"io/ioutil"
5+
"os"
6+
"testing"
7+
8+
tripSvc "github.com/Azure-Samples/openhack-devops-team/apis/trips/tripsgo"
9+
)
10+
11+
var healthRouteTests = []tripSvc.APITestCase{
12+
{
13+
Tag: "t0 - healthcheck",
14+
Method: "GET",
15+
URL: "/api/healthcheck/trips",
16+
Status: 200,
17+
ExpectedResponse: `{"message": "Trip Service Healthcheck","status": "Healthy"}`,
18+
},
19+
}
20+
21+
func TestHealthRoute(t *testing.T) {
22+
router := tripSvc.NewRouter()
23+
var debug, present = os.LookupEnv("DEBUG_LOGGING")
24+
25+
if present && debug == "true" {
26+
tripSvc.InitLogging(os.Stdout, os.Stdout, os.Stdout)
27+
} else {
28+
// if debug env is not present or false, do not log debug output to console
29+
tripSvc.InitLogging(os.Stdout, ioutil.Discard, os.Stdout)
30+
}
31+
tripSvc.RunAPITests(t, router, healthRouteTests[0:1])
32+
33+
}
Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
package tripsgo
2+
3+
import (
4+
"encoding/json"
5+
"io/ioutil"
6+
"log"
7+
"os"
8+
"strings"
9+
"testing"
10+
11+
tripSvc "github.com/Azure-Samples/openhack-devops-team/apis/trips/tripsgo"
12+
)
13+
14+
var tripID string
15+
16+
var apiTestList = []tripSvc.APITestCase{
17+
{
18+
Tag: "t1 - Get all trips",
19+
Method: "GET",
20+
URL: "/api/trips",
21+
Status: 200,
22+
},
23+
{
24+
Tag: "t2 - Get a nonexistent trip",
25+
Method: "GET",
26+
URL: "/api/trips/99999",
27+
Status: 404,
28+
},
29+
{
30+
Tag: "t3 - Create a Trip",
31+
Method: "POST",
32+
URL: "/api/trips",
33+
Body: `{
34+
"Name":"Trip CREATE TEST",
35+
"UserId":"GO_TEST",
36+
"RecordedTimeStamp": "2018-04-19T19:08:16.03Z",
37+
"EndTimeStamp": "2018-04-19T19:42:49.573Z",
38+
"Rating":95,
39+
"IsComplete":false,
40+
"HasSimulatedOBDData":true,
41+
"AverageSpeed":100,
42+
"FuelUsed":10.27193484,
43+
"HardStops":2,
44+
"HardAccelerations":4,
45+
"Distance":30.0275486,
46+
"CreatedAt":"2018-01-01T12:00:00Z",
47+
"UpdatedAt":"2001-01-01T12:00:00Z"
48+
}`,
49+
Status: 200,
50+
},
51+
{
52+
Tag: "t4 - Update a trip",
53+
Method: "PATCH",
54+
URL: "/api/trips/{tripID}",
55+
Body: `{
56+
"Name":"Trip UPDATE TEST",
57+
"UserId":"GO_TEST",
58+
"RecordedTimeStamp": "2018-04-19T19:08:16.03Z",
59+
"EndTimeStamp": "2018-04-19T19:42:49.573Z",
60+
"Rating":91005,
61+
"IsComplete":true,
62+
"HasSimulatedOBDData":true,
63+
"AverageSpeed":100,
64+
"FuelUsed":10.27193484,
65+
"HardStops":2,
66+
"HardAccelerations":4,
67+
"Distance":30.0275486,
68+
"CreatedAt":"2018-01-01T12:00:00Z",
69+
"UpdatedAt":"2001-01-01T12:00:00Z"
70+
}`,
71+
Status: 200,
72+
},
73+
{
74+
Tag: "t5 - Create Trip Point",
75+
Method: "POST",
76+
URL: "/api/trips/{tripID}/trippoints",
77+
Body: `{
78+
"TripId": "{tripID}",
79+
"Latitude": 47.67598,
80+
"Longitude": -122.10612,
81+
"Speed": -255,
82+
"RecordedTimeStamp": "2018-05-24T10:00:15.003Z",
83+
"Sequence": 2,
84+
"RPM": -255,
85+
"ShortTermFuelBank": -255,
86+
"LongTermFuelBank": -255,
87+
"ThrottlePosition": -255,
88+
"RelativeThrottlePosition": -255,
89+
"Runtime": -255,
90+
"DistanceWithMalfunctionLight": -255,
91+
"EngineLoad": -255,
92+
"EngineFuelRate": -255,
93+
"CreatedAt": "0001-01-01T00:00:00Z",
94+
"UpdatedAt": "0001-01-01T00:00:00Z"
95+
}`,
96+
Status: 200,
97+
},
98+
{
99+
Tag: "t6 - Update Trip Point",
100+
Method: "PATCH",
101+
URL: "/api/trips/{tripID}/trippoints/{tripPointID}",
102+
Body: `{
103+
"Id": "{tripPointID}",
104+
"TripId": "{tripID}",
105+
"Latitude": 47.67598,
106+
"Longitude": -122.10612,
107+
"Speed": -255,
108+
"RecordedTimeStamp": "2018-05-24T10:00:15.003Z",
109+
"Sequence": 2,
110+
"RPM": -255,
111+
"ShortTermFuelBank": -255,
112+
"LongTermFuelBank": -255,
113+
"ThrottlePosition": -255,
114+
"RelativeThrottlePosition": -255,
115+
"Runtime": -255,
116+
"DistanceWithMalfunctionLight": -255,
117+
"EngineLoad": -255,
118+
"EngineFuelRate": -255,
119+
"Created": "0001-01-01T00:00:00Z",
120+
"UpdatedAt": "0001-01-01T00:00:00Z"
121+
}`,
122+
ExpectedResponse: "",
123+
Status: 200,
124+
},
125+
{
126+
Tag: "t7 - Read Trip Points for Trip",
127+
Method: "GET",
128+
URL: "/api/trips/{tripID}/trippoints",
129+
Status: 200,
130+
},
131+
{
132+
Tag: "t8 - Read Trip Points By Trip Point ID",
133+
Method: "GET",
134+
URL: "/api/trips/{tripID}/trippoints/{tripPointID}",
135+
Status: 200,
136+
},
137+
{
138+
Tag: "t9 - Delete Trip Point",
139+
Method: "DELETE",
140+
URL: "/api/trips/{tripID}/trippoints/{tripPointID}",
141+
Status: 200,
142+
},
143+
{
144+
Tag: "t10 - Delete a Trip",
145+
Method: "DELETE",
146+
URL: "/api/trips/{tripID}",
147+
Status: 200,
148+
},
149+
}
150+
151+
func TestTrip(t *testing.T) {
152+
router := tripSvc.NewRouter()
153+
var debug, present = os.LookupEnv("DEBUG_LOGGING")
154+
155+
if present && debug == "true" {
156+
tripSvc.InitLogging(os.Stdout, os.Stdout, os.Stdout)
157+
} else {
158+
// if debug env is not present or false, do not log debug output to console
159+
tripSvc.InitLogging(os.Stdout, ioutil.Discard, os.Stdout)
160+
}
161+
tripSvc.RunAPITests(t, router, apiTestList[0:4])
162+
163+
// setup update trip test (URL, Body, expected Response)
164+
apiTestList[4].URL = strings.Replace(apiTestList[4].URL, "{tripID}", TripFromStr(apiTestList[3].ActualResponse).ID, 1)
165+
apiTestList[4].Body = GetUpdateTrip(apiTestList[3].ActualResponse, apiTestList[4].Body)
166+
apiTestList[4].ExpectedResponse = apiTestList[4].Body
167+
168+
// setup create trip point test
169+
apiTestList[5].URL = strings.Replace(apiTestList[5].URL, "{tripID}", TripFromStr(apiTestList[3].ActualResponse).ID, 1)
170+
apiTestList[5].Body = strings.Replace(apiTestList[5].Body, "{tripID}", TripFromStr(apiTestList[3].ActualResponse).ID, 1)
171+
172+
// run update trip and create trip point tests
173+
tripSvc.RunAPITests(t, router, apiTestList[4:6])
174+
175+
// setup update trip point test
176+
apiTestList[6].URL = strings.Replace(apiTestList[6].URL, "{tripID}", TripFromStr(apiTestList[3].ActualResponse).ID, 1)
177+
apiTestList[6].URL = strings.Replace(apiTestList[6].URL, "{tripPointID}", TripPointFromStr(apiTestList[5].ActualResponse).ID, 1)
178+
apiTestList[6].Body = GetUpdateTripPoint(apiTestList[5].ActualResponse, apiTestList[6].Body)
179+
//apiTestList[6].ExpectedResponse = apiTestList[6].Body
180+
181+
// setup read trip points for trip test
182+
apiTestList[7].URL = strings.Replace(apiTestList[7].URL, "{tripID}", TripFromStr(apiTestList[3].ActualResponse).ID, 1)
183+
184+
// setup ready trip points by trip point id test
185+
apiTestList[8].URL = strings.Replace(apiTestList[8].URL, "{tripID}", TripFromStr(apiTestList[3].ActualResponse).ID, 1)
186+
apiTestList[8].URL = strings.Replace(apiTestList[8].URL, "{tripPointID}", TripPointFromStr(apiTestList[5].ActualResponse).ID, 1)
187+
188+
//setup delete trip point test
189+
apiTestList[9].URL = strings.Replace(apiTestList[9].URL, "{tripID}", TripFromStr(apiTestList[3].ActualResponse).ID, 1)
190+
apiTestList[9].URL = strings.Replace(apiTestList[9].URL, "{tripPointID}", TripPointFromStr(apiTestList[5].ActualResponse).ID, 1)
191+
192+
// setup delete test (URL)
193+
apiTestList[10].URL = strings.Replace(apiTestList[10].URL, "{tripID}", TripFromStr(apiTestList[3].ActualResponse).ID, 1)
194+
// run update test
195+
tripSvc.RunAPITests(t, router, apiTestList[6:10])
196+
}
197+
198+
func GetUpdateTrip(tripCreate string, tripUpdate string) string {
199+
tripC := TripFromStr(tripCreate)
200+
tripU := TripFromStr(tripUpdate)
201+
202+
tripU.ID = tripC.ID
203+
204+
serializedTripUpdate, _ := json.Marshal(tripU)
205+
206+
return string(serializedTripUpdate)
207+
}
208+
209+
func GetUpdateTripPoint(tripPointCreate string, tripPointUpdate string) string {
210+
tripPointC := TripPointFromStr(tripPointCreate)
211+
tripPointU := TripPointFromStr(tripPointUpdate)
212+
213+
tripPointU.ID = tripPointC.ID
214+
tripPointU.TripID = tripPointC.TripID
215+
216+
serializedTripUpdate, _ := json.Marshal(tripPointU)
217+
218+
return string(serializedTripUpdate)
219+
}
220+
221+
func TripFromStr(tripStr string) tripSvc.Trip {
222+
trip := tripSvc.Trip{}
223+
224+
tripSvc.Debug.Println(tripStr)
225+
226+
errCreate := json.Unmarshal([]byte(tripStr), &trip)
227+
if errCreate != nil {
228+
log.Println("TripFromStr - Invalid trip string")
229+
log.Fatal(errCreate)
230+
}
231+
232+
return trip
233+
}
234+
235+
func TripPointFromStr(tripPointStr string) tripSvc.TripPoint {
236+
tripPoint := tripSvc.TripPoint{}
237+
238+
errCreate := json.Unmarshal([]byte(tripPointStr), &tripPoint)
239+
if errCreate != nil {
240+
log.Println("TripPointFromStr - Invalid trip point string")
241+
log.Fatal(errCreate)
242+
}
243+
244+
tripSvc.Debug.Println(tripPointStr)
245+
246+
return tripPoint
247+
}

0 commit comments

Comments
 (0)