|
| 1 | +// Copyright (C) 2017 ScyllaDB |
| 2 | +// Use of this source code is governed by a ALv2-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +// +build all integration |
| 6 | + |
| 7 | +package table_test |
| 8 | + |
| 9 | +import ( |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | + |
| 13 | + . "github.com/scylladb/gocqlx/v2/gocqlxtest" |
| 14 | + "github.com/scylladb/gocqlx/v2/qb" |
| 15 | + "github.com/scylladb/gocqlx/v2/table" |
| 16 | +) |
| 17 | + |
| 18 | +func TestRewriteRows(t *testing.T) { |
| 19 | + session := CreateSession(t) |
| 20 | + defer session.Close() |
| 21 | + |
| 22 | + if err := session.ExecStmt(`CREATE TABLE gocqlx_test.rewrite_table (testtext text PRIMARY KEY)`); err != nil { |
| 23 | + t.Fatal("create table:", err) |
| 24 | + } |
| 25 | + |
| 26 | + tbl := table.New(table.Metadata{ |
| 27 | + Name: "gocqlx_test.rewrite_table", |
| 28 | + Columns: []string{"testtext"}, |
| 29 | + PartKey: []string{"testtext"}, |
| 30 | + }) |
| 31 | + |
| 32 | + // Insert data with 500ms TTL |
| 33 | + q := tbl.InsertBuilder().TTL(500 * time.Millisecond).Query(session) |
| 34 | + if err := q.Bind("a").Exec(); err != nil { |
| 35 | + t.Fatal("insert:", err) |
| 36 | + } |
| 37 | + if err := q.Bind("b").Exec(); err != nil { |
| 38 | + t.Fatal("insert:", err) |
| 39 | + } |
| 40 | + if err := q.Bind("c").Exec(); err != nil { |
| 41 | + t.Fatal("insert:", err) |
| 42 | + } |
| 43 | + |
| 44 | + // Rewrite data without TTL |
| 45 | + if err := table.RewriteRows(session, tbl); err != nil { |
| 46 | + t.Fatal("rewrite:", err) |
| 47 | + } |
| 48 | + |
| 49 | + // Wait and check if data persisted |
| 50 | + time.Sleep(time.Second) |
| 51 | + |
| 52 | + var n int |
| 53 | + if err := qb.Select(tbl.Name()).CountAll().Query(session).Scan(&n); err != nil { |
| 54 | + t.Fatal("scan:", err) |
| 55 | + } |
| 56 | + if n != 3 { |
| 57 | + t.Fatal("expected 3 entries") |
| 58 | + } |
| 59 | +} |
0 commit comments