Skip to content

Commit

Permalink
Merge pull request #27 from navidys/unit_tests_mock
Browse files Browse the repository at this point in the history
Unit tests mock
  • Loading branch information
navidys authored Nov 17, 2023
2 parents ceb04b0 + 7fdb79c commit e0e3420
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
7 changes: 7 additions & 0 deletions mock_data/all_tracks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"icao24": "c060b9",
"callsign": "POE2136",
"startTime": 1689193028,
"endTime": 1689197805,
"path": null
}
5 changes: 4 additions & 1 deletion tracks.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ func parseFlightTrackResponse(response *FlightTrackResponse) (FlightTrack, error
flightTrack.EndTime = time.Unix(int64(response.EndTime), 0).Unix()
// the api is not returning proper start time value
// temporary checking if its <= 0 then allocated 1
if flightTrack.StartTime <= 0 {
startTime := time.Unix(int64(response.StartTime), 0).Unix()
if startTime <= 0 {
flightTrack.StartTime = 1
} else {
flightTrack.StartTime = startTime
}

for _, waypointData := range response.Path {
Expand Down
26 changes: 26 additions & 0 deletions tracks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

"github.com/h2non/gock"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

Expand All @@ -16,6 +17,31 @@ var _ = Describe("Tracks", func() {
conn, err := gopensky.NewConnection(context.Background(), "", "")
Expect(err).NotTo(HaveOccurred())

defer gock.Off()
gock.New(gopensky.OpenSkyAPIURL).
Get("/tracks/all").
Reply(200).
File("mock_data/all_tracks.json")

gclient, err := gopensky.GetClient(conn)
Expect(err).NotTo(HaveOccurred())
gock.InterceptClient(gclient)

track, err := gopensky.GetTrackByAircraft(conn, "c060b9", 1689193028)
Expect(err).NotTo(HaveOccurred())
Expect(track.Icao24).To(Equal("c060b9"))
Expect(*track.Callsign).To(Equal("POE2136"))
Expect(track.StartTime).To(Equal(int64(1689193028)))
Expect(track.EndTime).To(Equal(int64(1689197805)))
Expect(track.Path).To(BeNil())
})
})

Describe("GetTrackByAircraft - errors", func() {
It("tests GetTrackByAircraft errors", func() {
conn, err := gopensky.NewConnection(context.Background(), "", "")
Expect(err).NotTo(HaveOccurred())

_, err = gopensky.GetTrackByAircraft(conn, "", 1696755342)
Expect(err).To(Equal(gopensky.ErrInvalidAircraftName))

Expand Down

0 comments on commit e0e3420

Please sign in to comment.