Skip to content

Commit

Permalink
Supporting And/Or cases when there is only one expression passed as a…
Browse files Browse the repository at this point in the history
…n argument (#654)
  • Loading branch information
mateusz-sekara authored Jul 15, 2024
1 parent 7278464 commit b31fc8f
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/types/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,18 @@ func TxHash(txHash string) Expression {
}

func And(expressions ...Expression) Expression {
if len(expressions) == 1 {
return expressions[0]
}
return Expression{
BoolExpression: BoolExpression{Expressions: expressions, BoolOperator: AND},
}
}

func Or(expressions ...Expression) Expression {
if len(expressions) == 1 {
return expressions[0]
}
return Expression{
BoolExpression: BoolExpression{Expressions: expressions, BoolOperator: OR},
}
Expand Down
66 changes: 66 additions & 0 deletions pkg/types/query/query_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package query

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/smartcontractkit/chainlink-common/pkg/types/query/primitives"
)

func Test_AndOrEdgeCases(t *testing.T) {
tests := []struct {
name string
expressions []Expression
constructor func(expressions ...Expression) Expression
expected Expression
}{
{
name: "And with no expressions",
constructor: And,
expected: And(),
},
{
name: "Or with no expressions",
constructor: Or,
expected: Or(),
},
{
name: "And with one expression",
expressions: []Expression{TxHash("txHash")},
constructor: And,
expected: TxHash("txHash"),
},
{
name: "Or with one expression",
expressions: []Expression{TxHash("txHash")},
constructor: Or,
expected: TxHash("txHash"),
},
{
name: "And with multiple expressions",
expressions: []Expression{TxHash("txHash"), Block(123, primitives.Eq)},
constructor: And,
expected: And(
TxHash("txHash"),
Block(123, primitives.Eq),
),
},
{
name: "Or with multiple expressions",
expressions: []Expression{TxHash("txHash"), Block(123, primitives.Eq)},
constructor: Or,
expected: Or(
TxHash("txHash"),
Block(123, primitives.Eq),
),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.constructor(tt.expressions...)
require.Equal(t, tt.expected, got)
})
}
}

0 comments on commit b31fc8f

Please sign in to comment.