aboutsummaryrefslogtreecommitdiffstats
path: root/spec/active_relation/relations/rename_spec.rb
blob: 695e51c19128447b3d00c7d5817366852243ff49 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')

module ActiveRelation
  describe Rename do
    before do
      @relation1 = Table.new(:users)
      @relation2 = Table.new(:photos)
      @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[:name] => :dumpty), @relation1[:id] => :humpty)
      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
        @renamed_relation.attributes.should include(@renamed_relation[:schmid])
        @renamed_relation.should have(@relation1.attributes.size).attributes
      end
    end
  
    describe '[]' do
      describe 'when given a', Symbol do
        it 'indexes attributes by rename if the symbol names an attribute within the relation' do
          @renamed_relation[:id].should be_nil
          @renamed_relation[:schmid].should == @relation1[:id].as(:schmid).substitute(@renamed_relation)
          @renamed_relation[:does_not_exist].should be_nil
        end
      end
      
      describe 'when given an', Attribute do
        it 'manufactures a substituted and renamed attribute if the attribute is within the relation' do
          @renamed_relation[@relation1[:id]].should == @relation1[:id].as(:schmid).substitute(@renamed_relation)
          @renamed_relation[@relation1[:name]].should == @relation1[:name].substitute(@renamed_relation)
          @renamed_relation[@renamed_relation[:name]].should == @renamed_relation[:name]
          @renamed_relation[@relation2[:id]].should be_nil
        end
      end

      describe 'when given an', Expression do
        it "manufactures a substituted and renamed expression if the expression is within the relation" do
          pending
        end
      end
      
      describe 'when the rename is constructed with a derived attribute' do
        before do
          @renamed_renamed_relation = Rename.new(@renamed_relation, @relation1[:id] => :flid)
        end
        
        describe 'when given a', Symbol do
          it 'manufactures a substituted and renamed attribute if the attribute is within the relation' do
            @renamed_renamed_relation[:id].should be_nil
            @renamed_renamed_relation[:schmid].should be_nil
            @renamed_renamed_relation[:flid].should == @renamed_relation[:schmid].as(:flid).substitute(@renamed_renamed_relation)
          end
        end
        
        describe 'when given an', Attribute do
          it "manufactures a substituted and renamed attribute if the attribute is within the relation -- even if the provided attribute derived" do
            @renamed_renamed_relation[@renamed_relation[:schmid]].should == @renamed_relation[:schmid].as(:flid).substitute(@renamed_renamed_relation)
            @renamed_renamed_relation[@relation1[:id]].should == @renamed_relation[:schmid].as(:flid).substitute(@renamed_renamed_relation)
          end
        end
        
        describe 'when given an', Expression do
          before do
            @expression = @relation1[:id].count
            @aggregation = Aggregation.new(@relation1, :expressions => [@expression])
            @renamed_relation = Rename.new(@aggregation, @expression => :cnt)
          end
          
          it "manufactures a substituted and renamed expression if the expression is within the relation" do
            @renamed_relation[@expression].should == @aggregation[@expression].as(:cnt).substitute(@renamed_relation)
          end
        end
      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 renaming the attribute' do
        @renamed_relation.to_sql.should be_like("""
          SELECT `users`.`id` AS 'schmid', `users`.`name`
          FROM `users`
        """)
      end
    end
  end
end