FEATURE: Query result equality (#33)

FEATURE: Query result equality (#33)

Allow checking if 2 different results from DB.query are the same via ==

diff --git a/lib/mini_sql/result.rb b/lib/mini_sql/result.rb
index 4081df8..7925ad0 100644
--- a/lib/mini_sql/result.rb
+++ b/lib/mini_sql/result.rb
@@ -16,5 +16,15 @@ module MiniSql
     def values
       instance_variables.map { |f| instance_variable_get(f) }
     end
+
+    def ==(other_result)
+      self.class.decorator == other_result.class.decorator &&
+      self.instance_variables == other_result.instance_variables &&
+      self.values == other_result.values
+    end
+
+    def eql?(other_result)
+      self == other_result
+    end
   end
 end
diff --git a/test/mini_sql/connection_tests.rb b/test/mini_sql/connection_tests.rb
index 2b58462..90f0e2c 100644
--- a/test/mini_sql/connection_tests.rb
+++ b/test/mini_sql/connection_tests.rb
@@ -172,4 +172,42 @@ module MiniSql::ConnectionTests
     assert_equal(r.length, 1)
   end
 
+  def test_equality
+    r1 = @connection.query("select 1 one, 'two' two")
+    r2 = @connection.query("select 1 one, 'two' two")
+
+    assert_equal(r1, r2)
+    assert_equal([r1] | [r2], [r1])
+  end
+
+  def test_equality_with_decorator
+    r1 = @connection.query_decorator(ProductDecorator, 'select 20 price, 3 quantity')
+    r2 = @connection.query_decorator(ProductDecorator, 'select 20 price, 3 quantity')
+
+    assert_equal(r1, r2)
+    assert_equal([r1] | [r2], [r1])
+  end
+
+  def test_equality_mixed
+    r1 = @connection.query('select 20 price, 3 quantity')
+    r2 = @connection.query_decorator(ProductDecorator, 'select 20 price, 3 quantity')
+
+    assert r1 != r2
+    assert_equal([r1] | [r2], [r1] + [r2])
+  end
+
+  def test_equality_distinct_fields
+    r1 = @connection.query("select 1 one, 'two' two, 3 three")
+    r2 = @connection.query("select 1 one, 'two' two")
+
+    assert r1 != r2
+  end
+
+  def test_equality_distinct_values
+    r1 = @connection.query("select 1 one, 'two' two")
+    r2 = @connection.query("select 1 one, 2 two")
+
+    assert r1 != r2
+  end
+
 end

GitHub sha: adde3441

This commit appears in #33 which was merged by SamSaffron.