Skip to content

Commit

Permalink
Merge pull request #23 from navidys/unit_tests_mock
Browse files Browse the repository at this point in the history
unit tests
  • Loading branch information
navidys authored Nov 17, 2023
2 parents 75bcb7d + b858ae5 commit 8fe054a
Show file tree
Hide file tree
Showing 30 changed files with 3,383 additions and 2 deletions.
15 changes: 15 additions & 0 deletions export_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package gopensky

import (
"context"
"fmt"
"net/http"
)

var (
DecodeRawStateVector = decodeRawStateVector
DecodeWaypoint = decodeWaypoint
Expand All @@ -8,4 +14,13 @@ var (
GetTracksRequestParams = getTracksRequestParams
GetStateRequestParams = getStateRequestParams
ParseFlightTrackResponse = parseFlightTrackResponse
OpenSkyAPIURL = openSkyAPIURL
)

func GetClient(ctx context.Context) (*http.Client, error) {
if c, ok := ctx.Value(clientKey).(*Connection); ok {
return c.client, nil
}

return nil, fmt.Errorf("%w %s", errContextKey, clientKey)
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/navidys/gopensky
go 1.18

require (
github.com/h2non/gock v1.2.0
github.com/onsi/ginkgo/v2 v2.13.1
github.com/onsi/gomega v1.30.0
github.com/rs/zerolog v1.31.0
Expand All @@ -13,6 +14,7 @@ require (
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
golang.org/x/net v0.17.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,18 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 h1:yAJXTCF9TqKcTiHJAE8dj7HMvPfh66eeA2JYW7eFpSE=
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/h2non/gock v1.2.0 h1:K6ol8rfrRkUOefooBC8elXoaNGYkpp7y2qcxGG6BzUE=
github.com/h2non/gock v1.2.0/go.mod h1:tNhoxHYW2W42cYkYb1WqzdbYIieALC99kpYr7rH/BQk=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 h1:W6apQkHrMkS0Muv8G/TipAy/FJl/rCYT0+EuS8+Z0z4=
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms=
github.com/onsi/ginkgo/v2 v2.13.1 h1:LNGfMbR2OVGBfXjvRZIZ2YCTQdGKtPLvuI1rMCCj3OU=
github.com/onsi/ginkgo/v2 v2.13.1/go.mod h1:XStQ8QcGwLyF4HdfcZB8SFOS/MWCgDuXMSBe6zrvLgM=
github.com/onsi/gomega v1.30.0 h1:hvMK7xYz4D3HapigLTeGdId/NcfQx1VHMJc60ew99+8=
Expand Down
119 changes: 119 additions & 0 deletions mock_data/all_states.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
{
"time": 1518552809,
"states": [
[
"ac96b8",
"AAL2423 ",
"United States",
1518552809,
1518552809,
-93.4581,
44.9529,
1150.62,
false,
116.59,
94.3,
0,
null,
1143,
"2236",
false,
0
],
[
"aa56db",
"UAL1711 ",
"United States",
1518552808,
1518552809,
-80.8667,
41.8291,
11277.6,
false,
291.48,
97.3,
0,
null,
11376.66,
"1031",
false,
0
],
[
"aa56da",
"UAL2796 ",
"United States",
1518552809,
1518552809,
-76.0575,
39.2978,
8229.6,
false,
266.64,
42.03,
0.65,
null,
8435.34,
"0753",
false,
0
],
[
"a0cfbd",
"AAL1852 ",
"United States",
1518552807,
1518552807,
-67.3843,
19.9709,
9144,
false,
218.45,
321.21,
0,
null,
9593.58,
"2330",
false,
0
],
[
"7c6b2d",
"JST745 ",
"Australia",
null,
1518552775,
null,
null,
null,
true,
0.19,
180,
null,
null,
null,
"3662",
false,
0
],
[
"a77eae",
"N582PU ",
"United States",
1518552809,
1518552809,
-86.9988,
40.4198,
228.6,
false,
39.31,
83.99,
-1.3,
null,
342.9,
"1200",
false,
0
]
]
}
2 changes: 1 addition & 1 deletion states.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func decodeRawStateVector(data []interface{}) (*StateVector, error) { //nolint:f
stVector := StateVector{}
recvDataCount := len(data)

if recvDataCount < stateVecCategoryIndex+1 {
if recvDataCount < stateVecCategoryIndex {
return nil, errStateVecDataCount
}

Expand Down
35 changes: 35 additions & 0 deletions states_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package gopensky_test
import (
"context"
"fmt"
"strings"

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

Expand All @@ -16,8 +18,41 @@ var _ = Describe("States", func() {
conn, err := gopensky.NewConnection(context.Background(), "", "")
Expect(err).NotTo(HaveOccurred())

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

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

_, err = gopensky.GetStates(conn, -1, nil, nil, false)
Expect(err).To(Equal(gopensky.ErrInvalidUnixTime))

states, err := gopensky.GetStates(conn, 0, nil, nil, false)
Expect(err).NotTo(HaveOccurred())
Expect(len(states.States)).To(Equal(6))

firstState := states.States[0]
Expect(firstState.Icao24).To(Equal("ac96b8"))
Expect(strings.TrimSpace(*firstState.Callsign)).To(Equal("AAL2423"))
Expect(firstState.OriginCountry).To(Equal("United States"))
Expect(*firstState.TimePosition).To(Equal(int64(1518552809)))
Expect(firstState.LastContact).To(Equal(int64(1518552809)))
Expect(*firstState.Longitude).To(Equal(-93.4581))
Expect(*firstState.Latitude).To(Equal(44.9529))
Expect(*firstState.BaroAltitude).To(Equal(1150.62))
Expect(firstState.OnGround).To(Equal(false))
Expect(*firstState.Velocity).To(Equal(116.59))
Expect(*firstState.TrueTrack).To(Equal(94.3))
Expect(*firstState.VerticalRate).To(Equal(float64(0)))
Expect(firstState.Sensors).To(BeNil())
Expect(*firstState.GeoAltitude).To(Equal(float64(1143)))
Expect(*firstState.Squawk).To(Equal("2236"))
Expect(firstState.Spi).To(Equal(false))
Expect(firstState.PositionSource).To(Equal(0))
})
})

Expand Down
2 changes: 1 addition & 1 deletion tracks.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func parseFlightTrackResponse(response *FlightTrackResponse) (FlightTrack, error
}

func decodeWaypoint(data []interface{}) (*WayPoint, error) { //nolint:funlen,cyclop
if len(data) < trackOnGroundIndex+1 {
if len(data) < trackOnGroundIndex {
return nil, errWaypointsDataCount
}

Expand Down
12 changes: 12 additions & 0 deletions vendor/github.com/h2non/gock/.editorconfig

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions vendor/github.com/h2non/gock/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions vendor/github.com/h2non/gock/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8fe054a

Please sign in to comment.