blob: 70f2dff70a4ae1859753f71bca245d32c11057fa (
plain) (
tree)
|
|
require '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
|