Skip to content

Commit

Permalink
Reorganized tests
Browse files Browse the repository at this point in the history
  • Loading branch information
binarylogic committed Nov 10, 2008
1 parent d3625fc commit 2c22dfa
Show file tree
Hide file tree
Showing 54 changed files with 1,437 additions and 1,231 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rdoc
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
8 changes: 4 additions & 4 deletions lib/searchlogic/conditions/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
95 changes: 95 additions & 0 deletions test/active_record_tests/associations_test.rb
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
108 changes: 108 additions & 0 deletions test/active_record_tests/base_test.rb
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
54 changes: 54 additions & 0 deletions test/condition_tests/base_test.rb
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
11 changes: 11 additions & 0 deletions test/condition_tests/begins_with_test.rb
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
31 changes: 31 additions & 0 deletions test/condition_tests/blank_test.rb
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
17 changes: 17 additions & 0 deletions test/condition_tests/child_of_test.rb
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
16 changes: 16 additions & 0 deletions test/condition_tests/descendant_of_test.rb
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
11 changes: 11 additions & 0 deletions test/condition_tests/ends_with_test.rb
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
Loading

0 comments on commit 2c22dfa

Please sign in to comment.