package storage import ( "fmt" "testing" "time" _ "github.com/lib/pq" "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", "mobilityaccounts_tests") cfg.SetDefault("storage.db.psql.sslmode", "disable") cfg.SetDefault("storage.db.psql.schema", "carpool_incentives") cfg.SetDefault("storage.db.psql.tables.incentive_subscriptions", "incentive_subscriptions") 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["incentive_subscriptions"])) require.NoError(t, err) } func TestPostgresqlStorage_SubscribeIncentive(t *testing.T) { storage, err := NewPostgresqlStorage(cfg) require.NoError(t, err) defer storage.DbConnection.Close() err = storage.SubscribeIncentive(is1) require.NoError(t, err) err = storage.SubscribeIncentive(is2) require.NoError(t, err) err = storage.SubscribeIncentive(is3) require.NoError(t, err) res, err := storage.GetUserSubscriptions(testid) require.NoError(t, err) require.Len(t, res, 2) for _, is := range res { require.Contains(t, []string{is1.ID, is3.ID}, is.ID) } } func TestPostgresqlStorage_GetSubscriptions(t *testing.T) { storage, err := NewPostgresqlStorage(cfg) require.NoError(t, err) defer storage.DbConnection.Close() res, err := storage.GetUserSubscriptions(is2.UserID) require.NoError(t, err) require.Len(t, res, 1) fmt.Println(res[0]) fmt.Println(is2) require.Equal(t, is2.ID, res[0].ID) require.Equal(t, is2.IncentiveID, res[0].IncentiveID) require.Equal(t, is2.UserID, res[0].UserID) require.Equal(t, is2.Data, res[0].Data) require.Equal(t, is2.Declined, res[0].Declined) require.Equal(t, is2.IdentityVerificationIDs, res[0].IdentityVerificationIDs) require.WithinDuration(t, is2.SubscriptionDatetime, res[0].SubscriptionDatetime, time.Millisecond) }