-
Notifications
You must be signed in to change notification settings - Fork 133
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
d3625fc
commit 2c22dfa
Showing
54 changed files
with
1,437 additions
and
1,231 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
require File.dirname(__FILE__) + '/../test_helper.rb' | ||
|
||
module ActiveRecordTests | ||
class AssociationsTest < ActiveSupport::TestCase | ||
def test_has_many | ||
binary_logic = accounts(:binary_logic) | ||
ben = users(:ben) | ||
jennifer = users(:jennifer) | ||
|
||
search = binary_logic.users.new_search | ||
assert_kind_of Searchlogic::Search::Base, search | ||
assert_equal User, search.klass | ||
assert_equal({:conditions => "\"users\".account_id = #{binary_logic.id}"}, search.scope) | ||
|
||
assert_equal [jennifer, ben], search.all | ||
assert_equal jennifer, search.first | ||
assert_equal ((ben.id + jennifer.id) / 2.0), search.average("id") | ||
assert_equal 2, search.count | ||
|
||
search.conditions.first_name_contains = "Ben" | ||
|
||
assert_equal [ben], search.all | ||
assert_equal ben, search.first | ||
assert_equal ben.id, search.average("id") | ||
assert_equal 1, search.count | ||
|
||
assert_equal 2, binary_logic.users.count | ||
assert_equal 1, binary_logic.users.all(:conditions => {:first_name_contains => "Ben"}).size | ||
assert_equal 0, binary_logic.users.all(:conditions => {:first_name_contains => "No one"}).size | ||
assert_equal ben.id, binary_logic.users.sum("id", :conditions => {:first_name_contains => "Ben"}) | ||
assert_equal 0, binary_logic.users.sum("id", :conditions => {:first_name_contains => "No one"}) | ||
assert_equal ben.id, binary_logic.users.average("id", :conditions => {:first_name_contains => "Ben"}) | ||
end | ||
|
||
def test_has_many_through | ||
binary_logic = accounts(:binary_logic) | ||
|
||
search = binary_logic.orders.new_search | ||
assert_kind_of Searchlogic::Search::Base, search | ||
assert_equal Order, search.klass | ||
assert_equal({:joins => "INNER JOIN users ON orders.user_id = users.id ", :conditions => "(\"users\".account_id = #{binary_logic.id})"}, search.scope) | ||
|
||
bens_order = orders(:bens_order) | ||
assert_equal [bens_order], search.all | ||
assert_equal bens_order, search.first | ||
assert_equal bens_order.id, search.average("id") | ||
assert_equal 1, search.count | ||
|
||
search.conditions.total_gt = 100 | ||
|
||
assert_equal [bens_order], search.all | ||
assert_equal bens_order, search.first | ||
assert_equal bens_order.id, search.average("id") | ||
assert_equal 1, search.count | ||
|
||
assert_equal 1, binary_logic.orders.count | ||
assert_equal 1, binary_logic.orders.all(:conditions => {:total_gt => 100}).size | ||
assert_equal 0, binary_logic.orders.all(:conditions => {:total_gt => 1000}).size | ||
assert_equal bens_order.id, binary_logic.orders.sum("id", :conditions => {:total_gt => 100}) | ||
assert_equal 0, binary_logic.orders.sum("id", :conditions => {:total_gt => 1000}) | ||
assert_equal bens_order.id, binary_logic.orders.average("id", :conditions => {:total_gt => 100}) | ||
end | ||
|
||
def test_habtm | ||
neco = user_groups(:neco) | ||
ben = users(:ben) | ||
drew = users(:drew) | ||
|
||
search = neco.users.new_search | ||
assert_kind_of Searchlogic::Search::Base, search | ||
assert_equal User, search.klass | ||
assert_equal({:conditions => "\"user_groups_users\".user_group_id = #{neco.id} ", :joins => "INNER JOIN \"user_groups_users\" ON \"users\".id = \"user_groups_users\".user_id"}, search.scope) | ||
|
||
assert_equal [ben, drew], search.all | ||
|
||
assert_equal ben, search.first | ||
assert_equal ((ben.id + drew.id) / 2.0).to_s, search.average("id").to_s | ||
assert_equal 2, search.count | ||
|
||
search.conditions.first_name_contains = "Ben" | ||
|
||
assert_equal [ben], search.all | ||
assert_equal ben, search.first | ||
assert_equal ben.id, search.average("id") | ||
assert_equal 1, search.count | ||
|
||
assert_equal 2, neco.users.count | ||
assert_equal 1, neco.users.all(:conditions => {:first_name_contains => "Ben"}).size | ||
assert_equal 0, neco.users.all(:conditions => {:first_name_contains => "No one"}).size | ||
assert_equal ben.id, neco.users.sum("id", :conditions => {:first_name_contains => "Ben"}) | ||
assert_equal 0, neco.users.sum("id", :conditions => {:first_name_contains => "No one"}) | ||
assert_equal ben.id, neco.users.average("id", :conditions => {:first_name_contains => "Ben"}) | ||
end | ||
end | ||
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,108 @@ | ||
require File.dirname(__FILE__) + '/../test_helper.rb' | ||
|
||
module ActiveRecordTests | ||
class BaseTest < ActiveSupport::TestCase | ||
def test_standard_find | ||
binary_logic = accounts(:binary_logic) | ||
neco = accounts(:neco) | ||
binary_fun = accounts(:binary_fun) | ||
|
||
assert_equal [binary_logic, binary_fun, neco], Account.all | ||
assert_equal binary_logic, Account.first | ||
|
||
assert_equal [binary_logic, binary_fun, neco], Account.find(:all) | ||
assert_equal [binary_logic], Account.find(:all, :conditions => {:name => "Binary Logic"}) | ||
assert_equal [binary_logic], Account.find(:all, :conditions => ["name = ?", "Binary Logic"]) | ||
assert_equal [binary_logic], Account.find(:all, :conditions => "name = 'Binary Logic'") | ||
assert_equal binary_logic, Account.find(:first) | ||
assert_equal [binary_logic, binary_fun, neco], Account.find(:all, nil) | ||
assert_equal [binary_logic, binary_fun, neco], Account.find(:all, {}) | ||
assert_equal [binary_logic, binary_fun, neco], Account.find(:all, :select => "id, name") | ||
end | ||
|
||
def test_standard_calculations | ||
binary_logic = accounts(:binary_logic) | ||
neco = accounts(:neco) | ||
binary_fun = accounts(:binary_fun) | ||
|
||
assert_equal 3, Account.count({}) | ||
assert_equal 3, Account.count(nil) | ||
assert_equal 3, Account.count(:limit => 1) | ||
assert_equal 0, Account.count(:limit => 10, :offset => 10) | ||
assert_equal binary_logic.id + neco.id + binary_fun.id, Account.sum("id") | ||
assert_equal binary_logic.id + neco.id + binary_fun.id, Account.sum("id", {}) | ||
assert_equal (binary_logic.id + neco.id + binary_fun.id) / 3.0, Account.average("id") | ||
assert_equal neco.id, Account.maximum("id") | ||
assert_equal binary_logic.id, Account.minimum("id") | ||
end | ||
|
||
def test_valid_ar_options | ||
assert_equal [:conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :from, :lock], ActiveRecord::Base.valid_find_options | ||
assert_equal [:conditions, :joins, :order, :select, :group, :having, :distinct, :limit, :offset, :include, :from], ActiveRecord::Base.valid_calculations_options | ||
end | ||
|
||
def test_build_search | ||
search = Account.new_search(:conditions => {:name_keywords => "awesome"}, :page => 2, :per_page => 15) | ||
assert_kind_of Searchlogic::Search::Base, search | ||
assert_equal({}, search.scope) | ||
assert_equal Account, search.klass | ||
assert_equal "awesome", search.conditions.name_keywords | ||
assert_equal 2, search.page | ||
assert_equal 15, search.per_page | ||
end | ||
|
||
def test_searchlogic_searching | ||
binary_logic = accounts(:binary_logic) | ||
neco = accounts(:neco) | ||
binary_fun = accounts(:binary_fun) | ||
|
||
assert_equal [binary_logic, binary_fun], Account.all(:conditions => {:name_contains => "Binary"}) | ||
assert_equal [binary_logic], Account.all(:conditions => {:name_contains => "Binary", :users => {:first_name_starts_with => "Ben"}}) | ||
assert_equal [], Account.all(:conditions => {:name_contains => "Binary", :users => {:first_name_starts_with => "Ben", :last_name => "Mills"}}) | ||
assert_equal [binary_logic, neco], Account.all(:conditions => {:users => {:id_gt => 0}}, :include => :users) | ||
|
||
read_only_accounts = Account.all(:conditions => {:name_contains => "Binary"}, :readonly => true) | ||
assert read_only_accounts.first.readonly? | ||
|
||
assert_equal [binary_logic, binary_fun], Account.all(:conditions => {:name_contains => "Binary"}, :page => 2) | ||
assert_equal [], Account.all(:conditions => {:name_contains => "Binary"}, :page => 2, :per_page => 20) | ||
|
||
assert_equal [binary_logic], Account.scope1.all(:conditions => {:users => {:first_name_starts_with => "Ben"}}) | ||
end | ||
|
||
def test_searchlogic_counting | ||
assert_equal 2, Account.count(:conditions => {:name_contains => "Binary"}) | ||
assert_equal 1, Account.count(:conditions => {:name_contains => "Binary", :users => {:first_name_contains => "Ben"}}) | ||
assert_equal 1, Account.count(:conditions => {:name_contains => "Binary", :users => {:first_name_contains => "Ben"}}, :limit => 10, :offset => 10, :order_by => "id", :group => "id") | ||
end | ||
|
||
def test_scoping | ||
assert_equal({:conditions => {:name => "Binary"}, :limit => 10, :readonly => true}, Account.send(:with_scope, :find => {:conditions => {:name => "Binary"}, :limit => 10, :readonly => true}) { Account.send(:scope, :find) }) | ||
assert_equal({:conditions => ["\"accounts\".\"name\" LIKE ?", "%Binary%"], :limit => 10, :offset => 20}, Account.send(:with_scope, :find => {:conditions => {:name_contains => "Binary"}, :per_page => 10, :page => 3}) { Account.send(:scope, :find) }) | ||
end | ||
|
||
def test_accessible_conditions | ||
Account.conditions_accessible :name_contains | ||
assert_equal Set.new(["name_contains"]), Account.accessible_conditions | ||
Account.conditions_accessible :id_gt | ||
assert_equal Set.new(["id_gt", "name_contains"]), Account.accessible_conditions | ||
Account.conditions_accessible :id_gt, :name_contains | ||
assert_equal Set.new(["id_gt", "name_contains"]), Account.accessible_conditions | ||
Account.send(:write_inheritable_attribute, :conditions_accessible, nil) | ||
end | ||
|
||
def test_protected_conditions | ||
Account.conditions_protected :name_contains | ||
assert_equal Set.new(["name_contains"]), Account.protected_conditions | ||
Account.conditions_protected :id_gt | ||
assert_equal Set.new(["id_gt", "name_contains"]), Account.protected_conditions | ||
Account.conditions_protected :id_gt, :name_contains | ||
assert_equal Set.new(["id_gt", "name_contains"]), Account.protected_conditions | ||
Account.send(:write_inheritable_attribute, :conditions_protected, nil) | ||
end | ||
|
||
def test_includes | ||
assert_nothing_raised { Account.all(:conditions => {:users => {:first_name_like => "Ben"}}, :include => :users) } | ||
end | ||
end | ||
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,54 @@ | ||
require File.dirname(__FILE__) + '/../test_helper.rb' | ||
|
||
module ConditionTests | ||
class BaseTest < ActiveSupport::TestCase | ||
def test_condition_type_name | ||
assert_equal "equals", Searchlogic::Condition::Equals.condition_type_name | ||
assert_equal "keywords", Searchlogic::Condition::Keywords.condition_type_name | ||
assert_equal "greater_than_or_equal_to", Searchlogic::Condition::GreaterThanOrEqualTo.condition_type_name | ||
end | ||
|
||
def test_ignore_meaningless_value? | ||
assert !Searchlogic::Condition::Equals.ignore_meaningless_value? | ||
assert Searchlogic::Condition::Keywords.ignore_meaningless_value? | ||
assert !Searchlogic::Condition::NotEqual.ignore_meaningless_value? | ||
end | ||
|
||
def test_value_type | ||
assert_nil Searchlogic::Condition::Equals.value_type | ||
assert_nil Searchlogic::Condition::Keywords.value_type | ||
assert_equal :boolean, Searchlogic::Condition::Nil.value_type | ||
assert_equal :boolean, Searchlogic::Condition::Blank.value_type | ||
assert_nil Searchlogic::Condition::GreaterThan.value_type | ||
end | ||
|
||
def test_initialize | ||
condition = Searchlogic::Condition::Keywords.new(Account, :column => Account.columns_hash["name"]) | ||
assert_equal condition.klass, Account | ||
assert_equal Account.columns_hash["name"], condition.column | ||
|
||
condition = Searchlogic::Condition::GreaterThan.new(Account, :column => "id") | ||
assert_equal Account.columns_hash["id"], condition.column | ||
|
||
condition = Searchlogic::Condition::GreaterThan.new(Account, :column => "id", :column_type => :string, :column_sql_format => "some sql") | ||
assert_equal Account.columns_hash["id"], condition.column | ||
condition.value = "awesome" | ||
assert_equal ["some sql > ?", "awesome"], condition.sanitize | ||
end | ||
|
||
def test_explicitly_set_value | ||
condition = Searchlogic::Condition::Keywords.new(Account, :column => Account.columns_hash["name"]) | ||
assert !condition.explicitly_set_value? | ||
condition.value = "test" | ||
assert condition.explicitly_set_value? | ||
end | ||
|
||
def test_sanitize | ||
# This is tested thoroughly in test_condition_types | ||
end | ||
|
||
def test_value | ||
# This is tested thoroughly in test_condition_types | ||
end | ||
end | ||
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,11 @@ | ||
require File.dirname(__FILE__) + '/../test_helper.rb' | ||
|
||
module ConditionTests | ||
class BeginsWithTest < ActiveSupport::TestCase | ||
def test_sanitize | ||
condition = Searchlogic::Condition::BeginsWith.new(Account, :column => Account.columns_hash["name"]) | ||
condition.value = "Binary" | ||
assert_equal ["\"accounts\".\"name\" LIKE ?", "Binary%"], condition.sanitize | ||
end | ||
end | ||
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,31 @@ | ||
require File.dirname(__FILE__) + '/../test_helper.rb' | ||
|
||
module ConditionTests | ||
class BlankTest < ActiveSupport::TestCase | ||
def test_sanitize | ||
condition = Searchlogic::Condition::Blank.new(Account, :column => Account.columns_hash["id"]) | ||
condition.value = "true" | ||
assert_equal "\"accounts\".\"id\" IS NULL or \"accounts\".\"id\" = '' or \"accounts\".\"id\" = false", condition.sanitize | ||
|
||
condition = Searchlogic::Condition::Blank.new(Account, :column => Account.columns_hash["id"]) | ||
condition.value = "false" | ||
assert_equal "\"accounts\".\"id\" IS NOT NULL and \"accounts\".\"id\" != '' and \"accounts\".\"id\" != false", condition.sanitize | ||
|
||
condition = Searchlogic::Condition::Blank.new(Account, :column => Account.columns_hash["id"]) | ||
condition.value = true | ||
assert_equal "\"accounts\".\"id\" IS NULL or \"accounts\".\"id\" = '' or \"accounts\".\"id\" = false", condition.sanitize | ||
|
||
condition = Searchlogic::Condition::Blank.new(Account, :column => Account.columns_hash["id"]) | ||
condition.value = false | ||
assert_equal "\"accounts\".\"id\" IS NOT NULL and \"accounts\".\"id\" != '' and \"accounts\".\"id\" != false", condition.sanitize | ||
|
||
condition = Searchlogic::Condition::Blank.new(Account, :column => Account.columns_hash["id"]) | ||
condition.value = nil | ||
assert_nil condition.sanitize | ||
|
||
condition = Searchlogic::Condition::Blank.new(Account, :column => Account.columns_hash["id"]) | ||
condition.value = "" | ||
assert_nil condition.sanitize | ||
end | ||
end | ||
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,17 @@ | ||
require File.dirname(__FILE__) + '/../test_helper.rb' | ||
|
||
module ConditionTests | ||
class ChildOfTest < ActiveSupport::TestCase | ||
def test_sanitize | ||
ben = users(:ben) | ||
|
||
condition = Searchlogic::Condition::ChildOf.new(User) | ||
condition.value = ben.id | ||
assert_equal ["\"users\".\"parent_id\" = ?", ben.id], condition.sanitize | ||
|
||
condition = Searchlogic::Condition::ChildOf.new(User) | ||
condition.value = ben | ||
assert_equal ["\"users\".\"parent_id\" = ?", ben.id], condition.sanitize | ||
end | ||
end | ||
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,16 @@ | ||
require File.dirname(__FILE__) + '/../test_helper.rb' | ||
|
||
module ConditionTests | ||
class DescendantOfTest < ActiveSupport::TestCase | ||
def test_sanitize | ||
ben = users(:ben) | ||
drew = users(:drew) | ||
jennifer = users(:jennifer) | ||
tren = users(:tren) | ||
|
||
condition = Searchlogic::Condition::DescendantOf.new(User) | ||
condition.value = ben | ||
assert_equal ["\"users\".\"id\" = ? OR \"users\".\"id\" = ? OR \"users\".\"id\" = ?", drew.id, tren.id, jennifer.id], condition.sanitize | ||
end | ||
end | ||
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,11 @@ | ||
require File.dirname(__FILE__) + '/../test_helper.rb' | ||
|
||
module ConditionTests | ||
class EndsWithTest < ActiveSupport::TestCase | ||
def test_sanitize | ||
condition = Searchlogic::Condition::EndsWith.new(Account, :column => Account.columns_hash["name"]) | ||
condition.value = "Binary" | ||
assert_equal ["\"accounts\".\"name\" LIKE ?", "%Binary"], condition.sanitize | ||
end | ||
end | ||
end |
Oops, something went wrong.