Skip to content

Commit

Permalink
feature UpdateMany
Browse files Browse the repository at this point in the history
  • Loading branch information
rez1dent3 committed May 6, 2024
1 parent b7fcde2 commit bda4054
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
18 changes: 18 additions & 0 deletions stuber.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,27 @@ func NewBudgerigar(toggles features.Toggles) *Budgerigar {
}

func (b *Budgerigar) PutMany(values ...*Stub) []uuid.UUID {
for _, value := range values {
if value.Key() == uuid.Nil {
value.ID = uuid.New()
}
}

return b.searcher.upsert(values...)
}

func (b *Budgerigar) UpdateMany(values ...*Stub) []uuid.UUID {
updates := make([]*Stub, 0, len(values))

for _, value := range values {
if value.Key() != uuid.Nil {
updates = append(updates, value)
}
}

return b.searcher.upsert(updates...)
}

func (b *Budgerigar) DeleteByID(ids ...uuid.UUID) int {
return b.searcher.del(ids...)
}
Expand Down
33 changes: 33 additions & 0 deletions stuber_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,39 @@ func TestFindBy(t *testing.T) {
require.Len(t, s.All(), 7)
}

func TestPutMany_FixID(t *testing.T) {
s := stuber.NewBudgerigar(features.New())

require.Len(t, s.All(), 0)

stubs := []*stuber.Stub{
{Service: "Greeter1", Method: "SayHello1"},
{Service: "Greeter1", Method: "SayHello1"},
}

s.PutMany(stubs...)

require.Len(t, s.All(), 2)
require.NotNil(t, uuid.Nil, stubs[0].Key())
require.NotNil(t, uuid.Nil, stubs[1].Key())
}

func TestUpdateMany(t *testing.T) {
s := stuber.NewBudgerigar(features.New())

require.Len(t, s.All(), 0)

stubs := []*stuber.Stub{
{Service: "Greeter1", Method: "SayHello1", ID: uuid.New()},
{Service: "Greeter1", Method: "SayHello1"},
{Service: "Greeter1", Method: "SayHello1"},
}

s.UpdateMany(stubs...)

require.Len(t, s.All(), 1)
}

func TestRelationship(t *testing.T) {
s := stuber.NewBudgerigar(features.New())

Expand Down

0 comments on commit bda4054

Please sign in to comment.