The 'value' interface represents a CQL value for use in a comparison, update, or intitialization operation. A consistent interface for this allows us to easily support specifying default-named, custom-named, literal, and evaluated-function values in all these contexts. Parameters to Func should probably also be values to support full composition, but that would be a breaking change because Func's properties are exposed. The value interface could itself be exposed if we wanted to allow clients to pass their own values to SetValue, etc, but for now it is a package-internal abstraction. BLA
173 lines
2.6 KiB
Go
173 lines
2.6 KiB
Go
// Copyright (C) 2017 ScyllaDB
|
|
// Use of this source code is governed by a ALv2-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package qb
|
|
|
|
import (
|
|
"bytes"
|
|
"testing"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
)
|
|
|
|
func TestCmp(t *testing.T) {
|
|
table := []struct {
|
|
C Cmp
|
|
S string
|
|
N []string
|
|
}{
|
|
// Basic comparators
|
|
{
|
|
C: Eq("eq"),
|
|
S: "eq=?",
|
|
N: []string{"eq"},
|
|
},
|
|
{
|
|
C: Lt("lt"),
|
|
S: "lt<?",
|
|
N: []string{"lt"},
|
|
},
|
|
{
|
|
C: LtOrEq("lt"),
|
|
S: "lt<=?",
|
|
N: []string{"lt"},
|
|
},
|
|
{
|
|
C: Gt("gt"),
|
|
S: "gt>?",
|
|
N: []string{"gt"},
|
|
},
|
|
{
|
|
C: GtOrEq("gt"),
|
|
S: "gt>=?",
|
|
N: []string{"gt"},
|
|
},
|
|
{
|
|
C: In("in"),
|
|
S: "in IN ?",
|
|
N: []string{"in"},
|
|
},
|
|
{
|
|
C: Contains("cnt"),
|
|
S: "cnt CONTAINS ?",
|
|
N: []string{"cnt"},
|
|
},
|
|
|
|
// Custom bind names
|
|
{
|
|
C: EqNamed("eq", "name"),
|
|
S: "eq=?",
|
|
N: []string{"name"},
|
|
},
|
|
{
|
|
C: LtNamed("lt", "name"),
|
|
S: "lt<?",
|
|
N: []string{"name"},
|
|
},
|
|
{
|
|
C: LtOrEqNamed("lt", "name"),
|
|
S: "lt<=?",
|
|
N: []string{"name"},
|
|
},
|
|
{
|
|
C: GtNamed("gt", "name"),
|
|
S: "gt>?",
|
|
N: []string{"name"},
|
|
},
|
|
{
|
|
C: GtOrEqNamed("gt", "name"),
|
|
S: "gt>=?",
|
|
N: []string{"name"},
|
|
},
|
|
{
|
|
C: InNamed("in", "name"),
|
|
S: "in IN ?",
|
|
N: []string{"name"},
|
|
},
|
|
{
|
|
C: ContainsNamed("cnt", "name"),
|
|
S: "cnt CONTAINS ?",
|
|
N: []string{"name"},
|
|
},
|
|
|
|
// Literals
|
|
{
|
|
C: EqLit("eq", "litval"),
|
|
S: "eq=litval",
|
|
},
|
|
{
|
|
C: LtLit("lt", "litval"),
|
|
S: "lt<litval",
|
|
},
|
|
{
|
|
C: LtOrEqLit("lt", "litval"),
|
|
S: "lt<=litval",
|
|
},
|
|
{
|
|
C: GtLit("gt", "litval"),
|
|
S: "gt>litval",
|
|
},
|
|
{
|
|
C: GtOrEqLit("gt", "litval"),
|
|
S: "gt>=litval",
|
|
},
|
|
{
|
|
C: InLit("in", "litval"),
|
|
S: "in IN litval",
|
|
},
|
|
{
|
|
C: ContainsLit("cnt", "litval"),
|
|
S: "cnt CONTAINS litval",
|
|
},
|
|
|
|
// Functions
|
|
{
|
|
C: EqFunc("eq", Fn("fn", "arg0", "arg1")),
|
|
S: "eq=fn(?,?)",
|
|
N: []string{"arg0", "arg1"},
|
|
},
|
|
{
|
|
C: EqFunc("eq", MaxTimeuuid("arg0")),
|
|
S: "eq=maxTimeuuid(?)",
|
|
N: []string{"arg0"},
|
|
},
|
|
{
|
|
C: EqFunc("eq", MinTimeuuid("arg0")),
|
|
S: "eq=minTimeuuid(?)",
|
|
N: []string{"arg0"},
|
|
},
|
|
{
|
|
C: EqFunc("eq", Now()),
|
|
S: "eq=now()",
|
|
},
|
|
}
|
|
|
|
buf := bytes.Buffer{}
|
|
for _, test := range table {
|
|
buf.Reset()
|
|
name := test.C.writeCql(&buf)
|
|
if diff := cmp.Diff(test.S, buf.String()); diff != "" {
|
|
t.Error(diff)
|
|
}
|
|
if diff := cmp.Diff(test.N, name); diff != "" {
|
|
t.Error(diff)
|
|
}
|
|
}
|
|
}
|
|
|
|
func BenchmarkCmp(b *testing.B) {
|
|
buf := bytes.Buffer{}
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
buf.Reset()
|
|
c := cmps{
|
|
Eq("id"),
|
|
Lt("user_uuid"),
|
|
LtOrEq("firstname"),
|
|
Gt("stars"),
|
|
}
|
|
c.writeCql(&buf)
|
|
}
|
|
}
|