-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9a333df
commit 8dc46b5
Showing
5 changed files
with
229 additions
and
22 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
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
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module ActiveHashRelation | ||
VERSION = "1.2.0" | ||
VERSION = "1.3.0" | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
describe ActiveHashRelation do | ||
include Helpers | ||
|
||
context 'NOT filter' do | ||
it "one NOT clause" do | ||
hash = {not: {name: 'Filippos'}} | ||
|
||
query = HelperClass.new.apply_filters(User.all, hash).to_sql | ||
expected_query = q( | ||
"SELECT users.* FROM users WHERE (NOT (users.name = 'Filippos'))" | ||
) | ||
|
||
expect(strip(query)).to eq expected_query.to_s | ||
end | ||
|
||
it "multiple NOT clauses" do | ||
hash = {not: {name: 'Filippos', email: '[email protected]'}} | ||
|
||
query = HelperClass.new.apply_filters(User.all, hash).to_sql | ||
expected_query = q( | ||
"SELECT users.* FROM users WHERE", | ||
"(NOT (users.name = 'Filippos'))", | ||
"AND", | ||
"(NOT (users.email = '[email protected]'))" | ||
) | ||
|
||
expect(strip(query)).to eq expected_query.to_s | ||
end | ||
|
||
if ActiveRecord::VERSION::MAJOR >= 5 | ||
it "NOT clause inside OR clause" do | ||
hash = {or: [{not: {name: 'Filippos', token: '123'}}, {not: {name: 'Vasilis'}}]} | ||
|
||
query = HelperClass.new.apply_filters(User.all, hash).to_sql | ||
expected_query = q( | ||
"SELECT users.* FROM users", | ||
"WHERE", | ||
"(", | ||
"(NOT (users.name = 'Filippos')) AND (NOT (users.token = '123'))", | ||
"OR", | ||
"(NOT (users.name = 'Vasilis'))", | ||
")" | ||
) | ||
|
||
expect(strip(query)).to eq expected_query.to_s | ||
end | ||
end | ||
|
||
it "complex NOT clause" do | ||
hash = {not: {name: 'Filippos', email: {ends_with: '@gmail.com'}}} | ||
|
||
query = HelperClass.new.apply_filters(User.all, hash).to_sql | ||
expected_query = q( | ||
"SELECT users.* FROM users", | ||
"WHERE", | ||
"(NOT (users.name = 'Filippos'))", | ||
"AND", | ||
"(NOT (users.email LIKE '%@gmail.com'))", | ||
) | ||
|
||
expect(strip(query)).to eq expected_query.to_s | ||
end | ||
|
||
context "on NULL" do | ||
it "NOT clause on null" do | ||
hash = {not: {name: {null: true}}} | ||
|
||
query = HelperClass.new.apply_filters(User.all, hash).to_sql | ||
expected_query = q( | ||
"SELECT users.* FROM users WHERE (NOT (users.name IS NULL))" | ||
) | ||
|
||
expect(strip(query)).to eq expected_query.to_s | ||
end | ||
|
||
it "NOT clause on not null" do | ||
hash = {not: {name: {null: false}}} | ||
|
||
query = HelperClass.new.apply_filters(User.all, hash).to_sql | ||
expected_query = q( | ||
"SELECT users.* FROM users WHERE (NOT (users.name IS NOT NULL))" | ||
) | ||
|
||
expect(strip(query)).to eq expected_query.to_s | ||
end | ||
end | ||
|
||
it "NOT clause on associations" do | ||
hash = {microposts: {not: {content: 'Sveavägen 4', id: 1}}} | ||
|
||
query = HelperClass.new.apply_filters(User.all, hash, include_associations: true).to_sql | ||
expected_query = q( | ||
"SELECT users.* FROM users", | ||
"INNER JOIN microposts ON microposts.user_id = users.id", | ||
"WHERE", | ||
"(NOT (microposts.id = 1))", | ||
"AND", | ||
"(NOT (microposts.content = 'Sveavägen 4'))" | ||
) | ||
|
||
expect(strip(query)).to eq expected_query.to_s | ||
end | ||
|
||
end | ||
end |