aboutsummaryrefslogblamecommitdiffstats
path: root/spec/arel/engines/sql/unit/relations/project_spec.rb
blob: 5e29124cfa0dda81d7667b79a882821704458385 (plain) (tree)
1
2
3
4
5
6
7
8
9
                                                                                      
 
           
                     
             

                                   
       
 
                         

                                                             














                                                         
           
         
 

                                         
                                                                     
           
 
                                                    












                                                                                 

           
 
                                       
                                                              












                                                     
           
         
 

                                                 














                                                          
           

                                                          














                                                                
           
         

       
   
require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper')

module Arel
  describe Project do
    before do
      @relation = Table.new(:users)
      @attribute = @relation[:id]
    end

    describe '#to_sql' do
      describe 'when given an attribute' do
        it "manufactures sql with a limited select clause" do
          sql = Project.new(@relation, @attribute).to_sql

          adapter_is :mysql do
            sql.should be_like(%Q{
              SELECT `users`.`id`
              FROM `users`
            })
          end

          adapter_is_not :mysql do
            sql.should be_like(%Q{
              SELECT "users"."id"
              FROM "users"
            })
          end
        end
      end

      describe 'when given a relation' do
        before do
          @scalar_relation = Project.new(@relation, @relation[:name])
        end

        it "manufactures sql with scalar selects" do
          sql = Project.new(@relation, @scalar_relation).to_sql

          adapter_is :mysql do
            sql.should be_like(%Q{
              SELECT (SELECT `users`.`name` FROM `users`) AS `users` FROM `users`
            })
          end

          adapter_is_not :mysql do
            sql.should be_like(%Q{
              SELECT (SELECT "users"."name" FROM "users") AS "users" FROM "users"
            })
          end
        end
      end

      describe 'when given a string' do
        it "passes the string through to the select clause" do
          sql = Project.new(@relation, 'asdf').to_sql

          adapter_is :mysql do
            sql.should be_like(%Q{
              SELECT asdf FROM `users`
            })
          end

          adapter_is_not :mysql do
            sql.should be_like(%Q{
              SELECT asdf FROM "users"
            })
          end
        end
      end

      describe 'when given an expression' do
        it 'manufactures sql with expressions' do
          sql = @relation.project(@attribute.count).to_sql

          adapter_is :mysql do
            sql.should be_like(%Q{
              SELECT COUNT(`users`.`id`) AS count_id
              FROM `users`
            })
          end

          adapter_is_not :mysql do
            sql.should be_like(%Q{
              SELECT COUNT("users"."id") AS count_id
              FROM "users"
            })
          end
        end

        it 'manufactures sql with distinct expressions' do
          sql = @relation.project(@attribute.count(true)).to_sql

          adapter_is :mysql do
            sql.should be_like(%Q{
              SELECT COUNT(DISTINCT `users`.`id`) AS count_id
              FROM `users`
            })
          end

          adapter_is_not :mysql do
            sql.should be_like(%Q{
              SELECT COUNT(DISTINCT "users"."id") AS count_id
              FROM "users"
            })
          end
        end
      end
    end
  end
end