aboutsummaryrefslogtreecommitdiff
path: root/pkg/database
diff options
context:
space:
mode:
authorJulien Dessaux2021-09-08 15:23:50 +0200
committerJulien Dessaux2021-09-08 16:12:01 +0200
commit3c5e31b25a53268b413bc1e511b7486a2a1c80b9 (patch)
treef6fe9a82eab8abf7a01f84bce954edc7462f87a2 /pkg/database
parentRemoved useless user model code (diff)
downloadtrains-3c5e31b25a53268b413bc1e511b7486a2a1c80b9.tar.gz
trains-3c5e31b25a53268b413bc1e511b7486a2a1c80b9.tar.bz2
trains-3c5e31b25a53268b413bc1e511b7486a2a1c80b9.zip
Renamed TrainStop to simply Stop
Diffstat (limited to '')
-rw-r--r--pkg/database/migrations.go2
-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
3 files changed, 28 insertions, 28 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))