aboutsummaryrefslogtreecommitdiff
path: root/pkg/navitia_api_client/departures_test.go
diff options
context:
space:
mode:
authorJulien Dessaux2021-04-05 17:52:31 +0200
committerJulien Dessaux2021-04-05 17:52:31 +0200
commit1ffc9c42054e208a01d3e70e6b6f3e1781e798f8 (patch)
tree721f202dcf46fb5e9c181013237e4da9d27f796a /pkg/navitia_api_client/departures_test.go
parentReworked error handling for better and simpler tests (diff)
downloadtrains-1ffc9c42054e208a01d3e70e6b6f3e1781e798f8.tar.gz
trains-1ffc9c42054e208a01d3e70e6b6f3e1781e798f8.tar.bz2
trains-1ffc9c42054e208a01d3e70e6b6f3e1781e798f8.zip
Moved code around to conform best practices
Diffstat (limited to 'pkg/navitia_api_client/departures_test.go')
-rw-r--r--pkg/navitia_api_client/departures_test.go57
1 files changed, 57 insertions, 0 deletions
diff --git a/pkg/navitia_api_client/departures_test.go b/pkg/navitia_api_client/departures_test.go
new file mode 100644
index 0000000..a1658d2
--- /dev/null
+++ b/pkg/navitia_api_client/departures_test.go
@@ -0,0 +1,57 @@
+package navitia_api_client
+
+import (
+ "net/http"
+ "net/http/httptest"
+ "testing"
+)
+
+func TestGetDepartures(t *testing.T) {
+ // invalid characters in token
+ client := NewClient("}")
+ _, err := client.GetDepartures()
+ if err == nil {
+ t.Fatalf("invalid characters in token should raise an error")
+ }
+ // unreachable server
+ client = NewClient("https://")
+ _, err = client.GetDepartures()
+ if err == nil {
+ t.Fatalf("unreachable server should raise an error")
+ }
+ // invalid json
+ client, ts := NewTestClientFromFilename(t, "test_data/invalid.json")
+ defer ts.Close()
+ _, err = client.GetDepartures()
+ if err == nil {
+ t.Fatalf("invalid json should raise an error")
+ }
+ // http error
+ ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.WriteHeader(http.StatusNotFound)
+ }))
+ client = NewTestClient(ts)
+ _, err = client.GetDepartures()
+ if err == nil {
+ t.Fatalf("404 should raise an error")
+ }
+ // normal working request
+ client, ts = NewTestClientFromFilename(t, "test_data/normal-crepieux.json")
+ defer ts.Close()
+ departures, err := client.GetDepartures()
+ if err != nil {
+ t.Fatalf("could not get normal-crepieux departures : %s", err)
+ }
+ if len(departures.Departures) != 10 {
+ t.Fatalf("did not decode normal-crepieux departures properly, got %d departures when expected 10", len(departures.Departures))
+ }
+ // test the cache (assuming the test takes less than 60 seconds (and it really should) it will be accurate)
+ ts.Close()
+ departures, err = client.GetDepartures()
+ if err != nil {
+ t.Fatalf("could not get normal-crepieux departures : %s", err)
+ }
+ if len(departures.Departures) != 10 {
+ t.Fatalf("did not decode normal-crepieux departures properly, got %d departures when expected 10", len(departures.Departures))
+ }
+}