2013-02-06 03:16:51 +08:00
# encoding: utf-8
require 'spec_helper'
require_dependency 'sql_builder'
describe SqlBuilder do
2013-02-26 00:42:20 +08:00
describe " attached " do
before do
2013-02-10 20:37:24 +08:00
@builder = Post . sql_builder ( " select * from posts /*where*/ /*limit*/ " )
end
2013-02-06 03:16:51 +08:00
2013-02-26 00:42:20 +08:00
it " should find a post by id " do
2013-02-10 20:37:24 +08:00
p = Fabricate ( :post )
@builder . where ( 'id = :id and topic_id = :topic_id' , id : p . id , topic_id : p . topic_id )
p2 = @builder . exec . first
p2 . id . should == p . id
p2 . should == p
end
2013-02-06 03:16:51 +08:00
end
2013-05-23 13:21:07 +08:00
describe " map " do
class SqlBuilder :: TestClass
attr_accessor :int , :string , :date , :text
end
it " correctly maps to a klass " do
rows = SqlBuilder . new ( " SELECT 1 AS int, 'string' AS string, CAST(NOW() at time zone 'utc' AS timestamp without time zone) AS date, 'text'::text AS text " )
. map_exec ( SqlBuilder :: TestClass )
rows . count . should == 1
row = rows [ 0 ]
row . int . should == 1
row . string . should == " string "
row . text . should == " text "
row . date . should be_within ( 10 . seconds ) . of ( DateTime . now )
end
end
2013-02-26 00:42:20 +08:00
describe " detached " do
before do
2013-02-10 20:37:24 +08:00
@builder = SqlBuilder . new ( " select * from (select :a A union all select :b) as X /*where*/ /*order_by*/ /*limit*/ /*offset*/ " )
end
2013-02-06 03:16:51 +08:00
2013-02-26 00:42:20 +08:00
it " should allow for 1 param exec " do
@builder . exec ( a : 1 , b : 2 ) . values [ 0 ] [ 0 ] . should == '1'
2013-02-10 20:37:24 +08:00
end
2013-02-06 03:16:51 +08:00
2013-02-26 00:42:20 +08:00
it " should allow for a single where " do
2013-02-10 20:37:24 +08:00
@builder . where ( " :a = 1 " )
2013-02-26 00:42:20 +08:00
@builder . exec ( a : 1 , b : 2 ) . values [ 0 ] [ 0 ] . should == '1'
2013-02-10 20:37:24 +08:00
end
it " should allow where chaining " do
@builder . where ( " :a = 1 " )
@builder . where ( " 2 = 1 " )
@builder . exec ( a : 1 , b : 2 ) . to_a . length . should == 0
end
it " should allow order by " do
@builder . order_by ( " A desc " ) . limit ( 1 )
. exec ( a : 1 , b : 2 ) . values [ 0 ] [ 0 ] . should == " 2 "
end
it " should allow offset " do
@builder . order_by ( " A desc " ) . offset ( 1 )
. exec ( a : 1 , b : 2 ) . values [ 0 ] [ 0 ] . should == " 1 "
end
2013-02-06 03:16:51 +08:00
end
end