aboutsummaryrefslogtreecommitdiffstats
path: root/spec/active_relation/relations/rename_spec.rb
blob: 1616d5fdb74e1a06e3b915c16850988c299fa81d (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
require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')

module ActiveRelation
  describe Rename do
    before do
      @relation1 = Table.new(:foo)
      @relation2 = Table.new(:bar)
      @renamed_relation = Rename.new(@relation1, @relation1[:id] => :schmid)
    end

    describe '#initialize' do
      it "manufactures nested rename relations if multiple renames are provided" do
        Rename.new(@relation1, @relation1[:id] => :humpty, @relation1[:name] => :dumpty). \
          should == Rename.new(Rename.new(@relation1, @relation1[:id] => :humpty), @relation1[:name] => :dumpty)
      end
    end
    
    describe '==' do
      it "obtains if the relation, attribute, and rename are identical" do
        Rename.new(@relation1, @relation1[:id] => :humpty).should == Rename.new(@relation1, @relation1[:id] => :humpty)
        Rename.new(@relation1, @relation1[:id] => :humpty).should_not == Rename.new(@relation1, @relation1[:id] => :dumpty)
        Rename.new(@relation1, @relation1[:id] => :humpty).should_not == Rename.new(@relation2, @relation2[:id] => :humpty)
      end
    end
  
    describe '#attributes' do
      it "manufactures a list of attributes with the renamed attribute renameed" do
        Rename.new(@relation1, @relation1[:id] => :schmid).attributes.should ==
          (@relation1.attributes - [@relation1[:id]]) + [@relation1[:id].as(:schmid)]
      end
    end
  
    describe '[]' do
      it 'indexes attributes by rename' do
        @renamed_relation[:id].should be_nil
        @renamed_relation[:schmid].should == @relation1[:id].as(:schmid)
      end
    end
  
    describe '#qualify' do
      it "distributes over the relation and renames" do
        Rename.new(@relation1, @relation1[:id] => :schmid).qualify. \
          should == Rename.new(@relation1.qualify, @relation1[:id].qualify => :schmid)
      end
    end
  
    describe '#to_sql' do
      it 'manufactures sql renameing the attribute' do
        @renamed_relation.to_sql.should be_like("""
          SELECT `foo`.`name`, `foo`.`id` AS 'schmid'
          FROM `foo`
        """)
      end
    end
  end
end