Permalink
Browse files

Added read, write and delete adapter options.

Ensured that they do not clobber actual options that are necessary for
the adapter to work.
  • Loading branch information...
jnunemaker committed Nov 16, 2012
1 parent 6b6ade1 commit f857da0b5d5d30d05fb508ba9d1bd60850031ada
Showing with 169 additions and 12 deletions.
  1. +31 −5 lib/adapter/cassanity.rb
  2. +138 −7 spec/cassanity_spec.rb
View
@@ -9,19 +9,40 @@ module Cassanity
def_delegator :@client, :schema
def read(key)
- rows = client.select(where: where(key))
+ select_arguments = {where: where(key)}
+
+ if (read_options = options[:read_options])
+ filtered_options = without_keys(read_options, select_arguments.keys)
+ select_arguments.update(filtered_options)
+ end
+
+ rows = client.select(select_arguments)
rows.empty? ? nil : decode(rows.first)
end
def write(key, attributes)
- client.update({
+ update_arguments = {
set: encode(attributes),
- where: where(key),
- })
+ where: where(key)
+ }
+
+ if (write_options = options[:write_options])
+ filtered_options = without_keys(write_options, update_arguments.keys)
+ update_arguments.update(filtered_options)
+ end
+
+ client.update(update_arguments)
end
def delete(key)
- client.delete(where: where(key))
+ delete_arguments = {where: where(key)}
+
+ if (delete_options = options[:delete_options])
+ filtered_options = without_keys(delete_options, delete_arguments.keys)
+ delete_arguments.update(filtered_options)
+ end
+
+ client.delete(delete_arguments)
end
def clear
@@ -37,6 +58,11 @@ def where(criteria)
{primary_key => criteria}
end
end
+
+ # Private
+ def without_keys(options, keys)
+ options.reject { |key| keys.include?(key) }
+ end
end
end
View
@@ -1,6 +1,7 @@
require 'helper'
describe "Cassanity adapter" do
+ let(:adapter_name) { :cassanity }
context "single primary key" do
before do
@@ -9,27 +10,157 @@
@adapter.clear
end
- let(:adapter_name) { :cassanity }
-
let(:adapter) { @adapter }
let(:client) { @client }
it_behaves_like 'an adapter'
end
+ context "with adapter read options" do
+ it "uses read options for read method" do
+ client = COLUMN_FAMILIES[:single]
+ primary_key = client.schema.primary_keys.first
+ key = 'foo'
+ where = {primary_key => key}
+ using = {consistency: :quorum}
+
+ options = {read_options: {
+ using: {consistency: :quorum},
+ }}
+ adapter = Adapter[adapter_name].new(client, options)
+
+ client.should_receive(:select).with({
+ where: where,
+ using: using,
+ }).and_return([])
+
+ adapter.read(key)
+ end
+
+ it "does not override where" do
+ client = COLUMN_FAMILIES[:single]
+ primary_key = client.schema.primary_keys.first
+ key = 'foo'
+ where = {primary_key => key}
+ using = {consistency: :quorum}
+
+ options = {read_options: {
+ where: {primary_key => 'bar'},
+ using: {consistency: :quorum},
+ }}
+ adapter = Adapter[adapter_name].new(client, options)
+
+ client.should_receive(:select).with({
+ where: where,
+ using: using,
+ }).and_return([])
+
+ adapter.read(key)
+ end
+ end
+
+ context "with adapter write options" do
+ it "uses write options for write method" do
+ client = COLUMN_FAMILIES[:single]
+ primary_key = client.schema.primary_keys.first
+ key = 'foo'
+ set = {'name' => 'New Name'}
+ where = {primary_key => key}
+ using = {consistency: :quorum}
+
+ options = {write_options: {
+ using: {consistency: :quorum},
+ }}
+ adapter = Adapter[adapter_name].new(client, options)
+
+ client.should_receive(:update).with({
+ set: set,
+ where: where,
+ using: using,
+ })
+
+ adapter.write(key, set)
+ end
+
+ it "does not override where or set" do
+ client = COLUMN_FAMILIES[:single]
+ primary_key = client.schema.primary_keys.first
+ key = 'foo'
+ set = {'name' => 'New Name'}
+ where = {primary_key => key}
+ using = {consistency: :quorum}
+
+ options = {write_options: {
+ where: {primary_key => 'should not use this'},
+ set: {'name' => 'should not use this'},
+ using: {consistency: :quorum,
+ }}}
+ adapter = Adapter[adapter_name].new(client, options)
+
+ client.should_receive(:update).with({
+ set: set,
+ where: where,
+ using: using,
+ })
+
+ adapter.write(key, set)
+ end
+ end
+
+ context "with adapter delete options" do
+ it "uses delete options for delete method" do
+ client = COLUMN_FAMILIES[:single]
+ primary_key = client.schema.primary_keys.first
+ key = 'foo'
+ where = {primary_key => key}
+ using = {consistency: :quorum}
+
+ options = {delete_options: {
+ using: {consistency: :quorum},
+ }}
+ adapter = Adapter[adapter_name].new(client, options)
+
+ client.should_receive(:delete).with({
+ where: where,
+ using: using,
+ })
+
+ adapter.delete(key)
+ end
+
+ it "does not override where" do
+ client = COLUMN_FAMILIES[:single]
+ primary_key = client.schema.primary_keys.first
+ key = 'foo'
+ where = {primary_key => key}
+ using = {consistency: :quorum}
+
+ options = {delete_options: {
+ where: {primary_key => 'bar'},
+ using: {consistency: :quorum},
+ }}
+ adapter = Adapter[adapter_name].new(client, options)
+
+ client.should_receive(:delete).with({
+ where: where,
+ using: using,
+ })
+
+ adapter.delete(key)
+ end
+ end
+
context "composite primary key" do
before do
@client = COLUMN_FAMILIES[:composite]
@adapter = Adapter[adapter_name].new(@client)
@adapter.clear
end
- let(:adapter_name) { :cassanity }
-
- let(:adapter) { @adapter }
- let(:client) { @client }
-
it_behaves_like 'an adapter' do
+ let(:adapter) { @adapter }
+ let(:client) { @client }
+
let(:key) { {:bucket => '1', :id => CassandraCQL::UUID.new} }
let(:key2) { {:bucket => '2', :id => CassandraCQL::UUID.new} }

0 comments on commit f857da0

Please sign in to comment.