aboutsummaryrefslogtreecommitdiffstats
path: root/spec/active_relation/unit/relations/rename_spec.rb
blob: 9a63c4fc807ebe410ff84211b81251c994003413 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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 '#qualify' do
      it "descends" do
        Rename.new(@relation, @relation[:id] => :schmid).qualify. \
          should == Rename.new(@relation, @relation[:id] => :schmid).descend(&:qualify)
      end
    end
  
    describe '#descend' do
      it "distributes a block over the relation and renames" do
        Rename.new(@relation, @relation[:id] => :schmid).descend(&:qualify). \
          should == Rename.new(@relation.descend(&:qualify), @relation[:id].qualify => :schmid)
      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