45 lines
1.2 KiB
Go
45 lines
1.2 KiB
Go
|
|
package cassandra
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"testing"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"github.com/gocql/gocql"
|
||
|
|
"github.com/stretchr/testify/assert"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestBatchTx_AllSuccess(t *testing.T) {
|
||
|
|
t.Parallel()
|
||
|
|
|
||
|
|
ctx := context.Background()
|
||
|
|
ks := generateRandomKeySpace(t)
|
||
|
|
now := time.Now()
|
||
|
|
id1 := gocql.TimeUUID()
|
||
|
|
id2 := gocql.TimeUUID()
|
||
|
|
|
||
|
|
tx := cassandraDBTest.NewBatch(ctx, ks)
|
||
|
|
err := tx.Insert(&MonkeyEntity{ID: id1, Name: "Alice", UpdateAt: now, CreateAt: now})
|
||
|
|
assert.NoError(t, err)
|
||
|
|
err = tx.Insert(&MonkeyEntity{ID: id2, Name: "Bob", UpdateAt: now, CreateAt: now})
|
||
|
|
assert.NoError(t, err)
|
||
|
|
err = tx.Update(&MonkeyEntity{ID: id1, Name: "Alice", UpdateAt: now.Add(5 * time.Minute)})
|
||
|
|
assert.NoError(t, err)
|
||
|
|
err = tx.Delete(&MonkeyEntity{ID: id2, Name: "Bob"})
|
||
|
|
assert.NoError(t, err)
|
||
|
|
|
||
|
|
err = tx.Commit()
|
||
|
|
assert.NoError(t, err)
|
||
|
|
|
||
|
|
// Alice 應該還在,且被更新
|
||
|
|
var alice MonkeyEntity
|
||
|
|
alice.ID, alice.Name = id1, "Alice"
|
||
|
|
err = cassandraDBTest.Get(ctx, &alice, ks)
|
||
|
|
assert.NoError(t, err)
|
||
|
|
assert.WithinDuration(t, now.Add(5*time.Minute), alice.UpdateAt, time.Second)
|
||
|
|
|
||
|
|
// Bob 應該被刪除
|
||
|
|
err = cassandraDBTest.Get(ctx, &MonkeyEntity{ID: id2, Name: "Bob"}, ks)
|
||
|
|
assert.Error(t, err)
|
||
|
|
}
|