diff options
author | Julien Dessaux | 2021-09-08 15:23:50 +0200 |
---|---|---|
committer | Julien Dessaux | 2021-09-08 16:12:01 +0200 |
commit | 3c5e31b25a53268b413bc1e511b7486a2a1c80b9 (patch) | |
tree | f6fe9a82eab8abf7a01f84bce954edc7462f87a2 /pkg | |
parent | Removed useless user model code (diff) | |
download | trains-3c5e31b25a53268b413bc1e511b7486a2a1c80b9.tar.gz trains-3c5e31b25a53268b413bc1e511b7486a2a1c80b9.tar.bz2 trains-3c5e31b25a53268b413bc1e511b7486a2a1c80b9.zip |
Renamed TrainStop to simply Stop
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/database/migrations.go | 2 | ||||
-rw-r--r-- | pkg/database/stop.go (renamed from pkg/database/train_stop.go) | 10 | ||||
-rw-r--r-- | pkg/database/stop_test.go (renamed from pkg/database/train_stop_test.go) | 44 | ||||
-rw-r--r-- | pkg/model/stop.go (renamed from pkg/model/train_stop.go) | 2 | ||||
-rw-r--r-- | pkg/navitia_api_client/client.go | 2 | ||||
-rw-r--r-- | pkg/navitia_api_client/stops.go (renamed from pkg/navitia_api_client/train_stops.go) | 18 | ||||
-rw-r--r-- | pkg/navitia_api_client/stops_test.go (renamed from pkg/navitia_api_client/train_stops_test.go) | 18 |
7 files changed, 48 insertions, 48 deletions
diff --git a/pkg/database/migrations.go b/pkg/database/migrations.go index ada638a..c2f35dc 100644 --- a/pkg/database/migrations.go +++ b/pkg/database/migrations.go @@ -23,7 +23,7 @@ var allMigrations = []func(tx *sql.Tx) error{ created_at DATE DEFAULT (datetime('now')), FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE ); - CREATE TABLE train_stops ( + CREATE TABLE stops ( id TEXT PRIMARY KEY, name TEXT NOT NULL );` diff --git a/pkg/database/train_stop.go b/pkg/database/stop.go index ee7be00..519b9aa 100644 --- a/pkg/database/train_stop.go +++ b/pkg/database/stop.go @@ -4,8 +4,8 @@ import ( "git.adyxax.org/adyxax/trains/pkg/model" ) -func (env *DBEnv) CountTrainStops() (i int, err error) { - query := `SELECT count(*) from train_stops;` +func (env *DBEnv) CountStops() (i int, err error) { + query := `SELECT count(*) from stops;` err = env.db.QueryRow(query).Scan(&i) if err != nil { return 0, newQueryError("Could not run database query: most likely the schema is corrupted", err) @@ -13,10 +13,10 @@ func (env *DBEnv) CountTrainStops() (i int, err error) { return } -func (env *DBEnv) ReplaceAndImportTrainStops(trainStops []model.TrainStop) error { - pre_query := `DELETE FROM train_stops;` +func (env *DBEnv) ReplaceAndImportStops(trainStops []model.Stop) error { + pre_query := `DELETE FROM stops;` query := ` - INSERT INTO train_stops + INSERT INTO stops (id, name) VALUES ($1, $2);` diff --git a/pkg/database/train_stop_test.go b/pkg/database/stop_test.go index b3f9459..7329ac7 100644 --- a/pkg/database/train_stop_test.go +++ b/pkg/database/stop_test.go @@ -10,46 +10,46 @@ import ( "github.com/stretchr/testify/require" ) -func TestCountTrainStops(t *testing.T) { - trainStops := []model.TrainStop{ - model.TrainStop{Id: "id1", Name: "name1"}, - model.TrainStop{Id: "id2", Name: "name2"}, +func TestCountStops(t *testing.T) { + trainStops := []model.Stop{ + model.Stop{Id: "id1", Name: "name1"}, + model.Stop{Id: "id2", Name: "name2"}, } // test db setup db, err := InitDB("sqlite3", "file::memory:?_foreign_keys=on") require.NoError(t, err) // check sql error - i, err := db.CountTrainStops() + i, err := db.CountStops() require.Error(t, err) assert.Equalf(t, reflect.TypeOf(err), reflect.TypeOf(&QueryError{}), "Invalid error type. Got %s but expected %s", reflect.TypeOf(err), reflect.TypeOf(&QueryError{})) // normal check err = db.Migrate() require.NoError(t, err) - err = db.ReplaceAndImportTrainStops(trainStops) - i, err = db.CountTrainStops() + err = db.ReplaceAndImportStops(trainStops) + i, err = db.CountStops() require.NoError(t, err) assert.Equal(t, i, len(trainStops)) } -func TestReplaceAndImportTrainStops(t *testing.T) { +func TestReplaceAndImportStops(t *testing.T) { // test db setup db, err := InitDB("sqlite3", "file::memory:?_foreign_keys=on") require.NoError(t, err) err = db.Migrate() require.NoError(t, err) // datasets - data1 := []model.TrainStop{ - model.TrainStop{Id: "first", Name: "firstName"}, - model.TrainStop{Id: "second", Name: "secondName"}, + data1 := []model.Stop{ + model.Stop{Id: "first", Name: "firstName"}, + model.Stop{Id: "second", Name: "secondName"}, } - data2 := []model.TrainStop{ - model.TrainStop{Id: "first", Name: "firstTest"}, - model.TrainStop{Id: "secondTest", Name: "secondTest"}, - model.TrainStop{Id: "thirdTest", Name: "thirdTest"}, + data2 := []model.Stop{ + model.Stop{Id: "first", Name: "firstTest"}, + model.Stop{Id: "secondTest", Name: "secondTest"}, + model.Stop{Id: "thirdTest", Name: "thirdTest"}, } testCases := []struct { name string - input []model.TrainStop + input []model.Stop expectedError interface{} }{ {"Normal insert", data1, nil}, @@ -57,7 +57,7 @@ func TestReplaceAndImportTrainStops(t *testing.T) { } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - err := db.ReplaceAndImportTrainStops(tc.input) + err := db.ReplaceAndImportStops(tc.input) 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)) @@ -68,11 +68,11 @@ func TestReplaceAndImportTrainStops(t *testing.T) { } } -func TestReplaceAndImportTrainStopsWithSQLMock(t *testing.T) { +func TestReplaceAndImportStopsWithSQLMock(t *testing.T) { // datasets - data1 := []model.TrainStop{ - model.TrainStop{Id: "first", Name: "firstName"}, - model.TrainStop{Id: "second", Name: "secondName"}, + data1 := []model.Stop{ + model.Stop{Id: "first", Name: "firstName"}, + model.Stop{Id: "second", Name: "secondName"}, } // Transaction begin error dbBeginError, _, err := sqlmock.New() @@ -118,7 +118,7 @@ func TestReplaceAndImportTrainStopsWithSQLMock(t *testing.T) { } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - err := tc.db.ReplaceAndImportTrainStops(data1) + err := tc.db.ReplaceAndImportStops(data1) 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)) diff --git a/pkg/model/train_stop.go b/pkg/model/stop.go index e5b1f6e..d6522e3 100644 --- a/pkg/model/train_stop.go +++ b/pkg/model/stop.go @@ -1,6 +1,6 @@ package model -type TrainStop struct { +type Stop struct { Id string Name string } diff --git a/pkg/navitia_api_client/client.go b/pkg/navitia_api_client/client.go index ccd7198..2ca0e5d 100644 --- a/pkg/navitia_api_client/client.go +++ b/pkg/navitia_api_client/client.go @@ -11,7 +11,7 @@ import ( type Client interface { GetDepartures(trainStop string) (departures []model.Departure, err error) - GetTrainStops() (trainStops []model.TrainStop, err error) + GetStops() (trainStops []model.Stop, err error) } type NavitiaClient struct { diff --git a/pkg/navitia_api_client/train_stops.go b/pkg/navitia_api_client/stops.go index a31ccf7..f0cbcf8 100644 --- a/pkg/navitia_api_client/train_stops.go +++ b/pkg/navitia_api_client/stops.go @@ -8,7 +8,7 @@ import ( "git.adyxax.org/adyxax/trains/pkg/model" ) -type TrainStopsResponse struct { +type StopsResponse struct { Pagination struct { StartPage int `json:"start_page"` ItemsOnPage int `json:"items_on_page"` @@ -31,11 +31,11 @@ type TrainStopsResponse struct { Context interface{} `json:"context"` } -func (c *NavitiaClient) GetTrainStops() (trainStops []model.TrainStop, err error) { - return getTrainStopsPage(c, 0) +func (c *NavitiaClient) GetStops() (trainStops []model.Stop, err error) { + return getStopsPage(c, 0) } -func getTrainStopsPage(c *NavitiaClient, i int) (trainStops []model.TrainStop, err error) { +func getStopsPage(c *NavitiaClient, i int) (trainStops []model.Stop, err error) { request := fmt.Sprintf("%s/coverage/sncf/stop_areas?count=1000&start_page=%d", c.baseURL, i) req, err := http.NewRequest("GET", request, nil) if err != nil { @@ -47,24 +47,24 @@ func getTrainStopsPage(c *NavitiaClient, i int) (trainStops []model.TrainStop, e } defer resp.Body.Close() if resp.StatusCode == http.StatusOK { - var data TrainStopsResponse + var data StopsResponse if err = json.NewDecoder(resp.Body).Decode(&data); err != nil { - return nil, newJsonDecodeError("GetTrainStops ", err) + return nil, newJsonDecodeError("GetStops ", err) } for i := 0; i < len(data.StopAreas); i++ { if data.StopAreas[i].Label != "" { - trainStops = append(trainStops, model.TrainStop{data.StopAreas[i].ID, data.StopAreas[i].Label}) + trainStops = append(trainStops, model.Stop{data.StopAreas[i].ID, data.StopAreas[i].Label}) } } if data.Pagination.ItemsOnPage+data.Pagination.ItemsPerPage*data.Pagination.StartPage < data.Pagination.TotalResult { - tss, err := getTrainStopsPage(c, i+1) + tss, err := getStopsPage(c, i+1) if err != nil { return nil, err } trainStops = append(trainStops, tss...) } } else { - err = newApiError(resp.StatusCode, "GetTrainStops") + err = newApiError(resp.StatusCode, "GetStops") } return } diff --git a/pkg/navitia_api_client/train_stops_test.go b/pkg/navitia_api_client/stops_test.go index 9e1c982..0b94765 100644 --- a/pkg/navitia_api_client/train_stops_test.go +++ b/pkg/navitia_api_client/stops_test.go @@ -11,12 +11,12 @@ import ( "github.com/stretchr/testify/require" ) -func TestGetTrainStops(t *testing.T) { +func TestGetStops(t *testing.T) { // Simple Test cases testCases := []struct { name string inputNewCLient string - expected []model.TrainStop + expected []model.Stop expectedError interface{} }{ {"invalid characters in token should fail", "}", nil, &HttpClientError{}}, @@ -25,7 +25,7 @@ func TestGetTrainStops(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { client := NewClient(tc.inputNewCLient) - valid, err := client.GetTrainStops() + valid, err := client.GetStops() 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)) @@ -40,7 +40,7 @@ func TestGetTrainStops(t *testing.T) { testCasesFilename := []struct { name string inputFilename string - expected []model.TrainStop + expected []model.Stop expectedError interface{} }{ {"invalid json should fail", "test_data/invalid.json", nil, &JsonDecodeError{}}, @@ -49,7 +49,7 @@ func TestGetTrainStops(t *testing.T) { t.Run(tc.name, func(t *testing.T) { client, ts := newTestClientFromFilename(t, tc.inputFilename) defer ts.Close() - valid, err := client.GetTrainStops() + valid, err := client.GetStops() 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)) @@ -65,14 +65,14 @@ func TestGetTrainStops(t *testing.T) { w.WriteHeader(http.StatusNotFound) })) client := newTestClient(ts) - _, err := client.GetTrainStops() + _, err := client.GetStops() 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() + trainStops, err := client.GetStops() if err != nil { t.Fatalf("could not get train stops : %s", err) } @@ -87,7 +87,7 @@ func TestGetTrainStops(t *testing.T) { 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() + trainStops, err = client.GetStops() if err != nil { t.Fatalf("could not get train stops : %+v", err) } @@ -101,7 +101,7 @@ func TestGetTrainStops(t *testing.T) { 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() + trainStops, err = client.GetStops() if err == nil { t.Fatalf("should not be able to get train stops : %+v", trainStops) } |