Skip to content

Commit

Permalink
dynamic: implement Compare() and add tests to Compare()
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Nov 7, 2024
1 parent 7b94c78 commit ad2ab05
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 7 deletions.
10 changes: 3 additions & 7 deletions pkg/dynamic/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ type Diff struct {
// a (after)
// b (before)
func Compare(a, b interface{}) ([]Diff, error) {
var diffs []Diff

ra := reflect.ValueOf(a)
if ra.Kind() == reflect.Ptr {
ra = ra.Elem()
Expand Down Expand Up @@ -56,13 +54,11 @@ func Compare(a, b interface{}) ([]Diff, error) {
},
}, nil
}
} else if raKind == reflect.Struct {
return compareStruct(ra, rb)
}

if raKind == reflect.Struct {

}

return diffs, nil
return nil, nil
}

func compareStruct(a, b reflect.Value) ([]Diff, error) {
Expand Down
75 changes: 75 additions & 0 deletions pkg/dynamic/compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,81 @@ func Test_convertToStr(t *testing.T) {
})
}

func Test_Compare(t *testing.T) {
tests := []struct {
name string
a, b interface{}
want []Diff
wantErr assert.ErrorAssertionFunc
}{
{
name: "order",
wantErr: assert.NoError,
a: &types.Order{
SubmitOrder: types.SubmitOrder{
Symbol: "BTCUSDT",
Quantity: fixedpoint.NewFromFloat(100.0),
},
Status: types.OrderStatusFilled,
ExecutedQuantity: fixedpoint.NewFromFloat(100.0),
},
b: &types.Order{
SubmitOrder: types.SubmitOrder{
Symbol: "BTCUSDT",
Quantity: fixedpoint.NewFromFloat(100.0),
},
ExecutedQuantity: fixedpoint.NewFromFloat(50.0),
Status: types.OrderStatusPartiallyFilled,
},
want: []Diff{
{
Field: "Status",
Before: "PARTIALLY_FILLED",
After: "FILLED",
},
{
Field: "ExecutedQuantity",
Before: "50",
After: "100",
},
},
},
{
name: "deposit and order",
wantErr: assert.NoError,
a: &types.Deposit{
Address: "0x6666",
TransactionID: "0x3333",
Status: types.DepositPending,
Confirmation: "10/15",
},
b: &types.Deposit{
Address: "0x6666",
TransactionID: "0x3333",
Status: types.DepositPending,
Confirmation: "1/15",
},
want: []Diff{
{
Field: "Confirmation",
Before: "1/15",
After: "10/15",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Compare(tt.a, tt.b)
if !tt.wantErr(t, err, fmt.Sprintf("Compare(%v, %v)", tt.a, tt.b)) {
return
}

assert.Equalf(t, tt.want, got, "Compare(%v, %v)", tt.a, tt.b)
})
}
}

func Test_compareStruct(t *testing.T) {
tests := []struct {
name string
Expand Down

0 comments on commit ad2ab05

Please sign in to comment.