aboutsummaryrefslogtreecommitdiff
path: root/pkg/navitia_api_client/departures_test.go
blob: 0e92fe762ae835313e2dfc4acc701ef45a6f5a1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package navitia_api_client

import (
	"net/http"
	"net/http/httptest"
	"testing"

	"git.adyxax.org/adyxax/trains/pkg/model"
	"github.com/stretchr/testify/require"
)

func TestGetDepartures(t *testing.T) {
	// Simple Test cases
	testCases := []struct {
		name               string
		inputNewCLient     string
		inputGetDepartures string
		expected           []model.Departure
		expectedError      error
	}{
		{"invalid characters in token should fail", "}", "test", nil, HttpClientError{}},
		{"unreachable server should fail", "https://", "test", nil, HttpClientError{}},
	}
	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			client := NewClient(tc.inputNewCLient)
			valid, err := client.GetDepartures(tc.inputGetDepartures)
			if tc.expectedError != nil {
				require.Error(t, err)
				requireErrorTypeMatch(t, err, tc.expectedError)
				require.Equal(t, tc.expected, valid)
			} else {
				require.NoError(t, err)
				require.Equal(t, tc.expected, valid)
			}
		})
	}
	// Test cases with a filename
	testCasesFilename := []struct {
		name               string
		inputFilename      string
		inputGetDepartures string
		expected           []model.Departure
		expectedError      error
	}{
		{"invalid json should fail", "test_data/invalid.json", "test", nil, JsonDecodeError{}},
		{"invalid date should fail", "test_data/invalid_date.json", "test", nil, DateParsingError{}},
	}
	for _, tc := range testCasesFilename {
		t.Run(tc.name, func(t *testing.T) {
			client, ts := newTestClientFromFilename(t, tc.inputFilename)
			defer ts.Close()
			valid, err := client.GetDepartures(tc.inputGetDepartures)
			if tc.expectedError != nil {
				require.Error(t, err)
				requireErrorTypeMatch(t, err, tc.expectedError)
				require.Equal(t, tc.expected, valid)
			} else {
				require.NoError(t, err)
				require.Equal(t, tc.expected, valid)
			}
		})
	}
	// http error
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusNotFound)
	}))
	client := newTestClient(ts)
	_, err := client.GetDepartures("test")
	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("test")
	if err != nil {
		t.Fatalf("could not get normal-crepieux departures : %s", err)
	}
	if len(departures) != 10 {
		t.Fatalf("did not decode normal-crepieux departures properly, got %d departures when expected 10", len(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("test")
	if err != nil {
		t.Fatalf("could not get normal-crepieux departures : %s", err)
	}
	if len(departures) != 10 {
		t.Fatalf("did not decode normal-crepieux departures properly, got %d departures when expected 10", len(departures))
	}
}