first working version
This commit is contained in:
7
test/Makefile
Normal file
7
test/Makefile
Normal file
@@ -0,0 +1,7 @@
|
||||
.PHONY: test
|
||||
|
||||
test:
|
||||
docker run --rm -d -e POSTGRES_PASSWORD=postgres --name test-postgres -p 5432:5432 postgres:15.6-bookworm
|
||||
sleep 10
|
||||
PGX_DSN=postgres://postgres:postgres@127.0.0.1:5432 go test -v -cover
|
||||
docker stop test-postgres
|
18
test/go.mod
Normal file
18
test/go.mod
Normal file
@@ -0,0 +1,18 @@
|
||||
module pkgtest
|
||||
|
||||
go 1.24.2
|
||||
|
||||
require (
|
||||
code.uint32.ru/tiny/repo v0.0.0-20250504181319-b6dc6d3fce1a
|
||||
github.com/jackc/pgx/v5 v5.7.4
|
||||
github.com/mattn/go-sqlite3 v1.14.28
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
|
||||
github.com/jackc/puddle/v2 v2.2.2 // indirect
|
||||
golang.org/x/crypto v0.31.0 // indirect
|
||||
golang.org/x/sync v0.10.0 // indirect
|
||||
golang.org/x/text v0.21.0 // indirect
|
||||
)
|
32
test/go.sum
Normal file
32
test/go.sum
Normal file
@@ -0,0 +1,32 @@
|
||||
code.uint32.ru/tiny/repo v0.0.0-20250504181319-b6dc6d3fce1a h1:xlO71KbJ4fl3F05JN/I8rY3MjVF0CV3B1awa4TB6GN0=
|
||||
code.uint32.ru/tiny/repo v0.0.0-20250504181319-b6dc6d3fce1a/go.mod h1:yk97QS1fB9mdT/5iRN4ufo6IwZRDYHFl1kDtBSTpuk0=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
|
||||
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
|
||||
github.com/jackc/pgx/v5 v5.7.4 h1:9wKznZrhWa2QiHL+NjTSPP6yjl3451BX3imWDnokYlg=
|
||||
github.com/jackc/pgx/v5 v5.7.4/go.mod h1:ncY89UGWxg82EykZUwSpUKEfccBGGYq1xjrOpsbsfGQ=
|
||||
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
|
||||
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
|
||||
github.com/mattn/go-sqlite3 v1.14.28 h1:ThEiQrnbtumT+QMknw63Befp/ce/nUPgBPMlRFEum7A=
|
||||
github.com/mattn/go-sqlite3 v1.14.28/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
|
||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
|
||||
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
|
||||
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
|
||||
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
|
||||
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
159
test/repo_test.go
Normal file
159
test/repo_test.go
Normal file
@@ -0,0 +1,159 @@
|
||||
package repo_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/stdlib"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
|
||||
"code.uint32.ru/tiny/repo"
|
||||
)
|
||||
|
||||
type My struct {
|
||||
A string
|
||||
B int
|
||||
}
|
||||
|
||||
func TestSqlite3(t *testing.T) {
|
||||
db, err := sql.Open("sqlite3", "test.db")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
defer os.Remove("test.db")
|
||||
defer db.Close()
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
dotest(ctx, db, t)
|
||||
}
|
||||
|
||||
func TestPGX(t *testing.T) {
|
||||
dsn, ok := os.LookupEnv("PGX_DSN")
|
||||
if !ok {
|
||||
t.SkipNow()
|
||||
}
|
||||
|
||||
conf, err := pgx.ParseConfig(dsn)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
conf.DefaultQueryExecMode = pgx.QueryExecModeSimpleProtocol
|
||||
conf.DescriptionCacheCapacity = 0
|
||||
conf.StatementCacheCapacity = 0
|
||||
|
||||
db, err := sql.Open("pgx", stdlib.RegisterConnConfig(conf))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
defer db.Close()
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
dotest(ctx, db, t)
|
||||
}
|
||||
|
||||
func TestOpenOrCreateFails(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
if _, err := repo.OpenOrCreate[My](ctx, nil, ""); err == nil {
|
||||
t.Errorf("must fail on empty tablename")
|
||||
}
|
||||
|
||||
if _, err := repo.OpenOrCreate[My](ctx, nil, "test"); err == nil {
|
||||
t.Errorf("must fail on nil DB instance")
|
||||
}
|
||||
}
|
||||
|
||||
func dotest(ctx context.Context, db *sql.DB, t *testing.T) {
|
||||
r, err := repo.OpenOrCreate[My](ctx, db, "my_test")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
testID := "watermelon"
|
||||
incorrectID := "surelynonexistant"
|
||||
|
||||
one := &My{
|
||||
A: "hello",
|
||||
B: 42,
|
||||
}
|
||||
|
||||
if err := r.Create(ctx, testID, one); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := r.Create(ctx, testID, one); err == nil {
|
||||
// can not insert with same id
|
||||
t.Errorf("insert of the same id dows not fail")
|
||||
}
|
||||
|
||||
two, err := r.Read(ctx, testID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(one, two) {
|
||||
t.Errorf("instances are not equal")
|
||||
t.Logf("%+v", one)
|
||||
t.Logf("%+v", two)
|
||||
}
|
||||
|
||||
one.B = 13
|
||||
|
||||
if err := r.Update(ctx, testID, one); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
two, err = r.Read(ctx, testID)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(one, two) {
|
||||
t.Errorf("instances are not equal")
|
||||
t.Logf("%+v", one)
|
||||
t.Logf("%+v", two)
|
||||
}
|
||||
|
||||
if err := r.Delete(ctx, testID); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if err := r.Delete(ctx, testID); err == nil || !errors.Is(err, repo.ErrNotFound) {
|
||||
// can only delete once
|
||||
t.Errorf("incorrect error on delete for non-existing id, want: %v, have: %v", repo.ErrNotFound, err)
|
||||
}
|
||||
|
||||
if err := r.Purge(ctx, testID); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if err := r.Purge(ctx, testID); err == nil || !errors.Is(err, repo.ErrNotFound) {
|
||||
// can only purge once
|
||||
t.Errorf("incorrect error on delete for non-existing id, want: %v, have: %v", repo.ErrNotFound, err)
|
||||
}
|
||||
|
||||
if _, err := r.Read(ctx, incorrectID); err == nil || !errors.Is(err, repo.ErrNotFound) {
|
||||
t.Errorf("incorrect error on read for non-existing id, want: %v, have: %v", repo.ErrNotFound, err)
|
||||
}
|
||||
|
||||
if err := r.Update(ctx, incorrectID, one); err == nil || !errors.Is(err, repo.ErrNotFound) {
|
||||
t.Errorf("incorrect error on update for non-existing id, want: %v, have: %v", repo.ErrNotFound, err)
|
||||
}
|
||||
|
||||
if err := r.Delete(ctx, incorrectID); err == nil || !errors.Is(err, repo.ErrNotFound) {
|
||||
t.Errorf("incorrect error on delete for non-existing id, want: %v, have: %v", repo.ErrNotFound, err)
|
||||
}
|
||||
|
||||
if err := r.Purge(ctx, incorrectID); err == nil || !errors.Is(err, repo.ErrNotFound) {
|
||||
t.Errorf("incorrect error on purge for non-existing id, want: %v, have: %v", repo.ErrNotFound, err)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user