Skip to content

Commit

Permalink
Update repository interface to support optimistic lock (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fs02 committed Mar 25, 2022
1 parent 7dab843 commit 27f151d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 24 deletions.
34 changes: 17 additions & 17 deletions delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,31 @@ import (

type delete []*MockDelete

func (d *delete) register(ctxData ctxData, options ...rel.Cascade) *MockDelete {
func (d *delete) register(ctxData ctxData, mutators ...rel.Mutator) *MockDelete {
md := &MockDelete{
assert: &Assert{ctxData: ctxData, repeatability: 1},
argOptions: options,
assert: &Assert{ctxData: ctxData, repeatability: 1},
argMutators: mutators,
}
*d = append(*d, md)
return md
}

func (d delete) execute(ctx context.Context, record interface{}, options ...rel.Cascade) error {
func (d delete) execute(ctx context.Context, record interface{}, mutators ...rel.Mutator) error {
for _, md := range d {
if (md.argRecord == nil || reflect.DeepEqual(md.argRecord, record)) &&
(md.argRecordType == "" || md.argRecordType == reflect.TypeOf(record).String()) &&
(md.argRecordTable == "" || md.argRecordTable == rel.NewDocument(record, true).Table()) &&
(md.argRecordContains == nil || matchContains(md.argRecordContains, record)) &&
reflect.DeepEqual(md.argOptions, options) &&
matchMutators(md.argMutators, mutators) &&
md.assert.call(ctx) {
return md.retError
}
}

md := &MockDelete{
assert: &Assert{ctxData: fetchContext(ctx)},
argRecord: record,
argOptions: options,
assert: &Assert{ctxData: fetchContext(ctx)},
argRecord: record,
argMutators: mutators,
}

panic(failExecuteMessage(md, d))
Expand All @@ -60,7 +60,7 @@ type MockDelete struct {
argRecordType string
argRecordTable string
argRecordContains interface{}
argOptions []rel.Cascade
argMutators []rel.Mutator
retError error
}

Expand Down Expand Up @@ -118,20 +118,20 @@ func (md MockDelete) String() string {
argRecord = fmt.Sprintf("<Table: %s>", md.argRecordTable)
}

argCascade := ""
for i := range md.argOptions {
argCascade += fmt.Sprintf(", %v", md.argOptions[i])
argMutators := ""
for i := range md.argMutators {
argMutators += fmt.Sprintf(", %v", md.argMutators[i])
}

return md.assert.sprintf("Delete(ctx, %s%s)", argRecord, argCascade)
return md.assert.sprintf("Delete(ctx, %s%s)", argRecord, argMutators)
}

// ExpectString representation of mocked call.
func (md MockDelete) ExpectString() string {
argOptions := ""
for i := range md.argOptions {
argOptions += fmt.Sprintf("%v", md.argOptions[i])
argMutators := ""
for i := range md.argMutators {
argMutators += fmt.Sprintf("%v", md.argMutators[i])
}

return md.assert.sprintf("ExpectDelete(%s).ForType(\"%T\")", argOptions, md.argRecord)
return md.assert.sprintf("ExpectDelete(%s).ForType(\"%T\")", argMutators, md.argRecord)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module github.com/go-rel/reltest
go 1.15

require (
github.com/go-rel/rel v0.33.1
github.com/go-rel/rel v0.34.0
github.com/stretchr/testify v1.7.1
)
5 changes: 2 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/go-rel/rel v0.33.1 h1:yi3u6FDsQnOKZjo/DxEAC1WOLEjoas3fE1+Vyh6m6uY=
github.com/go-rel/rel v0.33.1/go.mod h1:DhB9Xmt/ymaumJAB6Z3Kq+IybLwQhhrzr6ZLeXMygPo=
github.com/go-rel/rel v0.34.0 h1:dynBkxkVOHxFMHtu3p8vdvAMjjCm+Yi2J0tJsjeoVjQ=
github.com/go-rel/rel v0.34.0/go.mod h1:YDHtnJhlaiSjANLGiZXvI57jg1amfpJSJlOimq6WJTE=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
Expand Down Expand Up @@ -39,7 +39,6 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/objx v0.3.0 h1:NGXK3lHquSN08v5vWalVI/L8XU9hdzE/G6xsrze47As=
github.com/stretchr/objx v0.3.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
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.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
Expand Down
6 changes: 3 additions & 3 deletions repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,18 +222,18 @@ func (r *Repository) ExpectUpdateAny(query rel.Query, mutates ...rel.Mutate) *Mo
}

// Delete provides a mock function with given fields: record
func (r *Repository) Delete(ctx context.Context, record interface{}, options ...rel.Cascade) error {
func (r *Repository) Delete(ctx context.Context, record interface{}, options ...rel.Mutator) error {
r.repo.Delete(ctx, record)
return r.delete.execute(ctx, record, options...)
}

// MustDelete provides a mock function with given fields: record
func (r *Repository) MustDelete(ctx context.Context, record interface{}, options ...rel.Cascade) {
func (r *Repository) MustDelete(ctx context.Context, record interface{}, options ...rel.Mutator) {
must(r.Delete(ctx, record, options...))
}

// ExpectDelete apply mocks and expectations for Delete
func (r *Repository) ExpectDelete(options ...rel.Cascade) *MockDelete {
func (r *Repository) ExpectDelete(options ...rel.Mutator) *MockDelete {
return r.delete.register(r.ctxData, options...)
}

Expand Down

0 comments on commit 27f151d

Please sign in to comment.