Files
gocqlx/qb/cmp_test.go
Marian Gappa 87976451ed test(qb): Add tests for qb section (#74)
test(qb): Add missing tests for qb directory

* Add a GROUP BY test without Columns
* Add LtFunc test
* Add LtOrEqFunc test
* Add GtFunc test
* Add GtOrEqFunc test
* Add a Select with a AS between one column test
* Add a Order By DESC test
* Add Sum test
* Add Avg test
* Add Max test
* Add 2 tests for FuncColumn
* Add SetNamed test
* Add AddFunc test
* Add RemoveFunc test
* Add TTL and TimestampNamed test
* Add TTLNamed and Timestamp test
* Add TTL with negative or no duration test
* Add LtFunc test
* Add LtOrEqFunc test
* Add GtFunc test
* Add GtOrEqFunc test
* Add a Select with a AS between one column test
* Add a Order By DESC test
* Add Sum test
* Add Avg test
* Add Max test
* Add SetNamed test
* Add AddFunc test
* Add RemoveFunc test
* Add TTL and TimestampNamed test
* Add TTLNamed and Timestamp test
* Add TTL with negative or no duration test
* Add LtFunc test
* Add LtOrEqFunc test
* Add GtFunc test
* Add GtOrEqFunc test
* Add a Select with a AS between one column test
* Add a Order By DESC test
* Add Sum test
* Add Avg test
* Add Max test
* Add 2 tests for FuncColumn
* Add SetNamed test
* Add AddFunc test
* Add RemoveFunc test
* In qb/select.go, if there are no colums and the query have a GroupBy
statement then the query will be malformated.
* In qb/utils.go, refactor writeCql with a strings.Join call.

Fixes #78
2018-12-17 15:40:03 +01:00

186 lines
2.9 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"},
},
{
C: ContainsKey("cntKey"),
S: "cntKey CONTAINS KEY ?",
N: []string{"cntKey"},
},
// 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"},
},
{
C: ContainsKeyNamed("cntKey", "name"),
S: "cntKey CONTAINS KEY ?",
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()",
},
{
C: LtFunc("eq", Now()),
S: "eq<now()",
},
{
C: LtOrEqFunc("eq", MaxTimeuuid("arg0")),
S: "eq<=maxTimeuuid(?)",
N: []string{"arg0"},
},
{
C: GtFunc("eq", Now()),
S: "eq>now()",
},
{
C: GtOrEqFunc("eq", MaxTimeuuid("arg0")),
S: "eq>=maxTimeuuid(?)",
N: []string{"arg0"},
},
}
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)
}
}
}