diff --git a/flights_test.go b/flights_test.go index 12bfb30..e8a100a 100644 --- a/flights_test.go +++ b/flights_test.go @@ -53,15 +53,19 @@ var _ = Describe("Flights", func() { conn, err := gopensky.NewConnection(context.Background(), "", "") Expect(err).NotTo(HaveOccurred()) + gclient, err := gopensky.GetClient(conn) + Expect(err).NotTo(HaveOccurred()) + gock.InterceptClient(gclient) + defer gock.Off() + gock.New(gopensky.OpenSkyAPIURL). Get("/flights/arrival"). Reply(200). - File("mock_data/flights_data.json") + BodyString("s") - gclient, err := gopensky.GetClient(conn) - Expect(err).NotTo(HaveOccurred()) - gock.InterceptClient(gclient) + _, err = gopensky.GetArrivalsByAirport(conn, "KEWR", 1696755342, 1696928142) + Expect(err.Error()).To(ContainSubstring("unmarshalling")) _, err = gopensky.GetArrivalsByAirport(conn, "", 1696755342, 1696928142) Expect(err).To(Equal(gopensky.ErrInvalidAirportName)) @@ -72,6 +76,14 @@ var _ = Describe("Flights", func() { _, err = gopensky.GetArrivalsByAirport(conn, "KEWR", 1696755342, -1) Expect(err).To(Equal(gopensky.ErrInvalidUnixTime)) + _, err = gopensky.GetArrivalsByAirport(context.Background(), "KEWR", 1696755342, 1696928142) + Expect(err.Error()).To(ContainSubstring("invalid context key")) + + gock.New(gopensky.OpenSkyAPIURL). + Get("/flights/arrival"). + Reply(200). + File("mock_data/flights_data.json") + flightData, err := gopensky.GetArrivalsByAirport(conn, "KEWR", 1696755342, 1696928142) Expect(err).NotTo(HaveOccurred()) Expect(len(flightData)).To(Equal(3)) @@ -96,15 +108,19 @@ var _ = Describe("Flights", func() { conn, err := gopensky.NewConnection(context.Background(), "", "") Expect(err).NotTo(HaveOccurred()) + gclient, err := gopensky.GetClient(conn) + Expect(err).NotTo(HaveOccurred()) + gock.InterceptClient(gclient) + defer gock.Off() + gock.New(gopensky.OpenSkyAPIURL). Get("/flights/departure"). Reply(200). - File("mock_data/flights_data.json") + BodyString("s") - gclient, err := gopensky.GetClient(conn) - Expect(err).NotTo(HaveOccurred()) - gock.InterceptClient(gclient) + _, err = gopensky.GetDeparturesByAirport(conn, "KEWR", 1696755342, 1696928142) + Expect(err.Error()).To(ContainSubstring("unmarshalling")) _, err = gopensky.GetDeparturesByAirport(conn, "", 1696755342, 1696928142) Expect(err).To(Equal(gopensky.ErrInvalidAirportName)) @@ -115,6 +131,14 @@ var _ = Describe("Flights", func() { _, err = gopensky.GetDeparturesByAirport(conn, "KEWR", 1696755342, -1) Expect(err).To(Equal(gopensky.ErrInvalidUnixTime)) + _, err = gopensky.GetDeparturesByAirport(context.Background(), "KEWR", 1696755342, 1696928142) + Expect(err.Error()).To(ContainSubstring("invalid context key")) + + gock.New(gopensky.OpenSkyAPIURL). + Get("/flights/departure"). + Reply(200). + File("mock_data/flights_data.json") + flightData, err := gopensky.GetDeparturesByAirport(conn, "KEWR", 1696755342, 1696928142) Expect(err).NotTo(HaveOccurred()) Expect(len(flightData)).To(Equal(3)) @@ -139,15 +163,19 @@ var _ = Describe("Flights", func() { conn, err := gopensky.NewConnection(context.Background(), "", "") Expect(err).NotTo(HaveOccurred()) + gclient, err := gopensky.GetClient(conn) + Expect(err).NotTo(HaveOccurred()) + gock.InterceptClient(gclient) + defer gock.Off() + gock.New(gopensky.OpenSkyAPIURL). Get("/flights/all"). Reply(200). - File("mock_data/flights_data.json") + BodyString("s") - gclient, err := gopensky.GetClient(conn) - Expect(err).NotTo(HaveOccurred()) - gock.InterceptClient(gclient) + _, err = gopensky.GetFlightsByInterval(conn, 1696755342, 1696928142) + Expect(err.Error()).To(ContainSubstring("unmarshalling")) _, err = gopensky.GetFlightsByInterval(conn, 0, 1696928142) Expect(err).To(Equal(gopensky.ErrInvalidUnixTime)) @@ -155,6 +183,14 @@ var _ = Describe("Flights", func() { _, err = gopensky.GetFlightsByInterval(conn, 1696755342, -1) Expect(err).To(Equal(gopensky.ErrInvalidUnixTime)) + _, err = gopensky.GetFlightsByInterval(context.Background(), 1696755342, 1696928142) + Expect(err.Error()).To(ContainSubstring("invalid context key")) + + gock.New(gopensky.OpenSkyAPIURL). + Get("/flights/all"). + Reply(200). + File("mock_data/flights_data.json") + flightData, err := gopensky.GetFlightsByInterval(conn, 1696755342, 1696928142) Expect(err).NotTo(HaveOccurred()) Expect(len(flightData)).To(Equal(3)) @@ -179,15 +215,23 @@ var _ = Describe("Flights", func() { conn, err := gopensky.NewConnection(context.Background(), "", "") Expect(err).NotTo(HaveOccurred()) + gclient, err := gopensky.GetClient(conn) + Expect(err).NotTo(HaveOccurred()) + gock.InterceptClient(gclient) + defer gock.Off() gock.New(gopensky.OpenSkyAPIURL). Get("/flights/aircraft"). Reply(200). - File("mock_data/flights_data.json") + BodyString("s") - gclient, err := gopensky.GetClient(conn) - Expect(err).NotTo(HaveOccurred()) - gock.InterceptClient(gclient) + _, err = gopensky.GetFlightsByAircraft(conn, "c060b9", 1696755342, 1696928142) + Expect(err.Error()).To(ContainSubstring("unmarshalling")) + + gock.New(gopensky.OpenSkyAPIURL). + Get("/flights/aircraft"). + Reply(200). + File("mock_data/flights_data.json") _, err = gopensky.GetFlightsByAircraft(conn, "", 0, 1696928142) Expect(err).To(Equal(gopensky.ErrInvalidAircraftName)) @@ -198,6 +242,9 @@ var _ = Describe("Flights", func() { _, err = gopensky.GetFlightsByAircraft(conn, "c060b9", 1696755342, -1) Expect(err).To(Equal(gopensky.ErrInvalidUnixTime)) + _, err = gopensky.GetFlightsByAircraft(context.Background(), "c060b9", 1696755342, 1696928142) + Expect(err.Error()).To(ContainSubstring("invalid context key")) + flightData, err := gopensky.GetFlightsByAircraft(conn, "c060b9", 1696755342, 1696928142) Expect(err).NotTo(HaveOccurred()) Expect(len(flightData)).To(Equal(3)) diff --git a/mock_data/errors/states17.json b/mock_data/errors/states17.json new file mode 100644 index 0000000..6860f3d --- /dev/null +++ b/mock_data/errors/states17.json @@ -0,0 +1,24 @@ +{ + "time": 1518552809, + "states": [ + [ + "ac96b8", + "AAL2423 ", + "United States", + 1518552809, + 1518552809, + -93.4581, + 44.9529, + 1150.62, + "a", + 116.59, + 94.3, + 0, + null, + 1143, + "2236", + false, + 0 + ] + ] + } diff --git a/mock_data/errors/states18.json b/mock_data/errors/states18.json new file mode 100644 index 0000000..f2dabff --- /dev/null +++ b/mock_data/errors/states18.json @@ -0,0 +1,24 @@ +{ + "time": 1518552809, + "states": [ + [ + "ac96b8", + "AAL2423 ", + "United States", + 1518552809, + 1518552809, + -93.4581, + 44.9529, + 1150.62, + true, + 116.59, + 94.3, + 0, + "a", + 1143, + "2236", + false, + 0 + ] + ] + } diff --git a/mock_data/errors/tracks01.json b/mock_data/errors/tracks01.json new file mode 100644 index 0000000..7bb428c --- /dev/null +++ b/mock_data/errors/tracks01.json @@ -0,0 +1,7 @@ +{ + "icao24": 1, + "callsign": "POE2136", + "startTime": 1689193028, + "endTime": 1689197805, + "path": null +} diff --git a/mock_data/errors/tracks02.json b/mock_data/errors/tracks02.json new file mode 100644 index 0000000..9e6312d --- /dev/null +++ b/mock_data/errors/tracks02.json @@ -0,0 +1,7 @@ +{ + "icao24": "c060b9", + "callsign": 1, + "startTime": 1689193028, + "endTime": 1689197805, + "path": null +} diff --git a/mock_data/errors/tracks03.json b/mock_data/errors/tracks03.json new file mode 100644 index 0000000..bbdb5fe --- /dev/null +++ b/mock_data/errors/tracks03.json @@ -0,0 +1,7 @@ +{ + "icao24": "c060b9", + "callsign": "POE2136", + "startTime": "a", + "endTime": 1689197805, + "path": null +} diff --git a/mock_data/errors/tracks04.json b/mock_data/errors/tracks04.json new file mode 100644 index 0000000..1cf79fd --- /dev/null +++ b/mock_data/errors/tracks04.json @@ -0,0 +1,7 @@ +{ + "icao24": "c060b9", + "callsign": "POE2136", + "startTime": 1689193028, + "endTime": "b", + "path": null +} diff --git a/states_test.go b/states_test.go index 5758b89..3a67886 100644 --- a/states_test.go +++ b/states_test.go @@ -66,8 +66,18 @@ var _ = Describe("States", func() { _, err = gopensky.GetStates(conn, -1, nil, nil, false) Expect(err).To(Equal(gopensky.ErrInvalidUnixTime)) + _, err = gopensky.GetStates(context.Background(), 0, nil, nil, false) + Expect(err.Error()).To(ContainSubstring("invalid context key")) + defer gock.Off() + gock.New(gopensky.OpenSkyAPIURL). + Get("/states/all"). + Reply(200). + BodyString("[{}]") + _, err = gopensky.GetStates(conn, 0, nil, nil, false) + Expect(errors.Unwrap(err).Error()).To(ContainSubstring("json: cannot unmarshal")) + // data count error gock.New(gopensky.OpenSkyAPIURL). Get("/states/all"). @@ -84,7 +94,7 @@ var _ = Describe("States", func() { File("mock_data/errors/states02.json") _, err = gopensky.GetStates(conn, 0, nil, nil, false) - Expect(errors.Unwrap(err).Error()).To(Equal("state vector icao24 assertion failed: 123")) + Expect(errors.Unwrap(err).Error()).To(ContainSubstring("icao24 assertion")) // callsign assertion error gock.New(gopensky.OpenSkyAPIURL). @@ -93,7 +103,7 @@ var _ = Describe("States", func() { File("mock_data/errors/states03.json") _, err = gopensky.GetStates(conn, 0, nil, nil, false) - Expect(errors.Unwrap(err).Error()).To(Equal("state vector callsign assertion failed: 123")) + Expect(errors.Unwrap(err).Error()).To(ContainSubstring("callsign assertion")) // country assertion error gock.New(gopensky.OpenSkyAPIURL). @@ -102,7 +112,7 @@ var _ = Describe("States", func() { File("mock_data/errors/states04.json") _, err = gopensky.GetStates(conn, 0, nil, nil, false) - Expect(errors.Unwrap(err).Error()).To(Equal("state vector origin country assertion failed: 123")) + Expect(errors.Unwrap(err).Error()).To(ContainSubstring("origin country assertion")) // time position assertion error gock.New(gopensky.OpenSkyAPIURL). @@ -111,7 +121,7 @@ var _ = Describe("States", func() { File("mock_data/errors/states05.json") _, err = gopensky.GetStates(conn, 0, nil, nil, false) - Expect(errors.Unwrap(err).Error()).To(Equal("state vector time position assertion failed: a")) + Expect(errors.Unwrap(err).Error()).To(ContainSubstring("time position assertion")) // last contact assertion error gock.New(gopensky.OpenSkyAPIURL). @@ -120,7 +130,7 @@ var _ = Describe("States", func() { File("mock_data/errors/states06.json") _, err = gopensky.GetStates(conn, 0, nil, nil, false) - Expect(errors.Unwrap(err).Error()).To(Equal("state vector last contact assertion failed: a")) + Expect(errors.Unwrap(err).Error()).To(ContainSubstring("last contact assertion")) // longitude assertion error gock.New(gopensky.OpenSkyAPIURL). @@ -129,7 +139,7 @@ var _ = Describe("States", func() { File("mock_data/errors/states07.json") _, err = gopensky.GetStates(conn, 0, nil, nil, false) - Expect(errors.Unwrap(err).Error()).To(Equal("state vector longitude assertion failed: a")) + Expect(errors.Unwrap(err).Error()).To(ContainSubstring("longitude assertion")) // latitude assertion error gock.New(gopensky.OpenSkyAPIURL). @@ -138,7 +148,7 @@ var _ = Describe("States", func() { File("mock_data/errors/states08.json") _, err = gopensky.GetStates(conn, 0, nil, nil, false) - Expect(errors.Unwrap(err).Error()).To(Equal("state vector latitude assertion failed: a")) + Expect(errors.Unwrap(err).Error()).To(ContainSubstring("latitude assertion")) // baro altitude assertion error gock.New(gopensky.OpenSkyAPIURL). @@ -147,7 +157,7 @@ var _ = Describe("States", func() { File("mock_data/errors/states09.json") _, err = gopensky.GetStates(conn, 0, nil, nil, false) - Expect(errors.Unwrap(err).Error()).To(Equal("state vector baro altitude assertion failed: a")) + Expect(errors.Unwrap(err).Error()).To(ContainSubstring("baro altitude assertion")) // velocity assertion error gock.New(gopensky.OpenSkyAPIURL). @@ -156,7 +166,7 @@ var _ = Describe("States", func() { File("mock_data/errors/states10.json") _, err = gopensky.GetStates(conn, 0, nil, nil, false) - Expect(errors.Unwrap(err).Error()).To(Equal("state vector velocity assertion failed: a")) + Expect(errors.Unwrap(err).Error()).To(ContainSubstring("velocity assertion")) // true track assertion error gock.New(gopensky.OpenSkyAPIURL). @@ -165,7 +175,7 @@ var _ = Describe("States", func() { File("mock_data/errors/states11.json") _, err = gopensky.GetStates(conn, 0, nil, nil, false) - Expect(errors.Unwrap(err).Error()).To(Equal("state vector true track assertion failed: a")) + Expect(errors.Unwrap(err).Error()).To(ContainSubstring("true track assertion")) // vertical rate assertion error gock.New(gopensky.OpenSkyAPIURL). @@ -174,7 +184,7 @@ var _ = Describe("States", func() { File("mock_data/errors/states12.json") _, err = gopensky.GetStates(conn, 0, nil, nil, false) - Expect(errors.Unwrap(err).Error()).To(Equal("state vector vertical rate assertion failed: a")) + Expect(errors.Unwrap(err).Error()).To(ContainSubstring("vertical rate assertion")) // geo altitude assertion error gock.New(gopensky.OpenSkyAPIURL). @@ -183,7 +193,7 @@ var _ = Describe("States", func() { File("mock_data/errors/states13.json") _, err = gopensky.GetStates(conn, 0, nil, nil, false) - Expect(errors.Unwrap(err).Error()).To(Equal("state vector geo altitude assertion failed: a")) + Expect(errors.Unwrap(err).Error()).To(ContainSubstring("geo altitude assertion")) // squawk assertion error gock.New(gopensky.OpenSkyAPIURL). @@ -192,7 +202,7 @@ var _ = Describe("States", func() { File("mock_data/errors/states14.json") _, err = gopensky.GetStates(conn, 0, nil, nil, false) - Expect(errors.Unwrap(err).Error()).To(Equal("state vector squawk assertion failed: 1")) + Expect(errors.Unwrap(err).Error()).To(ContainSubstring("squawk assertion")) // spi assertion error gock.New(gopensky.OpenSkyAPIURL). @@ -201,7 +211,7 @@ var _ = Describe("States", func() { File("mock_data/errors/states15.json") _, err = gopensky.GetStates(conn, 0, nil, nil, false) - Expect(errors.Unwrap(err).Error()).To(Equal("state vector spi assertion failed: a")) + Expect(errors.Unwrap(err).Error()).To(ContainSubstring("spi assertion")) // position source assertion error gock.New(gopensky.OpenSkyAPIURL). @@ -210,7 +220,25 @@ var _ = Describe("States", func() { File("mock_data/errors/states16.json") _, err = gopensky.GetStates(conn, 0, nil, nil, false) - Expect(errors.Unwrap(err).Error()).To(Equal("state vector position source assertion failed: a")) + Expect(errors.Unwrap(err).Error()).To(ContainSubstring("position source assertion")) + + // on ground assertion error + gock.New(gopensky.OpenSkyAPIURL). + Get("/states/all"). + Reply(200). + File("mock_data/errors/states17.json") + + _, err = gopensky.GetStates(conn, 0, nil, nil, false) + Expect(errors.Unwrap(err).Error()).To(ContainSubstring("on ground assertion")) + + // sensors assertion error + gock.New(gopensky.OpenSkyAPIURL). + Get("/states/all"). + Reply(200). + File("mock_data/errors/states18.json") + + _, err = gopensky.GetStates(conn, 0, nil, nil, false) + Expect(errors.Unwrap(err).Error()).To(ContainSubstring("sensors assertion")) }) }) diff --git a/tracks_test.go b/tracks_test.go index d9bb4e7..ebefc97 100644 --- a/tracks_test.go +++ b/tracks_test.go @@ -2,6 +2,7 @@ package gopensky_test import ( "context" + "errors" "fmt" "github.com/h2non/gock" @@ -42,11 +43,38 @@ var _ = Describe("Tracks", func() { conn, err := gopensky.NewConnection(context.Background(), "", "") Expect(err).NotTo(HaveOccurred()) + gclient, err := gopensky.GetClient(conn) + Expect(err).NotTo(HaveOccurred()) + gock.InterceptClient(gclient) + + _, err = gopensky.GetTrackByAircraft(context.Background(), "a835af", 1696755342) + Expect(err.Error()).To(ContainSubstring("invalid context key")) + _, err = gopensky.GetTrackByAircraft(conn, "", 1696755342) Expect(err).To(Equal(gopensky.ErrInvalidAircraftName)) _, err = gopensky.GetTrackByAircraft(conn, "a835af", -1) Expect(err).To(Equal(gopensky.ErrInvalidUnixTime)) + + defer gock.Off() + + gock.New(gopensky.OpenSkyAPIURL). + Get("/tracks/all"). + Reply(200). + BodyString("{'a': 2}") + + _, err = gopensky.GetTrackByAircraft(conn, "c060b9", 1689193028) + Expect(err.Error()).To(ContainSubstring("unmarshalling")) + + for _, tfile := range []string{"tracks01.json", "tracks02.json", "tracks03.json", "tracks04.json"} { + gock.New(gopensky.OpenSkyAPIURL). + Get("/tracks/all"). + Reply(200). + File("mock_data/errors/" + tfile) + + _, err = gopensky.GetTrackByAircraft(conn, "c060b9", 1689193028) + Expect(errors.Unwrap(err).Error()).To(ContainSubstring("json: cannot unmarshal")) + } }) })