Skip to content

Commit

Permalink
unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Navid Yaghoobi <[email protected]>
  • Loading branch information
navidys committed Nov 17, 2023
1 parent e0e3420 commit d7336e4
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 31 deletions.
79 changes: 63 additions & 16 deletions flights_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand All @@ -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))
Expand All @@ -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))
Expand All @@ -139,22 +163,34 @@ 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))

_, 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))
Expand All @@ -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))
Expand All @@ -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))
Expand Down
24 changes: 24 additions & 0 deletions mock_data/errors/states17.json
Original file line number Diff line number Diff line change
@@ -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
]
]
}
24 changes: 24 additions & 0 deletions mock_data/errors/states18.json
Original file line number Diff line number Diff line change
@@ -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
]
]
}
7 changes: 7 additions & 0 deletions mock_data/errors/tracks01.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"icao24": 1,
"callsign": "POE2136",
"startTime": 1689193028,
"endTime": 1689197805,
"path": null
}
7 changes: 7 additions & 0 deletions mock_data/errors/tracks02.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"icao24": "c060b9",
"callsign": 1,
"startTime": 1689193028,
"endTime": 1689197805,
"path": null
}
7 changes: 7 additions & 0 deletions mock_data/errors/tracks03.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"icao24": "c060b9",
"callsign": "POE2136",
"startTime": "a",
"endTime": 1689197805,
"path": null
}
7 changes: 7 additions & 0 deletions mock_data/errors/tracks04.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"icao24": "c060b9",
"callsign": "POE2136",
"startTime": 1689193028,
"endTime": "b",
"path": null
}
Loading

0 comments on commit d7336e4

Please sign in to comment.