spec/simple/sql/scope_spec.rb in simple-sql-0.5.4 vs spec/simple/sql/scope_spec.rb in simple-sql-0.5.5
- old
+ new
@@ -1,24 +1,24 @@
require "spec_helper"
-describe "Simple::SQL::Scope" do
+describe "Simple::SQL::Connection::Scope" do
def expects(expected_result, sql, *args)
expect(SQL.ask(sql, *args)).to eq(expected_result)
end
let!(:users) do
1.upto(2).map { |id| create(:user, id: id) }
end
it 'allows chaining of scopes' do
- scope1 = SQL::Scope.new "SELECT 1, 2 FROM users"
+ scope1 = SQL.scope "SELECT 1, 2 FROM users"
scope2 = scope1.where("FALSE")
expect(scope1.to_sql).not_to eq(scope2.to_sql)
end
context "without conditions" do
- let(:scope) { SQL::Scope.new "SELECT 1, 2 FROM users" }
+ let(:scope) { SQL.scope "SELECT 1, 2 FROM users" }
it "runs with SQL.ask" do
expect(SQL.ask(scope)).to eq([1, 2])
end
@@ -27,11 +27,11 @@
end
end
context "with hash conditions" do
let(:user_id) { SQL.ask "SELECT id FROM users LIMIT 1" }
- let(:scope) { SQL::Scope.new "SELECT 1 FROM users" }
+ let(:scope) { SQL.scope "SELECT 1 FROM users" }
context "that do not match" do
it "does not match with string keys" do
expect(SQL.ask(scope.where(id: -1))).to be_nil
end
@@ -69,11 +69,11 @@
end
context "with non-argument conditions" do
context "that do not match" do
let(:scope) do
- scope = SQL::Scope.new "SELECT 1, 2 FROM users"
+ scope = SQL.scope "SELECT 1, 2 FROM users"
scope = scope.where("id < 0")
scope.where("TRUE")
end
it "runs with SQL.ask" do
@@ -85,11 +85,11 @@
end
end
context "that do match" do
let(:scope) do
- scope = SQL::Scope.new "SELECT 1, 2 FROM users"
+ scope = SQL.scope "SELECT 1, 2 FROM users"
scope = scope.where("id >= 0")
scope.where("TRUE")
end
it "runs with SQL.ask" do
@@ -103,11 +103,11 @@
end
context "with argument conditions" do
context "that do not match" do
let(:scope) do
- scope = SQL::Scope.new "SELECT 1, 2 FROM users"
+ scope = SQL.scope "SELECT 1, 2 FROM users"
scope = scope.where("first_name NOT LIKE ?", "First%")
scope.where("id < ?", 0)
end
it "runs with SQL.ask" do
@@ -119,11 +119,11 @@
end
end
context "where both match" do
let(:scope) do
- scope = SQL::Scope.new "SELECT 1, 2 FROM users"
+ scope = SQL.scope "SELECT 1, 2 FROM users"
scope = scope.where("first_name LIKE ?", "First%")
scope.where("id >= ?", 0)
end
it "runs with SQL.ask" do
@@ -135,11 +135,11 @@
end
end
context "where first condition matches" do
let(:scope) do
- scope = SQL::Scope.new "SELECT 1, 2 FROM users"
+ scope = SQL.scope "SELECT 1, 2 FROM users"
scope = scope.where("first_name LIKE ?", "First%")
scope.where("id < ?", 0)
end
it "runs with SQL.ask" do
@@ -147,22 +147,22 @@
end
end
context "where second condition matches" do
let(:scope) do
- scope = SQL::Scope.new "SELECT 1, 2 FROM users"
+ scope = SQL.scope "SELECT 1, 2 FROM users"
scope = scope.where("first_name LIKE ?", "Boo%")
scope.where("id >= ?", 0)
end
it "runs with SQL.ask" do
expect(SQL.ask(scope)).to be_nil
end
end
describe "hash matches" do
- let(:scope) { SQL::Scope.new("SELECT id FROM users") }
+ let(:scope) { SQL.scope("SELECT id FROM users") }
it 'validates hash keys' do
expect {
scope.where("foo bar" => "baz")
}.to raise_error(ArgumentError)
@@ -176,11 +176,11 @@
UPDATE users SET metadata = jsonb_set(metadata, '{uid}', to_json(id)::jsonb);
SQL
end
def ids_matching(condition)
- scope = SQL::Scope.new("SELECT id FROM users")
+ scope = SQL.scope("SELECT id FROM users")
scope = scope.where(condition)
SQL.all(scope)
end
it "runs with SQL.ask" do
@@ -203,25 +203,25 @@
end
end
context "Building with Hash" do
it "runs with SQL.ask" do
- scope = SQL::Scope.new table: "users", select: "1, 2", where: "id >= 0"
+ scope = SQL.scope table: "users", select: "1, 2", where: "id >= 0"
expect(SQL.all(scope)).to eq([[1,2], [1,2]])
- scope = SQL::Scope.new table: "users", select: [1,3,4], where: "id >= 0"
+ scope = SQL.scope table: "users", select: [1,3,4], where: "id >= 0"
expect(SQL.all(scope)).to eq([[1,3,4], [1,3,4]])
end
it "raises an error with missing or invalid attributes" do
- expect { SQL::Scope.new table: "users", limit: 1 }.to raise_error(ArgumentError)
- expect { SQL::Scope.new select: "*" }.to raise_error(ArgumentError)
+ expect { SQL.scope table: "users", limit: 1 }.to raise_error(ArgumentError)
+ expect { SQL.scope select: "*" }.to raise_error(ArgumentError)
end
end
context "describe pagination" do
let(:scope) do
- scope = SQL::Scope.new "SELECT 1, 2 FROM users"
+ scope = SQL.scope "SELECT 1, 2 FROM users"
scope = scope.where("first_name LIKE ?", "First%")
scope.where("id > ?", 0)
end
it "sets paginated?" do