Permalink
Browse files

Fixed bug where nil pointer was thrown if column did not exist

  • Loading branch information...
1 parent 3f3a6a0 commit bc29a9771c13e190ce19af2f92ac27e5eb6e6420 Brad Seefeld committed Jun 8, 2011
Showing with 42 additions and 3 deletions.
  1. +2 −1 README.rdoc
  2. +7 −1 lib/rgviz/data_table/order.rb
  3. +1 −1 lib/rgviz/data_table/version.rb
  4. +32 −0 spec/rgviz/data_table/order_spec.rb
View
@@ -20,4 +20,5 @@ In order of needed:
* Implement labels
* Refactor where parsing to fix bug where (, ), AND or OR may appear in values (E.g., WHERE column = "AND")
-* Implement pivots
+* Implement pivots
+* Ordering by a column that does not exist causes a nil class exception.
@@ -15,7 +15,13 @@ def initialize(column, direction)
def compare(left, right)
equality = 0
- if left[column] > right[column]
+ if left[column].nil? && right[column].nil?
+ equality = 0
+ elsif left[column].nil?
+ equality = 1
+ elsif right[column].nil?
+ equality = -1
+ elsif left[column] > right[column]
equality = 1
elsif left[column] < right[column]
equality = -1
@@ -1,5 +1,5 @@
module Rgviz
module DataTable
- VERSION = "0.0.5"
+ VERSION = "0.0.6"
end
end
@@ -0,0 +1,32 @@
+require "spec_helper"
+
+describe Rgviz::DataTable::Order do
+
+ it "correctly compares two rows when sort order is ascending" do
+ order = Rgviz::DataTable::Order.new("col", Rgviz::DataTable::Order::ASCENDING)
+ left = {"col" => 7}
+ right = {"col" => 9}
+ order.compare(left, right).should == -1
+ end
+
+ it "correctly compares two rows when sort order is descending" do
+ order = Rgviz::DataTable::Order.new("col", Rgviz::DataTable::Order::DESCENDING)
+ left = {"col" => 7}
+ right = {"col" => 9}
+ order.compare(left, right).should == 1
+ end
+
+ it "returns zero when they are equal" do
+ order = Rgviz::DataTable::Order.new("col", Rgviz::DataTable::Order::DESCENDING)
+ left = {"col" => 7}
+ right = {"col" => 7}
+ order.compare(left, right).should == 0
+ end
+
+ it "returns zero when the column does not exist" do
+ order = Rgviz::DataTable::Order.new("col", Rgviz::DataTable::Order::DESCENDING)
+ left = {"a" => 7}
+ right = {"a" => 7}
+ order.compare(left, right).should == 0
+ end
+end

0 comments on commit bc29a97

Please sign in to comment.