Skip to content

Commit

Permalink
Add configurable consistency to query
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew-jarvie authored and willfaught committed Oct 21, 2023
1 parent 8fc81aa commit 7257eb1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
23 changes: 23 additions & 0 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ type Query interface {
// Release releases a query back into a pool of queries. Released queries
// cannot be reused.
Release()

// GetConsistency returns the currently configured consistency level for
// the query.
GetConsistency() gocql.Consistency

// SetConsistency sets the consistency level for this query.
SetConsistency(c gocql.Consistency)
}

var (
Expand Down Expand Up @@ -93,6 +100,14 @@ func (m QueryMock) Release() {
m.Called()
}

func (m QueryMock) GetConsistency() gocql.Consistency {
return m.Called().Get(0).(gocql.Consistency)
}

func (m QueryMock) SetConsistency(c gocql.Consistency) {
m.Called(c)
}

type query struct {
q *gocql.Query
}
Expand Down Expand Up @@ -128,3 +143,11 @@ func (q query) Scan(dest ...interface{}) error {
func (q query) Release() {
q.q.Release()
}

func (q query) GetConsistency() gocql.Consistency {
return q.q.GetConsistency()
}

func (q query) SetConsistency(c gocql.Consistency) {
q.q.SetConsistency(c)
}
19 changes: 19 additions & 0 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gockle
import (
"context"
"fmt"
"github.com/gocql/gocql"
"reflect"
"testing"
)
Expand Down Expand Up @@ -73,5 +74,23 @@ func TestQueryMock(t *testing.T) {
{"MapScan", []interface{}{map[string]interface{}(nil)}, []interface{}{nil}},
{"MapScan", []interface{}{map[string]interface{}{"a": 1}}, []interface{}{e}},
{"Release", nil, nil},
{"GetConsistency", nil, []interface{}{gocql.Quorum}},
{"SetConsistency", []interface{}{gocql.One}, nil},
})
}

func TestQueryConsistency(t *testing.T) {
var s = newSession(t)
defer s.Close()
q := s.Query("select * from gockle_test.test")
actual := q.GetConsistency()
if gocql.Quorum != actual {
t.Errorf("Actual consistency %s, expected %s", actual, gocql.Quorum)
}

q.SetConsistency(gocql.One)
actual = q.GetConsistency()
if gocql.One != actual {
t.Errorf("Actual consistency %s, expected %s", actual, gocql.One)
}
}

0 comments on commit 7257eb1

Please sign in to comment.