carpool-service/storage/postgresql_test.go

135 lines
4.2 KiB
Go
Raw Normal View History

package storage
import (
"fmt"
"testing"
"github.com/paulmach/orb/geojson"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"
)
var cfg *viper.Viper
func init() {
cfg = viper.New()
cfg.SetDefault("storage.db.psql.host", "localhost")
cfg.SetDefault("storage.db.psql.port", "5432")
cfg.SetDefault("storage.db.psql.user", "postgres")
cfg.SetDefault("storage.db.psql.password", "postgres")
cfg.SetDefault("storage.db.psql.dbname", "carpool_service_tests")
cfg.SetDefault("storage.db.psql.sslmode", "disable")
cfg.SetDefault("storage.db.psql.schema", "carpool_service")
cfg.SetDefault("storage.db.psql.tables.regular_routes", "regular_routes")
cfg.SetDefault("storage.db.psql.tables.regular_route_schedules", "regular_route_schedules")
cfg.SetDefault("storage.db.psql.tables.punctual_routes", "punctual_routes")
cfg.SetDefault("storage.db.psql.tables.bookings", "bookings")
cfg.SetDefault("storage.db.psql.tables.journeys_cache", "journeys_cache")
cfg.SetConfigName("config") // Override default values in a config.yaml file within this directory
cfg.AddConfigPath(".")
cfg.ReadInConfig()
}
func TestPostgresqlStorage_Initialize(t *testing.T) {
storage, err := NewPostgresqlStorage(cfg)
require.NoError(t, err)
defer storage.DbConnection.Close()
err = storage.Migrate()
require.NoError(t, err)
_, err = storage.DbConnection.Exec(fmt.Sprintf("DELETE FROM %s;", storage.Tables["regular_routes"]))
require.NoError(t, err)
_, err = storage.DbConnection.Exec(fmt.Sprintf("DELETE FROM %s;", storage.Tables["regular_route_schedules"]))
require.NoError(t, err)
// _, err = storage.DbConnection.Exec(fmt.Sprintf("DELETE FROM %s;", storage.Tables["punctual_routes"]))
// require.NoError(t, err)
_, err = storage.DbConnection.Exec(fmt.Sprintf("DELETE FROM %s;", storage.Tables["journeys_cache"]))
require.NoError(t, err)
}
func TestPostgresqlStorage_CreateRegularRoutes(t *testing.T) {
storage, err := NewPostgresqlStorage(cfg)
require.NoError(t, err)
defer storage.DbConnection.Close()
fc1, err := geojson.UnmarshalFeatureCollection([]byte(regularroute1))
require.NoError(t, err)
fc2, err := geojson.UnmarshalFeatureCollection([]byte(regularroute2))
require.NoError(t, err)
fc3, err := geojson.UnmarshalFeatureCollection([]byte(regularroute3))
require.NoError(t, err)
fc4, err := geojson.UnmarshalFeatureCollection([]byte(regularroute4))
require.NoError(t, err)
fc5, err := geojson.UnmarshalFeatureCollection([]byte(regularroute5))
require.NoError(t, err)
fc6, err := geojson.UnmarshalFeatureCollection([]byte(regularroute6))
require.NoError(t, err)
err = storage.CreateRegularRoutes([]*geojson.FeatureCollection{fc1, fc2, fc3, fc4, fc5, fc6})
require.NoError(t, err)
}
func TestPostgresqlStorage_GetUserRegularRoutes(t *testing.T) {
storage, err := NewPostgresqlStorage(cfg)
require.NoError(t, err)
defer storage.DbConnection.Close()
results, err := storage.GetUserRegularRoutes("ca435b62-3162-4c79-983a-2a0f4fb16aa0")
require.NoError(t, err)
require.Len(t, results, 2)
}
func TestPostgresqlStorage_GetRegularRoutesForTile(t *testing.T) {
storage, err := NewPostgresqlStorage(cfg)
require.NoError(t, err)
defer storage.DbConnection.Close()
results, err := storage.GetDriverRegularRoutesForTile("MON", 48067)
require.NoError(t, err)
require.Len(t, results, 3)
results, err = storage.GetPassengerRegularRoutesForTile("MON", 48067)
require.NoError(t, err)
require.Len(t, results, 3)
}
func TestPostgresqlStorage_CreateBooking(t *testing.T) {
storage, err := NewPostgresqlStorage(cfg)
require.NoError(t, err)
defer storage.DbConnection.Close()
err = storage.CreateBooking(booking1)
require.NoError(t, err)
err = storage.CreateBooking(booking2)
require.NoError(t, err)
err = storage.CreateBooking(booking3)
require.NoError(t, err)
}
func TestPostgresqlStorage_GetBooking(t *testing.T) {
storage, err := NewPostgresqlStorage(cfg)
require.NoError(t, err)
defer storage.DbConnection.Close()
_, err = storage.GetBooking(booking1id)
require.NoError(t, err)
}
func TestPostgresqlStorage_GetUserBookings(t *testing.T) {
storage, err := NewPostgresqlStorage(cfg)
require.NoError(t, err)
defer storage.DbConnection.Close()
results, err := storage.GetUserBookings(bookinguser1)
require.NoError(t, err)
require.Len(t, results, 2)
}