2013-02-05 14:16:51 -05:00
|
|
|
# encoding: utf-8
|
2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2013-02-05 14:16:51 -05:00
|
|
|
require_dependency 'sql_builder'
|
|
|
|
|
|
|
|
describe SqlBuilder do
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
describe "attached" do
|
|
|
|
before do
|
2013-02-10 07:37:24 -05:00
|
|
|
@builder = Post.sql_builder("select * from posts /*where*/ /*limit*/")
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
it "should find a post by id" do
|
2013-02-10 07:37:24 -05: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
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(p2.id).to eq(p.id)
|
|
|
|
expect(p2).to eq(p)
|
2013-02-10 07:37:24 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-06-11 21:14:08 -04:00
|
|
|
describe "map_exec" do
|
2013-05-23 01:21:07 -04:00
|
|
|
class SqlBuilder::TestClass
|
2013-06-11 21:14:08 -04:00
|
|
|
attr_accessor :int, :string, :date, :text, :bool
|
2013-05-23 01:21:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "correctly maps to a klass" do
|
2013-06-11 21:14:08 -04:00
|
|
|
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,
|
|
|
|
true AS bool")
|
2013-05-23 01:21:07 -04:00
|
|
|
.map_exec(SqlBuilder::TestClass)
|
|
|
|
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(rows.count).to eq(1)
|
2013-05-23 01:21:07 -04:00
|
|
|
row = rows[0]
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(row.int).to eq(1)
|
|
|
|
expect(row.string).to eq("string")
|
|
|
|
expect(row.text).to eq("text")
|
|
|
|
expect(row.bool).to eq(true)
|
|
|
|
expect(row.date).to be_within(10.seconds).of(DateTime.now)
|
2013-05-23 01:21:07 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
describe "detached" do
|
|
|
|
before do
|
2013-02-10 07:37:24 -05:00
|
|
|
@builder = SqlBuilder.new("select * from (select :a A union all select :b) as X /*where*/ /*order_by*/ /*limit*/ /*offset*/")
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
it "should allow for 1 param exec" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(@builder.exec(a: 1, b: 2).values[0][0]).to eq('1')
|
2013-02-10 07:37:24 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
it "should allow for a single where" do
|
2013-02-10 07:37:24 -05:00
|
|
|
@builder.where(":a = 1")
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(@builder.exec(a: 1, b: 2).values[0][0]).to eq('1')
|
2013-02-10 07:37:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should allow where chaining" do
|
|
|
|
@builder.where(":a = 1")
|
|
|
|
@builder.where("2 = 1")
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(@builder.exec(a: 1, b: 2).to_a.length).to eq(0)
|
2013-02-10 07:37:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should allow order by" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(@builder.order_by("A desc").limit(1)
|
2017-07-27 21:20:09 -04:00
|
|
|
.exec(a: 1, b: 2).values[0][0]).to eq("2")
|
2013-02-10 07:37:24 -05:00
|
|
|
end
|
|
|
|
it "should allow offset" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(@builder.order_by("A desc").offset(1)
|
2017-07-27 21:20:09 -04:00
|
|
|
.exec(a: 1, b: 2).values[0][0]).to eq("1")
|
2013-02-10 07:37:24 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|