Skip to content

Commit

Permalink
shift: Correct KV reporting for errors
Browse files Browse the repository at this point in the history
  • Loading branch information
echarrod committed Aug 21, 2024
1 parent e459dea commit e00cf99
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
6 changes: 3 additions & 3 deletions shift.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,16 +179,16 @@ func (fsm *GenFSM[T]) Update(ctx context.Context, dbc *sql.DB, from Status, to S
func (fsm *GenFSM[T]) UpdateTx(ctx context.Context, tx *sql.Tx, from Status, to Status, updater Updater[T]) (rsql.NotifyFunc, error) {
t, ok := fsm.states[to.ShiftStatus()]
if !ok {
return nil, errors.Wrap(ErrUnknownStatus, "unknown 'to' status", j.MKV{"from": fmt.Sprintf("%T", from), "to": fmt.Sprintf("%T", to)})
return nil, errors.Wrap(ErrUnknownStatus, "unknown 'to' status", j.MKV{"from": fmt.Sprintf("%v", from), "to": fmt.Sprintf("%v", to)})
}
if !sameType(t.req, updater) {
return nil, errors.Wrap(ErrInvalidType, "updater can't be used for this transition")
}
f, ok := fsm.states[from.ShiftStatus()]
if !ok {
return nil, errors.Wrap(ErrUnknownStatus, "unknown 'from' status", j.MKV{"from": fmt.Sprintf("%T", from), "to": fmt.Sprintf("%T", to)})
return nil, errors.Wrap(ErrUnknownStatus, "unknown 'from' status", j.MKV{"from": fmt.Sprintf("%v", from), "to": fmt.Sprintf("%v", to)})
} else if !f.next[to] {
return nil, errors.Wrap(ErrInvalidStateTransition, "", j.MKV{"from": fmt.Sprintf("%T", from), "to": fmt.Sprintf("%T", to)})
return nil, errors.Wrap(ErrInvalidStateTransition, "", j.MKV{"from": fmt.Sprintf("%v", from), "to": fmt.Sprintf("%v", to)})
}

return updateTx(ctx, tx, from, to, updater, fsm.events, t.t, fsm.options)
Expand Down
39 changes: 21 additions & 18 deletions shift_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package shift_test
import (
"context"
"database/sql"
"fmt"
"testing"
"time"

Expand Down Expand Up @@ -291,39 +290,43 @@ func TestGenFSM_Update(t *testing.T) {

var unknownShiftStatus TestStatus = 999
tests := []struct {
name string
from shift.Status
to shift.Status
expectedErr error
name string
from shift.Status
to shift.Status
expErr error
expKVs j.MKV
}{
{
name: "Valid",
from: StatusInit,
to: StatusUpdate,
},
{
name: "Invalid State Transition",
from: StatusComplete,
to: StatusUpdate,
expectedErr: shift.ErrInvalidStateTransition,
name: "Invalid State Transition",
from: StatusComplete,
to: StatusUpdate,
expErr: shift.ErrInvalidStateTransition,
expKVs: j.MKV{"from": StatusComplete, "to": StatusUpdate},
},
{
name: "Unknown 'from' status",
from: unknownShiftStatus,
to: StatusUpdate,
expectedErr: errors.Wrap(shift.ErrUnknownStatus, "unknown 'from' status", j.MKV{"from ": fmt.Sprintf("%T", unknownShiftStatus), "to": fmt.Sprintf("%T", StatusUpdate)}),
name: "Unknown 'from' status",
from: unknownShiftStatus,
to: StatusUpdate,
expErr: errors.Wrap(shift.ErrUnknownStatus, "unknown 'from' status"),
expKVs: j.MKV{"from": unknownShiftStatus, "to": StatusUpdate},
},
{
name: "Unknown 'to' status",
from: StatusUpdate,
to: unknownShiftStatus,
expectedErr: errors.Wrap(shift.ErrUnknownStatus, "unknown 'to' status", j.MKV{"from ": fmt.Sprintf("%T", StatusUpdate), "to": fmt.Sprintf("%T", unknownShiftStatus)}),
name: "Unknown 'to' status",
from: StatusUpdate,
to: unknownShiftStatus,
expErr: errors.Wrap(shift.ErrUnknownStatus, "unknown 'to' status"),
expKVs: j.MKV{"from": StatusUpdate, "to": unknownShiftStatus},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := fsm.Update(ctx, dbc, tt.from, tt.to, update{ID: id, Name: "updateMe", Amount: amount})
jtest.Assert(t, tt.expectedErr, err)
jtest.Assert(t, tt.expErr, err)
})
}
}

0 comments on commit e00cf99

Please sign in to comment.