Rename GormReadWriter -> Db
This commit is contained in:
1 parent
ee4a0d3f45
commit
670abaed88
6 files changed
+90
-90
No files matched your search
@@ -6,8 +6,8 @@ Just some high-level usage highlights to get you started. Read the godocs for a
|
||||
```go
|
||||
conn, _ := gorm.Open("postgres", url)
|
||||
|
||||
// GormReadWriter implements both the Reader and Writer interfaces
|
||||
rw := GormReadWriter{Tx: conn}
|
||||
// Db implements both the Reader and Writer interfaces
|
||||
rw := Db{Tx: conn}
|
||||
|
||||
// There are writer methods like: Create, Update and Delete
|
||||
// that will write Gorm struct to the db. These writer methods
|
||||
@@ -64,4 +64,4 @@ Just some high-level usage highlights to get you started. Read the godocs for a
|
||||
})
|
||||
|
||||
|
||||
```
|
||||
```
|
||||
+18
-18
@@ -107,17 +107,17 @@ type VetForWriter interface {
|
||||
VetForWrite(ctx context.Context, r Reader, opType OpType, opt ...Option) error
|
||||
}
|
||||
|
||||
// GormReadWriter uses a gorm DB connection for read/write
|
||||
type GormReadWriter struct {
|
||||
// Db uses a gorm DB connection for read/write
|
||||
type Db struct {
|
||||
Tx *gorm.DB
|
||||
}
|
||||
|
||||
// ensure that GormReadWriter implements the interfaces of: Reader and Writer
|
||||
var _ Reader = (*GormReadWriter)(nil)
|
||||
var _ Writer = (*GormReadWriter)(nil)
|
||||
// ensure that Db implements the interfaces of: Reader and Writer
|
||||
var _ Reader = (*Db)(nil)
|
||||
var _ Writer = (*Db)(nil)
|
||||
|
||||
// DB returns the sql.DB
|
||||
func (rw *GormReadWriter) DB() (*sql.DB, error) {
|
||||
func (rw *Db) DB() (*sql.DB, error) {
|
||||
if rw.Tx == nil {
|
||||
return nil, errors.New("create tx is nil for db")
|
||||
}
|
||||
@@ -125,13 +125,13 @@ func (rw *GormReadWriter) DB() (*sql.DB, error) {
|
||||
}
|
||||
|
||||
// Scan rows will scan the rows into the interface
|
||||
func (rw *GormReadWriter) ScanRows(rows *sql.Rows, result interface{}) error {
|
||||
func (rw *Db) ScanRows(rows *sql.Rows, result interface{}) error {
|
||||
return rw.Tx.ScanRows(rows, result)
|
||||
}
|
||||
|
||||
var ErrNotResourceWithId = errors.New("not a resource with an id")
|
||||
|
||||
func (rw *GormReadWriter) lookupAfterWrite(ctx context.Context, i interface{}, opt ...Option) error {
|
||||
func (rw *Db) lookupAfterWrite(ctx context.Context, i interface{}, opt ...Option) error {
|
||||
opts := GetOpts(opt...)
|
||||
withLookup := opts.withLookup
|
||||
|
||||
@@ -148,7 +148,7 @@ func (rw *GormReadWriter) lookupAfterWrite(ctx context.Context, i interface{}, o
|
||||
}
|
||||
|
||||
// Create an object in the db with options: WithOplog and WithLookup (to force a lookup after create))
|
||||
func (rw *GormReadWriter) Create(ctx context.Context, i interface{}, opt ...Option) error {
|
||||
func (rw *Db) Create(ctx context.Context, i interface{}, opt ...Option) error {
|
||||
if rw.Tx == nil {
|
||||
return errors.New("create tx is nil")
|
||||
}
|
||||
@@ -183,7 +183,7 @@ func (rw *GormReadWriter) Create(ctx context.Context, i interface{}, opt ...Opti
|
||||
|
||||
// Update an object in the db, if there's a fieldMask then only the field_mask.proto paths are updated, otherwise
|
||||
// it will send every field to the DB. Update supports embedding a struct (or structPtr) one level deep for updating
|
||||
func (rw *GormReadWriter) Update(ctx context.Context, i interface{}, fieldMaskPaths []string, opt ...Option) error {
|
||||
func (rw *Db) Update(ctx context.Context, i interface{}, fieldMaskPaths []string, opt ...Option) error {
|
||||
if rw.Tx == nil {
|
||||
return errors.New("update tx is nil")
|
||||
}
|
||||
@@ -262,7 +262,7 @@ func (rw *GormReadWriter) Update(ctx context.Context, i interface{}, fieldMaskPa
|
||||
}
|
||||
|
||||
// Delete an object in the db with options: WithOplog (which requires WithMetadata, WithWrapper)
|
||||
func (rw *GormReadWriter) Delete(ctx context.Context, i interface{}, opt ...Option) error {
|
||||
func (rw *Db) Delete(ctx context.Context, i interface{}, opt ...Option) error {
|
||||
if rw.Tx == nil {
|
||||
return errors.New("delete tx is nil")
|
||||
}
|
||||
@@ -287,7 +287,7 @@ func (rw *GormReadWriter) Delete(ctx context.Context, i interface{}, opt ...Opti
|
||||
return nil
|
||||
}
|
||||
|
||||
func (rw *GormReadWriter) addOplog(ctx context.Context, opType OpType, opts Options, i interface{}) error {
|
||||
func (rw *Db) addOplog(ctx context.Context, opType OpType, opts Options, i interface{}) error {
|
||||
oplogArgs := opts.oplogOpts
|
||||
if oplogArgs.wrapper == nil {
|
||||
return errors.New("error wrapper is nil for WithWrapper")
|
||||
@@ -347,7 +347,7 @@ func (rw *GormReadWriter) addOplog(ctx context.Context, opType OpType, opts Opti
|
||||
// you should ensure that any objects written to the db in your TxHandler are retryable, which
|
||||
// means that the object may be sent to the db several times (retried), so things like the primary key must
|
||||
// be reset before retry
|
||||
func (w *GormReadWriter) DoTx(ctx context.Context, retries uint, backOff Backoff, Handler TxHandler) (RetryInfo, error) {
|
||||
func (w *Db) DoTx(ctx context.Context, retries uint, backOff Backoff, Handler TxHandler) (RetryInfo, error) {
|
||||
if w.Tx == nil {
|
||||
return RetryInfo{}, errors.New("do tx is nil")
|
||||
}
|
||||
@@ -357,7 +357,7 @@ func (w *GormReadWriter) DoTx(ctx context.Context, retries uint, backOff Backoff
|
||||
return info, fmt.Errorf("Too many retries: %d of %d", attempts-1, retries+1)
|
||||
}
|
||||
newTx := w.Tx.BeginTx(ctx, nil)
|
||||
if err := Handler(&GormReadWriter{newTx}); err != nil {
|
||||
if err := Handler(&Db{newTx}); err != nil {
|
||||
if errors.Is(err, oplog.ErrTicketAlreadyRedeemed) {
|
||||
newTx.Rollback() // need to still handle rollback errors
|
||||
d := backOff.Duration(attempts)
|
||||
@@ -387,7 +387,7 @@ func (w *GormReadWriter) DoTx(ctx context.Context, retries uint, backOff Backoff
|
||||
}
|
||||
|
||||
// LookupByName will lookup resource my its friendly name which must be unique
|
||||
func (rw *GormReadWriter) LookupByName(ctx context.Context, resource ResourceNamer, opt ...Option) error {
|
||||
func (rw *Db) LookupByName(ctx context.Context, resource ResourceNamer, opt ...Option) error {
|
||||
if rw.Tx == nil {
|
||||
return errors.New("error tx nil for lookup by name")
|
||||
}
|
||||
@@ -413,7 +413,7 @@ func (rw *GormReadWriter) LookupByName(ctx context.Context, resource ResourceNam
|
||||
}
|
||||
|
||||
// LookupByPublicId will lookup resource my its public_id which must be unique
|
||||
func (rw *GormReadWriter) LookupByPublicId(ctx context.Context, resource ResourcePublicIder, opt ...Option) error {
|
||||
func (rw *Db) LookupByPublicId(ctx context.Context, resource ResourcePublicIder, opt ...Option) error {
|
||||
if rw.Tx == nil {
|
||||
return errors.New("error tx nil for lookup by public id")
|
||||
}
|
||||
@@ -439,7 +439,7 @@ func (rw *GormReadWriter) LookupByPublicId(ctx context.Context, resource Resourc
|
||||
}
|
||||
|
||||
// LookupWhere will lookup the first resource using a where clause with parameters (it only returns the first one)
|
||||
func (rw *GormReadWriter) LookupWhere(ctx context.Context, resource interface{}, where string, args ...interface{}) error {
|
||||
func (rw *Db) LookupWhere(ctx context.Context, resource interface{}, where string, args ...interface{}) error {
|
||||
if rw.Tx == nil {
|
||||
return errors.New("error tx nil for lookup by")
|
||||
}
|
||||
@@ -450,7 +450,7 @@ func (rw *GormReadWriter) LookupWhere(ctx context.Context, resource interface{},
|
||||
}
|
||||
|
||||
// SearchWhere will search for all the resources it can find using a where clause with parameters
|
||||
func (rw *GormReadWriter) SearchWhere(ctx context.Context, resources interface{}, where string, args ...interface{}) error {
|
||||
func (rw *Db) SearchWhere(ctx context.Context, resources interface{}, where string, args ...interface{}) error {
|
||||
if rw.Tx == nil {
|
||||
return errors.New("error tx nil for search by")
|
||||
}
|
||||
|
||||
@@ -12,14 +12,14 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestGormReadWriter_Update(t *testing.T) {
|
||||
func TestDb_Update(t *testing.T) {
|
||||
// intentionally not run with t.Parallel so we don't need to use DoTx for the Update tests
|
||||
cleanup, db := TestSetup(t, "postgres")
|
||||
defer cleanup()
|
||||
assert := assert.New(t)
|
||||
defer db.Close()
|
||||
t.Run("simple", func(t *testing.T) {
|
||||
w := GormReadWriter{Tx: db}
|
||||
w := Db{Tx: db}
|
||||
id, err := uuid.GenerateUUID()
|
||||
assert.Nil(err)
|
||||
user, err := db_test.NewTestUser()
|
||||
@@ -45,7 +45,7 @@ func TestGormReadWriter_Update(t *testing.T) {
|
||||
assert.Equal(foundUser.Name, user.Name)
|
||||
})
|
||||
t.Run("valid-WithOplog", func(t *testing.T) {
|
||||
w := GormReadWriter{Tx: db}
|
||||
w := Db{Tx: db}
|
||||
id, err := uuid.GenerateUUID()
|
||||
assert.Nil(err)
|
||||
user, err := db_test.NewTestUser()
|
||||
@@ -89,7 +89,7 @@ func TestGormReadWriter_Update(t *testing.T) {
|
||||
assert.Nil(err)
|
||||
})
|
||||
t.Run("nil-tx", func(t *testing.T) {
|
||||
w := GormReadWriter{Tx: nil}
|
||||
w := Db{Tx: nil}
|
||||
id, err := uuid.GenerateUUID()
|
||||
assert.Nil(err)
|
||||
user, err := db_test.NewTestUser()
|
||||
@@ -100,7 +100,7 @@ func TestGormReadWriter_Update(t *testing.T) {
|
||||
assert.Equal("update tx is nil", err.Error())
|
||||
})
|
||||
t.Run("no-wrapper-WithOplog", func(t *testing.T) {
|
||||
w := GormReadWriter{Tx: db}
|
||||
w := Db{Tx: db}
|
||||
id, err := uuid.GenerateUUID()
|
||||
assert.Nil(err)
|
||||
user, err := db_test.NewTestUser()
|
||||
@@ -131,7 +131,7 @@ func TestGormReadWriter_Update(t *testing.T) {
|
||||
assert.Equal("error wrapper is nil for WithWrapper", err.Error())
|
||||
})
|
||||
t.Run("no-metadata-WithOplog", func(t *testing.T) {
|
||||
w := GormReadWriter{Tx: db}
|
||||
w := Db{Tx: db}
|
||||
id, err := uuid.GenerateUUID()
|
||||
assert.Nil(err)
|
||||
user, err := db_test.NewTestUser()
|
||||
@@ -160,14 +160,14 @@ func TestGormReadWriter_Update(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestGormReadWriter_Create(t *testing.T) {
|
||||
func TestDb_Create(t *testing.T) {
|
||||
// intentionally not run with t.Parallel so we don't need to use DoTx for the Create tests
|
||||
cleanup, db := TestSetup(t, "postgres")
|
||||
defer cleanup()
|
||||
assert := assert.New(t)
|
||||
defer db.Close()
|
||||
t.Run("simple", func(t *testing.T) {
|
||||
w := GormReadWriter{Tx: db}
|
||||
w := Db{Tx: db}
|
||||
id, err := uuid.GenerateUUID()
|
||||
assert.Nil(err)
|
||||
user, err := db_test.NewTestUser()
|
||||
@@ -187,7 +187,7 @@ func TestGormReadWriter_Create(t *testing.T) {
|
||||
assert.Equal(foundUser.Id, user.Id)
|
||||
})
|
||||
t.Run("valid-WithOplog", func(t *testing.T) {
|
||||
w := GormReadWriter{Tx: db}
|
||||
w := Db{Tx: db}
|
||||
id, err := uuid.GenerateUUID()
|
||||
assert.Nil(err)
|
||||
user, err := db_test.NewTestUser()
|
||||
@@ -216,7 +216,7 @@ func TestGormReadWriter_Create(t *testing.T) {
|
||||
assert.Equal(foundUser.Id, user.Id)
|
||||
})
|
||||
t.Run("no-wrapper-WithOplog", func(t *testing.T) {
|
||||
w := GormReadWriter{Tx: db}
|
||||
w := Db{Tx: db}
|
||||
id, err := uuid.GenerateUUID()
|
||||
assert.Nil(err)
|
||||
user, err := db_test.NewTestUser()
|
||||
@@ -238,7 +238,7 @@ func TestGormReadWriter_Create(t *testing.T) {
|
||||
assert.Equal("error wrapper is nil for WithWrapper", err.Error())
|
||||
})
|
||||
t.Run("no-metadata-WithOplog", func(t *testing.T) {
|
||||
w := GormReadWriter{Tx: db}
|
||||
w := Db{Tx: db}
|
||||
id, err := uuid.GenerateUUID()
|
||||
assert.Nil(err)
|
||||
user, err := db_test.NewTestUser()
|
||||
@@ -256,7 +256,7 @@ func TestGormReadWriter_Create(t *testing.T) {
|
||||
assert.Equal("error no metadata for WithOplog", err.Error())
|
||||
})
|
||||
t.Run("nil-tx", func(t *testing.T) {
|
||||
w := GormReadWriter{Tx: nil}
|
||||
w := Db{Tx: nil}
|
||||
id, err := uuid.GenerateUUID()
|
||||
assert.Nil(err)
|
||||
user, err := db_test.NewTestUser()
|
||||
@@ -268,14 +268,14 @@ func TestGormReadWriter_Create(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestGormReadWriter_LookupByName(t *testing.T) {
|
||||
func TestDb_LookupByName(t *testing.T) {
|
||||
t.Parallel()
|
||||
cleanup, db := TestSetup(t, "postgres")
|
||||
defer cleanup()
|
||||
assert := assert.New(t)
|
||||
defer db.Close()
|
||||
t.Run("simple", func(t *testing.T) {
|
||||
w := GormReadWriter{Tx: db}
|
||||
w := Db{Tx: db}
|
||||
id, err := uuid.GenerateUUID()
|
||||
assert.Nil(err)
|
||||
user, err := db_test.NewTestUser()
|
||||
@@ -293,7 +293,7 @@ func TestGormReadWriter_LookupByName(t *testing.T) {
|
||||
assert.Equal(foundUser.Id, user.Id)
|
||||
})
|
||||
t.Run("tx-nil,", func(t *testing.T) {
|
||||
w := GormReadWriter{}
|
||||
w := Db{}
|
||||
foundUser, err := db_test.NewTestUser()
|
||||
assert.Nil(err)
|
||||
foundUser.Name = "fn-name"
|
||||
@@ -302,7 +302,7 @@ func TestGormReadWriter_LookupByName(t *testing.T) {
|
||||
assert.Equal("error tx nil for lookup by name", err.Error())
|
||||
})
|
||||
t.Run("no-friendly-name-set", func(t *testing.T) {
|
||||
w := GormReadWriter{Tx: db}
|
||||
w := Db{Tx: db}
|
||||
foundUser, err := db_test.NewTestUser()
|
||||
assert.Nil(err)
|
||||
err = w.LookupByName(context.Background(), foundUser)
|
||||
@@ -310,7 +310,7 @@ func TestGormReadWriter_LookupByName(t *testing.T) {
|
||||
assert.Equal("error name empty string for lookup by name", err.Error())
|
||||
})
|
||||
t.Run("not-found", func(t *testing.T) {
|
||||
w := GormReadWriter{Tx: db}
|
||||
w := Db{Tx: db}
|
||||
id, err := uuid.GenerateUUID()
|
||||
assert.Nil(err)
|
||||
|
||||
@@ -323,14 +323,14 @@ func TestGormReadWriter_LookupByName(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestGormReadWriter_LookupByPublicId(t *testing.T) {
|
||||
func TestDb_LookupByPublicId(t *testing.T) {
|
||||
t.Parallel()
|
||||
cleanup, db := TestSetup(t, "postgres")
|
||||
defer cleanup()
|
||||
assert := assert.New(t)
|
||||
defer db.Close()
|
||||
t.Run("simple", func(t *testing.T) {
|
||||
w := GormReadWriter{Tx: db}
|
||||
w := Db{Tx: db}
|
||||
id, err := uuid.GenerateUUID()
|
||||
assert.Nil(err)
|
||||
user, err := db_test.NewTestUser()
|
||||
@@ -348,7 +348,7 @@ func TestGormReadWriter_LookupByPublicId(t *testing.T) {
|
||||
assert.Equal(foundUser.Id, user.Id)
|
||||
})
|
||||
t.Run("tx-nil,", func(t *testing.T) {
|
||||
w := GormReadWriter{}
|
||||
w := Db{}
|
||||
foundUser, err := db_test.NewTestUser()
|
||||
assert.Nil(err)
|
||||
err = w.LookupByPublicId(context.Background(), foundUser)
|
||||
@@ -356,7 +356,7 @@ func TestGormReadWriter_LookupByPublicId(t *testing.T) {
|
||||
assert.Equal("error tx nil for lookup by public id", err.Error())
|
||||
})
|
||||
t.Run("no-public-id-set", func(t *testing.T) {
|
||||
w := GormReadWriter{Tx: db}
|
||||
w := Db{Tx: db}
|
||||
foundUser, err := db_test.NewTestUser()
|
||||
foundUser.PublicId = ""
|
||||
assert.Nil(err)
|
||||
@@ -365,7 +365,7 @@ func TestGormReadWriter_LookupByPublicId(t *testing.T) {
|
||||
assert.Equal("error public id empty string for lookup by public id", err.Error())
|
||||
})
|
||||
t.Run("not-found", func(t *testing.T) {
|
||||
w := GormReadWriter{Tx: db}
|
||||
w := Db{Tx: db}
|
||||
id, err := uuid.GenerateUUID()
|
||||
assert.Nil(err)
|
||||
|
||||
@@ -378,14 +378,14 @@ func TestGormReadWriter_LookupByPublicId(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestGormReadWriter_LookupWhere(t *testing.T) {
|
||||
func TestDb_LookupWhere(t *testing.T) {
|
||||
t.Parallel()
|
||||
cleanup, db := TestSetup(t, "postgres")
|
||||
defer cleanup()
|
||||
assert := assert.New(t)
|
||||
defer db.Close()
|
||||
t.Run("simple", func(t *testing.T) {
|
||||
w := GormReadWriter{Tx: db}
|
||||
w := Db{Tx: db}
|
||||
id, err := uuid.GenerateUUID()
|
||||
assert.Nil(err)
|
||||
user, err := db_test.NewTestUser()
|
||||
@@ -401,14 +401,14 @@ func TestGormReadWriter_LookupWhere(t *testing.T) {
|
||||
assert.Equal(foundUser.Id, user.Id)
|
||||
})
|
||||
t.Run("tx-nil,", func(t *testing.T) {
|
||||
w := GormReadWriter{}
|
||||
w := Db{}
|
||||
var foundUser db_test.TestUser
|
||||
err := w.LookupWhere(context.Background(), &foundUser, "public_id = ?", 1)
|
||||
assert.True(err != nil)
|
||||
assert.Equal("error tx nil for lookup by", err.Error())
|
||||
})
|
||||
t.Run("not-found", func(t *testing.T) {
|
||||
w := GormReadWriter{Tx: db}
|
||||
w := Db{Tx: db}
|
||||
id, err := uuid.GenerateUUID()
|
||||
assert.Nil(err)
|
||||
|
||||
@@ -418,7 +418,7 @@ func TestGormReadWriter_LookupWhere(t *testing.T) {
|
||||
assert.Equal(ErrRecordNotFound, err)
|
||||
})
|
||||
t.Run("bad-where", func(t *testing.T) {
|
||||
w := GormReadWriter{Tx: db}
|
||||
w := Db{Tx: db}
|
||||
id, err := uuid.GenerateUUID()
|
||||
assert.Nil(err)
|
||||
|
||||
@@ -428,14 +428,14 @@ func TestGormReadWriter_LookupWhere(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestGormReadWriter_SearchWhere(t *testing.T) {
|
||||
func TestDb_SearchWhere(t *testing.T) {
|
||||
t.Parallel()
|
||||
cleanup, db := TestSetup(t, "postgres")
|
||||
defer cleanup()
|
||||
assert := assert.New(t)
|
||||
defer db.Close()
|
||||
t.Run("simple", func(t *testing.T) {
|
||||
w := GormReadWriter{Tx: db}
|
||||
w := Db{Tx: db}
|
||||
id, err := uuid.GenerateUUID()
|
||||
assert.Nil(err)
|
||||
user, err := db_test.NewTestUser()
|
||||
@@ -451,14 +451,14 @@ func TestGormReadWriter_SearchWhere(t *testing.T) {
|
||||
assert.Equal(foundUsers[0].Id, user.Id)
|
||||
})
|
||||
t.Run("tx-nil,", func(t *testing.T) {
|
||||
w := GormReadWriter{}
|
||||
w := Db{}
|
||||
var foundUsers []db_test.TestUser
|
||||
err := w.SearchWhere(context.Background(), &foundUsers, "public_id = ?", 1)
|
||||
assert.True(err != nil)
|
||||
assert.Equal("error tx nil for search by", err.Error())
|
||||
})
|
||||
t.Run("not-found", func(t *testing.T) {
|
||||
w := GormReadWriter{Tx: db}
|
||||
w := Db{Tx: db}
|
||||
id, err := uuid.GenerateUUID()
|
||||
assert.Nil(err)
|
||||
|
||||
@@ -468,7 +468,7 @@ func TestGormReadWriter_SearchWhere(t *testing.T) {
|
||||
assert.Equal(0, len(foundUsers))
|
||||
})
|
||||
t.Run("bad-where", func(t *testing.T) {
|
||||
w := GormReadWriter{Tx: db}
|
||||
w := Db{Tx: db}
|
||||
id, err := uuid.GenerateUUID()
|
||||
assert.Nil(err)
|
||||
|
||||
@@ -478,14 +478,14 @@ func TestGormReadWriter_SearchWhere(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestGormReadWriter_DB(t *testing.T) {
|
||||
func TestDb_DB(t *testing.T) {
|
||||
t.Parallel()
|
||||
cleanup, db := TestSetup(t, "postgres")
|
||||
defer cleanup()
|
||||
assert := assert.New(t)
|
||||
defer db.Close()
|
||||
t.Run("valid", func(t *testing.T) {
|
||||
w := GormReadWriter{Tx: db}
|
||||
w := Db{Tx: db}
|
||||
d, err := w.DB()
|
||||
assert.Nil(err)
|
||||
assert.True(d != nil)
|
||||
@@ -493,7 +493,7 @@ func TestGormReadWriter_DB(t *testing.T) {
|
||||
assert.Nil(err)
|
||||
})
|
||||
t.Run("nil-tx", func(t *testing.T) {
|
||||
w := GormReadWriter{Tx: nil}
|
||||
w := Db{Tx: nil}
|
||||
d, err := w.DB()
|
||||
assert.True(err != nil)
|
||||
assert.True(d == nil)
|
||||
@@ -501,7 +501,7 @@ func TestGormReadWriter_DB(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestGormReadWriter_DoTx(t *testing.T) {
|
||||
func TestDb_DoTx(t *testing.T) {
|
||||
t.Parallel()
|
||||
cleanup, db := TestSetup(t, "postgres")
|
||||
defer cleanup()
|
||||
@@ -509,7 +509,7 @@ func TestGormReadWriter_DoTx(t *testing.T) {
|
||||
defer db.Close()
|
||||
|
||||
t.Run("valid-with-10-retries", func(t *testing.T) {
|
||||
w := &GormReadWriter{Tx: db}
|
||||
w := &Db{Tx: db}
|
||||
attempts := 0
|
||||
got, err := w.DoTx(context.Background(), 10, ExpBackoff{},
|
||||
func(Writer) error {
|
||||
@@ -524,7 +524,7 @@ func TestGormReadWriter_DoTx(t *testing.T) {
|
||||
assert.Equal(9, attempts) // attempted 1 + 8 retries
|
||||
})
|
||||
t.Run("valid-with-1-retries", func(t *testing.T) {
|
||||
w := &GormReadWriter{Tx: db}
|
||||
w := &Db{Tx: db}
|
||||
attempts := 0
|
||||
got, err := w.DoTx(context.Background(), 1, ExpBackoff{},
|
||||
func(Writer) error {
|
||||
@@ -539,7 +539,7 @@ func TestGormReadWriter_DoTx(t *testing.T) {
|
||||
assert.Equal(2, attempts) // attempted 1 + 8 retries
|
||||
})
|
||||
t.Run("valid-with-2-retries", func(t *testing.T) {
|
||||
w := &GormReadWriter{Tx: db}
|
||||
w := &Db{Tx: db}
|
||||
attempts := 0
|
||||
got, err := w.DoTx(context.Background(), 3, ExpBackoff{},
|
||||
func(Writer) error {
|
||||
@@ -554,7 +554,7 @@ func TestGormReadWriter_DoTx(t *testing.T) {
|
||||
assert.Equal(3, attempts) // attempted 1 + 8 retries
|
||||
})
|
||||
t.Run("valid-with-4-retries", func(t *testing.T) {
|
||||
w := &GormReadWriter{Tx: db}
|
||||
w := &Db{Tx: db}
|
||||
attempts := 0
|
||||
got, err := w.DoTx(context.Background(), 4, ExpBackoff{},
|
||||
func(Writer) error {
|
||||
@@ -569,7 +569,7 @@ func TestGormReadWriter_DoTx(t *testing.T) {
|
||||
assert.Equal(4, attempts) // attempted 1 + 8 retries
|
||||
})
|
||||
t.Run("zero-retries", func(t *testing.T) {
|
||||
w := &GormReadWriter{Tx: db}
|
||||
w := &Db{Tx: db}
|
||||
attempts := 0
|
||||
got, err := w.DoTx(context.Background(), 0, ExpBackoff{}, func(Writer) error { attempts += 1; return nil })
|
||||
assert.Nil(err)
|
||||
@@ -577,7 +577,7 @@ func TestGormReadWriter_DoTx(t *testing.T) {
|
||||
assert.Equal(1, attempts)
|
||||
})
|
||||
t.Run("nil-tx", func(t *testing.T) {
|
||||
w := &GormReadWriter{nil}
|
||||
w := &Db{nil}
|
||||
attempts := 0
|
||||
got, err := w.DoTx(context.Background(), 1, ExpBackoff{}, func(Writer) error { attempts += 1; return nil })
|
||||
assert.True(err != nil)
|
||||
@@ -585,14 +585,14 @@ func TestGormReadWriter_DoTx(t *testing.T) {
|
||||
assert.Equal("do tx is nil", err.Error())
|
||||
})
|
||||
t.Run("not-a-retry-err", func(t *testing.T) {
|
||||
w := &GormReadWriter{Tx: db}
|
||||
w := &Db{Tx: db}
|
||||
got, err := w.DoTx(context.Background(), 1, ExpBackoff{}, func(Writer) error { return errors.New("not a retry error") })
|
||||
assert.True(err != nil)
|
||||
assert.Equal(RetryInfo{}, got)
|
||||
assert.True(err != oplog.ErrTicketAlreadyRedeemed)
|
||||
})
|
||||
t.Run("too-many-retries", func(t *testing.T) {
|
||||
w := &GormReadWriter{Tx: db}
|
||||
w := &Db{Tx: db}
|
||||
attempts := 0
|
||||
got, err := w.DoTx(context.Background(), 2, ExpBackoff{}, func(Writer) error { attempts += 1; return oplog.ErrTicketAlreadyRedeemed })
|
||||
assert.True(err != nil)
|
||||
@@ -601,14 +601,14 @@ func TestGormReadWriter_DoTx(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestGormReadWriter_Delete(t *testing.T) {
|
||||
func TestDb_Delete(t *testing.T) {
|
||||
// intentionally not run with t.Parallel so we don't need to use DoTx for the Create tests
|
||||
cleanup, db := TestSetup(t, "postgres")
|
||||
defer cleanup()
|
||||
assert := assert.New(t)
|
||||
defer db.Close()
|
||||
t.Run("simple", func(t *testing.T) {
|
||||
w := GormReadWriter{Tx: db}
|
||||
w := Db{Tx: db}
|
||||
id, err := uuid.GenerateUUID()
|
||||
assert.Nil(err)
|
||||
user, err := db_test.NewTestUser()
|
||||
@@ -635,7 +635,7 @@ func TestGormReadWriter_Delete(t *testing.T) {
|
||||
assert.Equal(ErrRecordNotFound, err)
|
||||
})
|
||||
t.Run("valid-WithOplog", func(t *testing.T) {
|
||||
w := GormReadWriter{Tx: db}
|
||||
w := Db{Tx: db}
|
||||
id, err := uuid.GenerateUUID()
|
||||
assert.Nil(err)
|
||||
user, err := db_test.NewTestUser()
|
||||
@@ -682,7 +682,7 @@ func TestGormReadWriter_Delete(t *testing.T) {
|
||||
assert.Equal(ErrRecordNotFound, err)
|
||||
})
|
||||
t.Run("no-wrapper-WithOplog", func(t *testing.T) {
|
||||
w := GormReadWriter{Tx: db}
|
||||
w := Db{Tx: db}
|
||||
id, err := uuid.GenerateUUID()
|
||||
assert.Nil(err)
|
||||
user, err := db_test.NewTestUser()
|
||||
@@ -726,7 +726,7 @@ func TestGormReadWriter_Delete(t *testing.T) {
|
||||
assert.Equal("error wrapper is nil for WithWrapper", err.Error())
|
||||
})
|
||||
t.Run("no-metadata-WithOplog", func(t *testing.T) {
|
||||
w := GormReadWriter{Tx: db}
|
||||
w := Db{Tx: db}
|
||||
id, err := uuid.GenerateUUID()
|
||||
assert.Nil(err)
|
||||
user, err := db_test.NewTestUser()
|
||||
@@ -766,7 +766,7 @@ func TestGormReadWriter_Delete(t *testing.T) {
|
||||
assert.Equal("error no metadata for WithOplog", err.Error())
|
||||
})
|
||||
t.Run("nil-tx", func(t *testing.T) {
|
||||
w := GormReadWriter{Tx: nil}
|
||||
w := Db{Tx: nil}
|
||||
id, err := uuid.GenerateUUID()
|
||||
assert.Nil(err)
|
||||
user, err := db_test.NewTestUser()
|
||||
@@ -778,14 +778,14 @@ func TestGormReadWriter_Delete(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestGormReadWriter_ScanRows(t *testing.T) {
|
||||
func TestDb_ScanRows(t *testing.T) {
|
||||
t.Parallel()
|
||||
cleanup, db := TestSetup(t, "postgres")
|
||||
defer cleanup()
|
||||
assert := assert.New(t)
|
||||
defer db.Close()
|
||||
t.Run("valid", func(t *testing.T) {
|
||||
w := GormReadWriter{Tx: db}
|
||||
w := Db{Tx: db}
|
||||
user, err := db_test.NewTestUser()
|
||||
assert.Nil(err)
|
||||
err = w.Create(context.Background(), user)
|
||||
|
||||
@@ -19,7 +19,7 @@ func Test_Repository_CreateScope(t *testing.T) {
|
||||
defer conn.Close()
|
||||
|
||||
t.Run("valid-scope", func(t *testing.T) {
|
||||
rw := &db.GormReadWriter{Tx: conn}
|
||||
rw := &db.Db{Tx: conn}
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
id, err := uuid.GenerateUUID()
|
||||
@@ -58,7 +58,7 @@ func Test_Repository_UpdateScope(t *testing.T) {
|
||||
defer conn.Close()
|
||||
|
||||
t.Run("valid-scope", func(t *testing.T) {
|
||||
rw := &db.GormReadWriter{Tx: conn}
|
||||
rw := &db.Db{Tx: conn}
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
id, err := uuid.GenerateUUID()
|
||||
@@ -107,7 +107,7 @@ func Test_Repository_UpdateScope(t *testing.T) {
|
||||
assert.Nil(err)
|
||||
})
|
||||
t.Run("bad-parent-scope", func(t *testing.T) {
|
||||
rw := &db.GormReadWriter{Tx: conn}
|
||||
rw := &db.Db{Tx: conn}
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
id, err := uuid.GenerateUUID()
|
||||
@@ -142,7 +142,7 @@ func Test_Repository_LookupScope(t *testing.T) {
|
||||
defer conn.Close()
|
||||
|
||||
t.Run("found-and-not-found", func(t *testing.T) {
|
||||
rw := &db.GormReadWriter{Tx: conn}
|
||||
rw := &db.Db{Tx: conn}
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
id, err := uuid.GenerateUUID()
|
||||
|
||||
@@ -20,7 +20,7 @@ func TestNewRepository(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
defer conn.Close()
|
||||
|
||||
rw := &db.GormReadWriter{Tx: conn}
|
||||
rw := &db.Db{Tx: conn}
|
||||
wrapper := db.TestWrapper(t)
|
||||
type args struct {
|
||||
r db.Reader
|
||||
@@ -106,7 +106,7 @@ func Test_Repository_create(t *testing.T) {
|
||||
defer conn.Close()
|
||||
|
||||
t.Run("valid-scope", func(t *testing.T) {
|
||||
rw := &db.GormReadWriter{Tx: conn}
|
||||
rw := &db.Db{Tx: conn}
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
id, err := uuid.GenerateUUID()
|
||||
@@ -137,7 +137,7 @@ func Test_Repository_create(t *testing.T) {
|
||||
assert.Nil(err)
|
||||
})
|
||||
t.Run("nil-resource", func(t *testing.T) {
|
||||
rw := &db.GormReadWriter{Tx: conn}
|
||||
rw := &db.Db{Tx: conn}
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
resource, err := repo.create(context.Background(), nil)
|
||||
@@ -155,7 +155,7 @@ func Test_dbRepository_update(t *testing.T) {
|
||||
defer conn.Close()
|
||||
|
||||
t.Run("valid-scope", func(t *testing.T) {
|
||||
rw := &db.GormReadWriter{Tx: conn}
|
||||
rw := &db.Db{Tx: conn}
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
id, err := uuid.GenerateUUID()
|
||||
@@ -187,7 +187,7 @@ func Test_dbRepository_update(t *testing.T) {
|
||||
assert.Nil(err)
|
||||
})
|
||||
t.Run("nil-resource", func(t *testing.T) {
|
||||
rw := &db.GormReadWriter{Tx: conn}
|
||||
rw := &db.Db{Tx: conn}
|
||||
wrapper := db.TestWrapper(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
resource, err := repo.update(context.Background(), nil, nil)
|
||||
|
||||
@@ -18,7 +18,7 @@ func Test_NewScope(t *testing.T) {
|
||||
defer conn.Close()
|
||||
|
||||
t.Run("valid-org-with-project", func(t *testing.T) {
|
||||
w := db.GormReadWriter{Tx: conn}
|
||||
w := db.Db{Tx: conn}
|
||||
s, err := NewOrganization()
|
||||
assert.Nil(err)
|
||||
assert.True(s.Scope != nil)
|
||||
@@ -55,7 +55,7 @@ func Test_ScopeCreate(t *testing.T) {
|
||||
defer conn.Close()
|
||||
|
||||
t.Run("valid", func(t *testing.T) {
|
||||
w := db.GormReadWriter{Tx: conn}
|
||||
w := db.Db{Tx: conn}
|
||||
s, err := NewOrganization()
|
||||
assert.Nil(err)
|
||||
assert.True(s.Scope != nil)
|
||||
@@ -64,7 +64,7 @@ func Test_ScopeCreate(t *testing.T) {
|
||||
assert.True(s.PublicId != "")
|
||||
})
|
||||
t.Run("valid-with-parent", func(t *testing.T) {
|
||||
w := db.GormReadWriter{Tx: conn}
|
||||
w := db.Db{Tx: conn}
|
||||
s, err := NewOrganization()
|
||||
assert.Nil(err)
|
||||
assert.True(s.Scope != nil)
|
||||
@@ -95,7 +95,7 @@ func Test_ScopeUpdate(t *testing.T) {
|
||||
defer conn.Close()
|
||||
|
||||
t.Run("valid", func(t *testing.T) {
|
||||
w := db.GormReadWriter{Tx: conn}
|
||||
w := db.Db{Tx: conn}
|
||||
s, err := NewOrganization()
|
||||
assert.Nil(err)
|
||||
assert.True(s.Scope != nil)
|
||||
@@ -110,7 +110,7 @@ func Test_ScopeUpdate(t *testing.T) {
|
||||
assert.Nil(err)
|
||||
})
|
||||
t.Run("type-update-not-allowed", func(t *testing.T) {
|
||||
w := db.GormReadWriter{Tx: conn}
|
||||
w := db.Db{Tx: conn}
|
||||
s, err := NewOrganization()
|
||||
assert.Nil(err)
|
||||
assert.True(s.Scope != nil)
|
||||
@@ -130,7 +130,7 @@ func Test_ScopeGetScope(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
defer conn.Close()
|
||||
t.Run("valid-scope", func(t *testing.T) {
|
||||
w := db.GormReadWriter{Tx: conn}
|
||||
w := db.Db{Tx: conn}
|
||||
s, err := NewOrganization()
|
||||
assert.Nil(err)
|
||||
assert.True(s.Scope != nil)
|
||||
@@ -182,7 +182,7 @@ func TestScope_Clone(t *testing.T) {
|
||||
defer conn.Close()
|
||||
|
||||
t.Run("valid", func(t *testing.T) {
|
||||
w := db.GormReadWriter{Tx: conn}
|
||||
w := db.Db{Tx: conn}
|
||||
s, err := NewOrganization()
|
||||
assert.Nil(err)
|
||||
assert.True(s.Scope != nil)
|
||||
@@ -194,7 +194,7 @@ func TestScope_Clone(t *testing.T) {
|
||||
assert.True(proto.Equal(cp.(*Scope).Scope, s.Scope))
|
||||
})
|
||||
t.Run("not-equal", func(t *testing.T) {
|
||||
w := db.GormReadWriter{Tx: conn}
|
||||
w := db.Db{Tx: conn}
|
||||
s, err := NewOrganization()
|
||||
assert.Nil(err)
|
||||
assert.True(s.Scope != nil)
|
||||
|
||||
Reference in new issue
Block a user