From aeb09afd73cf188c6aee22bf4dd0f1beb937ac22 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Tue, 27 May 2008 21:56:38 -0400 Subject: AND/OR support for predicates --- spec/arel/unit/predicates/binary_spec.rb | 27 +++++++++++++++++++++++ spec/arel/unit/predicates/predicates_spec.rb | 33 ++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 spec/arel/unit/predicates/predicates_spec.rb (limited to 'spec') diff --git a/spec/arel/unit/predicates/binary_spec.rb b/spec/arel/unit/predicates/binary_spec.rb index 5dee4833d4..56fcf2d8ad 100644 --- a/spec/arel/unit/predicates/binary_spec.rb +++ b/spec/arel/unit/predicates/binary_spec.rb @@ -13,6 +13,33 @@ module Arel end end + describe "with compound predicates" do + before do + @operand1 = ConcreteBinary.new(@attribute1, 1) + @operand2 = ConcreteBinary.new(@attribute2, "name") + end + + describe Or do + describe "#to_sql" do + it "manufactures sql with an OR operation" do + Or.new(@operand1, @operand2).to_sql.should be_like(" + (`users`.`id` <=> 1 OR `users`.`name` <=> 'name') + ") + end + end + end + + describe And do + describe "#to_sql" do + it "manufactures sql with an AND operation" do + And.new(@operand1, @operand2).to_sql.should be_like(" + (`users`.`id` <=> 1 AND `users`.`name` <=> 'name') + ") + end + end + end + end + describe '#to_sql' do describe 'when relating two attributes' do it 'manufactures sql with a binary operation' do diff --git a/spec/arel/unit/predicates/predicates_spec.rb b/spec/arel/unit/predicates/predicates_spec.rb new file mode 100644 index 0000000000..d11637cabe --- /dev/null +++ b/spec/arel/unit/predicates/predicates_spec.rb @@ -0,0 +1,33 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper') + +module Arel + describe Predicate do + before do + @relation = Table.new(:users) + @attribute1 = @relation[:id] + @attribute2 = @relation[:name] + @operand1 = Equality.new(@attribute1, 1) + @operand2 = Equality.new(@attribute2, "name") + end + + describe "when being combined with another predicate with AND logic" do + describe "#to_sql" do + it "manufactures sql with an AND operation" do + @operand1.and(@operand2).to_sql.should be_like(" + (`users`.`id` = 1 AND `users`.`name` = 'name') + ") + end + end + end + + describe "when being combined with another predicate with OR logic" do + describe "#to_sql" do + it "manufactures sql with an OR operation" do + @operand1.or(@operand2).to_sql.should be_like(" + (`users`.`id` = 1 OR `users`.`name` = 'name') + ") + end + end + end + end +end \ No newline at end of file -- cgit v1.2.3