Permalink
Browse files

Reorganized tests

  • Loading branch information...
1 parent d3625fc commit 2c22dfa3736c15961ea9e957bd4639aa0cecbc76 @binarylogic committed Nov 10, 2008
Showing with 1,437 additions and 1,231 deletions.
  1. +4 −0 CHANGELOG.rdoc
  2. +4 −4 lib/searchlogic/conditions/base.rb
  3. +95 −0 test/active_record_tests/associations_test.rb
  4. +108 −0 test/active_record_tests/base_test.rb
  5. +54 −0 test/condition_tests/base_test.rb
  6. +11 −0 test/condition_tests/begins_with_test.rb
  7. +31 −0 test/condition_tests/blank_test.rb
  8. +17 −0 test/condition_tests/child_of_test.rb
  9. +16 −0 test/condition_tests/descendant_of_test.rb
  10. +11 −0 test/condition_tests/ends_with_test.rb
  11. +19 −0 test/condition_tests/equals_test.rb
  12. +11 −0 test/condition_tests/greater_than_or_equal_to_test.rb
  13. +11 −0 test/condition_tests/greater_than_test.rb
  14. +16 −0 test/condition_tests/inclusive_descendant_of_test.rb
  15. +19 −0 test/condition_tests/keyswords_test.rb
  16. +11 −0 test/condition_tests/less_than_or_equal_to_test.rb
  17. +11 −0 test/condition_tests/less_than_test.rb
  18. +11 −0 test/condition_tests/like_test.rb
  19. +31 −0 test/condition_tests/nil_test.rb
  20. +8 −0 test/condition_tests/not_begin_with_test.rb
  21. +8 −0 test/condition_tests/not_blank_test.rb
  22. +8 −0 test/condition_tests/not_end_with_test.rb
  23. +19 −0 test/condition_tests/not_equal_test.rb
  24. +8 −0 test/condition_tests/not_have_keywords_test.rb
  25. +8 −0 test/condition_tests/not_like_test.rb
  26. +13 −0 test/condition_tests/not_nil_test.rb
  27. +15 −0 test/condition_tests/sibling_of_test.rb
  28. +221 −0 test/conditions_tests/base_test.rb
  29. +18 −0 test/conditions_tests/protection_test.rb
  30. +1 −1 test/{test_config.rb → config_test.rb}
  31. +0 −3 test/fixtures/accounts.yml
  32. +7 −0 test/fixtures/animals.yml
  33. +0 −3 test/fixtures/cats.yml
  34. +0 −3 test/fixtures/dogs.yml
  35. +2 −4 test/fixtures/orders.yml
  36. +3 −9 test/fixtures/user_groups.yml
  37. +6 −12 test/fixtures/users.yml
  38. +237 −0 test/search_tests/base_test.rb
  39. +21 −0 test/search_tests/conditions_test.rb
  40. +167 −0 test/search_tests/ordering_test.rb
  41. +74 −0 test/search_tests/pagination_test.rb
  42. +26 −0 test/search_tests/protection_test.rb
  43. +0 −81 test/test_active_record_associations.rb
  44. +0 −93 test/test_active_record_base.rb
  45. +0 −52 test/test_condition_base.rb
  46. +0 −143 test/test_condition_types.rb
  47. +0 −219 test/test_conditions_base.rb
  48. +0 −16 test/test_conditions_protection.rb
  49. +76 −81 test/test_helper.rb
  50. +0 −227 test/test_search_base.rb
  51. +0 −19 test/test_search_conditions.rb
  52. +0 −165 test/test_search_ordering.rb
  53. +0 −72 test/test_search_pagination.rb
  54. +0 −24 test/test_search_protection.rb
View
4 CHANGELOG.rdoc
@@ -1,3 +1,7 @@
+== 1.5.4 released 2008-10-30
+
+* Removed subclass requirement for adding conditions in Conditions::Base
+
== 1.5.3 released 2008-10-30
* Removed ilike conditions and let the like condition determine if it should use like or ilike depending on the connection adapter.
View
8 lib/searchlogic/conditions/base.rb
@@ -44,8 +44,8 @@ def column_details # :nodoc:
# class SoundsLike < Searchlogic::Condition::Base
# # The name of the conditions. By default its the name of the class, if you want alternate or alias conditions just add them on.
# # If you don't want to add aliases you don't even need to define this method
- # def self.name_for_column(column)
- # super
+ # def self.condition_names_for_column(column)
+ # super + ["similar_to", "sounds"]
# end
#
# # You can return an array or a string. NOT a hash, because all of these conditions
@@ -57,7 +57,7 @@ def column_details # :nodoc:
# end
# end
#
- # Searchlogic::Seearch::Conditions.register_condition(SoundsLike)
+ # Searchlogic::Conditions::Base.register_condition(SoundsLike)
def register_condition(condition_class)
raise(ArgumentError, "You can only register conditions that extend Searchlogic::Condition::Base") unless condition_class.ancestors.include?(Searchlogic::Condition::Base)
conditions << condition_class unless conditions.include?(condition_class)
@@ -409,7 +409,7 @@ def setup_condition(name)
def add_condition!(condition, name, options = {})
self.class.condition_names << name
- options[:column] = options[:column].name if options[:column].class < ::ActiveRecord::ConnectionAdapters::Column
+ options[:column] = options[:column].name
self.class.class_eval <<-"end_eval", __FILE__, __LINE__
def #{name}_object
View
95 test/active_record_tests/associations_test.rb
@@ -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
View
108 test/active_record_tests/base_test.rb
@@ -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
View
54 test/condition_tests/base_test.rb
@@ -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
View
11 test/condition_tests/begins_with_test.rb
@@ -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
View
31 test/condition_tests/blank_test.rb
@@ -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
View
17 test/condition_tests/child_of_test.rb
@@ -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
View
16 test/condition_tests/descendant_of_test.rb
@@ -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
View
11 test/condition_tests/ends_with_test.rb
@@ -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
View
19 test/condition_tests/equals_test.rb
@@ -0,0 +1,19 @@
+require File.dirname(__FILE__) + '/../test_helper.rb'
+
+module ConditionTests
+ class EqualsTest < ActiveSupport::TestCase
+ def test_sanitize
+ condition = Searchlogic::Condition::Equals.new(Account, :column => Account.columns_hash["id"])
+ condition.value = 12
+ assert_equal ["\"accounts\".\"id\" = ?", 12], condition.sanitize
+
+ condition = Searchlogic::Condition::Equals.new(Account, :column => Account.columns_hash["id"])
+ condition.value = [1,2,3,4]
+ assert_equal ["\"accounts\".\"id\" IN (?)", [1, 2, 3, 4]], condition.sanitize
+
+ condition = Searchlogic::Condition::Equals.new(Account, :column => Account.columns_hash["id"])
+ condition.value = (1..10)
+ assert_equal ["\"accounts\".\"id\" BETWEEN ? AND ?", 1, 10], condition.sanitize
+ end
+ end
+end
View
11 test/condition_tests/greater_than_or_equal_to_test.rb
@@ -0,0 +1,11 @@
+require File.dirname(__FILE__) + '/../test_helper.rb'
+
+module ConditionTests
+ class GreaterThanOrEqualToTest < ActiveSupport::TestCase
+ def test_sanitize
+ condition = Searchlogic::Condition::GreaterThanOrEqualTo.new(Account, :column => Account.columns_hash["id"])
+ condition.value = 2
+ assert_equal ["\"accounts\".\"id\" >= ?", 2], condition.sanitize
+ end
+ end
+end
View
11 test/condition_tests/greater_than_test.rb
@@ -0,0 +1,11 @@
+require File.dirname(__FILE__) + '/../test_helper.rb'
+
+module ConditionTests
+ class GreaterThanTest < ActiveSupport::TestCase
+ def test_sanitize
+ condition = Searchlogic::Condition::GreaterThan.new(Account, :column => Account.columns_hash["id"])
+ condition.value = 2
+ assert_equal ["\"accounts\".\"id\" > ?", 2], condition.sanitize
+ end
+ end
+end
View
16 test/condition_tests/inclusive_descendant_of_test.rb
@@ -0,0 +1,16 @@
+require File.dirname(__FILE__) + '/../test_helper.rb'
+
+module ConditionTests
+ class InclusiveDescendantOfTest < ActiveSupport::TestCase
+ def test_sanitize
+ ben = users(:ben)
+ drew = users(:drew)
+ jennifer = users(:jennifer)
+ tren = users(:tren)
+
+ condition = Searchlogic::Condition::InclusiveDescendantOf.new(User)
+ condition.value = ben
+ assert_equal ["(\"users\".\"id\" = ?) OR (\"users\".\"id\" = ? OR \"users\".\"id\" = ? OR \"users\".\"id\" = ?)", ben.id, drew.id, tren.id, jennifer.id], condition.sanitize
+ end
+ end
+end
View
19 test/condition_tests/keyswords_test.rb
@@ -0,0 +1,19 @@
+require File.dirname(__FILE__) + '/../test_helper.rb'
+
+module ConditionTests
+ class KeywordsTest < ActiveSupport::TestCase
+ def test_sanitize
+ condition = Searchlogic::Condition::Keywords.new(Account, :column => Account.columns_hash["name"])
+ condition.value = "freedom yeah, freedom YEAH right"
+ assert_equal ["\"accounts\".\"name\" LIKE ? AND \"accounts\".\"name\" LIKE ? AND \"accounts\".\"name\" LIKE ?", "%freedom%", "%yeah%", "%right%"], condition.sanitize
+
+ condition = Searchlogic::Condition::Keywords.new(Account, :column => Account.columns_hash["name"])
+ condition.value = "%^$*(^$)"
+ assert_nil condition.sanitize
+
+ condition = Searchlogic::Condition::Keywords.new(Account, :column => Account.columns_hash["name"])
+ condition.value = "%^$*(^$) àáâãäåßéèêëìíîïñòóôõöùúûüýÿ"
+ assert_equal ["\"accounts\".\"name\" LIKE ?", "%àáâãäåßéèêëìíîïñòóôõöùúûüýÿ%"], condition.sanitize
+ end
+ end
+end
View
11 test/condition_tests/less_than_or_equal_to_test.rb
@@ -0,0 +1,11 @@
+require File.dirname(__FILE__) + '/../test_helper.rb'
+
+module ConditionTests
+ class LessThanOrEqualToTest < ActiveSupport::TestCase
+ def test_sanitize
+ condition = Searchlogic::Condition::LessThanOrEqualTo.new(Account, :column => Account.columns_hash["id"])
+ condition.value = 2
+ assert_equal ["\"accounts\".\"id\" <= ?", 2], condition.sanitize
+ end
+ end
+end
View
11 test/condition_tests/less_than_test.rb
@@ -0,0 +1,11 @@
+require File.dirname(__FILE__) + '/../test_helper.rb'
+
+module ConditionTests
+ class LessThanTest < ActiveSupport::TestCase
+ def test_sanitize
+ condition = Searchlogic::Condition::LessThan.new(Account, :column => Account.columns_hash["id"])
+ condition.value = 2
+ assert_equal ["\"accounts\".\"id\" < ?", 2], condition.sanitize
+ end
+ end
+end
View
11 test/condition_tests/like_test.rb
@@ -0,0 +1,11 @@
+require File.dirname(__FILE__) + '/../test_helper.rb'
+
+module ConditionTests
+ class LikeTest < ActiveSupport::TestCase
+ def test_sanitize
+ condition = Searchlogic::Condition::Like.new(Account, :column => Account.columns_hash["name"])
+ condition.value = "Binary and blah"
+ assert_equal ["\"accounts\".\"name\" LIKE ?", "%Binary and blah%"], condition.sanitize
+ end
+ end
+end
View
31 test/condition_tests/nil_test.rb
@@ -0,0 +1,31 @@
+require File.dirname(__FILE__) + '/../test_helper.rb'
+
+module ConditionTests
+ class NilTest < ActiveSupport::TestCase
+ def test_sanitize
+ condition = Searchlogic::Condition::Nil.new(Account, :column => Account.columns_hash["id"])
+ condition.value = true
+ assert_equal "\"accounts\".\"id\" IS NULL", condition.sanitize
+
+ condition = Searchlogic::Condition::Nil.new(Account, :column => Account.columns_hash["id"])
+ condition.value = false
+ assert_equal "\"accounts\".\"id\" IS NOT NULL", condition.sanitize
+
+ condition = Searchlogic::Condition::Nil.new(Account, :column => Account.columns_hash["id"])
+ condition.value = "true"
+ assert_equal "\"accounts\".\"id\" IS NULL", condition.sanitize
+
+ condition = Searchlogic::Condition::Nil.new(Account, :column => Account.columns_hash["id"])
+ condition.value = "false"
+ assert_equal "\"accounts\".\"id\" IS NOT NULL", condition.sanitize
+
+ condition = Searchlogic::Condition::Nil.new(Account, :column => Account.columns_hash["id"])
+ condition.value = nil
+ assert_nil condition.sanitize
+
+ condition = Searchlogic::Condition::Nil.new(Account, :column => Account.columns_hash["id"])
+ condition.value = ""
+ assert_nil condition.sanitize
+ end
+ end
+end
View
8 test/condition_tests/not_begin_with_test.rb
@@ -0,0 +1,8 @@
+require File.dirname(__FILE__) + '/../test_helper.rb'
+
+module ConditionTests
+ class NotBeginWithTest < ActiveSupport::TestCase
+ def test_sanitize
+ end
+ end
+end
View
8 test/condition_tests/not_blank_test.rb
@@ -0,0 +1,8 @@
+require File.dirname(__FILE__) + '/../test_helper.rb'
+
+module ConditionTests
+ class NotBlankTest < ActiveSupport::TestCase
+ def test_sanitize
+ end
+ end
+end
View
8 test/condition_tests/not_end_with_test.rb
@@ -0,0 +1,8 @@
+require File.dirname(__FILE__) + '/../test_helper.rb'
+
+module ConditionTests
+ class NotEndWithTest < ActiveSupport::TestCase
+ def test_sanitize
+ end
+ end
+end
View
19 test/condition_tests/not_equal_test.rb
@@ -0,0 +1,19 @@
+require File.dirname(__FILE__) + '/../test_helper.rb'
+
+module ConditionTests
+ class NotEqualTest < ActiveSupport::TestCase
+ def test_sanitize
+ condition = Searchlogic::Condition::NotEqual.new(Account, :column => Account.columns_hash["id"])
+ condition.value = 12
+ assert_equal ["\"accounts\".\"id\" != ?", 12], condition.sanitize
+
+ condition = Searchlogic::Condition::NotEqual.new(Account, :column => Account.columns_hash["id"])
+ condition.value = [1,2,3,4]
+ assert_equal ["\"accounts\".\"id\" NOT IN (?)", [1, 2, 3, 4]], condition.sanitize
+
+ condition = Searchlogic::Condition::NotEqual.new(Account, :column => Account.columns_hash["id"])
+ condition.value = (1..10)
+ assert_equal ["\"accounts\".\"id\" NOT BETWEEN ? AND ?", 1, 10], condition.sanitize
+ end
+ end
+end
View
8 test/condition_tests/not_have_keywords_test.rb
@@ -0,0 +1,8 @@
+require File.dirname(__FILE__) + '/../test_helper.rb'
+
+module ConditionTests
+ class NotHaveKeywordsTest < ActiveSupport::TestCase
+ def test_sanitize
+ end
+ end
+end
View
8 test/condition_tests/not_like_test.rb
@@ -0,0 +1,8 @@
+require File.dirname(__FILE__) + '/../test_helper.rb'
+
+module ConditionTests
+ class NotLikeTest < ActiveSupport::TestCase
+ def test_sanitize
+ end
+ end
+end
View
13 test/condition_tests/not_nil_test.rb
@@ -0,0 +1,13 @@
+require File.dirname(__FILE__) + '/../test_helper.rb'
+
+module ConditionTests
+ class NotNilTest < ActiveSupport::TestCase
+ def test_sanitize
+ condition = Searchlogic::Condition::NotNil.new(Account, :column => Account.columns_hash["created_at"])
+ condition.value = "1"
+ assert_equal "\"accounts\".\"created_at\" IS NOT NULL", condition.sanitize
+ condition.value = "false"
+ assert_equal "\"accounts\".\"created_at\" IS NULL", condition.sanitize
+ end
+ end
+end
View
15 test/condition_tests/sibling_of_test.rb
@@ -0,0 +1,15 @@
+require File.dirname(__FILE__) + '/../test_helper.rb'
+
+module ConditionTests
+ class SiblingOfTest < ActiveSupport::TestCase
+ def test_sanitize
+ ben = users(:ben)
+ drew = users(:drew)
+ jennifer = users(:jennifer)
+
+ condition = Searchlogic::Condition::SiblingOf.new(User)
+ condition.value = drew
+ assert_equal ["(\"users\".\"id\" != ?) AND (\"users\".\"parent_id\" = ?)", drew.id, ben.id], condition.sanitize
+ end
+ end
+end
View
221 test/conditions_tests/base_test.rb
@@ -0,0 +1,221 @@
+require File.dirname(__FILE__) + '/../test_helper.rb'
+
+module ConditionsTests
+ class BaseTest < ActiveSupport::TestCase
+ def test_register_conditions
+ Searchlogic::Conditions::Base.register_condition(Searchlogic::Condition::Keywords)
+ assert [Searchlogic::Condition::Keywords], Searchlogic::Conditions::Base.conditions
+
+ Searchlogic::Conditions::Base.register_condition(Searchlogic::Condition::Like)
+ assert [Searchlogic::Condition::Keywords, Searchlogic::Condition::Like], Searchlogic::Conditions::Base.conditions
+ end
+
+ def test_association_names
+ ["dogs", "children", "user_groups", "orders", "account", "parent", "cats"].each do |name|
+ assert Searchlogic::Cache::UserConditions.association_names.include? name
+ end
+ ["admin", "orders", "users"].each do |name|
+ Searchlogic::Cache::AccountConditions.association_names.include? name
+ end
+ end
+
+ def test_needed
+ assert (not Searchlogic::Conditions::Base.needed?(User, {}))
+ assert (not Searchlogic::Conditions::Base.needed?(User, {:first_name => "Ben"}))
+ assert Searchlogic::Conditions::Base.needed?(User, {:first_name_contains => "Awesome"})
+ assert (not Searchlogic::Conditions::Base.needed?(User, {"orders.id" => 2}))
+ end
+
+ def test_initialize
+ conditions = Searchlogic::Cache::AccountConditions.new(:name_contains => "Binary")
+ assert_equal conditions.klass, Account
+ assert_equal conditions.name_contains, "Binary"
+ end
+
+ def test_any
+ conditions = Searchlogic::Cache::AccountConditions.new
+ assert (not conditions.any?)
+ conditions = Searchlogic::Cache::AccountConditions.new(:any => true)
+ assert conditions.any?
+ conditions.any = "false"
+ assert (not conditions.any?)
+ conditions = Searchlogic::Cache::AccountConditions.new
+ conditions.name_contains = "Binary"
+ assert_equal_sql ["\"accounts\".\"name\" LIKE ?", "%Binary%"], conditions.sanitize
+ conditions.id = 1
+ assert_equal_sql ["(\"accounts\".\"id\" = ?) AND (\"accounts\".\"name\" LIKE ?)", 1, "%Binary%"], conditions.sanitize
+ conditions.any = true
+ assert_equal_sql ["(\"accounts\".\"id\" = ?) OR (\"accounts\".\"name\" LIKE ?)", 1, "%Binary%"], conditions.sanitize
+ conditions.any = false
+ assert_equal_sql ["(\"accounts\".\"id\" = ?) AND (\"accounts\".\"name\" LIKE ?)", 1, "%Binary%"], conditions.sanitize
+ end
+
+ def test_auto_joins
+ conditions = Searchlogic::Cache::AccountConditions.new
+ assert_equal conditions.auto_joins, nil
+
+ conditions.name_like = "Binary"
+ assert_equal conditions.auto_joins, nil
+
+ conditions.users.first_name_like = "Ben"
+ assert_equal conditions.auto_joins, :users
+
+ conditions.users.orders.description_like = "apple"
+ assert_equal conditions.auto_joins, {:users => :orders}
+ end
+
+ def test_inspect
+ conditions = Searchlogic::Cache::AccountConditions.new
+ assert_nothing_raised { conditions.inspect }
+ end
+
+ def test_sanitize
+ conditions = Searchlogic::Cache::AccountConditions.new
+ conditions.name_contains = "Binary"
+ conditions.id_gt = 5
+ now = Time.now
+ conditions.created_after = now
+ assert_equal_sql ["(\"accounts\".\"created_at\" > ?) AND (\"accounts\".\"id\" > ?) AND (\"accounts\".\"name\" LIKE ?)", now, 5, "%Binary%"], conditions.sanitize
+
+ # test out associations
+ conditions.users.first_name_like = "Ben"
+ conditions.users.id_gt = 10
+ conditions.users.orders.total_lt = 500
+ assert_equal_sql ["(\"accounts\".\"created_at\" > ?) AND (\"accounts\".\"id\" > ?) AND (\"accounts\".\"name\" LIKE ?) AND ((\"users\".\"first_name\" LIKE ?) AND (\"users\".\"id\" > ?) AND (\"orders\".\"total\" < ?))", now, 5, "%Binary%", "%Ben%", 10, 500], conditions.sanitize
+
+ # test that raw sql is returned
+ conditions.conditions = "awesome"
+ assert_equal "awesome", conditions.sanitize
+ end
+
+ def test_conditions
+ conditions = Searchlogic::Cache::AccountConditions.new
+ now = Time.now
+ v = {:name_like => "Binary", :created_at_greater_than => now}
+ conditions.conditions = v
+ assert_equal v, conditions.conditions
+
+ sql = "id in (1,2,3,4)"
+ conditions.conditions = sql
+ assert_equal sql, conditions.conditions
+ assert_equal({}, conditions.send(:objects))
+
+ v2 = {:id_less_than => 5, :name_begins_with => "Beginning of string"}
+ conditions.conditions = v2
+ assert_equal v2, conditions.conditions
+
+ v = {:name_like => "Binary", :created_at_greater_than => now}
+ conditions.conditions = v
+ assert_equal v2.merge(v), conditions.conditions
+
+ sql2 = "id > 5 and name = 'Test'"
+ conditions.conditions = sql2
+ assert_equal sql2, conditions.conditions
+ assert_equal({}, conditions.send(:objects))
+
+ conditions.name_contains = "awesome"
+ assert_equal({:name_like => "awesome"}, conditions.conditions)
+
+ now = Time.now
+ conditions.conditions = {:id_gt => "", :id => "", :name => ["", "", ""], :created_at => ["", now], :name_starts_with => "Ben"}
+ assert_equal({:name_like => "awesome", :name_begins_with => "Ben", :created_at_equals => now}, conditions.conditions)
+ end
+
+ # Other general usage tests, need to clean these up
+
+ def test_setting_conditions
+ [Account, User, Order].each do |klass|
+ conditions = "Searchlogic::Cache::#{klass}Conditions".constantize.new
+ conditions.class.condition_names.each do |condition_name|
+ conditions.send("#{condition_name}=", 1)
+ assert_equal 1, conditions.send(condition_name)
+ end
+ end
+ end
+
+ def test_accessible_protected_conditions
+ Account.conditions_accessible << :name_contains
+ conditions = Searchlogic::Cache::AccountConditions.new
+ conditions.conditions = {:created_after => Time.now, :name_contains => "Binary"}
+ assert({:name_contains => "Binary"}, conditions.conditions)
+ Account.send(:write_inheritable_attribute, :conditions_accessible, nil)
+
+ Account.conditions_protected << :name_contains
+ conditions = Searchlogic::Cache::AccountConditions.new
+ now = Time.now
+ conditions.conditions = {:created_after => now, :name_contains => "Binary"}
+ assert({:created_after => now}, conditions.conditions)
+ Account.send(:write_inheritable_attribute, :conditions_protected, nil)
+ end
+
+ def test_assert_valid_values
+ conditions = Searchlogic::Cache::UserConditions.new
+ assert_raise(NoMethodError) { conditions.conditions = {:unknown => "blah"} }
+ assert_nothing_raised { conditions.conditions = {:first_name => "blah"} }
+ assert_nothing_raised { conditions.conditions = {:first_name_contains => "blah"} }
+ end
+
+ def test_setting_associations
+ conditions = Searchlogic::Cache::AccountConditions.new(:users => {:first_name_like => "Ben"})
+ assert_equal conditions.users.first_name_like, "Ben"
+
+ conditions.users.last_name_begins_with = "Ben"
+ assert_equal conditions.users.last_name_begins_with, "Ben"
+ end
+
+ def test_virtual_columns
+ conditions = Searchlogic::Cache::AccountConditions.new
+ conditions.hour_of_created_gt = 2
+ assert_equal_sql ["strftime('%H', \"accounts\".\"created_at\") > ?", 2], conditions.sanitize
+ conditions.dow_of_created_at_most = 5
+ assert_equal_sql ["(strftime('%w', \"accounts\".\"created_at\") <= ?) AND (strftime('%H', \"accounts\".\"created_at\") > ?)", 5, 2], conditions.sanitize
+ conditions.month_of_created_at_nil = true
+ assert_equal_sql ["(strftime('%w', \"accounts\".\"created_at\") <= ?) AND (strftime('%H', \"accounts\".\"created_at\") > ?) AND (strftime('%m', \"accounts\".\"created_at\") IS NULL)", 5, 2], conditions.sanitize
+ conditions.min_of_hour_of_month_of_created_at_nil = true
+ assert_equal_sql ["(strftime('%w', \"accounts\".\"created_at\") <= ?) AND (strftime('%H', \"accounts\".\"created_at\") > ?) AND (strftime('%m', strftime('%H', strftime('%M', \"accounts\".\"created_at\"))) IS NULL) AND (strftime('%m', \"accounts\".\"created_at\") IS NULL)", 5, 2], conditions.sanitize
+ end
+
+ def test_objects
+ conditions = Searchlogic::Cache::AccountConditions.new
+ assert_equal({}, conditions.send(:objects))
+
+ conditions.name_contains = "Binary"
+ assert_equal 1, conditions.send(:objects).size
+
+ conditions.users.first_name_contains = "Ben"
+ assert_equal 2, conditions.send(:objects).size
+ end
+
+ def test_reset
+ conditions = Searchlogic::Cache::AccountConditions.new
+
+ conditions.name_contains = "Binary"
+ assert_equal 1, conditions.send(:objects).size
+
+ conditions.reset_name_like!
+ conditions.reset_name_contains! # should set up aliases for reset
+ assert_equal({}, conditions.send(:objects))
+
+ conditions.users.first_name_like = "Ben"
+ assert_equal 1, conditions.send(:objects).size
+
+ conditions.reset_users!
+ assert_equal({}, conditions.send(:objects))
+
+ conditions.name_begins_with ="Binary"
+ conditions.users.orders.total_gt = 200
+ assert_equal 2, conditions.send(:objects).size
+
+ conditions.reset_name_begins_with!
+ assert_equal 1, conditions.send(:objects).size
+
+ conditions.reset_users!
+ assert_equal({}, conditions.send(:objects))
+ end
+
+ def test_method_conflicts
+ conditions = Searchlogic::Cache::AccountConditions.new
+ assert_nil conditions.id
+ end
+ end
+end
View
18 test/conditions_tests/protection_test.rb
@@ -0,0 +1,18 @@
+require File.dirname(__FILE__) + '/../test_helper.rb'
+
+module ConditionsTests
+ class ProtectionTest < ActiveSupport::TestCase
+ def test_protection
+ assert_raise(ArgumentError) { Account.new_search(:conditions => "(DELETE FROM users)") }
+ assert_nothing_raised { Account.new_search!(:conditions => "(DELETE FROM users)") }
+
+ account = Account.first
+
+ assert_raise(ArgumentError) { account.users.new_search(:conditions => "(DELETE FROM users)") }
+ assert_nothing_raised { account.users.new_search!(:conditions => "(DELETE FROM users)") }
+
+ search = Account.new_search
+ assert_raise(ArgumentError) { search.conditions = "(DELETE FROM users)" }
+ end
+ end
+end
View
2 test/test_config.rb → test/config_test.rb
@@ -1,6 +1,6 @@
require File.dirname(__FILE__) + '/test_helper.rb'
-class TestConfig < Test::Unit::TestCase
+class ConfigTest < ActiveSupport::TestCase
def test_per_page
Searchlogic::Config.search.per_page = 1
View
3 test/fixtures/accounts.yml
@@ -1,15 +1,12 @@
binary_logic:
- id: 1
name: "Binary Logic"
active: true
neco:
- id: 2
name: "NECO"
active: false
binary_fun:
- id: 3
name: "Binary Fun"
active: true
View
7 test/fixtures/animals.yml
@@ -0,0 +1,7 @@
+pepper:
+ type: Cat
+ description: pepper meows
+
+harry:
+ type: Doc
+ description: harry is hairy
View
3 test/fixtures/cats.yml
@@ -1,3 +0,0 @@
-pepper:
- id: 1
- description: pepper meows
View
3 test/fixtures/dogs.yml
@@ -1,3 +0,0 @@
-harry:
- id: 2
- description: harry is hairy
View
6 test/fixtures/orders.yml
@@ -1,13 +1,11 @@
bens_order:
- id: 1
- user_id: 1
+ user: ben
total: 500.23
description: A bunch of apple products, etc.
receipt: some binary text
drews_order:
- id: 2
- user_id: 2
+ user: drew
total: 2.12
description: Some more apple projects, ipod, etc
receipt: some more binary text
View
12 test/fixtures/user_groups.yml
@@ -1,13 +1,7 @@
neco:
- id: 1
name: NECO
- user_ids:
- - 1
- - 2
-
+ users: ben, drew
+
johnsons:
- id: 2
name: Johnsons
- user_ids:
- - 1
- - 3
+ users: ben, jennifer
View
18 test/fixtures/users.yml
@@ -1,34 +1,28 @@
ben:
- id: 1
- account_id: 1
- parent_id: null
+ account: binary_logic
first_name: Ben
last_name: Johnson
active: true
bio: Totally awesome!
drew:
- id: 2
- account_id: 2
- parent_id: 1
+ account: neco
+ parent: ben
first_name: Drew
last_name: Mills
active: false
bio: Totally not awesome!
jennifer:
- id: 3
- account_id: 1
- parent_id: 2
+ account: binary_logic
+ parent: drew
first_name: Jennifer
last_name: Hopkins
active: false
bio: Totally not awesome at all!
tren:
- id: 4
- account_id:
- parent_id:
+ parent: ben
first_name: Tren
last_name: Garfield
active: false
View
237 test/search_tests/base_test.rb
@@ -0,0 +1,237 @@
+require File.dirname(__FILE__) + '/../test_helper.rb'
+
+module SearchTests
+ class BaseTest < ActiveSupport::TestCase
+ def test_needed
+ assert Searchlogic::Search::Base.needed?(Account, :page => 2, :conditions => {:name => "Ben"})
+ assert !Searchlogic::Search::Base.needed?(Account, :conditions => {:name => "Ben"})
+ assert Searchlogic::Search::Base.needed?(Account, :limit => 2, :conditions => {:name_contains => "Ben"})
+ assert !Searchlogic::Search::Base.needed?(Account, :limit => 2)
+ assert Searchlogic::Search::Base.needed?(Account, :per_page => 2)
+ end
+
+ def test_initialize
+ assert_nothing_raised { Account.new_search }
+ search = Account.new_search!(:conditions => {:name_like => "binary"}, :page => 2, :limit => 10, :readonly => true)
+ assert_equal Account, search.klass
+ assert_equal "binary", search.conditions.name_like
+ assert_equal 2, search.page
+ assert_equal 10, search.limit
+ assert_equal true, search.readonly
+ end
+
+ def test_acting_as_filter
+ search = Account.new_search
+ search.acting_as_filter = true
+ assert search.acting_as_filter?
+ search.acting_as_filter = false
+ assert !search.acting_as_filter?
+ end
+
+ def test_setting_first_level_options
+ search = Account.new_search!(:include => :users, :joins => :users, :offset => 5, :limit => 20, :order => "name ASC", :select => "name", :readonly => true, :group => "name", :from => "accounts", :lock => true)
+ assert_equal :users, search.include
+ assert_equal :users, search.joins
+ assert_equal 5, search.offset
+ assert_equal 20, search.limit
+ assert_equal "name ASC", search.order
+ assert_equal "name", search.select
+ assert_equal true, search.readonly
+ assert_equal "name", search.group
+ assert_equal "accounts", search.from
+ assert_equal true, search.lock
+
+ search = Account.new_search(:per_page => nil)
+
+ search.include = :users
+ assert_equal :users, search.include
+
+ # treat it like SQL, just like AR
+ search.joins = "users"
+ assert_equal "users", search.joins
+
+ search.page = 5
+ assert_equal 1, search.page
+ assert_nil search.offset
+
+ search.limit = 20
+ assert_equal search.limit, 20
+ assert_equal search.per_page, 20
+ assert_equal search.page, 5
+ assert_equal search.offset, 80
+ search.limit = nil
+ assert_nil search.limit
+ assert_nil search.per_page
+ assert_equal 1, search.page
+ assert_nil search.offset
+
+ search.offset = 50
+ assert_equal 50, search.offset
+ assert_equal 1, search.page
+ search.limit = 50
+ assert_equal 2, search.page
+ search.offset = nil
+ assert_nil search.offset
+ assert_equal 1, search.page
+
+ search.per_page = 2
+ assert_equal 2, search.per_page
+ assert_equal 2, search.limit
+ search.offset = 50
+ assert_equal 26, search.page
+ assert_equal 50, search.offset
+
+ search.order = "name ASC"
+ assert_equal search.order, "name ASC"
+
+ search.select = "name"
+ assert_equal search.select, "name"
+
+ search.readonly = true
+ assert_equal search.readonly, true
+
+ search.group = "name"
+ assert_equal search.group, "name"
+
+ search.from = "accounts"
+ assert_equal search.from, "accounts"
+
+ search.lock = true
+ assert_equal search.lock, true
+ end
+
+ def test_joins
+ search = Account.new_search
+ assert_nil search.joins
+ search.conditions.name_contains = "Binary"
+ assert_nil search.joins
+ search.conditions.users.first_name_contains = "Ben"
+ assert_equal(:users, search.joins)
+ search.conditions.users.orders.id_gt = 2
+ assert_equal({:users => :orders}, search.joins)
+ search.conditions.users.reset_orders!
+ assert_equal(:users, search.joins)
+ search.conditions.users.orders.id_gt = 2
+ search.conditions.reset_users!
+ assert_nil search.joins
+ end
+
+ def test_limit
+ search = Account.new_search
+ search.limit = 10
+ assert_equal 10, search.limit
+ search.page = 2
+ assert_equal 10, search.offset
+ search.limit = 25
+ assert_equal 25, search.offset
+ assert_equal 2, search.page
+ search.page = 5
+ assert_equal 5, search.page
+ assert_equal 25, search.limit
+ search.limit = 3
+ assert_equal 12, search.offset
+ end
+
+ def test_options
+ end
+
+ def test_sanitize
+ search = Account.new_search
+ search.per_page = 2
+ search.conditions.name_like = "Binary"
+ search.conditions.users.id_greater_than = 2
+ search.page = 3
+ search.readonly = true
+ assert_equal_find_options({:joins => :users, :offset => 4, :select => "DISTINCT \"accounts\".*", :readonly => true, :conditions => ["(\"accounts\".\"name\" LIKE ?) AND (\"users\".\"id\" > ?)", "%Binary%", 2], :limit => 2 }, search.sanitize)
+ end
+
+ def test_scope
+ search = Account.new_search!
+ search.conditions = "some sql"
+ conditions = search.conditions
+ assert_equal "some sql", search.conditions.conditions
+ search.conditions = nil
+ assert_nil search.conditions.conditions
+ search.conditions = "some sql"
+ assert_equal "some sql", search.conditions.conditions
+ search.conditions = "some sql"
+ assert_equal "some sql", search.conditions.conditions
+ end
+
+ def test_searching
+ binary_logic = accounts(:binary_logic)
+ neco = accounts(:neco)
+ binary_fun = accounts(:binary_fun)
+
+ search = Account.new_search
+ search.conditions.name_like = "Binary"
+ assert_equal [binary_logic, binary_fun], search.all
+ assert_equal [binary_logic, binary_fun], search.find(:all)
+ assert_equal binary_logic, search.first
+ assert_equal binary_logic, search.find(:first)
+
+ search.per_page = 20
+ search.page = 2
+
+ assert_equal [], search.all
+ assert_equal [], search.find(:all)
+ assert_nil search.first
+ assert_nil search.find(:first)
+
+ search.per_page = 0
+ search.page = nil
+ search.conditions.users.first_name_contains = "Ben"
+ search.conditions.users.orders.description_keywords = "products, &*ap#ple $%^&*"
+ assert_equal [binary_logic], search.all
+ assert_equal [binary_logic], search.find(:all)
+ assert_equal binary_logic, search.first
+ assert_equal binary_logic, search.find(:first)
+
+ search = Account.new_search
+ search.select = "id, name"
+ assert_equal Account.all, search.all
+
+ search = Account.scope1.new_search!(:conditions => {:users => {:first_name_starts_with => "Ben"}})
+ assert_equal [binary_logic], search.all
+ search2 = search.dup
+ assert_equal [binary_logic], search2.all
+ end
+
+ def test_calculations
+ binary_logic = accounts(:binary_logic)
+ binary_fun = accounts(:binary_fun)
+ bens_order = orders(:bens_order)
+
+ search = Account.new_search
+ search.conditions.name_like = "Binary"
+ assert_equal (binary_logic.id + binary_fun.id) / 2.0, search.average('id')
+ assert_equal (binary_logic.id + binary_fun.id) / 2.0, search.calculate(:avg, 'id')
+ assert_equal binary_fun.id, search.calculate(:max, 'id')
+ assert_equal 2, search.count
+ assert_equal binary_fun.id, search.maximum('id')
+ assert_equal binary_logic.id, search.minimum('id')
+ assert_equal binary_logic.id + binary_fun.id, search.sum('id')
+
+ search.readonly = true
+ assert_equal binary_logic.id + binary_fun.id, search.sum('id')
+
+ search = Account.new_search(:conditions => {:users => {:orders => {:id_gt => bens_order.id}}})
+ assert_equal 1, search.count
+
+ search = Order.new_search(:conditions => {:user => {:account => {:id_gt => binary_logic.id}}})
+ assert_equal 1, search.count
+
+ search = UserGroup.new_search(:conditions => {:users => {:orders => {:id_gt => bens_order.id}}})
+ assert_equal 1, search.count
+ end
+
+ def test_inspect
+ search = Account.new_search
+ assert_nothing_raised { search.inspect }
+ end
+
+ def test_sti
+
+ end
+ end
+end
View
21 test/search_tests/conditions_test.rb
@@ -0,0 +1,21 @@
+require File.dirname(__FILE__) + '/../test_helper.rb'
+
+module SearchTests
+ class TestSearchConditions < ActiveSupport::TestCase
+ def test_conditions
+ search = Account.new_search
+ assert_kind_of Searchlogic::Conditions::Base, search.conditions
+ assert_equal search.conditions.klass, Account
+
+ search.conditions = {:name_like => "Binary"}
+ assert_kind_of Searchlogic::Conditions::Base, search.conditions
+
+ search = Account.new_search(:conditions => {:name_like => "Ben"})
+ assert_equal({:name_like => "Ben"}, search.conditions.conditions)
+ end
+
+ def test_sanitize
+ # This is tested in test_search_base
+ end
+ end
+end
View
167 test/search_tests/ordering_test.rb
@@ -0,0 +1,167 @@
+require File.dirname(__FILE__) + '/../test_helper.rb'
+
+module SearchTests
+ class OrderingTest < ActiveSupport::TestCase
+ def test_order_to_order_by
+ search = Account.new_search
+ search.order = "name"
+ assert_equal "name", search.order_by
+ search.order = "users.first_name"
+ assert_equal({"users" => "first_name"}, search.order_by)
+ search.order = "\"users\".\"first_name\""
+ assert_equal({"users" => "first_name"}, search.order_by)
+ search.order = "\"users\".\"first_name\", name ASC"
+ assert_equal([{"users" => "first_name"}, "name"], search.order_by)
+ end
+
+ def test_order_by
+ search = Account.new_search
+ assert_nil search.order
+ assert_nil search.order_by
+
+ search.order_by = "first_name"
+ assert_equal "first_name", search.order_by
+ assert_equal "\"accounts\".\"first_name\"", search.order
+
+ search.order_by = "last_name"
+ assert_equal "last_name", search.order_by
+ assert_equal "\"accounts\".\"last_name\"", search.order
+
+ search.order_by = ["first_name", "last_name"]
+ assert_equal ["first_name", "last_name"], search.order_by
+ assert_equal "\"accounts\".\"first_name\", \"accounts\".\"last_name\"", search.order
+
+ search.order = "created_at DESC"
+ assert_equal "created_at", search.order_by
+ assert_equal "created_at DESC", search.order
+
+ search.order = "\"users\".updated_at ASC"
+ assert_equal({"users" => "updated_at"}, search.order_by)
+ assert_equal "\"users\".updated_at ASC", search.order
+
+ search.order = "`users`.first_name DESC"
+ assert_equal({"users" => "first_name"}, search.order_by)
+ assert_equal "`users`.first_name DESC", search.order
+
+ search.order = "`accounts`.name DESC"
+ assert_equal "name", search.order_by
+ assert_equal "`accounts`.name DESC", search.order
+
+ search.order = "accounts.name DESC"
+ assert_equal "name", search.order_by
+ assert_equal "accounts.name DESC", search.order
+
+ search.order = "`users`.first_name DESC, name DESC, `accounts`.id DESC"
+ assert_equal [{"users" => "first_name"}, "name", "id"], search.order_by
+ assert_equal "`users`.first_name DESC, name DESC, `accounts`.id DESC", search.order
+
+ search.order = "`users`.first_name DESC, `line_items`.id DESC, `accounts`.id DESC"
+ assert_equal [{"users" => "first_name"}, "id"], search.order_by
+ assert_equal "`users`.first_name DESC, `line_items`.id DESC, `accounts`.id DESC", search.order
+
+ search.order = "`line_items`.id DESC"
+ assert_nil search.order_by
+ assert_equal "`line_items`.id DESC", search.order
+ end
+
+ def test_order_as
+ search = Account.new_search
+ assert_nil search.order
+ assert_nil search.order_as
+ assert search.asc?
+ assert !search.desc?
+
+ search.order_as = "DESC"
+ assert_nil search.order_as
+ assert !search.desc?
+ assert_nil search.order
+
+ search.order_by = "name"
+ assert_equal "\"accounts\".\"name\" DESC", search.order
+
+ search.order_as = "ASC"
+ assert_equal "\"accounts\".\"name\" ASC", search.order
+ assert search.asc?
+
+ search.order = "id ASC"
+ assert_equal "ASC", search.order_as
+ assert search.asc?
+ assert_equal "id ASC", search.order
+
+ search.order = "id DESC"
+ assert_equal "DESC", search.order_as
+ assert search.desc?
+ assert_equal "id DESC", search.order
+
+ search.order_by = "name"
+ assert_equal "DESC", search.order_as
+ assert search.desc?
+ assert_equal "\"accounts\".\"name\" DESC", search.order
+
+ assert_raise(ArgumentError) { search.order_as = "awesome" }
+ end
+
+ def test_order_by_auto_joins
+ search = Account.new_search
+ assert_nil search.order_by_auto_joins
+ search.order_by = :name
+ assert_nil search.order_by_auto_joins
+ search.order_by = {:users => :first_name}
+ assert_equal :users, search.order_by_auto_joins
+ search.order_by = [{:users => :first_name}, {:orders => :total}, {:users => {:user_groups => :name}}]
+ assert_equal [:users, :orders, {:users => :user_groups}], search.order_by_auto_joins
+ search.priority_order_by = {:users => :first_name}
+ assert_equal [:users, :orders, {:users => :user_groups}], search.order_by_auto_joins
+ search.priority_order_by = {:users => {:orders => :total}}
+ assert_equal({:users => :orders}, search.priority_order_by_auto_joins)
+ end
+
+ def test_priority_order_by
+ search = Account.new_search
+ assert_nil search.priority_order
+ assert_nil search.priority_order_by
+ assert_nil search.priority_order_as
+
+ search.priority_order_by = :name
+ assert_equal "\"accounts\".\"name\"", search.priority_order
+ assert_equal "\"accounts\".\"name\"", search.sanitize[:order]
+ assert_nil search.order
+ assert_equal :name, search.priority_order_by
+ assert_nil search.priority_order_as
+
+ search.order_by = :id
+ assert_equal "\"accounts\".\"name\", \"accounts\".\"id\"", search.sanitize[:order]
+ search.order_as = "DESC"
+ assert_equal "\"accounts\".\"name\", \"accounts\".\"id\" DESC", search.sanitize[:order]
+ end
+
+ def test_priority_order_as
+ search = Account.new_search
+ assert_nil search.priority_order_as
+ assert_nil search.order_as
+ search.priority_order_as = "ASC"
+ assert_nil search.priority_order_as
+ assert_nil search.order_as
+ search.priority_order_by = :name
+ assert_equal "ASC", search.priority_order_as
+ assert_nil search.order_as
+ search.priority_order_as = "DESC"
+ assert_equal "DESC", search.priority_order_as
+ assert_nil search.order_as
+ assert_raise(ArgumentError) { search.priority_order_as = "awesome" }
+ search.priority_order = nil
+ assert_nil search.priority_order_as
+ assert_nil search.order_as
+ end
+
+ def test_sanitize
+ # tested in test_priority_order_by
+ end
+
+ def test_ordering_includes_blank
+ search = User.new_search!
+ search.order_by = {:account => :name}
+ assert_equal 4, search.count
+ end
+ end
+end
View
74 test/search_tests/pagination_test.rb
@@ -0,0 +1,74 @@
+require File.dirname(__FILE__) + '/../test_helper.rb'
+
+module SearchTests
+ class PaginationTest < ActiveSupport::TestCase
+ def test_limit
+ search = Account.new_search
+ search.limit = 10
+ assert_equal 10, search.limit
+ search.page = 2
+ assert_equal 10, search.offset
+ search.limit = 25
+ assert_equal 25, search.offset
+ assert_equal 2, search.page
+ search.page = 5
+ assert_equal 5, search.page
+ assert_equal 25, search.limit
+ search.limit = 3
+ assert_equal 12, search.offset
+ end
+
+ def test_page
+ search = Account.new_search(:per_page => nil)
+ search.page = 2
+ assert_equal 1, search.page
+ search.per_page = 20
+ assert_equal 2, search.page
+ search.limit = 0
+ assert_equal 1, search.page
+ search.per_page = 20
+ assert_equal 2, search.page
+ search.limit = nil
+ assert_equal 1, search.page
+ end
+
+ def test_per_page
+ search = Account.new_search
+ search.per_page = 10
+ assert_equal 10, search.per_page
+ search.per_page = ""
+ assert_nil search.per_page
+ search.per_page = 40
+ assert_equal 40, search.per_page
+ search.per_page = nil
+ assert_nil search.per_page
+ search.per_page = 60
+ assert_equal 60, search.per_page
+ search.per_page = false
+ assert_nil search.per_page
+ end
+
+ def test_next_page
+
+ end
+
+ def test_prev_page
+
+ end
+
+ def test_page_count
+ search = Account.new_search
+ assert_equal 1, search.page_count
+ search.per_page = 1
+ assert_equal 3, search.page_count
+ search.per_page = 100
+ assert_equal 1, search.page_count
+
+ Searchlogic::Config.search.per_page = 1
+ search = Account.new_search
+ assert_equal 3, search.page_count
+ search.conditions.users.first_name_contains
+ assert_equal 3, search.page_count
+ end
+ end
+end
View
26 test/search_tests/protection_test.rb
@@ -0,0 +1,26 @@
+require File.dirname(__FILE__) + '/../test_helper.rb'
+
+module SearchTests
+ class ProtectionTest < ActiveSupport::TestCase
+ def test_protection
+ assert_raise(ArgumentError) { Account.build_search(:conditions => "(DELETE FROM users)", :page => 2, :per_page => 15) }
+ Searchlogic::Search::Base::VULNERABLE_FIND_OPTIONS.each { |option| assert_raise(ArgumentError) { Account.build_search(option => "(DELETE FROM users)") } }
+
+ assert_nothing_raised { Account.build_search!(:conditions => "(DELETE FROM users)", :page => 2, :per_page => 15) }
+ Searchlogic::Search::Base::VULNERABLE_FIND_OPTIONS.each { |option| assert_nothing_raised { Account.build_search!(option => "(DELETE FROM users)") } }
+
+ account = Account.first
+
+ assert_raise(ArgumentError) { account.users.build_search(:conditions => "(DELETE FROM users)", :page => 2, :per_page => 15) }
+ Searchlogic::Search::Base::VULNERABLE_FIND_OPTIONS.each { |option| assert_raise(ArgumentError) { account.users.build_search(option => "(DELETE FROM users)") } }
+
+ assert_nothing_raised { account.users.build_search!(:conditions => "(DELETE FROM users)", :page => 2, :per_page => 15) }
+ Searchlogic::Search::Base::VULNERABLE_FIND_OPTIONS.each { |option| assert_nothing_raised { account.users.build_search!(option => "(DELETE FROM users)") } }
+
+ assert_raise(ArgumentError) { Account.build_search(:order_by => "unknown_column") }
+ assert_nothing_raised { Account.build_search!(:order_by => "unknown_column") }
+ assert_raise(ArgumentError) { Account.build_search(:order_by => ["name", "unknown_column"]) }
+ assert_nothing_raised { Account.build_search!(:order_by => ["name", "unknown_column"]) }
+ end
+ end
+end
View
81 test/test_active_record_associations.rb
@@ -1,81 +0,0 @@
-require File.dirname(__FILE__) + '/test_helper.rb'
-
-class TestActiveRecordAssociations < Test::Unit::TestCase
- def test_has_many
- search = Account.find(1).users.new_search
- assert_kind_of Searchlogic::Search::Base, search
- assert_equal User, search.klass
- assert_equal({:conditions => "\"users\".account_id = 1"}, search.scope)
-
- assert_equal User.find(1, 3), search.all
- assert_equal User.find(1), search.first
- assert_equal 2, search.average("id")
- assert_equal 2, search.count
-
- search.conditions.first_name_contains = "Ben"
-
- assert_equal [User.find(1)], search.all
- assert_equal User.find(1), search.first
- assert_equal 1, search.average("id")
- assert_equal 1, search.count
-
- assert_equal 2, Account.find(1).users.count
- assert_equal 1, Account.find(1).users.all(:conditions => {:first_name_contains => "Ben"}).size
- assert_equal 0, Account.find(1).users.all(:conditions => {:first_name_contains => "No one"}).size
- assert_equal 1, Account.find(1).users.sum("id", :conditions => {:first_name_contains => "Ben"})
- assert_equal 0, Account.find(1).users.sum("id", :conditions => {:first_name_contains => "No one"})
- assert_equal 1, Account.find(1).users.average("id", :conditions => {:first_name_contains => "Ben"})
- end
-
- def test_has_many_through
- search = Account.find(1).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 = 1)"}, search.scope)
-
- assert_equal [Order.find(1)], search.all
- assert_equal Order.find(1), search.first
- assert_equal 1, search.average("id")
- assert_equal 1, search.count
-
- search.conditions.total_gt = 100
-
- assert_equal [Order.find(1)], search.all
- assert_equal Order.find(1), search.first
- assert_equal 1, search.average("id")
- assert_equal 1, search.count
-
- assert_equal 1, Account.find(1).orders.count
- assert_equal 1, Account.find(1).orders.all(:conditions => {:total_gt => 100}).size
- assert_equal 0, Account.find(1).orders.all(:conditions => {:total_gt => 1000}).size
- assert_equal 1, Account.find(1).orders.sum("id", :conditions => {:total_gt => 100})
- assert_equal 0, Account.find(1).orders.sum("id", :conditions => {:total_gt => 1000})
- assert_equal 1, Account.find(1).orders.average("id", :conditions => {:total_gt => 100})
- end
-
- def test_habtm
- search = UserGroup.find(1).users.new_search
- assert_kind_of Searchlogic::Search::Base, search
- assert_equal User, search.klass
- assert_equal({:conditions => "\"user_groups_users\".user_group_id = 1 ", :joins => "INNER JOIN \"user_groups_users\" ON \"users\".id = \"user_groups_users\".user_id"}, search.scope)
-
- assert_equal User.find(1, 2), search.all
- assert_equal User.find(1), search.first
- assert_equal (1.5).to_s, search.average("id").to_s
- assert_equal 2, search.count
-
- search.conditions.first_name_contains = "Ben"
-
- assert_equal [User.find(1)], search.all
- assert_equal User.find(1), search.first
- assert_equal 1, search.average("id")
- assert_equal 1, search.count
-
- assert_equal 2, UserGroup.find(1).users.count
- assert_equal 1, UserGroup.find(1).users.all(:conditions => {:first_name_contains => "Ben"}).size
- assert_equal 0, UserGroup.find(1).users.all(:conditions => {:first_name_contains => "No one"}).size
- assert_equal 1, UserGroup.find(1).users.sum("id", :conditions => {:first_name_contains => "Ben"})
- assert_equal 0, UserGroup.find(1).users.sum("id", :conditions => {:first_name_contains => "No one"})
- assert_equal 1, UserGroup.find(1).users.average("id", :conditions => {:first_name_contains => "Ben"})
- end
-end
View
93 test/test_active_record_base.rb
@@ -1,93 +0,0 @@
-require File.dirname(__FILE__) + '/test_helper.rb'
-
-class TestActiveRecordBase < Test::Unit::TestCase
- def test_standard_find
- assert_equal [1,2,3], Account.all.map(&:id)
- assert_equal 1, Account.first.id
- assert_equal [1,2,3], Account.find(:all).map(&:id)
- assert_equal [1], Account.find(:all, :conditions => {:name => "Binary Logic"}).map(&:id)
- assert_equal [1], Account.find(:all, :conditions => ["name = ?", "Binary Logic"]).map(&:id)
- assert_equal [1], Account.find(:all, :conditions => "name = 'Binary Logic'").map(&:id)
- assert_equal 1, Account.find(:first).id
- assert_equal [1,2,3], Account.find(:all, nil).map(&:id)
- assert_equal [1,2,3], Account.find(:all, {}).map(&:id)
- assert_equal [1,2,3], Account.find(:all, :select => "id, name").map(&:id)
- end
-
- def test_standard_calculations
- 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 6, Account.sum("id")
- assert_equal 6, Account.sum("id", {})
- assert_equal 2, Account.average("id")
- assert_equal 3, Account.maximum("id")
- assert_equal 1, 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
- assert_equal Account.find(1, 3), Account.all(:conditions => {:name_contains => "Binary"})
- assert_equal [Account.find(1)], 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 Account.find(1, 2), 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 Account.find(1, 3), Account.all(:conditions => {:name_contains => "Binary"}, :page => 2)
- assert_equal [], Account.all(:conditions => {:name_contains => "Binary"}, :page => 2, :per_page => 20)
-
- assert_equal [Account.find(1)], 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
View
52 test/test_condition_base.rb
@@ -1,52 +0,0 @@
-require File.dirname(__FILE__) + '/test_helper.rb'
-
-class TestConditionBase < Test::Unit::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_equal nil, Searchlogic::Condition::Equals.value_type
- assert_equal nil, Searchlogic::Condition::Keywords.value_type
- assert_equal :boolean, Searchlogic::Condition::Nil.value_type
- assert_equal :boolean, Searchlogic::Condition::Blank.value_type
- assert_equal 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
View
143 test/test_condition_types.rb
@@ -1,143 +0,0 @@
-require File.dirname(__FILE__) + '/test_helper.rb'
-
-class TestConditionTypes < Test::Unit::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
-
- 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_equal nil, condition.sanitize
-
- condition = Searchlogic::Condition::Blank.new(Account, :column => Account.columns_hash["id"])
- condition.value = ""
- assert_equal nil, condition.sanitize
-
- condition = Searchlogic::Condition::ChildOf.new(User)
- condition.value = User.first.id
- assert_equal ["\"users\".\"parent_id\" = ?", User.first.id], condition.sanitize
-
- condition = Searchlogic::Condition::ChildOf.new(User)
- condition.value = User.first
- assert_equal ["\"users\".\"parent_id\" = ?", User.first.id], condition.sanitize
-
- condition = Searchlogic::Condition::DescendantOf.new(User)
- condition.value = User.find(1)
- assert_equal ["\"users\".\"id\" = ? OR \"users\".\"id\" = ?", 2, 3], condition.sanitize
-
- condition = Searchlogic::Condition::EndsWith.new(Account, :column => Account.columns_hash["name"])
- condition.value = "Binary"
- assert_equal ["\"accounts\".\"name\" LIKE ?", "%Binary"], condition.sanitize
-
- condition = Searchlogic::Condition::Equals.new(Account, :column => Account.columns_hash["id"])
- condition.value = 12
- assert_equal ["\"accounts\".\"id\" = ?", 12], condition.sanitize
-
- condition = Searchlogic::Condition::Equals.new(Account, :column => Account.columns_hash["id"])
- condition.value = [1,2,3,4]
- assert_equal ["\"accounts\".\"id\" IN (?)", [1, 2, 3, 4]], condition.sanitize
-
- condition = Searchlogic::Condition::Equals.new(Account, :column => Account.columns_hash["id"])
- condition.value = (1..10)
- assert_equal ["\"accounts\".\"id\" BETWEEN ? AND ?", 1, 10], condition.sanitize
-
- condition = Searchlogic::Condition::GreaterThan.new(Account, :column => Account.columns_hash["id"])
- condition.value = 2
- assert_equal ["\"accounts\".\"id\" > ?", 2], condition.sanitize
-
- condition = Searchlogic::Condition::GreaterThanOrEqualTo.new(Account, :column => Account.columns_hash["id"])
- condition.value = 2
- assert_equal ["\"accounts\".\"id\" >= ?", 2], condition.sanitize
-
- condition = Searchlogic::Condition::InclusiveDescendantOf.new(User)
- condition.value = User.find(1)
- assert_equal ["(\"users\".\"id\" = ?) OR (\"users\".\"id\" = ? OR \"users\".\"id\" = ?)", 1, 2, 3], condition.sanitize
-
- condition = Searchlogic::Condition::Like.new(Account, :column => Account.columns_hash["name"])
- condition.value = "Binary and blah"
- assert_equal ["\"accounts\".\"name\" LIKE ?", "%Binary and blah%"], condition.sanitize
-
- condition = Searchlogic::Condition::Nil.new(Account, :column => Account.columns_hash["id"])
- condition.value = true
- assert_equal "\"accounts\".\"id\" IS NULL", condition.sanitize
-
- condition = Searchlogic::Condition::Nil.new(Account, :column => Account.columns_hash["id"])
- condition.value = false
- assert_equal "\"accounts\".\"id\" IS NOT NULL", condition.sanitize
-
- condition = Searchlogic::Condition::Nil.new(Account, :column => Account.columns_hash["id"])
- condition.value = "true"
- assert_equal "\"accounts\".\"id\" IS NULL", condition.sanitize
-
- condition = Searchlogic::Condition::Nil.new(Account, :column => Account.columns_hash["id"])
- condition.value = "false"
- assert_equal "\"accounts\".\"id\" IS NOT NULL", condition.sanitize
-
- condition = Searchlogic::Condition::Nil.new(Account, :column => Account.columns_hash["id"])
- condition.value = nil
- assert_equal nil, condition.sanitize
-
- condition = Searchlogic::Condition::Nil.new(Account, :column => Account.columns_hash["id"])
- condition.value = ""
- assert_equal nil, condition.sanitize
-
- condition = Searchlogic::Condition::NotEqual.new(Account, :column => Account.columns_hash["id"])
- condition.value = 12
- assert_equal ["\"accounts\".\"id\" != ?", 12], condition.sanitize
-
- condition = Searchlogic::Condition::NotEqual.new(Account, :column => Account.columns_hash["id"])
- condition.value = [1,2,3,4]
- assert_equal ["\"accounts\".\"id\" NOT IN (?)", [1, 2, 3, 4]], condition.sanitize
-
- condition = Searchlogic::Condition::NotEqual.new(Account, :column => Account.columns_hash["id"])
- condition.value = (1..10)
- assert_equal ["\"accounts\".\"id\" NOT BETWEEN ? AND ?", 1, 10], condition.sanitize
-
- condition = Searchlogic::Condition::NotNil.new(Account, :column => Account.columns_hash["created_at"])
- condition.value = "1"
- assert_equal "\"accounts\".\"created_at\" IS NOT NULL", condition.sanitize
- condition.value = "false"
- assert_equal "\"accounts\".\"created_at\" IS NULL", condition.sanitize
-
- condition = Searchlogic::Condition::Keywords.new(Account, :column => Account.columns_hash["name"])
- condition.value = "freedom yeah, freedom YEAH right"
- assert_equal ["\"accounts\".\"name\" LIKE ? AND \"accounts\".\"name\" LIKE ? AND \"accounts\".\"name\" LIKE ?", "%freedom%", "%yeah%", "%right%"], condition.sanitize
-
- condition = Searchlogic::Condition::Keywords.new(Account, :column => Account.columns_hash["name"])
- condition.value = "%^$*(^$)"
- assert_equal nil, condition.sanitize
-
- condition = Searchlogic::Condition::Keywords.new(Account, :column => Account.columns_hash["name"])
- condition.value = "%^$*(^$) àáâãäåßéèêëìíîïñòóôõöùúûüýÿ"
- assert_equal ["\"accounts\".\"name\" LIKE ?", "%àáâãäåßéèêëìíîïñòóôõöùúûüýÿ%"], condition.sanitize
-
- condition = Searchlogic::Condition::LessThan.new(Account, :column => Account.columns_hash["id"])
- condition.value = 2
- assert_equal ["\"accounts\".\"id\" < ?", 2], condition.sanitize
-
- condition = Searchlogic::Condition::LessThanOrEqualTo.new(Account, :column => Account.columns_hash["id"])
- condition.value = 2
- assert_equal ["\"accounts\".\"id\" <= ?", 2], condition.sanitize
-
- condition = Searchlogic::Condition::SiblingOf.new(User)
- condition.value = User.find(2)
- assert_equal ["(\"users\".\"id\" != ?) AND (\"users\".\"parent_id\" = ?)", 2, 1], condition.sanitize
- end
-end
View
219 test/test_conditions_base.rb
@@ -1,219 +0,0 @@
-require File.dirname(__FILE__) + '/test_helper.rb'
-
-class TestConditionsBase < Test::Unit::TestCase
- def test_register_conditions
- Searchlogic::Conditions::Base.register_condition(Searchlogic::Condition::Keywords)
- assert [Searchlogic::Condition::Keywords], Searchlogic::Conditions::Base.conditions
-
- Searchlogic::Conditions::Base.register_condition(Searchlogic::Condition::Like)
- assert [Searchlogic::Condition::Keywords, Searchlogic::Condition::Like], Searchlogic::Conditions::Base.conditions
- end
-
- def test_association_names
- ["dogs", "children", "user_groups", "orders", "account", "parent", "cats"].each do |name|
- assert Searchlogic::Cache::UserConditions.association_names.include? name
- end
- ["admin", "orders", "users"].each do |name|
- Searchlogic::Cache::AccountConditions.association_names.include? name
- end
- end
-
- def test_needed
- assert (not Searchlogic::Conditions::Base.needed?(User, {}))
- assert (not Searchlogic::Conditions::Base.needed?(User, {:first_name => "Ben"}))
- assert Searchlogic::Conditions::Base.needed?(User, {:first_name_contains => "Awesome"})
- assert (not Searchlogic::Conditions::Base.needed?(User, {"orders.id" => 2}))
- end
-
- def test_initialize