This commit is contained in:
Michał Matczuk
2017-07-27 13:39:27 +02:00
parent 546e11d58f
commit 2afe71e2b9
10 changed files with 363 additions and 156 deletions

View File

@@ -8,10 +8,7 @@ import (
)
func TestUpdateBuilder(t *testing.T) {
m := mockExpr{
cql: "expr",
names: []string{"expr"},
}
w := EqNamed("id", "expr")
table := []struct {
B *UpdateBuilder
@@ -20,62 +17,50 @@ func TestUpdateBuilder(t *testing.T) {
}{
// Basic test for update
{
B: Update("cycling.cyclist_name").Set("id", "user_uuid", "firstname").Where(m),
S: "UPDATE cycling.cyclist_name SET id=?,user_uuid=?,firstname=? WHERE expr ",
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(m).Table("Foobar"),
S: "UPDATE Foobar SET id=?,user_uuid=?,firstname=? WHERE expr ",
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(m).Set("stars"),
S: "UPDATE cycling.cyclist_name SET id=?,user_uuid=?,firstname=?,stars=? WHERE expr ",
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 WHERE
{
B: Update("cycling.cyclist_name").Set("id", "user_uuid", "firstname").Where(m).Where(mockExpr{
cql: "expr_1",
names: []string{"expr_1"},
}, mockExpr{
cql: "expr_2",
names: []string{"expr_2"},
}),
S: "UPDATE cycling.cyclist_name SET id=?,user_uuid=?,firstname=? WHERE expr AND expr_1 AND expr_2 ",
N: []string{"id", "user_uuid", "firstname", "expr", "expr_1", "expr_2"},
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(m).If(mockExpr{
cql: "expr_1",
names: []string{"expr_1"},
}, mockExpr{
cql: "expr_2",
names: []string{"expr_2"},
}),
S: "UPDATE cycling.cyclist_name SET id=?,user_uuid=?,firstname=? WHERE expr IF expr_1 AND expr_2 ",
N: []string{"id", "user_uuid", "firstname", "expr", "expr_1", "expr_2"},
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(m).TTL(time.Second * 86400),
S: "UPDATE cycling.cyclist_name USING TTL 86400 SET id=?,user_uuid=?,firstname=? WHERE expr ",
B: Update("cycling.cyclist_name").Set("id", "user_uuid", "firstname").Where(w).TTL(time.Second * 86400),
S: "UPDATE cycling.cyclist_name USING TTL 86400 SET id=?,user_uuid=?,firstname=? WHERE id=? ",
N: []string{"id", "user_uuid", "firstname", "expr"},
},
// Add TIMESTAMP
{
B: Update("cycling.cyclist_name").Set("id", "user_uuid", "firstname").Where(m).Timestamp(time.Unix(0, 0).Add(time.Microsecond * 123456789)),
S: "UPDATE cycling.cyclist_name USING TIMESTAMP 123456789 SET id=?,user_uuid=?,firstname=? WHERE expr ",
B: Update("cycling.cyclist_name").Set("id", "user_uuid", "firstname").Where(w).Timestamp(time.Unix(0, 0).Add(time.Microsecond * 123456789)),
S: "UPDATE cycling.cyclist_name USING TIMESTAMP 123456789 SET id=?,user_uuid=?,firstname=? WHERE id=? ",
N: []string{"id", "user_uuid", "firstname", "expr"},
},
// Add IF EXISTS
{
B: Update("cycling.cyclist_name").Set("id", "user_uuid", "firstname").Where(m).Existing(),
S: "UPDATE cycling.cyclist_name SET id=?,user_uuid=?,firstname=? WHERE expr 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"},
},
}
@@ -95,12 +80,8 @@ func TestUpdateBuilder(t *testing.T) {
}
func BenchmarkUpdateBuilder(b *testing.B) {
m := mockExpr{
cql: "expr",
names: []string{"expr"},
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
Update("cycling.cyclist_name").Set("id", "user_uuid", "firstname", "stars").Where(m)
Update("cycling.cyclist_name").Set("id", "user_uuid", "firstname", "stars").Where(Eq("id"))
}
}