aboutsummaryrefslogtreecommitdiffstats
path: root/spec/active_relation/unit/primitives/attribute_spec.rb
blob: 08ed6d3621c4b953da54c300d4dd13aeab9ec42b (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper')

module ActiveRelation
  describe Attribute do
    before do
      @relation = Table.new(:users)
      @attribute = Attribute.new(@relation, :id)
    end
  
    describe Attribute::Transformations do
      describe '#as' do
        it "manufactures an aliased attributed" do
          @attribute.as(:alias).should == Attribute.new(@relation, @attribute.name, :alias => :alias, :ancestor => @attribute)
        end
      end
    
      describe '#bind' do
        it "manufactures an attribute with the relation bound and self as an ancestor" do
          derived_relation = @relation.select(@relation[:id].eq(1))
          @attribute.bind(derived_relation).should == Attribute.new(derived_relation, @attribute.name, :ancestor => @attribute)
        end
        
        it "returns self if the substituting to the same relation" do
          @attribute.bind(@relation).should == @attribute
        end
      end
    
      describe '#to_attribute' do
        it "returns self" do
          @attribute.to_attribute.should == @attribute
        end
      end
    end
    
    describe '#column' do
      it "returns the corresponding column in the relation" do
        @attribute.column.should == @relation.column_for(@attribute)
      end
    end
    
    describe '#qualified_name' do
      it "manufactures an attribute name prefixed with the relation's name" do
        @attribute.qualified_name.should == "#{@relation.prefix_for(@attribute)}.id"
      end
    end
    
    describe '#engine' do
      it "delegates to its relation" do
        Attribute.new(@relation, :id).engine.should == @relation.engine
      end
    end
    
    describe Attribute::Congruence do
      describe '=~' do
        it "obtains if the attributes are identical" do
          Attribute.new(@relation, :name).should =~ Attribute.new(@relation, :name)
        end
      
        it "obtains if the attributes have an overlapping history" do
          Attribute.new(@relation, :name, :ancestor => Attribute.new(@relation, :name)).should =~ Attribute.new(@relation, :name)
          Attribute.new(@relation, :name).should =~ Attribute.new(@relation, :name, :ancestor => Attribute.new(@relation, :name))
        end
      end
      
      describe 'hashing' do
        it "implements hash equality" do
          Attribute.new(@relation, 'name').should hash_the_same_as(Attribute.new(@relation, 'name'))
          Attribute.new(@relation, 'name').should_not hash_the_same_as(Attribute.new(@relation, 'id'))
        end
      end
    end
    
    describe '#to_sql' do
      describe 'for a simple attribute' do
        it "manufactures sql with an alias" do
          @attribute.to_sql.should be_like("`users`.`id`")
        end
      end
      
      describe 'for an attribute in a join relation where the source relation is aliased' do
        before do
          another_relation = Table.new(:photos)
          @join_with_alias = @relation.as(:alias).join(another_relation).on(@relation[:id].eq(another_relation[:user_id]))
        end
        
        it "manufactures sql with an alias" do
          @join_with_alias[@attribute].to_sql.should be_like("`alias`.`id`")
        end
      end
    end
  
    describe Attribute::Predications do
      before do
        @attribute = Attribute.new(@relation, :name)
      end
    
      describe '#eq' do
        it "manufactures an equality predicate" do
          @attribute.eq('name').should == Equality.new(@attribute, 'name')
        end
      end
    
      describe '#lt' do
        it "manufactures a less-than predicate" do
          @attribute.lt(10).should == LessThan.new(@attribute, 10)
        end
      end
    
      describe '#lteq' do
        it "manufactures a less-than or equal-to predicate" do
          @attribute.lteq(10).should == LessThanOrEqualTo.new(@attribute, 10)
        end
      end
    
      describe '#gt' do
        it "manufactures a greater-than predicate" do
          @attribute.gt(10).should == GreaterThan.new(@attribute, 10)
        end
      end
    
      describe '#gteq' do
        it "manufactures a greater-than or equal-to predicate" do
          @attribute.gteq(10).should == GreaterThanOrEqualTo.new(@attribute, 10)
        end
      end
    
      describe '#matches' do
        it "manufactures a match predicate" do
          @attribute.matches(/.*/).should == Match.new(@attribute, /.*/)
        end
      end
      
      describe '#in' do
        it "manufactures an in predicate" do
          @attribute.in(1..30).should == In.new(@attribute, (1..30))
        end
      end
    end
  
    describe Attribute::Expressions do
      before do
        @attribute = Attribute.new(@relation, :name)    
      end
    
      describe '#count' do
        it "manufactures a count Expression" do
          @attribute.count.should == Expression.new(@attribute, "COUNT")
        end
      end
    
      describe '#sum' do
        it "manufactures a sum Expression" do
          @attribute.sum.should == Expression.new(@attribute, "SUM")
        end
      end
    
      describe '#maximum' do
        it "manufactures a maximum Expression" do
          @attribute.maximum.should == Expression.new(@attribute, "MAX")
        end
      end
    
      describe '#minimum' do
        it "manufactures a minimum Expression" do
          @attribute.minimum.should == Expression.new(@attribute, "MIN")
        end
      end
    
      describe '#average' do
        it "manufactures an average Expression" do
          @attribute.average.should == Expression.new(@attribute, "AVG")
        end
      end 
    end
  end
end