Skip to content

Commit

Permalink
Fix lt and lte operators (#150)
Browse files Browse the repository at this point in the history
Signed-off-by: Clément Salaün <[email protected]>

Co-authored-by: Maxence Maireaux <[email protected]>
  • Loading branch information
altitude and flemzord authored Feb 3, 2022
1 parent a35c801 commit a4fe5fa
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
4 changes: 2 additions & 2 deletions pkg/core/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ type ExprLt struct {
}

func (o *ExprLt) Eval(ctx EvalContext) bool {
return o.Op1.eval(ctx).(float64) > o.Op2.eval(ctx).(float64)
return o.Op1.eval(ctx).(float64) < o.Op2.eval(ctx).(float64)
}

func (e ExprLt) MarshalJSON() ([]byte, error) {
Expand Down Expand Up @@ -122,7 +122,7 @@ type ExprLte struct {
}

func (o *ExprLte) Eval(ctx EvalContext) bool {
return o.Op1.eval(ctx).(float64) >= o.Op2.eval(ctx).(float64)
return o.Op1.eval(ctx).(float64) <= o.Op2.eval(ctx).(float64)
}

func (e ExprLte) MarshalJSON() ([]byte, error) {
Expand Down
54 changes: 53 additions & 1 deletion pkg/core/expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package core
import (
"encoding/json"
"fmt"
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestRules(t *testing.T) {
Expand Down Expand Up @@ -44,6 +45,57 @@ func TestRules(t *testing.T) {
},
shouldBeAccepted: true,
},
{
rule: map[string]interface{}{
"$or": []interface{}{
map[string]interface{}{
"$gte": []interface{}{
"$balance", float64(0),
},
},
map[string]interface{}{
"$lte": []interface{}{
"$balance", float64(0),
},
},
},
},
context: EvalContext{
Variables: map[string]interface{}{
"balance": float64(-100),
},
Metadata: map[string]json.RawMessage{},
},
shouldBeAccepted: true,
},
{
rule: map[string]interface{}{
"$lt": []interface{}{
"$balance", float64(0),
},
},
context: EvalContext{
Variables: map[string]interface{}{
"balance": float64(100),
},
Metadata: map[string]json.RawMessage{},
},
shouldBeAccepted: false,
},
{
rule: map[string]interface{}{
"$lte": []interface{}{
"$balance", float64(0),
},
},
context: EvalContext{
Variables: map[string]interface{}{
"balance": float64(0),
},
Metadata: map[string]json.RawMessage{},
},
shouldBeAccepted: true,
},
{
rule: map[string]interface{}{
"$and": []interface{}{
Expand Down
3 changes: 2 additions & 1 deletion pkg/ledger/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package ledger
import (
"context"
"fmt"
"time"

"github.com/numary/ledger/pkg/storage"
"github.com/pkg/errors"
"time"

"github.com/numary/ledger/pkg/core"
"github.com/numary/ledger/pkg/ledger/query"
Expand Down

0 comments on commit a4fe5fa

Please sign in to comment.