refactor to use db.New() and clean up error msgs (#45)

This commit is contained in:
Jim authored and GitHub committed 2020-05-12 06:51:02 -04:00
1 parent ad8d4e9c80
commit 61ca88dac9
5 files changed
+36 -36

No files matched your search

+9 -9
View File
@@ -123,7 +123,7 @@ func New(underlying *gorm.DB) *Db {
// DB returns the sql.DB
func (rw *Db) DB() (*sql.DB, error) {
if rw.underlying == nil {
return nil, errors.New("create tx is nil for db")
return nil, errors.New("underlying db is nil")
}
return rw.underlying.DB(), nil
}
@@ -154,7 +154,7 @@ func (rw *Db) lookupAfterWrite(ctx context.Context, i interface{}, opt ...Option
// Create an object in the db with options: WithOplog and WithLookup (to force a lookup after create))
func (rw *Db) Create(ctx context.Context, i interface{}, opt ...Option) error {
if rw.underlying == nil {
return errors.New("create tx is nil")
return errors.New("create underlying db is nil")
}
opts := GetOpts(opt...)
withOplog := opts.withOplog
@@ -189,7 +189,7 @@ func (rw *Db) Create(ctx context.Context, i interface{}, opt ...Option) error {
// it will send every field to the DB. Update supports embedding a struct (or structPtr) one level deep for updating
func (rw *Db) Update(ctx context.Context, i interface{}, fieldMaskPaths []string, opt ...Option) error {
if rw.underlying == nil {
return errors.New("update tx is nil")
return errors.New("update underlying db is nil")
}
opts := GetOpts(opt...)
withDebug := opts.withDebug
@@ -268,7 +268,7 @@ func (rw *Db) Update(ctx context.Context, i interface{}, fieldMaskPaths []string
// Delete an object in the db with options: WithOplog (which requires WithMetadata, WithWrapper)
func (rw *Db) Delete(ctx context.Context, i interface{}, opt ...Option) error {
if rw.underlying == nil {
return errors.New("delete tx is nil")
return errors.New("delete underlying db is nil")
}
if i == nil {
return errors.New("delete interface is nil")
@@ -353,7 +353,7 @@ func (rw *Db) addOplog(ctx context.Context, opType OpType, opts Options, i inter
// be reset before retry
func (w *Db) DoTx(ctx context.Context, retries uint, backOff Backoff, Handler TxHandler) (RetryInfo, error) {
if w.underlying == nil {
return RetryInfo{}, errors.New("do tx is nil")
return RetryInfo{}, errors.New("do underlying db is nil")
}
info := RetryInfo{}
for attempts := uint(1); ; attempts++ {
@@ -393,7 +393,7 @@ func (w *Db) DoTx(ctx context.Context, retries uint, backOff Backoff, Handler Tx
// LookupByName will lookup resource my its friendly name which must be unique
func (rw *Db) LookupByName(ctx context.Context, resource ResourceNamer, opt ...Option) error {
if rw.underlying == nil {
return errors.New("error tx nil for lookup by name")
return errors.New("error underlying db nil for lookup by name")
}
opts := GetOpts(opt...)
withDebug := opts.withDebug
@@ -419,7 +419,7 @@ func (rw *Db) LookupByName(ctx context.Context, resource ResourceNamer, opt ...O
// LookupByPublicId will lookup resource my its public_id which must be unique
func (rw *Db) LookupByPublicId(ctx context.Context, resource ResourcePublicIder, opt ...Option) error {
if rw.underlying == nil {
return errors.New("error tx nil for lookup by public id")
return errors.New("error underlying db nil for lookup by public id")
}
opts := GetOpts(opt...)
withDebug := opts.withDebug
@@ -445,7 +445,7 @@ func (rw *Db) LookupByPublicId(ctx context.Context, resource ResourcePublicIder,
// LookupWhere will lookup the first resource using a where clause with parameters (it only returns the first one)
func (rw *Db) LookupWhere(ctx context.Context, resource interface{}, where string, args ...interface{}) error {
if rw.underlying == nil {
return errors.New("error tx nil for lookup by")
return errors.New("error underlying db nil for lookup by")
}
if reflect.ValueOf(resource).Kind() != reflect.Ptr {
return errors.New("error interface parameter must to be a pointer for lookup by")
@@ -456,7 +456,7 @@ func (rw *Db) LookupWhere(ctx context.Context, resource interface{}, where strin
// SearchWhere will search for all the resources it can find using a where clause with parameters
func (rw *Db) SearchWhere(ctx context.Context, resources interface{}, where string, args ...interface{}) error {
if rw.underlying == nil {
return errors.New("error tx nil for search by")
return errors.New("error underlying db nil for search by")
}
if reflect.ValueOf(resources).Kind() != reflect.Ptr {
return errors.New("error interface parameter must to be a pointer for search by")
+9 -9
View File
@@ -97,7 +97,7 @@ func TestDb_Update(t *testing.T) {
user.Name = "foo-" + id
err = w.Update(context.Background(), user, nil)
assert.True(err != nil)
assert.Equal("update tx is nil", err.Error())
assert.Equal("update underlying db is nil", err.Error())
})
t.Run("no-wrapper-WithOplog", func(t *testing.T) {
w := Db{underlying: db}
@@ -264,7 +264,7 @@ func TestDb_Create(t *testing.T) {
user.Name = "foo-" + id
err = w.Create(context.Background(), user)
assert.True(err != nil)
assert.Equal("create tx is nil", err.Error())
assert.Equal("create underlying db is nil", err.Error())
})
}
@@ -299,7 +299,7 @@ func TestDb_LookupByName(t *testing.T) {
foundUser.Name = "fn-name"
err = w.LookupByName(context.Background(), foundUser)
assert.True(err != nil)
assert.Equal("error tx nil for lookup by name", err.Error())
assert.Equal("error underlying db nil for lookup by name", err.Error())
})
t.Run("no-friendly-name-set", func(t *testing.T) {
w := Db{underlying: db}
@@ -353,7 +353,7 @@ func TestDb_LookupByPublicId(t *testing.T) {
assert.Nil(err)
err = w.LookupByPublicId(context.Background(), foundUser)
assert.True(err != nil)
assert.Equal("error tx nil for lookup by public id", err.Error())
assert.Equal("error underlying db nil for lookup by public id", err.Error())
})
t.Run("no-public-id-set", func(t *testing.T) {
w := Db{underlying: db}
@@ -405,7 +405,7 @@ func TestDb_LookupWhere(t *testing.T) {
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())
assert.Equal("error underlying db nil for lookup by", err.Error())
})
t.Run("not-found", func(t *testing.T) {
w := Db{underlying: db}
@@ -455,7 +455,7 @@ func TestDb_SearchWhere(t *testing.T) {
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())
assert.Equal("error underlying db nil for search by", err.Error())
})
t.Run("not-found", func(t *testing.T) {
w := Db{underlying: db}
@@ -497,7 +497,7 @@ func TestDb_DB(t *testing.T) {
d, err := w.DB()
assert.True(err != nil)
assert.True(d == nil)
assert.Equal("create tx is nil for db", err.Error())
assert.Equal("underlying db is nil", err.Error())
})
}
@@ -582,7 +582,7 @@ func TestDb_DoTx(t *testing.T) {
got, err := w.DoTx(context.Background(), 1, ExpBackoff{}, func(Writer) error { attempts += 1; return nil })
assert.True(err != nil)
assert.Equal(RetryInfo{}, got)
assert.Equal("do tx is nil", err.Error())
assert.Equal("do underlying db is nil", err.Error())
})
t.Run("not-a-retry-err", func(t *testing.T) {
w := &Db{underlying: db}
@@ -774,7 +774,7 @@ func TestDb_Delete(t *testing.T) {
user.Name = "foo-" + id
err = w.Create(context.Background(), user)
assert.True(err != nil)
assert.Equal("create tx is nil", err.Error())
assert.Equal("create underlying db is nil", err.Error())
})
}
+4 -4
View File
@@ -19,7 +19,7 @@ func Test_Repository_CreateScope(t *testing.T) {
defer conn.Close()
t.Run("valid-scope", func(t *testing.T) {
rw := &db.Db{Tx: conn}
rw := db.New(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.Db{Tx: conn}
rw := db.New(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.Db{Tx: conn}
rw := db.New(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.Db{Tx: conn}
rw := db.New(conn)
wrapper := db.TestWrapper(t)
repo, err := NewRepository(rw, rw, wrapper)
id, err := uuid.GenerateUUID()
+5 -5
View File
@@ -20,7 +20,7 @@ func TestNewRepository(t *testing.T) {
assert := assert.New(t)
defer conn.Close()
rw := &db.Db{Tx: conn}
rw := db.New(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.Db{Tx: conn}
rw := db.New(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.Db{Tx: conn}
rw := db.New(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.Db{Tx: conn}
rw := db.New(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.Db{Tx: conn}
rw := db.New(conn)
wrapper := db.TestWrapper(t)
repo, err := NewRepository(rw, rw, wrapper)
resource, err := repo.update(context.Background(), nil, nil)
+9 -9
View File
@@ -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.Db{Tx: conn}
w := db.New(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.Db{Tx: conn}
w := db.New(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.Db{Tx: conn}
w := db.New(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.Db{Tx: conn}
w := db.New(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.Db{Tx: conn}
w := db.New(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.Db{Tx: conn}
w := db.New(conn)
s, err := NewOrganization()
assert.Nil(err)
assert.True(s.Scope != nil)
@@ -145,7 +145,7 @@ func Test_ScopeGetScope(t *testing.T) {
err = w.Create(context.Background(), project)
assert.Nil(err)
projectOrg, err := project.GetScope(context.Background(), &w)
projectOrg, err := project.GetScope(context.Background(), w)
assert.Nil(err)
assert.True(projectOrg != nil)
assert.Equal(projectOrg.PublicId, project.ParentId)
@@ -182,7 +182,7 @@ func TestScope_Clone(t *testing.T) {
defer conn.Close()
t.Run("valid", func(t *testing.T) {
w := db.Db{Tx: conn}
w := db.New(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.Db{Tx: conn}
w := db.New(conn)
s, err := NewOrganization()
assert.Nil(err)
assert.True(s.Scope != nil)