Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from eprothro/consistency
Consistency
- Loading branch information
Showing
12 changed files
with
318 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
module Cassie::Queries::Statement | ||
module Consistency | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
attr_writer :consistency | ||
end | ||
|
||
module ClassMethods | ||
def inherited(subclass) | ||
subclass.consistency = consistency | ||
super | ||
end | ||
|
||
def consistency=(val) | ||
@consistency = val | ||
end | ||
|
||
def consistency(val=:get) | ||
if val == :get | ||
@consistency if defined?(@consistency) | ||
else | ||
self.consistency = val | ||
end | ||
end | ||
end | ||
|
||
def consistency | ||
return @consistency if defined?(@consistency) | ||
self.class.consistency | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
require_relative 'consistency' | ||
|
||
module Cassie::Queries::Statement | ||
module Execution | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
include Consistency | ||
end | ||
|
||
# Executes the statment, populates result | ||
# returns true or false indicating a successful execution or not | ||
def execute | ||
@result = session.execute(statement, execution_options) | ||
execution_successful? | ||
end | ||
|
||
def execution_options | ||
{}.tap do |opts| | ||
#TODO: rework consistency module to be more | ||
# abstract implementation for all execution options | ||
opts[:consistency] = consistency if consistency | ||
end | ||
end | ||
|
||
protected | ||
|
||
def execution_successful? | ||
#TODO: rethink this, it knows too much | ||
raise "execution not complete, no results to parse" unless result | ||
|
||
# empty select | ||
return true if result.empty? | ||
|
||
# failed upsert | ||
return false if (!result.rows.first["[applied]"].nil?) && (result.rows.first["[applied]"] == false) | ||
|
||
true | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
RSpec.describe Cassie::Queries::Statement::Consistency do | ||
let(:base_class)do | ||
Class.new do | ||
include Cassie::Queries::Statement::Consistency | ||
end | ||
end | ||
let(:subclass){ Class.new(base_class) } | ||
let(:object) { subclass.new } | ||
let(:consistency){ Cassandra::CONSISTENCIES.sample } | ||
# let(:alt_consistency){ (Cassandra::CONSISTENCIES - [alt_consistency]).sample } | ||
let(:default_consistency){ nil } | ||
|
||
# before(:each){ @original = base_class.consistency } | ||
# after(:each){ base_class.consistency = @original } | ||
describe "BaseClass" do | ||
describe ".consistency" do | ||
it "defaults to nil" do | ||
expect(base_class.consistency).to eq(default_consistency) | ||
end | ||
|
||
context "when set" do | ||
it "overrides default" do | ||
base_class.consistency = consistency | ||
expect(base_class.consistency).to eq(consistency) | ||
end | ||
end | ||
end | ||
end | ||
|
||
describe "SubClass" do | ||
describe ".consistency" do | ||
it "defaults to base default" do | ||
expect(subclass.consistency).to eq(default_consistency) | ||
end | ||
|
||
context "when base class has been set" do | ||
before(:each) { base_class.consistency = consistency } | ||
|
||
it "inherits base class setting" do | ||
expect(subclass.consistency).to eq(consistency) | ||
end | ||
end | ||
|
||
context "when set with setter" do | ||
before(:each){ subclass.consistency = consistency } | ||
|
||
it "overrides default" do | ||
expect(subclass.consistency).to eq(consistency) | ||
end | ||
it "doesn't change base class" do | ||
expect(base_class.consistency).to eq(default_consistency) | ||
end | ||
end | ||
|
||
context "when set with getter for DSL feel" do | ||
before(:each){ subclass.consistency(consistency) } | ||
|
||
it "overrides default" do | ||
expect(subclass.consistency).to eq(consistency) | ||
end | ||
it "doesn't change base class" do | ||
expect(base_class.consistency).to eq(default_consistency) | ||
end | ||
end | ||
|
||
context "when overwritten" do | ||
let(:consistency){ :three } | ||
let(:subclass) do | ||
Class.new(base_class) do | ||
def self.consistency | ||
:three | ||
end | ||
end | ||
end | ||
|
||
it "overrides base class setting" do | ||
expect(subclass.consistency).to eq(consistency) | ||
end | ||
it "doesn't change base class" do | ||
expect(base_class.consistency).to eq(default_consistency) | ||
end | ||
end | ||
end | ||
|
||
describe "#consistency" do | ||
it "defaults to base_class value" do | ||
expect(object.consistency).to eq(default_consistency) | ||
end | ||
|
||
context "when set" do | ||
before(:each){ object.consistency = consistency } | ||
|
||
it "overrides default" do | ||
expect(object.consistency).to eq(consistency) | ||
end | ||
it "doesn't change subclass" do | ||
expect(subclass.consistency).to eq(default_consistency) | ||
end | ||
it "doesn't change base class" do | ||
expect(base_class.consistency).to eq(default_consistency) | ||
end | ||
|
||
context "when .consistency overwritten" do | ||
let(:consistency){ :three } | ||
let(:subclass) do | ||
Class.new(base_class) do | ||
def self.consistency | ||
:three | ||
end | ||
end | ||
end | ||
|
||
it "uses object value" do | ||
expect(object.consistency).to eq(consistency) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.