aboutsummaryrefslogtreecommitdiffstats
path: root/spec/active_relation/unit/relations/rename_spec.rb
blob: 192c81984819b4b9f2939e6e3631e583f55c8ae5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper')

module ActiveRelation
  describe Rename do
    before do
      @relation = Table.new(:users)
    end

    describe '#initialize' do
      it "manufactures nested rename relations if multiple renames are provided" do
        Rename.new(@relation, @relation[:id] => :humpty, @relation[:name] => :dumpty). \
          should == Rename.new(Rename.new(@relation, @relation[:name] => :dumpty), @relation[:id] => :humpty)
      end
    end
    
    describe '==' do
      before do
        @another_relation = Table.new(:photos)
      end
      
      it "obtains if the relation, attribute, and rename are identical" do
        Rename.new(@relation, @relation[:id] => :humpty).should == Rename.new(@relation, @relation[:id] => :humpty)
        Rename.new(@relation, @relation[:id] => :humpty).should_not == Rename.new(@relation, @relation[:id] => :dumpty)
        Rename.new(@relation, @relation[:id] => :humpty).should_not == Rename.new(@another_relation, @relation[:id] => :humpty)
      end
    end
  
    describe '#attributes' do
      before do
        @renamed_relation = Rename.new(@relation, @relation[:id] => :schmid)
      end
      
      it "manufactures a list of attributes with the renamed attribute renameed" do
        @renamed_relation.attributes.should include(@relation[:id].as(:schmid).bind(@renamed_relation))
        @renamed_relation.attributes.should_not include(@relation[:id].bind(@renamed_relation))
        @renamed_relation.attributes.should include(@relation[:name].bind(@renamed_relation))
        @renamed_relation.should have(@relation.attributes.size).attributes
      end
    end

    describe '#to_sql' do
      it 'manufactures sql renaming the attribute' do
        Rename.new(@relation, @relation[:id] => :schmid).to_sql.should be_like("
          SELECT `users`.`id` AS 'schmid', `users`.`name`
          FROM `users`
        ")
      end
    end
  end
end