Skip to content

Commit

Permalink
Merge pull request #37 from jnunemaker/result-size-length-count-support
Browse files Browse the repository at this point in the history
Make sure #size, #length, and #count work.
  • Loading branch information
Kelley Reynolds committed Nov 8, 2012
2 parents 012bd70 + b382f23 commit a47d154
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
14 changes: 9 additions & 5 deletions lib/cassandra-cql/result.rb
Expand Up @@ -35,7 +35,7 @@ def initialize(schema)
}
end
end

class Result
attr_reader :result, :schema, :cursor

Expand All @@ -44,11 +44,11 @@ def initialize(result)
@schema = ResultSchema.new(result.schema) if rows?
@cursor = 0
end

def void?
@result.type == CassandraCQL::Thrift::CqlResultType::VOID
end

def int?
@result.type == CassandraCQL::Thrift::CqlResultType::INT
end
Expand All @@ -61,12 +61,16 @@ def rows
@result.rows.size
end

alias_method :size, :rows
alias_method :count, :rows
alias_method :length, :rows

def cursor=(cursor)
@cursor = cursor.to_i
rescue Exception => e
raise Error::InvalidCursor, e.to_s
end

def fetch_row
case @result.type
when CassandraCQL::Thrift::CqlResultType::ROWS
Expand Down Expand Up @@ -113,7 +117,7 @@ def fetch_hash
end
end
end

def fetch_array
if block_given?
while row = fetch_row
Expand Down
36 changes: 24 additions & 12 deletions spec/result_spec.rb
Expand Up @@ -57,30 +57,42 @@
it "should have two rows" do
@result.rows.should eq(2)
end


it "should know size of rows" do
@result.size.should eq(2)
end

it "should know count of rows" do
@result.count.should eq(2)
end

it "should know length of rows" do
@result.length.should eq(2)
end

context "initialize" do
it "should have a cursor set to 0" do
@result.instance_variable_get(:@cursor).should eq(0)
end

it "should have a result" do
@result.instance_variable_get(:@result).should be_kind_of(CassandraCQL::Thrift::CqlResult)
end
end

context "setting the cursor" do
it "should set the cursor" do
expect {
@result.cursor = 15
}.to_not raise_error
@result.instance_variable_get(:@cursor).should eq(15)
end
end

it "should not set the cursor" do
expect {
@result.cursor = Object
}.to raise_error(CassandraCQL::Error::InvalidCursor)
end
end
end

context "fetching a single row" do
Expand All @@ -90,12 +102,12 @@

@result.fetch_row.should be_kind_of(Row)
@result.instance_variable_get(:@cursor).should eq(2)

@result.fetch_row.should be_nil
@result.instance_variable_get(:@cursor).should eq(2)
end
end

context "resetting cursor should fetch the same row" do
it "should return the same row" do
@result.instance_variable_get(:@cursor).should eq(0)
Expand All @@ -104,20 +116,20 @@
arr.should eq(@result.fetch_array)
end
end

context "fetch without a block" do
it "should return a row twice then nil" do
@result.fetch.should be_kind_of(Row)
@result.instance_variable_get(:@cursor).should eq(1)

@result.fetch.should be_kind_of(Row)
@result.instance_variable_get(:@cursor).should eq(2)

@result.fetch.should be_nil
@result.instance_variable_get(:@cursor).should eq(2)
end
end

context "fetch with a block" do
it "fetched count should equal the number of rows" do
counter = 0
Expand All @@ -138,7 +150,7 @@
arr.should eq(row.column_values)
end
end

context "fetch_array_with a block" do
it "fetched count should equal the number of rows" do
counter = 0
Expand All @@ -164,7 +176,7 @@
@result.fetch_hash.should be_nil
end
end

context "fetch_hash_with a block" do
it "should iterate rows() times and return hashes" do
counter = 0
Expand Down

0 comments on commit a47d154

Please sign in to comment.