Skip to content

Commit

Permalink
feat: is Trans judge
Browse files Browse the repository at this point in the history
  • Loading branch information
FeifeiyuM committed Oct 26, 2020
1 parent c35bf7d commit 16fd41e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
9 changes: 9 additions & 0 deletions capsule.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,12 @@ func (c *Capsule) ExecMany(ctx context.Context, queries []string) error {
func (c *Capsule) Close() error {
return c.sqlY.Close()
}

// IsTrans is enable transaction
func (c *Capsule) IsTrans(ctx context.Context) (bool, error) {
cs, err := c.getCapsule(ctx)
if err != nil {
return false, err
}
return cs.isTrans, nil
}
53 changes: 53 additions & 0 deletions capsule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,5 +312,58 @@ func TestCapsule_raw3(t *testing.T) {
t.Error(err)
}
fmt.Sprintln(aff)
}

func TestCapsule_Close(t *testing.T) {
db, err := New(opt)
if err != nil {
t.Error(err)
}
capsule := NewCapsule(db)
err = capsule.Close()
if err != nil {
t.Error(err)
}
}

func TestCapsule_IsTrans(t *testing.T) {
db, err := New(opt)
if err != nil {
t.Error(err)
}
capsule := NewCapsule(db)
ctx := context.TODO()
isTrans, err := capsule.IsTrans(ctx)
if err != nil {
t.Error(err)
}
if isTrans {
t.Error("is not trans")
}
_, err = capsule.StartCapsule(ctx, false, func(ctx context.Context) (interface{}, error) {
isTrans, err := capsule.IsTrans(ctx)
if err != nil {
return nil, err
}
if isTrans {
return nil, errors.New("is not trans")
}
return nil, nil
})
if err != nil {
t.Error(err)
}
_, err = capsule.StartCapsule(ctx, true, func(ctx context.Context) (interface{}, error) {
isTrans, err := capsule.IsTrans(ctx)
if err != nil {
return nil, err
}
if !isTrans {
return nil, errors.New("is trans")
}
return nil, nil
})
if err != nil {
t.Error(err)
}
}

0 comments on commit 16fd41e

Please sign in to comment.