Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add equality to ALL THE THINGS (that matter)

People are often trying to use ARel nodes inside ActiveRecord, and when
they do so, lots of things can break, because ActiveRecord relies on
Array#uniq and sometimes hash key equality to handle values that end up
in wheres, havings, etc. By implementing equality for all the nodes, we
should hopefully be able to prevent any nodes (even nodes containing
other nodes) from failing an equality check they should otherwise pass,
and alleviate many of these errors.

Fixes #130
  • Loading branch information...
commit 6e638bba594b6164190d2a6fb96ffa07a20b11f3 1 parent 1de1041
Ernie Miller ernie authored
Showing with 737 additions and 4 deletions.
  1. +10 −0 lib/arel/nodes/and.rb
  2. +11 −0 lib/arel/nodes/binary.rb
  3. +11 −0 lib/arel/nodes/extract.rb
  4. +7 −0 lib/arel/nodes/false.rb
  5. +11 −0 lib/arel/nodes/function.rb
  6. +12 −0 lib/arel/nodes/insert_statement.rb
  7. +9 −0 lib/arel/nodes/named_function.rb
  8. +20 −0 lib/arel/nodes/select_core.rb
  9. +15 −1 lib/arel/nodes/select_statement.rb
  10. +7 −0 lib/arel/nodes/terminal.rb
  11. +7 −0 lib/arel/nodes/true.rb
  12. +10 −0 lib/arel/nodes/unary.rb
  13. +15 −0 lib/arel/nodes/update_statement.rb
  14. +29 −1 lib/arel/nodes/window.rb
  15. +13 −0 lib/arel/table.rb
  16. +20 −0 test/nodes/test_and.rb
  17. +12 −0 test/nodes/test_as.rb
  18. +10 −0 test/nodes/test_ascending.rb
  19. +10 −0 test/nodes/test_bin.rb
  20. +12 −0 test/nodes/test_count.rb
  21. +20 −0 test/nodes/test_delete_statement.rb
  22. +10 −0 test/nodes/test_descending.rb
  23. +20 −0 test/nodes/test_distinct.rb
  24. +10 −0 test/nodes/test_equality.rb
  25. +14 −0 test/nodes/test_extract.rb
  26. +20 −0 test/nodes/test_false.rb
  27. +12 −0 test/nodes/test_grouping.rb
  28. +10 −0 test/nodes/test_infix_operation.rb
  29. +24 −0 test/nodes/test_insert_statement.rb
  30. +16 −0 test/nodes/test_named_function.rb
  31. +12 −0 test/nodes/test_not.rb
  32. +12 −0 test/nodes/test_or.rb
  33. +18 −0 test/nodes/test_over.rb
  34. +38 −0 test/nodes/test_select_core.rb
  35. +36 −0 test/nodes/test_select_statement.rb
  36. +10 −0 test/nodes/test_sql_literal.rb
  37. +12 −0 test/nodes/test_sum.rb
  38. +22 −2 test/nodes/test_table_alias.rb
  39. +21 −0 test/nodes/test_true.rb
  40. +40 −0 test/nodes/test_update_statement.rb
  41. +73 −0 test/nodes/test_window.rb
  42. +12 −0 test/test_attributes.rb
  43. +24 −0 test/test_table.rb
10 lib/arel/nodes/and.rb
View
@@ -18,6 +18,16 @@ def left
def right
children[1]
end
+
+ def hash
+ children.hash
+ end
+
+ def eql? other
+ self.class == other.class &&
+ self.children == other.children
+ end
+ alias :== :eql?
end
end
end
11 lib/arel/nodes/binary.rb
View
@@ -13,6 +13,17 @@ def initialize_copy other
@left = @left.clone if @left
@right = @right.clone if @right
end
+
+ def hash
+ [@left, @right].hash
+ end
+
+ def eql? other
+ self.class == other.class &&
+ self.left == other.left &&
+ self.right == other.right
+ end
+ alias :== :eql?
end
%w{
11 lib/arel/nodes/extract.rb
View
@@ -18,6 +18,17 @@ def as aliaz
self.alias = SqlLiteral.new(aliaz)
self
end
+
+ def hash
+ super ^ [@field, @alias].hash
+ end
+
+ def eql? other
+ super &&
+ self.field == other.field &&
+ self.alias == other.alias
+ end
+ alias :== :eql?
end
end
end
7 lib/arel/nodes/false.rb
View
@@ -1,6 +1,13 @@
module Arel
module Nodes
class False < Arel::Nodes::Node
+ def hash
+ self.class.hash
+ end
+
+ def eql? other
+ self.class == other.class
+ end
end
end
end
11 lib/arel/nodes/function.rb
View
@@ -16,6 +16,17 @@ def as aliaz
self.alias = SqlLiteral.new(aliaz)
self
end
+
+ def hash
+ [@expressions, @alias, @distinct].hash
+ end
+
+ def eql? other
+ self.class == other.class &&
+ self.expressions == other.expressions &&
+ self.alias == other.alias &&
+ self.distinct == other.distinct
+ end
end
%w{
12 lib/arel/nodes/insert_statement.rb
View
@@ -14,6 +14,18 @@ def initialize_copy other
@columns = @columns.clone
@values = @values.clone if @values
end
+
+ def hash
+ [@relation, @columns, @values].hash
+ end
+
+ def eql? other
+ self.class == other.class &&
+ self.relation == other.relation &&
+ self.columns == other.columns &&
+ self.values == other.values
+ end
+ alias :== :eql?
end
end
end
9 lib/arel/nodes/named_function.rb
View
@@ -7,6 +7,15 @@ def initialize name, expr, aliaz = nil
super(expr, aliaz)
@name = name
end
+
+ def hash
+ super ^ @name.hash
+ end
+
+ def eql? other
+ super && self.name == other.name
+ end
+ alias :== :eql?
end
end
end
20 lib/arel/nodes/select_core.rb
View
@@ -37,6 +37,26 @@ def initialize_copy other
@having = @having.clone if @having
@windows = @windows.clone
end
+
+ def hash
+ [
+ @source, @top, @set_quantifier, @projections,
+ @wheres, @groups, @having, @windows
+ ].hash
+ end
+
+ def eql? other
+ self.class == other.class &&
+ self.source == other.source &&
+ self.top == other.top &&
+ self.set_quantifier == other.set_quantifier &&
+ self.projections == other.projections &&
+ self.wheres == other.wheres &&
+ self.groups == other.groups &&
+ self.having == other.having &&
+ self.windows == other.windows
+ end
+ alias :== :eql?
end
end
end
16 lib/arel/nodes/select_statement.rb
View
@@ -5,7 +5,6 @@ class SelectStatement < Arel::Nodes::Node
attr_accessor :limit, :orders, :lock, :offset, :with
def initialize cores = [SelectCore.new]
- #puts caller
@cores = cores
@orders = []
@limit = nil
@@ -19,6 +18,21 @@ def initialize_copy other
@cores = @cores.map { |x| x.clone }
@orders = @orders.map { |x| x.clone }
end
+
+ def hash
+ [@cores, @orders, @limit, @lock, @offset, @with].hash
+ end
+
+ def eql? other
+ self.class == other.class &&
+ self.cores == other.cores &&
+ self.orders == other.orders &&
+ self.limit == other.limit &&
+ self.lock == other.lock &&
+ self.offset == other.offset &&
+ self.with == other.with
+ end
+ alias :== :eql?
end
end
end
7 lib/arel/nodes/terminal.rb
View
@@ -1,6 +1,13 @@
module Arel
module Nodes
class Distinct < Arel::Nodes::Node
+ def hash
+ self.class.hash
+ end
+
+ def eql? other
+ self.class == other.class
+ end
end
end
end
7 lib/arel/nodes/true.rb
View
@@ -1,6 +1,13 @@
module Arel
module Nodes
class True < Arel::Nodes::Node
+ def hash
+ self.class.hash
+ end
+
+ def eql? other
+ self.class == other.class
+ end
end
end
end
10 lib/arel/nodes/unary.rb
View
@@ -7,6 +7,16 @@ class Unary < Arel::Nodes::Node
def initialize expr
@expr = expr
end
+
+ def hash
+ @expr.hash
+ end
+
+ def eql? other
+ self.class == other.class &&
+ self.expr == other.expr
+ end
+ alias :== :eql?
end
%w{
15 lib/arel/nodes/update_statement.rb
View
@@ -18,6 +18,21 @@ def initialize_copy other
@wheres = @wheres.clone
@values = @values.clone
end
+
+ def hash
+ [@relation, @wheres, @values, @orders, @limit, @key].hash
+ end
+
+ def eql? other
+ self.class == other.class &&
+ self.relation == other.relation &&
+ self.wheres == other.wheres &&
+ self.values == other.values &&
+ self.orders == other.orders &&
+ self.limit == other.limit &&
+ self.key == other.key
+ end
+ alias :== :eql?
end
end
end
30 lib/arel/nodes/window.rb
View
@@ -32,6 +32,17 @@ def initialize_copy other
super
@orders = @orders.map { |x| x.clone }
end
+
+ def hash
+ [@orders, @framing].hash
+ end
+
+ def eql? other
+ self.class == other.class &&
+ self.orders == other.orders &&
+ self.framing == other.framing
+ end
+ alias :== :eql?
end
class NamedWindow < Window
@@ -46,6 +57,15 @@ def initialize_copy other
super
@name = other.name.clone
end
+
+ def hash
+ super ^ @name.hash
+ end
+
+ def eql? other
+ super && self.name == other.name
+ end
+ alias :== :eql?
end
class Rows < Unary
@@ -60,7 +80,15 @@ def initialize(expr = nil)
end
end
- class CurrentRow < Arel::Nodes::Node; end
+ class CurrentRow < Node
+ def hash
+ self.class.hash
+ end
+
+ def eql? other
+ self.class == other.class
+ end
+ end
class Preceding < Unary
def initialize(expr = nil)
13 lib/arel/table.rb
View
@@ -123,6 +123,19 @@ def insert_manager
InsertManager.new(@engine)
end
+ def hash
+ [@name, @engine, @aliases, @table_alias].hash
+ end
+
+ def eql? other
+ self.class == other.class &&
+ self.name == other.name &&
+ self.engine == other.engine &&
+ self.aliases == other.aliases &&
+ self.table_alias == other.table_alias
+ end
+ alias :== :eql?
+
private
def attributes_for columns
20 test/nodes/test_and.rb
View
@@ -0,0 +1,20 @@
+require 'helper'
+
+module Arel
+ module Nodes
+ describe 'And' do
+ describe 'equality' do
+ it 'is equal with equal ivars' do
+ array = [And.new(['foo', 'bar']), And.new(['foo', 'bar'])]
+ assert_equal 1, array.uniq.size
+ end
+
+ it 'is not equal with different ivars' do
+ array = [And.new(['foo', 'bar']), And.new(['foo', 'baz'])]
+ assert_equal 2, array.uniq.size
+ end
+ end
+ end
+ end
+end
+
12 test/nodes/test_as.rb
View
@@ -17,6 +17,18 @@ module Nodes
assert_kind_of Arel::Nodes::SqlLiteral, as.right
end
end
+
+ describe 'equality' do
+ it 'is equal with equal ivars' do
+ array = [As.new('foo', 'bar'), As.new('foo', 'bar')]
+ assert_equal 1, array.uniq.size
+ end
+
+ it 'is not equal with different ivars' do
+ array = [As.new('foo', 'bar'), As.new('foo', 'baz')]
+ assert_equal 2, array.uniq.size
+ end
+ end
end
end
end
10 test/nodes/test_ascending.rb
View
@@ -29,6 +29,16 @@ def test_descending?
ascending = Ascending.new 'zomg'
assert !ascending.descending?
end
+
+ def test_equality_with_same_ivars
+ array = [Ascending.new('zomg'), Ascending.new('zomg')]
+ assert_equal 1, array.uniq.size
+ end
+
+ def test_inequality_with_different_ivars
+ array = [Ascending.new('zomg'), Ascending.new('zomg!')]
+ assert_equal 2, array.uniq.size
+ end
end
end
end
10 test/nodes/test_bin.rb
View
@@ -18,6 +18,16 @@ def test_mysql_to_sql
node = Arel::Nodes::Bin.new(Arel.sql('zomg'))
assert_equal 'BINARY zomg', viz.accept(node)
end
+
+ def test_equality_with_same_ivars
+ array = [Bin.new('zomg'), Bin.new('zomg')]
+ assert_equal 1, array.uniq.size
+ end
+
+ def test_inequality_with_different_ivars
+ array = [Bin.new('zomg'), Bin.new('zomg!')]
+ assert_equal 2, array.uniq.size
+ end
end
end
end
12 test/nodes/test_count.rb
View
@@ -24,4 +24,16 @@
}
end
end
+
+ describe 'equality' do
+ it 'is equal with equal ivars' do
+ array = [Arel::Nodes::Count.new('foo'), Arel::Nodes::Count.new('foo')]
+ assert_equal 1, array.uniq.size
+ end
+
+ it 'is not equal with different ivars' do
+ array = [Arel::Nodes::Count.new('foo'), Arel::Nodes::Count.new('foo!')]
+ assert_equal 2, array.uniq.size
+ end
+ end
end
20 test/nodes/test_delete_statement.rb
View
@@ -11,4 +11,24 @@
dolly.wheres.wont_be_same_as statement.wheres
end
end
+
+ describe 'equality' do
+ it 'is equal with equal ivars' do
+ statement1 = Arel::Nodes::DeleteStatement.new
+ statement1.wheres = %w[a b c]
+ statement2 = Arel::Nodes::DeleteStatement.new
+ statement2.wheres = %w[a b c]
+ array = [statement1, statement2]
+ assert_equal 1, array.uniq.size
+ end
+
+ it 'is not equal with different ivars' do
+ statement1 = Arel::Nodes::DeleteStatement.new
+ statement1.wheres = %w[a b c]
+ statement2 = Arel::Nodes::DeleteStatement.new
+ statement2.wheres = %w[1 2 3]
+ array = [statement1, statement2]
+ assert_equal 2, array.uniq.size
+ end
+ end
end
10 test/nodes/test_descending.rb
View
@@ -29,6 +29,16 @@ def test_descending?
descending = Descending.new 'zomg'
assert descending.descending?
end
+
+ def test_equality_with_same_ivars
+ array = [Descending.new('zomg'), Descending.new('zomg')]
+ assert_equal 1, array.uniq.size
+ end
+
+ def test_inequality_with_different_ivars
+ array = [Descending.new('zomg'), Descending.new('zomg!')]
+ assert_equal 2, array.uniq.size
+ end
end
end
end
20 test/nodes/test_distinct.rb
View
@@ -0,0 +1,20 @@
+require 'helper'
+
+module Arel
+ module Nodes
+ describe 'Distinct' do
+ describe 'equality' do
+ it 'is equal to other distinct nodes' do
+ array = [Distinct.new, Distinct.new]
+ assert_equal 1, array.uniq.size
+ end
+
+ it 'is not equal with other nodes' do
+ array = [Distinct.new, Node.new]
+ assert_equal 2, array.uniq.size
+ end
+ end
+ end
+ end
+end
+
10 test/nodes/test_equality.rb
View
@@ -69,6 +69,16 @@ def quote_table_name(*args) @quote_count += 1; super; end
node.right.must_equal right
end
end
+
+ it 'is equal with equal ivars' do
+ array = [Equality.new('foo', 'bar'), Equality.new('foo', 'bar')]
+ assert_equal 1, array.uniq.size
+ end
+
+ it 'is not equal with different ivars' do
+ array = [Equality.new('foo', 'bar'), Equality.new('foo', 'baz')]
+ assert_equal 2, array.uniq.size
+ end
end
end
end
14 test/nodes/test_extract.rb
View
@@ -16,4 +16,18 @@
}
end
end
+
+ describe 'equality' do
+ it 'is equal with equal ivars' do
+ table = Arel::Table.new :users
+ array = [table[:attr].extract('foo'), table[:attr].extract('foo')]
+ assert_equal 1, array.uniq.size
+ end
+
+ it 'is not equal with different ivars' do
+ table = Arel::Table.new :users
+ array = [table[:attr].extract('foo'), table[:attr].extract('bar')]
+ assert_equal 2, array.uniq.size
+ end
+ end
end
20 test/nodes/test_false.rb
View
@@ -0,0 +1,20 @@
+require 'helper'
+
+module Arel
+ module Nodes
+ describe 'False' do
+ describe 'equality' do
+ it 'is equal to other false nodes' do
+ array = [False.new, False.new]
+ assert_equal 1, array.uniq.size
+ end
+
+ it 'is not equal with other nodes' do
+ array = [False.new, Node.new]
+ assert_equal 2, array.uniq.size
+ end
+ end
+ end
+ end
+end
+
12 test/nodes/test_grouping.rb
View
@@ -7,6 +7,18 @@ module Nodes
grouping = Grouping.new('foo')
grouping.eq('foo').to_sql.must_be_like %q{('foo') = 'foo'}
end
+
+ describe 'equality' do
+ it 'is equal with equal ivars' do
+ array = [Grouping.new('foo'), Grouping.new('foo')]
+ assert_equal 1, array.uniq.size
+ end
+
+ it 'is not equal with different ivars' do
+ array = [Grouping.new('foo'), Grouping.new('bar')]
+ assert_equal 2, array.uniq.size
+ end
+ end
end
end
end
10 test/nodes/test_infix_operation.rb
View
@@ -25,6 +25,16 @@ def test_opertaion_ordering
assert_equal operation, ordering.expr
assert ordering.descending?
end
+
+ def test_equality_with_same_ivars
+ array = [InfixOperation.new(:+, 1, 2), InfixOperation.new(:+, 1, 2)]
+ assert_equal 1, array.uniq.size
+ end
+
+ def test_inequality_with_different_ivars
+ array = [InfixOperation.new(:+, 1, 2), InfixOperation.new(:+, 1, 3)]
+ assert_equal 2, array.uniq.size
+ end
end
end
end
24 test/nodes/test_insert_statement.rb
View
@@ -15,4 +15,28 @@
dolly.values.wont_be_same_as statement.values
end
end
+
+ describe 'equality' do
+ it 'is equal with equal ivars' do
+ statement1 = Arel::Nodes::InsertStatement.new
+ statement1.columns = %w[a b c]
+ statement1.values = %w[x y z]
+ statement2 = Arel::Nodes::InsertStatement.new
+ statement2.columns = %w[a b c]
+ statement2.values = %w[x y z]
+ array = [statement1, statement2]
+ assert_equal 1, array.uniq.size
+ end
+
+ it 'is not equal with different ivars' do
+ statement1 = Arel::Nodes::InsertStatement.new
+ statement1.columns = %w[a b c]
+ statement1.values = %w[x y z]
+ statement2 = Arel::Nodes::InsertStatement.new
+ statement2.columns = %w[a b c]
+ statement2.values = %w[1 2 3]
+ array = [statement1, statement2]
+ assert_equal 2, array.uniq.size
+ end
+ end
end
16 test/nodes/test_named_function.rb
View
@@ -25,6 +25,22 @@ def test_construct_with_alias
assert_kind_of SqlLiteral, function.alias
assert_equal 'wth', function.alias
end
+
+ def test_equality_with_same_ivars
+ array = [
+ NamedFunction.new('omg', 'zomg', 'wth'),
+ NamedFunction.new('omg', 'zomg', 'wth')
+ ]
+ assert_equal 1, array.uniq.size
+ end
+
+ def test_inequality_with_different_ivars
+ array = [
+ NamedFunction.new('omg', 'zomg', 'wth'),
+ NamedFunction.new('zomg', 'zomg', 'wth')
+ ]
+ assert_equal 2, array.uniq.size
+ end
end
end
end
12 test/nodes/test_not.rb
View
@@ -12,6 +12,18 @@ module Nodes
node.expr.must_equal expr
end
end
+
+ describe 'equality' do
+ it 'is equal with equal ivars' do
+ array = [Not.new('foo'), Not.new('foo')]
+ assert_equal 1, array.uniq.size
+ end
+
+ it 'is not equal with different ivars' do
+ array = [Not.new('foo'), Not.new('baz')]
+ assert_equal 2, array.uniq.size
+ end
+ end
end
end
end
12 test/nodes/test_or.rb
View
@@ -17,6 +17,18 @@ module Nodes
oror.expr.right.must_equal right
end
end
+
+ describe 'equality' do
+ it 'is equal with equal ivars' do
+ array = [Or.new('foo', 'bar'), Or.new('foo', 'bar')]
+ assert_equal 1, array.uniq.size
+ end
+
+ it 'is not equal with different ivars' do
+ array = [Or.new('foo', 'bar'), Or.new('foo', 'baz')]
+ assert_equal 2, array.uniq.size
+ end
+ end
end
end
end
18 test/nodes/test_over.rb
View
@@ -46,4 +46,22 @@
}
end
end
+
+ describe 'equality' do
+ it 'is equal with equal ivars' do
+ array = [
+ Arel::Nodes::Over.new('foo', 'bar'),
+ Arel::Nodes::Over.new('foo', 'bar')
+ ]
+ assert_equal 1, array.uniq.size
+ end
+
+ it 'is not equal with different ivars' do
+ array = [
+ Arel::Nodes::Over.new('foo', 'bar'),
+ Arel::Nodes::Over.new('foo', 'baz')
+ ]
+ assert_equal 2, array.uniq.size
+ end
+ end
end
38 test/nodes/test_select_core.rb
View
@@ -26,6 +26,44 @@ def test_set_quantifier
viz = Arel::Visitors::ToSql.new Table.engine.connection_pool
assert_match 'DISTINCT', viz.accept(core)
end
+
+ def test_equality_with_same_ivars
+ core1 = SelectCore.new
+ core1.froms = %w[a b c]
+ core1.projections = %w[d e f]
+ core1.wheres = %w[g h i]
+ core1.groups = %w[j k l]
+ core1.windows = %w[m n o]
+ core1.having = %w[p q r]
+ core2 = SelectCore.new
+ core2.froms = %w[a b c]
+ core2.projections = %w[d e f]
+ core2.wheres = %w[g h i]
+ core2.groups = %w[j k l]
+ core2.windows = %w[m n o]
+ core2.having = %w[p q r]
+ array = [core1, core2]
+ assert_equal 1, array.uniq.size
+ end
+
+ def test_inequality_with_different_ivars
+ core1 = SelectCore.new
+ core1.froms = %w[a b c]
+ core1.projections = %w[d e f]
+ core1.wheres = %w[g h i]
+ core1.groups = %w[j k l]
+ core1.windows = %w[m n o]
+ core1.having = %w[p q r]
+ core2 = SelectCore.new
+ core2.froms = %w[a b c]
+ core2.projections = %w[d e f]
+ core2.wheres = %w[g h i]
+ core2.groups = %w[j k l]
+ core2.windows = %w[m n o]
+ core2.having = %w[l o l]
+ array = [core1, core2]
+ assert_equal 2, array.uniq.size
+ end
end
end
end
36 test/nodes/test_select_statement.rb
View
@@ -10,4 +10,40 @@
dolly.cores.wont_be_same_as statement.cores
end
end
+
+ describe 'equality' do
+ it 'is equal with equal ivars' do
+ statement1 = Arel::Nodes::SelectStatement.new %w[a b c]
+ statement1.offset = 1
+ statement1.limit = 2
+ statement1.lock = false
+ statement1.orders = %w[x y z]
+ statement1.with = 'zomg'
+ statement2 = Arel::Nodes::SelectStatement.new %w[a b c]
+ statement2.offset = 1
+ statement2.limit = 2
+ statement2.lock = false
+ statement2.orders = %w[x y z]
+ statement2.with = 'zomg'
+ array = [statement1, statement2]
+ assert_equal 1, array.uniq.size
+ end
+
+ it 'is not equal with different ivars' do
+ statement1 = Arel::Nodes::SelectStatement.new %w[a b c]
+ statement1.offset = 1
+ statement1.limit = 2
+ statement1.lock = false
+ statement1.orders = %w[x y z]
+ statement1.with = 'zomg'
+ statement2 = Arel::Nodes::SelectStatement.new %w[a b c]
+ statement2.offset = 1
+ statement2.limit = 2
+ statement2.lock = false
+ statement2.orders = %w[x y z]
+ statement2.with = 'wth'
+ array = [statement1, statement2]
+ assert_equal 2, array.uniq.size
+ end
+ end
end
10 test/nodes/test_sql_literal.rb
View
@@ -31,6 +31,16 @@ module Nodes
node = SqlLiteral.new('foo').eq(1)
@visitor.accept(node).must_be_like %{ foo = 1 }
end
+
+ it 'is equal with equal contents' do
+ array = [SqlLiteral.new('foo'), SqlLiteral.new('foo')]
+ assert_equal 1, array.uniq.size
+ end
+
+ it 'is not equal with different contents' do
+ array = [SqlLiteral.new('foo'), SqlLiteral.new('bar')]
+ assert_equal 2, array.uniq.size
+ end
end
describe 'grouped "or" equality' do
12 test/nodes/test_sum.rb
View
@@ -9,4 +9,16 @@
}
end
end
+
+ describe 'equality' do
+ it 'is equal with equal ivars' do
+ array = [Arel::Nodes::Sum.new('foo'), Arel::Nodes::Sum.new('foo')]
+ assert_equal 1, array.uniq.size
+ end
+
+ it 'is not equal with different ivars' do
+ array = [Arel::Nodes::Sum.new('foo'), Arel::Nodes::Sum.new('foo!')]
+ assert_equal 2, array.uniq.size
+ end
+ end
end
24 test/nodes/test_table_alias.rb
View
@@ -5,12 +5,32 @@ module Arel
module Nodes
describe 'table alias' do
it 'has an #engine which delegates to the relation' do
- engine = Object.new
- relation = OpenStruct.new(:engine => engine)
+ engine = 'vroom'
+ relation = Table.new(:users, engine)
node = TableAlias.new relation, :foo
node.engine.must_equal engine
end
+
+ describe 'equality' do
+ it 'is equal with equal ivars' do
+ relation1 = Table.new(:users, 'vroom')
+ node1 = TableAlias.new relation1, :foo
+ relation2 = Table.new(:users, 'vroom')
+ node2 = TableAlias.new relation2, :foo
+ array = [node1, node2]
+ assert_equal 1, array.uniq.size
+ end
+
+ it 'is not equal with different ivars' do
+ relation1 = Table.new(:users, 'vroom')
+ node1 = TableAlias.new relation1, :foo
+ relation2 = Table.new(:users, 'vroom')
+ node2 = TableAlias.new relation2, :bar
+ array = [node1, node2]
+ assert_equal 2, array.uniq.size
+ end
+ end
end
end
end
21 test/nodes/test_true.rb
View
@@ -0,0 +1,21 @@
+require 'helper'
+
+module Arel
+ module Nodes
+ describe 'True' do
+ describe 'equality' do
+ it 'is equal to other true nodes' do
+ array = [True.new, True.new]
+ assert_equal 1, array.uniq.size
+ end
+
+ it 'is not equal with other nodes' do
+ array = [True.new, Node.new]
+ assert_equal 2, array.uniq.size
+ end
+ end
+ end
+ end
+end
+
+
40 test/nodes/test_update_statement.rb
View
@@ -15,4 +15,44 @@
dolly.values.wont_be_same_as statement.values
end
end
+
+ describe 'equality' do
+ it 'is equal with equal ivars' do
+ statement1 = Arel::Nodes::UpdateStatement.new
+ statement1.relation = 'zomg'
+ statement1.wheres = 2
+ statement1.values = false
+ statement1.orders = %w[x y z]
+ statement1.limit = 42
+ statement1.key = 'zomg'
+ statement2 = Arel::Nodes::UpdateStatement.new
+ statement2.relation = 'zomg'
+ statement2.wheres = 2
+ statement2.values = false
+ statement2.orders = %w[x y z]
+ statement2.limit = 42
+ statement2.key = 'zomg'
+ array = [statement1, statement2]
+ assert_equal 1, array.uniq.size
+ end
+
+ it 'is not equal with different ivars' do
+ statement1 = Arel::Nodes::UpdateStatement.new
+ statement1.relation = 'zomg'
+ statement1.wheres = 2
+ statement1.values = false
+ statement1.orders = %w[x y z]
+ statement1.limit = 42
+ statement1.key = 'zomg'
+ statement2 = Arel::Nodes::UpdateStatement.new
+ statement2.relation = 'zomg'
+ statement2.wheres = 2
+ statement2.values = false
+ statement2.orders = %w[x y z]
+ statement2.limit = 42
+ statement2.key = 'wth'
+ array = [statement1, statement2]
+ assert_equal 2, array.uniq.size
+ end
+ end
end
73 test/nodes/test_window.rb
View
@@ -0,0 +1,73 @@
+require 'helper'
+
+module Arel
+ module Nodes
+ describe 'Window' do
+ describe 'equality' do
+ it 'is equal with equal ivars' do
+ window1 = Window.new
+ window1.orders = [1, 2]
+ window1.frame 3
+ window2 = Window.new
+ window2.orders = [1, 2]
+ window2.frame 3
+ array = [window1, window2]
+ assert_equal 1, array.uniq.size
+ end
+
+ it 'is not equal with different ivars' do
+ window1 = Window.new
+ window1.orders = [1, 2]
+ window1.frame 3
+ window2 = Window.new
+ window2.orders = [1, 2]
+ window2.frame 4
+ array = [window1, window2]
+ assert_equal 2, array.uniq.size
+ end
+ end
+ end
+
+ describe 'NamedWindow' do
+ describe 'equality' do
+ it 'is equal with equal ivars' do
+ window1 = NamedWindow.new 'foo'
+ window1.orders = [1, 2]
+ window1.frame 3
+ window2 = NamedWindow.new 'foo'
+ window2.orders = [1, 2]
+ window2.frame 3
+ array = [window1, window2]
+ assert_equal 1, array.uniq.size
+ end
+
+ it 'is not equal with different ivars' do
+ window1 = NamedWindow.new 'foo'
+ window1.orders = [1, 2]
+ window1.frame 3
+ window2 = NamedWindow.new 'bar'
+ window2.orders = [1, 2]
+ window2.frame 3
+ array = [window1, window2]
+ assert_equal 2, array.uniq.size
+ end
+ end
+ end
+
+ describe 'CurrentRow' do
+ describe 'equality' do
+ it 'is equal to other current row nodes' do
+ array = [CurrentRow.new, CurrentRow.new]
+ assert_equal 1, array.uniq.size
+ end
+
+ it 'is not equal with other nodes' do
+ array = [CurrentRow.new, Node.new]
+ assert_equal 2, array.uniq.size
+ end
+ end
+ end
+ end
+end
+
+
12 test/test_attributes.rb
View
@@ -10,6 +10,18 @@ module Arel
assert_equal [attribute], node.expressions
end
+ describe 'equality' do
+ it 'is equal with equal ivars' do
+ array = [Attribute.new('foo', 'bar'), Attribute.new('foo', 'bar')]
+ assert_equal 1, array.uniq.size
+ end
+
+ it 'is not equal with different ivars' do
+ array = [Attribute.new('foo', 'bar'), Attribute.new('foo', 'baz')]
+ assert_equal 2, array.uniq.size
+ end
+ end
+
describe 'for' do
it 'deals with unknown column types' do
column = Struct.new(:type).new :crazy
24 test/test_table.rb
View
@@ -180,5 +180,29 @@ module Arel
end
end
end
+
+ describe 'equality' do
+ it 'is equal with equal ivars' do
+ relation1 = Table.new(:users, 'vroom')
+ relation1.aliases = %w[a b c]
+ relation1.table_alias = 'zomg'
+ relation2 = Table.new(:users, 'vroom')
+ relation2.aliases = %w[a b c]
+ relation2.table_alias = 'zomg'
+ array = [relation1, relation2]
+ assert_equal 1, array.uniq.size
+ end
+
+ it 'is not equal with different ivars' do
+ relation1 = Table.new(:users, 'vroom')
+ relation1.aliases = %w[a b c]
+ relation1.table_alias = 'zomg'
+ relation2 = Table.new(:users, 'vroom')
+ relation2.aliases = %w[x y z]
+ relation2.table_alias = 'zomg'
+ array = [relation1, relation2]
+ assert_equal 2, array.uniq.size
+ end
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.