Skip to content

Commit

Permalink
added postgres tests into the postgres driver
Browse files Browse the repository at this point in the history
  • Loading branch information
Kansuler committed Dec 22, 2023
1 parent bf4e293 commit 8025817
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 107 deletions.
4 changes: 2 additions & 2 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ services:
POSTGRES_DB: testdb
ports:
- "5432:5432"
test_postgres:
test:
image: golang:1.21
environment:
DSN: postgresql://user:password@postgres:5432/testdb?sslmode=disable&connect_timeout=10
Expand All @@ -20,4 +20,4 @@ services:
source: ./
target: /workspace
working_dir: /workspace
command: ["go", "test", "-count=1", "-v", "./example/postgres"]
command: ["go", "test", "-count=1", "-v", "./driver/..."]
2 changes: 2 additions & 0 deletions driver/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/jackc/pgx/v5/pgconn"
)

type Driver octobe.Driver[postgres, config, Builder]

// postgres holds the connection pool and default configuration for the postgres driver
type postgres struct {
pool *pgx.Conn
Expand Down
105 changes: 104 additions & 1 deletion driver/postgres/postgres_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,104 @@
package postgres
package postgres_test

import (
"context"
"github.com/Kansuler/octobe/v2"
"github.com/Kansuler/octobe/v2/driver/postgres"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"os"
"testing"
)

func TestPostgres(t *testing.T) {
ctx := context.Background()
dsn := os.Getenv("DSN")
if dsn == "" {
panic("DSN is not set")
}

ob, err := octobe.New(postgres.Open(ctx, dsn, postgres.WithTransaction(postgres.TxOptions{})))
if !assert.NoError(t, err) {
t.FailNow()
}

session, err := ob.Begin(context.Background())
if !assert.NoError(t, err) {
t.FailNow()
}

defer session.WatchRollback(func() error {
return err
})

_, err = postgres.Execute(session, Migration())
if !assert.NoError(t, err) {
t.FailNow()
}

name := uuid.New().String()
product1, err := postgres.Execute(session, AddProduct(name))
if !assert.NoError(t, err) {
t.FailNow()
}

assert.Equal(t, name, product1.Name)
assert.NotZero(t, product1.ID)

product2, err := postgres.Execute(session, ProductByName(name))
if !assert.NoError(t, err) {
t.FailNow()
}

assert.Equal(t, name, product2.Name)
assert.NotZero(t, product2.ID)

err = session.Commit()
if !assert.NoError(t, err) {
t.FailNow()
}
}

func Migration() postgres.Handler[octobe.Void] {
return func(builder postgres.Builder) (octobe.Void, error) {
query := builder(`
CREATE TABLE IF NOT EXISTS products (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL
);
`)
_, err := query.Exec()
return nil, err
}
}

type Product struct {
ID int
Name string
}

func AddProduct(name string) postgres.Handler[Product] {
return func(builder postgres.Builder) (Product, error) {
var product Product
query := builder(`
INSERT INTO products (name) VALUES ($1) RETURNING id, name;
`)

query.Arguments(name)
err := query.QueryRow(&product.ID, &product.Name)
return product, err
}
}

func ProductByName(name string) postgres.Handler[Product] {
return func(builder postgres.Builder) (Product, error) {
var product Product
query := builder(`
SELECT id, name FROM products WHERE name = $1;
`)

query.Arguments(name)
err := query.QueryRow(&product.ID, &product.Name)
return product, err
}
}
49 changes: 27 additions & 22 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,49 @@ module github.com/Kansuler/octobe/v2
go 1.21.5

require (
cloud.google.com/go/firestore v1.14.0
github.com/google/uuid v1.5.0
github.com/googleapis/gax-go/v2 v2.12.0
github.com/jackc/pgx/v5 v5.5.1
github.com/stretchr/testify v1.8.4
google.golang.org/api v0.154.0
google.golang.org/grpc v1.60.1
)

require (
cloud.google.com/go v0.110.2 // indirect
cloud.google.com/go/compute v1.19.3 // indirect
cloud.google.com/go/compute v1.23.3 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/firestore v1.14.0 // indirect
cloud.google.com/go/longrunning v0.5.0 // indirect
cloud.google.com/go/longrunning v0.5.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.3.0 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/s2a-go v0.1.4 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.4 // indirect
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
github.com/google/s2a-go v0.1.7 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/kr/text v0.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/oauth2 v0.8.0 // indirect
golang.org/x/sync v0.2.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/api v0.128.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230530153820-e85fd2cbaebc // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230530153820-e85fd2cbaebc // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc // indirect
google.golang.org/grpc v1.56.1 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.46.1 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.1 // indirect
go.opentelemetry.io/otel v1.21.0 // indirect
go.opentelemetry.io/otel/metric v1.21.0 // indirect
go.opentelemetry.io/otel/trace v1.21.0 // indirect
golang.org/x/crypto v0.16.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/oauth2 v0.15.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto v0.0.0-20231120223509-83a465c0220f // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20231120223509-83a465c0220f // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231127180814-3a041ad873d4 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 8025817

Please sign in to comment.