Skip to content

Commit

Permalink
clause tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RangelReale committed Jun 5, 2024
1 parent 70d6d42 commit a277807
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 2 deletions.
80 changes: 80 additions & 0 deletions expr/clause_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,90 @@ package expr
import (
"testing"

"github.com/rrgmc/litsql"
"github.com/rrgmc/litsql/internal/testutils"
)

func TestC(t *testing.T) {
ex := C("test_me = ?", 98)
testutils.TestExpression(t, ex, "test_me = $1", 98)
}

func TestClause(t *testing.T) {
for _, test := range []struct {
name string
expr litsql.Expression
expected string
expectedArgs []any
expectedError error
}{
{
name: "plain",
expr: clause{
query: "SELECT a, b FROM alphabet",
},
expected: `SELECT a, b FROM alphabet`,
},
{
name: "escaped args",
expr: clause{
query: `SELECT a, b FROM "alphabet\?" WHERE c = ? AND d <= ?`,
args: []any{1, 2},
},
expected: `SELECT a, b FROM "alphabet?" WHERE c = $1 AND d <= $2`,
expectedArgs: []any{1, 2},
},
{
name: "mismatched args and placeholders",
expr: clause{
query: "SELECT a, b FROM alphabet WHERE c = ? AND d <= ?",
},
expected: `SELECT a, b FROM alphabet WHERE c = $1 AND d <= $2`,
expectedError: &clauseError{args: 0, placeholders: 2},
},
{
name: "numbered args",
expr: clause{
query: "SELECT a, b FROM alphabet WHERE c = ? AND d <= ?",
args: []any{1, 2},
},
expected: `SELECT a, b FROM alphabet WHERE c = $1 AND d <= $2`,
expectedArgs: []any{1, 2},
},
{
name: "expr args",
expr: clause{
query: "SELECT a, b FROM alphabet WHERE c IN (?) AND d <= ?",
args: []any{In(5, 6, 7), 2},
},
expected: `SELECT a, b FROM alphabet WHERE c IN ($1, $2, $3) AND d <= $4`,
expectedArgs: []any{5, 6, 7, 2},
},
{
name: "expr args group",
expr: clause{
query: "SELECT a, b FROM alphabet WHERE c IN ? AND d <= ?",
args: []any{InP(5, 6, 7), 2},
},
expected: `SELECT a, b FROM alphabet WHERE c IN ($1, $2, $3) AND d <= $4`,
expectedArgs: []any{5, 6, 7, 2},
},
{
name: "expr args quote",
expr: clause{
query: "SELECT a, b FROM alphabet WHERE c = ? AND d <= ?",
args: []any{Quote("AA"), 2},
},
expected: `SELECT a, b FROM alphabet WHERE c = "AA" AND d <= $1`,
expectedArgs: []any{2},
},
} {
t.Run(test.name, func(t *testing.T) {
if test.expectedError != nil {
testutils.TestExpressionErrorIs(t, test.expr, test.expectedError)
} else {
testutils.TestExpression(t, test.expr, test.expected, test.expectedArgs...)
}
})
}
}
16 changes: 14 additions & 2 deletions internal/testutils/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
"gotest.tools/v3/assert"
)

type errorEquals interface {
Equal(I error) bool
}

func TestExpression(t *testing.T, e litsql.Expression, output string, args ...any) {
t.Helper()

Expand Down Expand Up @@ -51,7 +55,11 @@ func TestExpressionErrorIs(t *testing.T, e litsql.Expression, errIs error) {

_, err := litsql.Express(w, NewTestDialect(), 1, e)
if errIs != nil {
assert.ErrorIs(t, err, errIs)
if eeq, ok := errIs.(errorEquals); ok {
assert.Assert(t, eeq.Equal(err))
} else {
assert.ErrorIs(t, err, errIs)
}
} else {
assert.Assert(t, err != nil)
}
Expand All @@ -71,7 +79,11 @@ func TestExpressionSliceErrorIs(t *testing.T, e []litsql.Expression, errIs error

_, err := litsql.ExpressSlice(w, NewTestDialect(), 1, e, nil, nil, nil)
if errIs != nil {
assert.ErrorIs(t, err, errIs)
if eeq, ok := errIs.(errorEquals); ok {
assert.Assert(t, eeq.Equal(err))
} else {
assert.ErrorIs(t, err, errIs)
}
} else {
assert.Assert(t, err != nil)
}
Expand Down

0 comments on commit a277807

Please sign in to comment.