diff options
author | Julien Dessaux | 2021-05-04 15:50:49 +0200 |
---|---|---|
committer | Julien Dessaux | 2021-05-04 15:50:49 +0200 |
commit | bb5b7dd3f5490c97e23c392a240face4f911e190 (patch) | |
tree | bff4c78d4b7ec758fb0cd2b3901cbb893c343e8a | |
parent | Cleaned the navitia - webui calls (diff) | |
download | trains-bb5b7dd3f5490c97e23c392a240face4f911e190.tar.gz trains-bb5b7dd3f5490c97e23c392a240face4f911e190.tar.bz2 trains-bb5b7dd3f5490c97e23c392a240face4f911e190.zip |
Reworked the navitia_api_client to be mockable
Diffstat (limited to '')
-rw-r--r-- | internal/webui/root_test.go | 49 | ||||
-rw-r--r-- | internal/webui/utils.go | 2 | ||||
-rw-r--r-- | pkg/navitia_api_client/client.go | 12 | ||||
-rw-r--r-- | pkg/navitia_api_client/client_test.go | 15 | ||||
-rw-r--r-- | pkg/navitia_api_client/departures.go | 2 |
5 files changed, 46 insertions, 34 deletions
diff --git a/internal/webui/root_test.go b/internal/webui/root_test.go index 8faee77..cd2da0f 100644 --- a/internal/webui/root_test.go +++ b/internal/webui/root_test.go @@ -4,11 +4,21 @@ import ( "net/http" "testing" + "git.adyxax.org/adyxax/trains/pkg/config" "git.adyxax.org/adyxax/trains/pkg/database" "git.adyxax.org/adyxax/trains/pkg/model" "github.com/stretchr/testify/require" ) +type NavitiaMockClient struct { + departures []model.Departure + err error +} + +func (c *NavitiaMockClient) GetDepartures(trainStop string) (departures []model.Departure, err error) { + return c.departures, c.err +} + func TestRootHandler(t *testing.T) { // test environment setup dbEnv, err := database.InitDB("sqlite3", "file::memory:?_foreign_keys=on") @@ -21,12 +31,19 @@ func TestRootHandler(t *testing.T) { require.Nil(t, err) token1, err := dbEnv.CreateSession(user1) require.Nil(t, err) - e := &env{ + e := env{ dbEnv: dbEnv, - // TODO mock navitia + conf: &config.Config{TrainStop: "test"}, } + departures1 := []model.Departure{ + model.Departure{ + Direction: "test direction", + Arrival: "20210503T150405", + }, + } + e.navitia = &NavitiaMockClient{departures: departures1, err: nil} // test GET requests - runHttpTest(t, e, rootHandler, &httpTestCase{ + runHttpTest(t, &e, rootHandler, &httpTestCase{ name: "a simple get when not logged in should redirect to the login page", input: httpTestInput{ method: http.MethodGet, @@ -37,18 +54,16 @@ func TestRootHandler(t *testing.T) { location: "/login", }, }) - // TODO mock navitia - _ = token1 - //runHttpTest(t, e, rootHandler, &httpTestCase{ - // name: "a simple get when logged in should display the departure times", - // input: httpTestInput{ - // method: http.MethodGet, - // path: "/", - // cookie: &http.Cookie{Name: sessionCookieName, Value: *token1}, - // }, - // expect: httpTestExpect{ - // code: http.StatusOK, - // bodyString: "Horaires des prochains trains", - // }, - //}) + runHttpTest(t, &e, rootHandler, &httpTestCase{ + name: "a simple get when logged in should display the departure times", + input: httpTestInput{ + method: http.MethodGet, + path: "/", + cookie: &http.Cookie{Name: sessionCookieName, Value: *token1}, + }, + expect: httpTestExpect{ + code: http.StatusOK, + bodyString: "Horaires des prochains trains", + }, + }) } diff --git a/internal/webui/utils.go b/internal/webui/utils.go index 7152401..be8baf3 100644 --- a/internal/webui/utils.go +++ b/internal/webui/utils.go @@ -20,7 +20,7 @@ var staticFS embed.FS type env struct { conf *config.Config dbEnv *database.DBEnv - navitia *navitia_api_client.Client + navitia navitia_api_client.Client } type handlerError interface { diff --git a/pkg/navitia_api_client/client.go b/pkg/navitia_api_client/client.go index aef8d0e..71b82fe 100644 --- a/pkg/navitia_api_client/client.go +++ b/pkg/navitia_api_client/client.go @@ -5,9 +5,15 @@ import ( "net/http" "sync" "time" + + "git.adyxax.org/adyxax/trains/pkg/model" ) -type Client struct { +type Client interface { + GetDepartures(trainStop string) (departures []model.Departure, err error) +} + +type NavitiaClient struct { baseURL string httpClient *http.Client @@ -20,8 +26,8 @@ type cachedResult struct { result interface{} } -func NewClient(token string) *Client { - return &Client{ +func NewClient(token string) Client { + return &NavitiaClient{ baseURL: fmt.Sprintf("https://%s@api.sncf.com/v1", token), httpClient: &http.Client{ Timeout: time.Minute, diff --git a/pkg/navitia_api_client/client_test.go b/pkg/navitia_api_client/client_test.go index df23790..009712d 100644 --- a/pkg/navitia_api_client/client_test.go +++ b/pkg/navitia_api_client/client_test.go @@ -9,15 +9,15 @@ import ( ) // package utilities -func newTestClient(ts *httptest.Server) *Client { - return &Client{ +func newTestClient(ts *httptest.Server) *NavitiaClient { + return &NavitiaClient{ baseURL: fmt.Sprintf(ts.URL), httpClient: ts.Client(), cache: make(map[string]cachedResult), } } -func newTestClientFromFilename(t *testing.T, filename string) (*Client, *httptest.Server) { +func newTestClientFromFilename(t *testing.T, filename string) (*NavitiaClient, *httptest.Server) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { page, err := ioutil.ReadFile(filename) if err != nil { @@ -27,12 +27,3 @@ func newTestClientFromFilename(t *testing.T, filename string) (*Client, *httptes })) return newTestClient(ts), ts } - -// tests -func TestNewClient(t *testing.T) { - client := NewClient("test") - want := "https://test@api.sncf.com/v1" - if client.baseURL != want { - t.Fatal("Invalid new client") - } -} diff --git a/pkg/navitia_api_client/departures.go b/pkg/navitia_api_client/departures.go index 3b7053d..7447cdc 100644 --- a/pkg/navitia_api_client/departures.go +++ b/pkg/navitia_api_client/departures.go @@ -45,7 +45,7 @@ type DeparturesResponse struct { } `json:"context"` } -func (c *Client) GetDepartures(trainStop string) (departures []model.Departure, err error) { +func (c *NavitiaClient) GetDepartures(trainStop string) (departures []model.Departure, err error) { request := fmt.Sprintf("%s/coverage/sncf/stop_areas/%s/departures", c.baseURL, trainStop) start := time.Now() c.mutex.Lock() |