Skip to content

Commit

Permalink
Merge pull request #19 from Alviner/hotfix/pgSchemas
Browse files Browse the repository at this point in the history
feat: add postges schema
  • Loading branch information
Alviner authored Apr 13, 2024
2 parents 0840e84 + b99bbdd commit e8dd1ed
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 24 deletions.
11 changes: 5 additions & 6 deletions tempdb/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,23 @@ type Querier interface {
DeleteDatabase(ctx context.Context, name string) error
}

func New(dbUrl string) (Querier, error) {
parsedUrl, err := url.Parse(dbUrl)
func New(dbURL string) (Querier, error) {
parsedURL, err := url.Parse(dbURL)
if err != nil {
return nil, err
}
querier, err := querierFromUrl(parsedUrl)
querier, err := querierFromURL(parsedURL)
if err != nil {
return nil, fmt.Errorf("cannot create querier: %w", err)
}
return querier, nil

}

func querierFromUrl(url *url.URL) (Querier, error) {
func querierFromURL(url *url.URL) (Querier, error) {
dialect := url.Scheme

switch dialect {
case "postgresql":
case "postgresql", "postgres":
q, err := postgres.New(url)
if err != nil {
return nil, fmt.Errorf("cannot create postgres: %w", err)
Expand Down
19 changes: 13 additions & 6 deletions tempdb/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package tempdb
import (
"testing"

"github.com/Alviner/drillerfy/tempdb/postgres"
"github.com/Alviner/drillerfy/utils_test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/Alviner/drillerfy/tempdb/postgres"
)

func TestCreate(t *testing.T) {
Expand All @@ -15,17 +15,25 @@ func TestCreate(t *testing.T) {
require := require.New(t)
assert := assert.New(t)

t.Run("postgresql", func(t *testing.T) {
t.Parallel()
// act
db, err := New("postgresql://pguser:pgpass@localhost:5432/pgdb")
require.NoError(err)
// assert
assert.IsType(new(postgres.Postgres), db)
})
t.Run("postgres", func(t *testing.T) {
t.Parallel()
//act
db, err := New(utils_test.PostgresDNS(t))
// act
db, err := New("postgres://pguser:pgpass@localhost:5432/pgdb")
require.NoError(err)
// assert
assert.IsType(new(postgres.Postgres), db)
})
t.Run("unknown", func(t *testing.T) {
t.Parallel()
//act
// act
_, err := New("unknown://database.url")
// assert
require.EqualErrorf(
Expand All @@ -35,5 +43,4 @@ func TestCreate(t *testing.T) {
"unknown",
)
})

}
25 changes: 13 additions & 12 deletions tempdb/postgres/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
"testing"
"time"

"github.com/Alviner/drillerfy/utils_test"
"github.com/Alviner/drillerfy/utils_test/postgres"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/Alviner/drillerfy/utils_test"
"github.com/Alviner/drillerfy/utils_test/postgres"
)

func TestPostgres(t *testing.T) {
Expand All @@ -19,7 +20,7 @@ func TestPostgres(t *testing.T) {
require := require.New(t)
assert := assert.New(t)

dbUrl, err := url.Parse(utils_test.PostgresDNS(t))
dbURL, err := url.Parse(utils_test.PostgresDNS(t))
require.NoError(err)

t.Run("create", func(t *testing.T) {
Expand All @@ -29,13 +30,13 @@ func TestPostgres(t *testing.T) {
ctx, done := context.WithTimeout(context.Background(), 2*time.Second)
defer done()

pg, err := New(dbUrl)
pg, err := New(dbURL)
require.NoError(err)
//act
// act
require.NoError(pg.CreateDatabase(ctx, dbName, ""))
defer func() { require.NoError(pg.DeleteDatabase(ctx, dbName)) }()
// assert
names, err := postgres.DBNames(t, dbUrl)
names, err := postgres.DBNames(t, dbURL)
require.NoError(err)
assert.Contains(names, dbName)
})
Expand All @@ -47,17 +48,17 @@ func TestPostgres(t *testing.T) {
ctx, done := context.WithTimeout(context.Background(), 5*time.Second)
defer done()

pg, err := New(dbUrl)
pg, err := New(dbURL)
require.NoError(err)

require.NoError(pg.CreateDatabase(ctx, templateName, ""))
defer func() { require.NoError(pg.DeleteDatabase(ctx, templateName)) }()
//act
// act
require.NoError(pg.CreateDatabase(ctx, dbName, templateName))
defer func() { require.NoError(pg.DeleteDatabase(ctx, dbName)) }()

// assert
names, err := postgres.DBNames(t, dbUrl)
names, err := postgres.DBNames(t, dbURL)
require.NoError(err)
assert.Contains(names, dbName)
})
Expand All @@ -69,15 +70,15 @@ func TestPostgres(t *testing.T) {
ctx, done := context.WithTimeout(context.Background(), 5*time.Second)
defer done()

pg, err := New(dbUrl)
pg, err := New(dbURL)
require.NoError(err)

require.NoError(pg.CreateDatabase(ctx, dbName, ""))
//act
// act
require.NoError(pg.DeleteDatabase(ctx, dbName))

// assert
names, err := postgres.DBNames(t, dbUrl)
names, err := postgres.DBNames(t, dbURL)
require.NoError(err)
assert.NotContains(names, dbName)
})
Expand Down

0 comments on commit e8dd1ed

Please sign in to comment.