-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds new JSON::Any criteria methods for querying jsonb columns that c…
- Loading branch information
Showing
3 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
require "../spec_helper" | ||
|
||
private class QueryMe < BaseModel | ||
COLUMN_SQL = "users.id, users.created_at, users.updated_at, users.preferences" | ||
|
||
table users do | ||
column preferences : JSON::Any | ||
end | ||
end | ||
|
||
describe JSON::Any::Lucky::Criteria do | ||
describe "has_key" do | ||
it "?" do | ||
preferences.has_key("theme").to_sql.should eq ["SELECT #{QueryMe::COLUMN_SQL} FROM users WHERE users.preferences ? $1", "theme"] | ||
end | ||
|
||
it "negates with NOT()" do | ||
preferences.not.has_key("theme").to_sql.should eq ["SELECT #{QueryMe::COLUMN_SQL} FROM users WHERE NOT(users.preferences ? $1)", "theme"] | ||
end | ||
end | ||
|
||
describe "has_any_keys" do | ||
it "?|" do | ||
preferences.has_any_keys(["theme", "style"]).to_sql.should eq ["SELECT #{QueryMe::COLUMN_SQL} FROM users WHERE users.preferences ?| $1", ["theme", "style"]] | ||
end | ||
|
||
it "negates with NOT()" do | ||
preferences.not.has_any_keys(["theme", "style"]).to_sql.should eq ["SELECT #{QueryMe::COLUMN_SQL} FROM users WHERE NOT(users.preferences ?| $1)", ["theme", "style"]] | ||
end | ||
end | ||
|
||
describe "has_all_keys" do | ||
it "?&" do | ||
preferences.has_all_keys(["theme", "style"]).to_sql.should eq ["SELECT #{QueryMe::COLUMN_SQL} FROM users WHERE users.preferences ?& $1", ["theme", "style"]] | ||
end | ||
|
||
it "negates with NOT()" do | ||
preferences.not.has_all_keys(["theme", "style"]).to_sql.should eq ["SELECT #{QueryMe::COLUMN_SQL} FROM users WHERE NOT(users.preferences ?& $1)", ["theme", "style"]] | ||
end | ||
end | ||
end | ||
|
||
private def preferences | ||
QueryMe::BaseQuery.new.preferences | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters