Delete tests

This commit is contained in:
2025-11-20 21:56:49 +01:00
parent 84c58f45a3
commit be8c537b9f
20 changed files with 0 additions and 4208 deletions

View File

@@ -1,14 +0,0 @@
// 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 "testing"
func BenchmarkBatchBuilder(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
Batch().Add(mockBuilder{"INSERT INTO cycling.cyclist_name (id,user_uuid,firstname) VALUES (?,?,?) ", []string{"id", "user_uuid", "firstname"}}).ToCql()
}
}

View File

@@ -1,97 +0,0 @@
// 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 (
"testing"
"time"
"github.com/google/go-cmp/cmp"
)
type mockBuilder struct {
stmt string
names []string
}
func (b mockBuilder) ToCql() (stmt string, names []string) {
return b.stmt, b.names
}
func TestBatchBuilder(t *testing.T) {
m := mockBuilder{"INSERT INTO cycling.cyclist_name (id,user_uuid,firstname) VALUES (?,?,?) ", []string{"id", "user_uuid", "firstname"}}
table := []struct {
B *BatchBuilder
N []string
S string
}{
// Basic test for Batch
{
B: Batch().Add(m),
S: "BEGIN BATCH INSERT INTO cycling.cyclist_name (id,user_uuid,firstname) VALUES (?,?,?) ; APPLY BATCH ",
N: []string{"id", "user_uuid", "firstname"},
},
// Add statement
{
B: Batch().
AddWithPrefix("a", m).
AddWithPrefix("b", m),
S: "BEGIN BATCH INSERT INTO cycling.cyclist_name (id,user_uuid,firstname) VALUES (?,?,?) ; " +
"INSERT INTO cycling.cyclist_name (id,user_uuid,firstname) VALUES (?,?,?) ; APPLY BATCH ",
N: []string{"a.id", "a.user_uuid", "a.firstname", "b.id", "b.user_uuid", "b.firstname"},
},
// Add UNLOGGED
{
B: Batch().UnLogged(),
S: "BEGIN UNLOGGED BATCH APPLY BATCH ",
},
// Add COUNTER
{
B: Batch().Counter(),
S: "BEGIN COUNTER BATCH APPLY BATCH ",
},
// Add TTL
{
B: Batch().TTL(time.Second),
S: "BEGIN BATCH USING TTL 1 APPLY BATCH ",
},
{
B: Batch().TTLNamed("ttl"),
S: "BEGIN BATCH USING TTL ? APPLY BATCH ",
N: []string{"ttl"},
},
// Add TIMESTAMP
{
B: Batch().Timestamp(time.Date(2005, 5, 5, 0, 0, 0, 0, time.UTC)),
S: "BEGIN BATCH USING TIMESTAMP 1115251200000000 APPLY BATCH ",
},
{
B: Batch().TimestampNamed("ts"),
S: "BEGIN BATCH USING TIMESTAMP ? APPLY BATCH ",
N: []string{"ts"},
},
// Add TIMEOUT
{
B: Batch().Timeout(time.Second),
S: "BEGIN BATCH USING TIMEOUT 1s APPLY BATCH ",
},
{
B: Batch().TimeoutNamed("to"),
S: "BEGIN BATCH USING TIMEOUT ? APPLY BATCH ",
N: []string{"to"},
},
}
for _, test := range table {
stmt, names := test.B.ToCql()
if diff := cmp.Diff(test.S, stmt); diff != "" {
t.Error(diff)
}
if diff := cmp.Diff(test.N, names); diff != "" {
t.Error(diff)
}
}
}

View File

@@ -1,25 +0,0 @@
// 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"
)
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)
}
}

View File

@@ -1,14 +0,0 @@
// 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 "testing"
func BenchmarkDeleteBuilder(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
Delete("cycling.cyclist_name").Columns("id", "user_uuid", "firstname", "stars").Where(Eq("id")).ToCql()
}
}

View File

@@ -1,109 +0,0 @@
// 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 (
"testing"
"time"
"github.com/google/go-cmp/cmp"
)
func TestDeleteBuilder(t *testing.T) {
w := EqNamed("id", "expr")
table := []struct {
B *DeleteBuilder
N []string
S string
}{
// Basic test for delete
{
B: Delete("cycling.cyclist_name").Where(w),
S: "DELETE FROM cycling.cyclist_name WHERE id=? ",
N: []string{"expr"},
},
// Change table name
{
B: Delete("cycling.cyclist_name").Where(w).From("Foobar"),
S: "DELETE FROM Foobar WHERE id=? ",
N: []string{"expr"},
},
// Add column
{
B: Delete("cycling.cyclist_name").Where(w).Columns("stars"),
S: "DELETE stars FROM cycling.cyclist_name WHERE id=? ",
N: []string{"expr"},
},
// Add WHERE
{
B: Delete("cycling.cyclist_name").Where(w, Gt("firstname")),
S: "DELETE FROM cycling.cyclist_name WHERE id=? AND firstname>? ",
N: []string{"expr", "firstname"},
},
// Add a tuple column
{
B: Delete("cycling.cyclist_name").Where(EqTuple("id", 2)).Columns("stars"),
S: "DELETE stars FROM cycling.cyclist_name WHERE id=(?,?) ",
N: []string{"id[0]", "id[1]"},
},
// Add WHERE for tuple column
{
B: Delete("cycling.cyclist_name").Where(w, GtTuple("firstname", 2)),
S: "DELETE FROM cycling.cyclist_name WHERE id=? AND firstname>(?,?) ",
N: []string{"expr", "firstname[0]", "firstname[1]"},
},
// Add WHERE for all tuple columns
{
B: Delete("cycling.cyclist_name").Where(EqTuple("id", 2), GtTuple("firstname", 2)),
S: "DELETE FROM cycling.cyclist_name WHERE id=(?,?) AND firstname>(?,?) ",
N: []string{"id[0]", "id[1]", "firstname[0]", "firstname[1]"},
},
// Add IF
{
B: Delete("cycling.cyclist_name").Where(w).If(Gt("firstname")),
S: "DELETE FROM cycling.cyclist_name WHERE id=? IF firstname>? ",
N: []string{"expr", "firstname"},
},
// Add TIMESTAMP
{
B: Delete("cycling.cyclist_name").Where(w).Timestamp(time.Date(2005, 5, 5, 0, 0, 0, 0, time.UTC)),
S: "DELETE FROM cycling.cyclist_name USING TIMESTAMP 1115251200000000 WHERE id=? ",
N: []string{"expr"},
},
{
B: Delete("cycling.cyclist_name").Where(w).TimestampNamed("ts"),
S: "DELETE FROM cycling.cyclist_name USING TIMESTAMP ? WHERE id=? ",
N: []string{"ts", "expr"},
},
// Add TIMEOUT
{
B: Delete("cycling.cyclist_name").Where(w).Timeout(time.Second),
S: "DELETE FROM cycling.cyclist_name USING TIMEOUT 1s WHERE id=? ",
N: []string{"expr"},
},
{
B: Delete("cycling.cyclist_name").Where(w).TimeoutNamed("to"),
S: "DELETE FROM cycling.cyclist_name USING TIMEOUT ? WHERE id=? ",
N: []string{"to", "expr"},
},
// Add IF EXISTS
{
B: Delete("cycling.cyclist_name").Where(w).Existing(),
S: "DELETE FROM cycling.cyclist_name WHERE id=? IF EXISTS ",
N: []string{"expr"},
},
}
for _, test := range table {
stmt, names := test.B.ToCql()
if diff := cmp.Diff(test.S, stmt); diff != "" {
t.Error(diff)
}
if diff := cmp.Diff(test.N, names); diff != "" {
t.Error(diff, names)
}
}
}

View File

@@ -1,14 +0,0 @@
// 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 "testing"
func BenchmarkInsertBuilder(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
Insert("cycling.cyclist_name").Columns("id", "user_uuid", "firstname", "stars").ToCql()
}
}

View File

@@ -1,150 +0,0 @@
// 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 (
"testing"
"time"
"github.com/google/go-cmp/cmp"
)
func TestInsertBuilder(t *testing.T) {
table := []struct {
B *InsertBuilder
N []string
S string
}{
// Basic test for insert
{
B: Insert("cycling.cyclist_name").Columns("id", "user_uuid", "firstname"),
S: "INSERT INTO cycling.cyclist_name (id,user_uuid,firstname) VALUES (?,?,?) ",
N: []string{"id", "user_uuid", "firstname"},
},
// Basic test for insert JSON
{
B: Insert("cycling.cyclist_name").Columns("id", "user_uuid", "firstname").Json(),
S: "INSERT INTO cycling.cyclist_name JSON ?",
N: nil,
},
// Change table name
{
B: Insert("cycling.cyclist_name").Columns("id", "user_uuid", "firstname").Into("Foobar"),
S: "INSERT INTO Foobar (id,user_uuid,firstname) VALUES (?,?,?) ",
N: []string{"id", "user_uuid", "firstname"},
},
// Add columns
{
B: Insert("cycling.cyclist_name").Columns("id", "user_uuid", "firstname").Columns("stars"),
S: "INSERT INTO cycling.cyclist_name (id,user_uuid,firstname,stars) VALUES (?,?,?,?) ",
N: []string{"id", "user_uuid", "firstname", "stars"},
},
// Add a named column
{
B: Insert("cycling.cyclist_name").Columns("id", "user_uuid", "firstname").NamedColumn("stars", "stars_name"),
S: "INSERT INTO cycling.cyclist_name (id,user_uuid,firstname,stars) VALUES (?,?,?,?) ",
N: []string{"id", "user_uuid", "firstname", "stars_name"},
},
// Add a literal column
{
B: Insert("cycling.cyclist_name").Columns("id", "user_uuid", "firstname").LitColumn("stars", "stars_lit"),
S: "INSERT INTO cycling.cyclist_name (id,user_uuid,firstname,stars) VALUES (?,?,?,stars_lit) ",
N: []string{"id", "user_uuid", "firstname"},
},
// Add TTL
{
B: Insert("cycling.cyclist_name").Columns("id", "user_uuid", "firstname").TTL(time.Second),
S: "INSERT INTO cycling.cyclist_name (id,user_uuid,firstname) VALUES (?,?,?) USING TTL 1 ",
N: []string{"id", "user_uuid", "firstname"},
},
{
B: Insert("cycling.cyclist_name").Columns("id", "user_uuid", "firstname").TTLNamed("ttl"),
S: "INSERT INTO cycling.cyclist_name (id,user_uuid,firstname) VALUES (?,?,?) USING TTL ? ",
N: []string{"id", "user_uuid", "firstname", "ttl"},
},
// Add TIMESTAMP
{
B: Insert("cycling.cyclist_name").Columns("id", "user_uuid", "firstname").Timestamp(time.Date(2005, 5, 5, 0, 0, 0, 0, time.UTC)),
S: "INSERT INTO cycling.cyclist_name (id,user_uuid,firstname) VALUES (?,?,?) USING TIMESTAMP 1115251200000000 ",
N: []string{"id", "user_uuid", "firstname"},
},
{
B: Insert("cycling.cyclist_name").Columns("id", "user_uuid", "firstname").TimestampNamed("ts"),
S: "INSERT INTO cycling.cyclist_name (id,user_uuid,firstname) VALUES (?,?,?) USING TIMESTAMP ? ",
N: []string{"id", "user_uuid", "firstname", "ts"},
},
// Add TIMESTAMP
{
B: Insert("cycling.cyclist_name").Columns("id", "user_uuid", "firstname").Timestamp(time.Date(2005, 5, 5, 0, 0, 0, 0, time.UTC)),
S: "INSERT INTO cycling.cyclist_name (id,user_uuid,firstname) VALUES (?,?,?) USING TIMESTAMP 1115251200000000 ",
N: []string{"id", "user_uuid", "firstname"},
},
{
B: Insert("cycling.cyclist_name").Columns("id", "user_uuid", "firstname").TimestampNamed("ts"),
S: "INSERT INTO cycling.cyclist_name (id,user_uuid,firstname) VALUES (?,?,?) USING TIMESTAMP ? ",
N: []string{"id", "user_uuid", "firstname", "ts"},
},
// Add TIMESTAMP
{
B: Insert("cycling.cyclist_name").Columns("id", "user_uuid", "firstname").Timestamp(time.Date(2005, 5, 5, 0, 0, 0, 0, time.UTC)),
S: "INSERT INTO cycling.cyclist_name (id,user_uuid,firstname) VALUES (?,?,?) USING TIMESTAMP 1115251200000000 ",
N: []string{"id", "user_uuid", "firstname"},
},
{
B: Insert("cycling.cyclist_name").Columns("id", "user_uuid", "firstname").TimestampNamed("ts"),
S: "INSERT INTO cycling.cyclist_name (id,user_uuid,firstname) VALUES (?,?,?) USING TIMESTAMP ? ",
N: []string{"id", "user_uuid", "firstname", "ts"},
},
// Add TIMEOUT
{
B: Insert("cycling.cyclist_name").Columns("id", "user_uuid", "firstname").Timeout(time.Second),
S: "INSERT INTO cycling.cyclist_name (id,user_uuid,firstname) VALUES (?,?,?) USING TIMEOUT 1s ",
N: []string{"id", "user_uuid", "firstname"},
},
{
B: Insert("cycling.cyclist_name").Columns("id", "user_uuid", "firstname").TimeoutNamed("to"),
S: "INSERT INTO cycling.cyclist_name (id,user_uuid,firstname) VALUES (?,?,?) USING TIMEOUT ? ",
N: []string{"id", "user_uuid", "firstname", "to"},
},
// Add TupleColumn
{
B: Insert("cycling.cyclist_name").TupleColumn("id", 2),
S: "INSERT INTO cycling.cyclist_name (id) VALUES ((?,?)) ",
N: []string{"id[0]", "id[1]"},
},
{
B: Insert("cycling.cyclist_name").TupleColumn("id", 2).Columns("user_uuid"),
S: "INSERT INTO cycling.cyclist_name (id,user_uuid) VALUES ((?,?),?) ",
N: []string{"id[0]", "id[1]", "user_uuid"},
},
// Add IF NOT EXISTS
{
B: Insert("cycling.cyclist_name").Columns("id", "user_uuid", "firstname").Unique(),
S: "INSERT INTO cycling.cyclist_name (id,user_uuid,firstname) VALUES (?,?,?) IF NOT EXISTS ",
N: []string{"id", "user_uuid", "firstname"},
},
// Add FuncColumn
{
B: Insert("cycling.cyclist_name").FuncColumn("id", Now()),
S: "INSERT INTO cycling.cyclist_name (id) VALUES (now()) ",
N: nil,
},
{
B: Insert("cycling.cyclist_name").FuncColumn("id", Now()).Columns("user_uuid"),
S: "INSERT INTO cycling.cyclist_name (id,user_uuid) VALUES (now(),?) ",
N: []string{"user_uuid"},
},
}
for _, test := range table {
stmt, names := test.B.ToCql()
if diff := cmp.Diff(test.S, stmt); diff != "" {
t.Error(diff)
}
if diff := cmp.Diff(test.N, names); diff != "" {
t.Error(diff)
}
}
}

View File

@@ -1,31 +0,0 @@
// 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 "testing"
func BenchmarkSelectBuilder(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
Select("cycling.cyclist_name").
Columns("id", "user_uuid", "firstname", "surname", "stars").
Where(Eq("id")).
ToCql()
}
}
func BenchmarkSelectBuildAssign(b *testing.B) {
b.ResetTimer()
cols := []string{
"id", "user_uuid", "firstname",
"surname", "stars",
}
for i := 0; i < b.N; i++ {
Select("cycling.cyclist_name").
Columns(cols...).
Where(Eq("id")).
ToCql()
}
}

View File

@@ -1,221 +0,0 @@
// 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 (
"testing"
"time"
"github.com/google/go-cmp/cmp"
)
func TestSelectBuilder(t *testing.T) {
w := EqNamed("id", "expr")
table := []struct {
B *SelectBuilder
N []string
S string
}{
// Basic test for select *
{
B: Select("cycling.cyclist_name"),
S: "SELECT * FROM cycling.cyclist_name ",
},
// Basic test for select columns
{
B: Select("cycling.cyclist_name").Columns("id", "user_uuid", "firstname"),
S: "SELECT id,user_uuid,firstname FROM cycling.cyclist_name ",
},
// Add a SELECT AS column
{
B: Select("cycling.cyclist_name").Columns("id", "user_uuid", As("firstname", "name")),
S: "SELECT id,user_uuid,firstname AS name FROM cycling.cyclist_name ",
},
// Basic test for select columns as JSON
{
B: Select("cycling.cyclist_name").Columns("id", "user_uuid", "firstname").Json(),
S: "SELECT JSON id,user_uuid,firstname FROM cycling.cyclist_name ",
},
// Add a SELECT AS column as JSON
{
B: Select("cycling.cyclist_name").Columns("id", "user_uuid", As("firstname", "name")).Json(),
S: "SELECT JSON id,user_uuid,firstname AS name FROM cycling.cyclist_name ",
},
// Add a SELECT AS column 2
{
B: Select("cycling.cyclist_name").
Columns(As("firstname", "name"), "id", As("user_uuid", "user")),
S: "SELECT firstname AS name,id,user_uuid AS user FROM cycling.cyclist_name ",
},
// Basic test for select distinct
{
B: Select("cycling.cyclist_name").Distinct("id"),
S: "SELECT DISTINCT id FROM cycling.cyclist_name ",
},
// Change table name
{
B: Select("cycling.cyclist_name").From("Foobar"),
S: "SELECT * FROM Foobar ",
},
// Add WHERE
{
B: Select("cycling.cyclist_name").Where(w, Gt("firstname")),
S: "SELECT * FROM cycling.cyclist_name WHERE id=? AND firstname>? ",
N: []string{"expr", "firstname"},
},
// Add WHERE with tuple
{
B: Select("cycling.cyclist_name").Where(EqTuple("id", 2), Gt("firstname")),
S: "SELECT * FROM cycling.cyclist_name WHERE id=(?,?) AND firstname>? ",
N: []string{"id[0]", "id[1]", "firstname"},
},
// Add WHERE with only tuples
{
B: Select("cycling.cyclist_name").Where(EqTuple("id", 2), GtTuple("firstname", 2)),
S: "SELECT * FROM cycling.cyclist_name WHERE id=(?,?) AND firstname>(?,?) ",
N: []string{"id[0]", "id[1]", "firstname[0]", "firstname[1]"},
},
// Add TIMEOUT
{
B: Select("cycling.cyclist_name").Where(w, Gt("firstname")).Timeout(time.Second),
S: "SELECT * FROM cycling.cyclist_name WHERE id=? AND firstname>? USING TIMEOUT 1s ",
N: []string{"expr", "firstname"},
},
{
B: Select("cycling.cyclist_name").Where(w, Gt("firstname")).TimeoutNamed("to"),
S: "SELECT * FROM cycling.cyclist_name WHERE id=? AND firstname>? USING TIMEOUT ? ",
N: []string{"expr", "firstname", "to"},
},
// Add GROUP BY
{
B: Select("cycling.cyclist_name").Columns("MAX(stars) as max_stars").GroupBy("id"),
S: "SELECT id,MAX(stars) as max_stars FROM cycling.cyclist_name GROUP BY id ",
},
// Add GROUP BY
{
B: Select("cycling.cyclist_name").GroupBy("id"),
S: "SELECT id FROM cycling.cyclist_name GROUP BY id ",
},
// Add GROUP BY two columns
{
B: Select("cycling.cyclist_name").GroupBy("id", "user_uuid"),
S: "SELECT id,user_uuid FROM cycling.cyclist_name GROUP BY id,user_uuid ",
},
// Add ORDER BY
{
B: Select("cycling.cyclist_name").Where(w).OrderBy("firstname", ASC),
S: "SELECT * FROM cycling.cyclist_name WHERE id=? ORDER BY firstname ASC ",
N: []string{"expr"},
},
// Add ORDER BY
{
B: Select("cycling.cyclist_name").Where(w).OrderBy("firstname", DESC),
S: "SELECT * FROM cycling.cyclist_name WHERE id=? ORDER BY firstname DESC ",
N: []string{"expr"},
},
// Add ORDER BY two columns
{
B: Select("cycling.cyclist_name").Where(w).OrderBy("firstname", ASC).OrderBy("lastname", DESC),
S: "SELECT * FROM cycling.cyclist_name WHERE id=? ORDER BY firstname ASC,lastname DESC ",
N: []string{"expr"},
},
// Add LIMIT
{
B: Select("cycling.cyclist_name").Where(w).Limit(10),
S: "SELECT * FROM cycling.cyclist_name WHERE id=? LIMIT 10 ",
N: []string{"expr"},
},
// Add named LIMIT
{
B: Select("cycling.cyclist_name").Where(w).LimitNamed("limit"),
S: "SELECT * FROM cycling.cyclist_name WHERE id=? LIMIT ? ",
N: []string{"expr", "limit"},
},
// Add PER PARTITION LIMIT
{
B: Select("cycling.cyclist_name").Where(w).LimitPerPartition(10),
S: "SELECT * FROM cycling.cyclist_name WHERE id=? PER PARTITION LIMIT 10 ",
N: []string{"expr"},
},
// Add named PER PARTITION LIMIT
{
B: Select("cycling.cyclist_name").Where(w).LimitPerPartitionNamed("partition_limit"),
S: "SELECT * FROM cycling.cyclist_name WHERE id=? PER PARTITION LIMIT ? ",
N: []string{"expr", "partition_limit"},
},
// Add PER PARTITION LIMIT and LIMIT
{
B: Select("cycling.cyclist_name").Where(w).LimitPerPartition(2).Limit(10),
S: "SELECT * FROM cycling.cyclist_name WHERE id=? PER PARTITION LIMIT 2 LIMIT 10 ",
N: []string{"expr"},
},
// Add named PER PARTITION LIMIT and LIMIT
{
B: Select("cycling.cyclist_name").Where(w).LimitPerPartitionNamed("partition_limit").LimitNamed("limit"),
S: "SELECT * FROM cycling.cyclist_name WHERE id=? PER PARTITION LIMIT ? LIMIT ? ",
N: []string{"expr", "partition_limit", "limit"},
},
// Add ALLOW FILTERING
{
B: Select("cycling.cyclist_name").Where(w).AllowFiltering(),
S: "SELECT * FROM cycling.cyclist_name WHERE id=? ALLOW FILTERING ",
N: []string{"expr"},
},
// Add ALLOW FILTERING and BYPASS CACHE
{
B: Select("cycling.cyclist_name").Where(w).AllowFiltering().BypassCache(),
S: "SELECT * FROM cycling.cyclist_name WHERE id=? ALLOW FILTERING BYPASS CACHE ",
N: []string{"expr"},
},
// Add BYPASS CACHE
{
B: Select("cycling.cyclist_name").Where(w).BypassCache(),
S: "SELECT * FROM cycling.cyclist_name WHERE id=? BYPASS CACHE ",
N: []string{"expr"},
},
// Add COUNT all
{
B: Select("cycling.cyclist_name").CountAll().Where(Gt("stars")),
S: "SELECT count(*) FROM cycling.cyclist_name WHERE stars>? ",
N: []string{"stars"},
},
// Add COUNT with GROUP BY
{
B: Select("cycling.cyclist_name").Count("stars").GroupBy("id"),
S: "SELECT id,count(stars) FROM cycling.cyclist_name GROUP BY id ",
},
// Add Min
{
B: Select("cycling.cyclist_name").Min("stars"),
S: "SELECT min(stars) FROM cycling.cyclist_name ",
},
// Add Sum
{
B: Select("cycling.cyclist_name").Sum("*"),
S: "SELECT sum(*) FROM cycling.cyclist_name ",
},
// Add Avg
{
B: Select("cycling.cyclist_name").Avg("stars"),
S: "SELECT avg(stars) FROM cycling.cyclist_name ",
},
// Add Max
{
B: Select("cycling.cyclist_name").Max("stars"),
S: "SELECT max(stars) FROM cycling.cyclist_name ",
},
}
for _, test := range table {
stmt, names := test.B.ToCql()
if diff := cmp.Diff(test.S, stmt); diff != "" {
t.Error(diff)
}
if diff := cmp.Diff(test.N, names); diff != "" {
t.Error(diff)
}
}
}

View File

@@ -1,136 +0,0 @@
// 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 TestToken(t *testing.T) {
table := []struct {
C Cmp
S string
N []string
}{
// Basic comparators
{
C: Token("a", "b").Eq(),
S: "token(a,b)=token(?,?)",
N: []string{"a", "b"},
},
{
C: Token("a", "b").Lt(),
S: "token(a,b)<token(?,?)",
N: []string{"a", "b"},
},
{
C: Token("a", "b").LtOrEq(),
S: "token(a,b)<=token(?,?)",
N: []string{"a", "b"},
},
{
C: Token("a", "b").Gt(),
S: "token(a,b)>token(?,?)",
N: []string{"a", "b"},
},
{
C: Token("a", "b").GtOrEq(),
S: "token(a,b)>=token(?,?)",
N: []string{"a", "b"},
},
// Custom bind names
{
C: Token("a", "b").EqNamed("c", "d"),
S: "token(a,b)=token(?,?)",
N: []string{"c", "d"},
},
{
C: Token("a", "b").LtNamed("c", "d"),
S: "token(a,b)<token(?,?)",
N: []string{"c", "d"},
},
{
C: Token("a", "b").LtOrEqNamed("c", "d"),
S: "token(a,b)<=token(?,?)",
N: []string{"c", "d"},
},
{
C: Token("a", "b").GtNamed("c", "d"),
S: "token(a,b)>token(?,?)",
N: []string{"c", "d"},
},
{
C: Token("a", "b").GtOrEqNamed("c", "d"),
S: "token(a,b)>=token(?,?)",
N: []string{"c", "d"},
},
{
C: Token("a", "b").EqValue(),
S: "token(a,b)=?",
N: []string{"token"},
},
{
C: Token("a", "b").EqValueNamed("c"),
S: "token(a,b)=?",
N: []string{"c"},
},
{
C: Token("a", "b").LtValue(),
S: "token(a,b)<?",
N: []string{"token"},
},
{
C: Token("a", "b").LtValueNamed("c"),
S: "token(a,b)<?",
N: []string{"c"},
},
{
C: Token("a", "b").LtOrEqValue(),
S: "token(a,b)<=?",
N: []string{"token"},
},
{
C: Token("a", "b").LtOrEqValueNamed("c"),
S: "token(a,b)<=?",
N: []string{"c"},
},
{
C: Token("a", "b").GtValue(),
S: "token(a,b)>?",
N: []string{"token"},
},
{
C: Token("a", "b").GtValueNamed("c"),
S: "token(a,b)>?",
N: []string{"c"},
},
{
C: Token("a", "b").GtOrEqValue(),
S: "token(a,b)>=?",
N: []string{"token"},
},
{
C: Token("a", "b").GtOrEqValueNamed("c"),
S: "token(a,b)>=?",
N: []string{"c"},
},
}
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)
}
}
}

View File

@@ -1,14 +0,0 @@
// 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 "testing"
func BenchmarkUpdateBuilder(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
Update("cycling.cyclist_name").Set("id", "user_uuid", "firstname", "stars").Where(Eq("id")).ToCql()
}
}

View File

@@ -1,181 +0,0 @@
// 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 (
"testing"
"time"
"github.com/google/go-cmp/cmp"
)
func TestUpdateBuilder(t *testing.T) {
w := EqNamed("id", "expr")
table := []struct {
B *UpdateBuilder
N []string
S string
}{
// Basic test for update
{
B: Update("cycling.cyclist_name").Set("id", "user_uuid", "firstname").Where(w),
S: "UPDATE cycling.cyclist_name SET id=?,user_uuid=?,firstname=? WHERE id=? ",
N: []string{"id", "user_uuid", "firstname", "expr"},
},
// Change table name
{
B: Update("cycling.cyclist_name").Set("id", "user_uuid", "firstname").Where(w).Table("Foobar"),
S: "UPDATE Foobar SET id=?,user_uuid=?,firstname=? WHERE id=? ",
N: []string{"id", "user_uuid", "firstname", "expr"},
},
// Add SET
{
B: Update("cycling.cyclist_name").Set("id", "user_uuid", "firstname").Where(w).Set("stars"),
S: "UPDATE cycling.cyclist_name SET id=?,user_uuid=?,firstname=?,stars=? WHERE id=? ",
N: []string{"id", "user_uuid", "firstname", "stars", "expr"},
},
// Add SET literal
{
B: Update("cycling.cyclist_name").SetLit("user_uuid", "literal_uuid").Where(w).Set("stars"),
S: "UPDATE cycling.cyclist_name SET user_uuid=literal_uuid,stars=? WHERE id=? ",
N: []string{"stars", "expr"},
},
// Add SET tuple
{
B: Update("cycling.cyclist_name").SetTuple("id", 2).Set("user_uuid", "firstname").Where(EqTuple("id", 2)),
S: "UPDATE cycling.cyclist_name SET id=(?,?),user_uuid=?,firstname=? WHERE id=(?,?) ",
N: []string{"id[0]", "id[1]", "user_uuid", "firstname", "id[0]", "id[1]"},
},
// Add SET SetFunc
{
B: Update("cycling.cyclist_name").SetFunc("user_uuid", Fn("someFunc", "param_0", "param_1")).Where(w).Set("stars"),
S: "UPDATE cycling.cyclist_name SET user_uuid=someFunc(?,?),stars=? WHERE id=? ",
N: []string{"param_0", "param_1", "stars", "expr"},
},
// Add SET Add
{
B: Update("cycling.cyclist_name").Add("total").Where(w),
S: "UPDATE cycling.cyclist_name SET total=total+? WHERE id=? ",
N: []string{"total", "expr"},
},
// Add SET AddNamed
{
B: Update("cycling.cyclist_name").AddNamed("total", "inc").Where(w),
S: "UPDATE cycling.cyclist_name SET total=total+? WHERE id=? ",
N: []string{"inc", "expr"},
},
// Add SET AddLit
{
B: Update("cycling.cyclist_name").AddLit("total", "1").Where(w),
S: "UPDATE cycling.cyclist_name SET total=total+1 WHERE id=? ",
N: []string{"expr"},
},
// Add SET Remove
{
B: Update("cycling.cyclist_name").Remove("total").Where(w),
S: "UPDATE cycling.cyclist_name SET total=total-? WHERE id=? ",
N: []string{"total", "expr"},
},
// Add SET RemoveNamed
{
B: Update("cycling.cyclist_name").RemoveNamed("total", "dec").Where(w),
S: "UPDATE cycling.cyclist_name SET total=total-? WHERE id=? ",
N: []string{"dec", "expr"},
},
// Add SET RemoveLit
{
B: Update("cycling.cyclist_name").RemoveLit("total", "1").Where(w),
S: "UPDATE cycling.cyclist_name SET total=total-1 WHERE id=? ",
N: []string{"expr"},
},
// Add WHERE
{
B: Update("cycling.cyclist_name").Set("id", "user_uuid", "firstname").Where(w, Gt("firstname")),
S: "UPDATE cycling.cyclist_name SET id=?,user_uuid=?,firstname=? WHERE id=? AND firstname>? ",
N: []string{"id", "user_uuid", "firstname", "expr", "firstname"},
},
// Add IF
{
B: Update("cycling.cyclist_name").Set("id", "user_uuid", "firstname").Where(w).If(Gt("firstname")),
S: "UPDATE cycling.cyclist_name SET id=?,user_uuid=?,firstname=? WHERE id=? IF firstname>? ",
N: []string{"id", "user_uuid", "firstname", "expr", "firstname"},
},
// Add TTL
{
B: Update("cycling.cyclist_name").Set("id", "user_uuid", "firstname").Where(w).TTL(time.Second),
S: "UPDATE cycling.cyclist_name USING TTL 1 SET id=?,user_uuid=?,firstname=? WHERE id=? ",
N: []string{"id", "user_uuid", "firstname", "expr"},
},
{
B: Update("cycling.cyclist_name").Set("id", "user_uuid", "firstname").Where(w).TTLNamed("ttl"),
S: "UPDATE cycling.cyclist_name USING TTL ? SET id=?,user_uuid=?,firstname=? WHERE id=? ",
N: []string{"ttl", "id", "user_uuid", "firstname", "expr"},
},
// Add TIMESTAMP
{
B: Update("cycling.cyclist_name").Set("id", "user_uuid", "firstname").Where(w).Timestamp(time.Date(2005, 5, 5, 0, 0, 0, 0, time.UTC)),
S: "UPDATE cycling.cyclist_name USING TIMESTAMP 1115251200000000 SET id=?,user_uuid=?,firstname=? WHERE id=? ",
N: []string{"id", "user_uuid", "firstname", "expr"},
},
{
B: Update("cycling.cyclist_name").Set("id", "user_uuid", "firstname").Where(w).TimestampNamed("ts"),
S: "UPDATE cycling.cyclist_name USING TIMESTAMP ? SET id=?,user_uuid=?,firstname=? WHERE id=? ",
N: []string{"ts", "id", "user_uuid", "firstname", "expr"},
},
// Add TIMEOUT
{
B: Update("cycling.cyclist_name").Set("id", "user_uuid", "firstname").Where(w).Timeout(time.Second),
S: "UPDATE cycling.cyclist_name USING TIMEOUT 1s SET id=?,user_uuid=?,firstname=? WHERE id=? ",
N: []string{"id", "user_uuid", "firstname", "expr"},
},
{
B: Update("cycling.cyclist_name").Set("id", "user_uuid", "firstname").Where(w).TimeoutNamed("to"),
S: "UPDATE cycling.cyclist_name USING TIMEOUT ? SET id=?,user_uuid=?,firstname=? WHERE id=? ",
N: []string{"to", "id", "user_uuid", "firstname", "expr"},
},
// Add IF EXISTS
{
B: Update("cycling.cyclist_name").Set("id", "user_uuid", "firstname").Where(w).Existing(),
S: "UPDATE cycling.cyclist_name SET id=?,user_uuid=?,firstname=? WHERE id=? IF EXISTS ",
N: []string{"id", "user_uuid", "firstname", "expr"},
},
// Add SET column
{
B: Update("cycling.cyclist_name").SetNamed("firstname", "name"),
S: "UPDATE cycling.cyclist_name SET firstname=? ",
N: []string{"name"},
},
// Add AddFunc
{
B: Update("cycling.cyclist_name").AddFunc("timestamp", Now()),
S: "UPDATE cycling.cyclist_name SET timestamp=timestamp+now() ",
N: nil,
},
// Add RemoveFunc
{
B: Update("cycling.cyclist_name").RemoveFunc("timestamp", Now()),
S: "UPDATE cycling.cyclist_name SET timestamp=timestamp-now() ",
N: nil,
},
// Add ALLOW FILTERING
{
B: Update("cycling.cyclist_name").Set("id", "user_uuid", "firstname").Where(w).AllowFiltering(),
S: "UPDATE cycling.cyclist_name SET id=?,user_uuid=?,firstname=? WHERE id=? ALLOW FILTERING ",
N: []string{"id", "user_uuid", "firstname", "expr"},
},
}
for _, test := range table {
stmt, names := test.B.ToCql()
if diff := cmp.Diff(test.S, stmt); diff != "" {
t.Error(diff)
}
if diff := cmp.Diff(test.N, names); diff != "" {
t.Error(diff)
}
}
}

View File

@@ -1,160 +0,0 @@
// 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"
"time"
"github.com/google/go-cmp/cmp"
)
func TestTTL(t *testing.T) {
if TTL(time.Second*86400) != 86400 {
t.Fatal("wrong ttl")
}
}
func TestTimestamp(t *testing.T) {
if Timestamp(time.Unix(0, 0).Add(time.Microsecond*123456789)) != 123456789 {
t.Fatal("wrong timestamp")
}
}
func TestUsing(t *testing.T) {
table := []struct {
B *using
N []string
S string
}{
// TTL
{
B: new(using).TTL(time.Second),
S: "USING TTL 1 ",
},
// TTLNamed
{
B: new(using).TTLNamed("ttl"),
S: "USING TTL ? ",
N: []string{"ttl"},
},
// Timestamp
{
B: new(using).Timestamp(time.Date(2005, 5, 5, 0, 0, 0, 0, time.UTC)),
S: "USING TIMESTAMP 1115251200000000 ",
},
// TimestampNamed
{
B: new(using).TimestampNamed("ts"),
S: "USING TIMESTAMP ? ",
N: []string{"ts"},
},
// Timeout
{
B: new(using).Timeout(time.Second),
S: "USING TIMEOUT 1s ",
},
// Timeout faction
{
B: new(using).Timeout(time.Second + 100*time.Millisecond),
S: "USING TIMEOUT 1s100ms ",
},
// TimeoutNamed
{
B: new(using).TimeoutNamed("to"),
S: "USING TIMEOUT ? ",
N: []string{"to"},
},
// TTL Timestamp
{
B: new(using).TTL(time.Second).Timestamp(time.Date(2005, 5, 5, 0, 0, 0, 0, time.UTC)),
S: "USING TTL 1 AND TIMESTAMP 1115251200000000 ",
},
// TTL TimestampNamed
{
B: new(using).TTL(time.Second).TimestampNamed("ts"),
S: "USING TTL 1 AND TIMESTAMP ? ",
N: []string{"ts"},
},
// TTLNamed TimestampNamed
{
B: new(using).TTLNamed("ttl").TimestampNamed("ts"),
S: "USING TTL ? AND TIMESTAMP ? ",
N: []string{"ttl", "ts"},
},
// TTLNamed Timestamp
{
B: new(using).TTLNamed("ttl").Timestamp(time.Date(2005, 5, 5, 0, 0, 0, 0, time.UTC)),
S: "USING TTL ? AND TIMESTAMP 1115251200000000 ",
N: []string{"ttl"},
},
// TTL Timeout
{
B: new(using).TTL(time.Second).Timeout(time.Second),
S: "USING TTL 1 AND TIMEOUT 1s ",
},
// TTL TimeoutNamed
{
B: new(using).TTL(time.Second).TimeoutNamed("to"),
S: "USING TTL 1 AND TIMEOUT ? ",
N: []string{"to"},
},
// TTL Timestamp Timeout
{
B: new(using).TTL(time.Second).Timestamp(time.Date(2005, 5, 5, 0, 0, 0, 0, time.UTC)).Timeout(time.Second),
S: "USING TTL 1 AND TIMESTAMP 1115251200000000 AND TIMEOUT 1s ",
},
// TTL with no duration
{
B: new(using).TTL(0 * time.Second),
S: "USING TTL 0 ",
},
{
B: new(using).TTL(-1 * time.Second),
S: "USING TTL 0 ",
},
{
// TODO patch this maybe in the future
B: new(using).TTL(-2 * time.Second),
S: "USING TTL -2 ",
},
// TTL TTLNamed
{
B: new(using).TTL(time.Second).TTLNamed("ttl"),
S: "USING TTL ? ",
N: []string{"ttl"},
},
// TTLNamed TTL
{
B: new(using).TTLNamed("ttl").TTL(time.Second),
S: "USING TTL 1 ",
},
// Timestamp TimestampNamed
{
B: new(using).Timestamp(time.Date(2005, 5, 5, 0, 0, 0, 0, time.UTC)).TimestampNamed("ts"),
S: "USING TIMESTAMP ? ",
N: []string{"ts"},
},
// TimestampNamed Timestamp
{
B: new(using).TimestampNamed("ts").Timestamp(time.Date(2005, 5, 5, 0, 0, 0, 0, time.UTC)),
S: "USING TIMESTAMP 1115251200000000 ",
},
}
for _, test := range table {
buf := bytes.NewBuffer(nil)
names := test.B.writeCql(buf)
stmt := buf.String()
if diff := cmp.Diff(test.S, stmt); diff != "" {
t.Error(diff)
}
if diff := cmp.Diff(test.N, names); diff != "" {
t.Error(diff)
}
}
}

View File

@@ -1,61 +0,0 @@
package qb
import (
"testing"
"time"
)
func TestFormatDuration(t *testing.T) {
tests := []struct {
name string
input time.Duration
expected string
}{
{
name: "Zero duration",
input: 0,
expected: "",
},
{
input: 500 * time.Millisecond,
expected: "500ms",
},
{
input: 10 * time.Second,
expected: "10s",
},
{
input: 3 * time.Minute,
expected: "3m",
},
{
input: (2 * time.Minute) + (30 * time.Second),
expected: "2m30s",
},
{
input: (15 * time.Second) + (250 * time.Millisecond),
expected: "15s250ms",
},
{
input: (1 * time.Minute) + (45 * time.Second) + (123 * time.Millisecond),
expected: "1m45s123ms",
},
{
input: (5 * time.Minute) + (1 * time.Second) + (999 * time.Millisecond),
expected: "5m1s999ms",
},
{
input: (2 * time.Second) + (1500 * time.Millisecond), // 3 seconds, 500ms
expected: "3s500ms",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := formatDuration(tt.input)
if actual != tt.expected {
t.Errorf("got %q, want %q", actual, tt.expected)
}
})
}
}