Permalink
Browse files

Make sure #size, #length, and #count work.

  • Loading branch information...
jnunemaker committed Nov 3, 2012
1 parent 3393c97 commit b382f2369b6e84ea6a23c1b4c8bdab521f4552fb
Showing with 33 additions and 17 deletions.
  1. +9 −5 lib/cassandra-cql/result.rb
  2. +24 −12 spec/result_spec.rb
@@ -35,7 +35,7 @@ def initialize(schema)
}
end
end
-
+
class Result
attr_reader :result, :schema, :cursor
@@ -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
@@ -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
@@ -113,7 +117,7 @@ def fetch_hash
end
end
end
-
+
def fetch_array
if block_given?
while row = fetch_row
View
@@ -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
@@ -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)
@@ -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
@@ -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
@@ -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

0 comments on commit b382f23

Please sign in to comment.