aboutsummaryrefslogtreecommitdiff
path: root/pkg/navitia_api_client/train_stops_test.go
blob: 93fc43f59c28cca185d5ed772ed618c8c9b9f007 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package navitia_api_client

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

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

func TestGetTrainStops(t *testing.T) {
	// Simple Test cases
	testCases := []struct {
		name           string
		inputNewCLient string
		expected       []model.TrainStop
		expectedError  interface{}
	}{
		{"invalid characters in token should fail", "}", nil, &HttpClientError{}},
		{"unreachable server should fail", "https://", nil, &HttpClientError{}},
	}
	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			client := NewClient(tc.inputNewCLient)
			valid, err := client.GetTrainStops()
			if tc.expectedError != nil {
				require.Error(t, err)
				assert.Equalf(t, reflect.TypeOf(err), reflect.TypeOf(tc.expectedError), "Invalid error type. Got %s but expected %s", reflect.TypeOf(err), reflect.TypeOf(tc.expectedError))
				assert.Equal(t, tc.expected, valid)
			} else {
				require.NoError(t, err)
				assert.Equal(t, tc.expected, valid)
			}
		})
	}
	// Test cases with a filename
	testCasesFilename := []struct {
		name          string
		inputFilename string
		expected      []model.TrainStop
		expectedError interface{}
	}{
		{"invalid json should fail", "test_data/invalid.json", nil, &JsonDecodeError{}},
	}
	for _, tc := range testCasesFilename {
		t.Run(tc.name, func(t *testing.T) {
			client, ts := newTestClientFromFilename(t, tc.inputFilename)
			defer ts.Close()
			valid, err := client.GetTrainStops()
			if tc.expectedError != nil {
				require.Error(t, err)
				assert.Equalf(t, reflect.TypeOf(err), reflect.TypeOf(tc.expectedError), "Invalid error type. Got %s but expected %s", reflect.TypeOf(err), reflect.TypeOf(tc.expectedError))
				assert.Equal(t, tc.expected, valid)
			} else {
				require.NoError(t, err)
				assert.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.GetTrainStops()
	if err == nil {
		t.Fatalf("404 should raise an error")
	}
	// normal working request
	client, ts = newTestClientFromFilename(t, "test_data/4-train-stops.json")
	defer ts.Close()
	trainStops, err := client.GetTrainStops()
	if err != nil {
		t.Fatalf("could not get train stops : %s", err)
	}
	if len(trainStops) != 4 {
		t.Fatalf("did not decode train stops properly, got %d train stops when expected 4", len(trainStops))
	}
	// normal request in multiple pages
	client, ts = newTestClientFromFilenames(t, []testClientCase{
		testClientCase{"/coverage/sncf/stop_areas?count=1000&start_page=0", "test_data/4-train-stops-page-0.json"},
		testClientCase{"/coverage/sncf/stop_areas?count=1000&start_page=1", "test_data/4-train-stops-page-1.json"},
		testClientCase{"/coverage/sncf/stop_areas?count=1000&start_page=2", "test_data/4-train-stops-page-2.json"},
	})
	defer ts.Close()
	trainStops, err = client.GetTrainStops()
	if err != nil {
		t.Fatalf("could not get train stops : %+v", err)
	}
	if len(trainStops) != 12 {
		t.Fatalf("did not decode train stops properly, got %d train stops when expected 4", len(trainStops))
	}
	// failing request in multiple pages with last one missing
	client, ts = newTestClientFromFilenames(t, []testClientCase{
		testClientCase{"/coverage/sncf/stop_areas?count=1000&start_page=0", "test_data/4-train-stops-page-0.json"},
		testClientCase{"/coverage/sncf/stop_areas?count=1000&start_page=1", "test_data/4-train-stops-page-1.json"},
	})
	defer ts.Close()
	trainStops, err = client.GetTrainStops()
	if err == nil {
		t.Fatalf("should not be able to get train stops : %+v", trainStops)
	}
}