Sha256: 07cb52caaf1c923a42166c5e93f054c3135b7e3c6051a3806c5a6d8ceb748d76
Contents?: true
Size: 1.79 KB
Versions: 53
Compression:
Stored size: 1.79 KB
Contents
require "spec_helper" describe "Simple::SQL.ask into: :struct" do let!(:users) { 1.upto(USER_COUNT).map { create(:user) } } describe "all into: X" do it "calls the database" do r = SQL.all("SELECT id FROM users", into: Hash) expect(r).to be_a(Array) expect(r.length).to eq(USER_COUNT) expect(r.map(&:class).uniq).to eq([Hash]) end it "returns an empty array when there is no match" do r = SQL.all("SELECT * FROM users WHERE FALSE", into: Hash) expect(r).to eq([]) end end describe "into: :struct" do it "calls the database" do r = SQL.ask("SELECT COUNT(*) AS count FROM users", into: :struct) expect(r.count).to eq(2) expect(r.class.members).to eq([:count]) end it "reuses the struct Class" do r1 = SQL.ask("SELECT COUNT(*) AS count FROM users", into: :struct) r2 = SQL.ask("SELECT COUNT(*) AS count FROM users", into: :struct) expect(r1.class.object_id).to eq(r2.class.object_id) end end describe "into: Hash" do it "calls the database" do r = SQL.ask("SELECT COUNT(*) AS count FROM users", into: Hash) expect(r).to eq({count: 2}) end it "returns nil when there is no match" do r = SQL.ask("SELECT * FROM users WHERE FALSE", into: Hash) expect(r).to be_nil end end describe "into: OpenStruct" do it "returns a OpenStruct with into: OpenStruct" do r = SQL.ask("SELECT COUNT(*) AS count FROM users", into: OpenStruct) expect(r).to be_a(OpenStruct) expect(r).to eq(OpenStruct.new(count: 2)) end it "supports the into: option even with parameters" do r = SQL.ask("SELECT $1::integer AS count FROM users", 2, into: OpenStruct) expect(r).to be_a(OpenStruct) expect(r).to eq(OpenStruct.new(count: 2)) end end end
Version data entries
53 entries across 53 versions & 1 rubygems