searchlogic 1.6.6 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (178) hide show
  1. data/.gitignore +6 -0
  2. data/CHANGELOG.rdoc +17 -0
  3. data/{MIT-LICENSE → LICENSE} +2 -2
  4. data/README.rdoc +128 -379
  5. data/Rakefile +56 -20
  6. data/VERSION.yml +4 -0
  7. data/init.rb +1 -1
  8. data/lib/searchlogic.rb +18 -98
  9. data/lib/searchlogic/core_ext/object.rb +33 -13
  10. data/lib/searchlogic/core_ext/proc.rb +11 -0
  11. data/lib/searchlogic/named_scopes/alias_scope.rb +63 -0
  12. data/lib/searchlogic/named_scopes/associations.rb +126 -0
  13. data/lib/searchlogic/named_scopes/conditions.rb +215 -0
  14. data/lib/searchlogic/named_scopes/ordering.rb +53 -0
  15. data/lib/searchlogic/rails_helpers.rb +69 -0
  16. data/lib/searchlogic/search.rb +146 -0
  17. data/rails/init.rb +1 -0
  18. data/searchlogic.gemspec +69 -0
  19. data/spec/core_ext/object_spec.rb +7 -0
  20. data/spec/core_ext/proc_spec.rb +9 -0
  21. data/spec/named_scopes/alias_scope_spec.rb +15 -0
  22. data/spec/named_scopes/associations_spec.rb +120 -0
  23. data/spec/named_scopes/conditions_spec.rb +253 -0
  24. data/spec/named_scopes/ordering_spec.rb +23 -0
  25. data/spec/search_spec.rb +283 -0
  26. data/spec/spec_helper.rb +78 -0
  27. metadata +40 -231
  28. data/Manifest.txt +0 -158
  29. data/TODO.rdoc +0 -4
  30. data/lib/searchlogic/active_record/associations.rb +0 -52
  31. data/lib/searchlogic/active_record/base.rb +0 -224
  32. data/lib/searchlogic/active_record/connection_adapters/mysql_adapter.rb +0 -176
  33. data/lib/searchlogic/active_record/connection_adapters/postgresql_adapter.rb +0 -172
  34. data/lib/searchlogic/active_record/connection_adapters/sqlite_adapter.rb +0 -80
  35. data/lib/searchlogic/condition/base.rb +0 -165
  36. data/lib/searchlogic/condition/begins_with.rb +0 -17
  37. data/lib/searchlogic/condition/blank.rb +0 -24
  38. data/lib/searchlogic/condition/child_of.rb +0 -11
  39. data/lib/searchlogic/condition/descendant_of.rb +0 -11
  40. data/lib/searchlogic/condition/ends_with.rb +0 -17
  41. data/lib/searchlogic/condition/equals.rb +0 -33
  42. data/lib/searchlogic/condition/greater_than.rb +0 -15
  43. data/lib/searchlogic/condition/greater_than_or_equal_to.rb +0 -15
  44. data/lib/searchlogic/condition/inclusive_descendant_of.rb +0 -10
  45. data/lib/searchlogic/condition/keywords.rb +0 -52
  46. data/lib/searchlogic/condition/less_than.rb +0 -15
  47. data/lib/searchlogic/condition/less_than_or_equal_to.rb +0 -15
  48. data/lib/searchlogic/condition/like.rb +0 -15
  49. data/lib/searchlogic/condition/nested_set.rb +0 -17
  50. data/lib/searchlogic/condition/nil.rb +0 -21
  51. data/lib/searchlogic/condition/not_begin_with.rb +0 -20
  52. data/lib/searchlogic/condition/not_blank.rb +0 -19
  53. data/lib/searchlogic/condition/not_end_with.rb +0 -20
  54. data/lib/searchlogic/condition/not_equal.rb +0 -27
  55. data/lib/searchlogic/condition/not_have_keywords.rb +0 -20
  56. data/lib/searchlogic/condition/not_like.rb +0 -20
  57. data/lib/searchlogic/condition/not_nil.rb +0 -19
  58. data/lib/searchlogic/condition/sibling_of.rb +0 -14
  59. data/lib/searchlogic/conditions/any_or_all.rb +0 -42
  60. data/lib/searchlogic/conditions/base.rb +0 -244
  61. data/lib/searchlogic/conditions/groups.rb +0 -74
  62. data/lib/searchlogic/conditions/magic_methods.rb +0 -286
  63. data/lib/searchlogic/conditions/multiparameter_attributes.rb +0 -105
  64. data/lib/searchlogic/conditions/protection.rb +0 -36
  65. data/lib/searchlogic/config.rb +0 -31
  66. data/lib/searchlogic/config/helpers.rb +0 -338
  67. data/lib/searchlogic/config/search.rb +0 -53
  68. data/lib/searchlogic/core_ext/hash.rb +0 -75
  69. data/lib/searchlogic/helpers/control_types/link.rb +0 -310
  70. data/lib/searchlogic/helpers/control_types/links.rb +0 -242
  71. data/lib/searchlogic/helpers/control_types/remote_link.rb +0 -87
  72. data/lib/searchlogic/helpers/control_types/remote_links.rb +0 -72
  73. data/lib/searchlogic/helpers/control_types/remote_select.rb +0 -36
  74. data/lib/searchlogic/helpers/control_types/select.rb +0 -82
  75. data/lib/searchlogic/helpers/form.rb +0 -208
  76. data/lib/searchlogic/helpers/utilities.rb +0 -197
  77. data/lib/searchlogic/modifiers/absolute.rb +0 -15
  78. data/lib/searchlogic/modifiers/acos.rb +0 -11
  79. data/lib/searchlogic/modifiers/asin.rb +0 -11
  80. data/lib/searchlogic/modifiers/atan.rb +0 -11
  81. data/lib/searchlogic/modifiers/avg.rb +0 -15
  82. data/lib/searchlogic/modifiers/base.rb +0 -27
  83. data/lib/searchlogic/modifiers/ceil.rb +0 -15
  84. data/lib/searchlogic/modifiers/char_length.rb +0 -15
  85. data/lib/searchlogic/modifiers/cos.rb +0 -15
  86. data/lib/searchlogic/modifiers/cot.rb +0 -15
  87. data/lib/searchlogic/modifiers/count.rb +0 -11
  88. data/lib/searchlogic/modifiers/day_of_month.rb +0 -15
  89. data/lib/searchlogic/modifiers/day_of_week.rb +0 -15
  90. data/lib/searchlogic/modifiers/day_of_year.rb +0 -15
  91. data/lib/searchlogic/modifiers/degrees.rb +0 -11
  92. data/lib/searchlogic/modifiers/exp.rb +0 -15
  93. data/lib/searchlogic/modifiers/floor.rb +0 -15
  94. data/lib/searchlogic/modifiers/hex.rb +0 -11
  95. data/lib/searchlogic/modifiers/hour.rb +0 -11
  96. data/lib/searchlogic/modifiers/log.rb +0 -15
  97. data/lib/searchlogic/modifiers/log10.rb +0 -11
  98. data/lib/searchlogic/modifiers/log2.rb +0 -11
  99. data/lib/searchlogic/modifiers/lower.rb +0 -15
  100. data/lib/searchlogic/modifiers/ltrim.rb +0 -15
  101. data/lib/searchlogic/modifiers/md5.rb +0 -11
  102. data/lib/searchlogic/modifiers/microseconds.rb +0 -11
  103. data/lib/searchlogic/modifiers/milliseconds.rb +0 -11
  104. data/lib/searchlogic/modifiers/minute.rb +0 -15
  105. data/lib/searchlogic/modifiers/month.rb +0 -15
  106. data/lib/searchlogic/modifiers/octal.rb +0 -15
  107. data/lib/searchlogic/modifiers/radians.rb +0 -11
  108. data/lib/searchlogic/modifiers/round.rb +0 -11
  109. data/lib/searchlogic/modifiers/rtrim.rb +0 -15
  110. data/lib/searchlogic/modifiers/second.rb +0 -15
  111. data/lib/searchlogic/modifiers/sign.rb +0 -11
  112. data/lib/searchlogic/modifiers/sin.rb +0 -11
  113. data/lib/searchlogic/modifiers/square_root.rb +0 -15
  114. data/lib/searchlogic/modifiers/sum.rb +0 -11
  115. data/lib/searchlogic/modifiers/tan.rb +0 -15
  116. data/lib/searchlogic/modifiers/trim.rb +0 -15
  117. data/lib/searchlogic/modifiers/upper.rb +0 -15
  118. data/lib/searchlogic/modifiers/week.rb +0 -11
  119. data/lib/searchlogic/modifiers/year.rb +0 -11
  120. data/lib/searchlogic/search/base.rb +0 -148
  121. data/lib/searchlogic/search/conditions.rb +0 -53
  122. data/lib/searchlogic/search/ordering.rb +0 -244
  123. data/lib/searchlogic/search/pagination.rb +0 -121
  124. data/lib/searchlogic/search/protection.rb +0 -89
  125. data/lib/searchlogic/search/searching.rb +0 -32
  126. data/lib/searchlogic/shared/utilities.rb +0 -57
  127. data/lib/searchlogic/shared/virtual_classes.rb +0 -39
  128. data/lib/searchlogic/version.rb +0 -79
  129. data/test/active_record_tests/associations_test.rb +0 -94
  130. data/test/active_record_tests/base_test.rb +0 -115
  131. data/test/condition_tests/base_test.rb +0 -62
  132. data/test/condition_tests/begins_with_test.rb +0 -11
  133. data/test/condition_tests/blank_test.rb +0 -31
  134. data/test/condition_tests/child_of_test.rb +0 -17
  135. data/test/condition_tests/descendant_of_test.rb +0 -12
  136. data/test/condition_tests/ends_with_test.rb +0 -11
  137. data/test/condition_tests/equals_test.rb +0 -28
  138. data/test/condition_tests/greater_than_or_equal_to_test.rb +0 -11
  139. data/test/condition_tests/greater_than_test.rb +0 -11
  140. data/test/condition_tests/inclusive_descendant_of_test.rb +0 -12
  141. data/test/condition_tests/keywords_test.rb +0 -23
  142. data/test/condition_tests/less_than_or_equal_to_test.rb +0 -11
  143. data/test/condition_tests/less_than_test.rb +0 -11
  144. data/test/condition_tests/like_test.rb +0 -11
  145. data/test/condition_tests/nil_test.rb +0 -31
  146. data/test/condition_tests/not_begin_with_test.rb +0 -8
  147. data/test/condition_tests/not_blank_test.rb +0 -8
  148. data/test/condition_tests/not_end_with_test.rb +0 -8
  149. data/test/condition_tests/not_equal_test.rb +0 -19
  150. data/test/condition_tests/not_have_keywords_test.rb +0 -8
  151. data/test/condition_tests/not_like_test.rb +0 -8
  152. data/test/condition_tests/not_nil_test.rb +0 -13
  153. data/test/condition_tests/sibling_of_test.rb +0 -15
  154. data/test/conditions_tests/any_or_all_test.rb +0 -23
  155. data/test/conditions_tests/base_test.rb +0 -185
  156. data/test/conditions_tests/groups_test.rb +0 -68
  157. data/test/conditions_tests/magic_methods_test.rb +0 -36
  158. data/test/conditions_tests/multiparameter_attributes_test.rb +0 -15
  159. data/test/conditions_tests/protection_test.rb +0 -18
  160. data/test/config_test.rb +0 -23
  161. data/test/fixtures/accounts.yml +0 -12
  162. data/test/fixtures/animals.yml +0 -7
  163. data/test/fixtures/orders.yml +0 -12
  164. data/test/fixtures/user_groups.yml +0 -5
  165. data/test/fixtures/users.yml +0 -45
  166. data/test/libs/awesome_nested_set.rb +0 -545
  167. data/test/libs/awesome_nested_set/.autotest +0 -13
  168. data/test/libs/awesome_nested_set/compatability.rb +0 -29
  169. data/test/libs/awesome_nested_set/helper.rb +0 -40
  170. data/test/libs/awesome_nested_set/named_scope.rb +0 -140
  171. data/test/libs/rexml_fix.rb +0 -14
  172. data/test/modifier_tests/day_of_month_test.rb +0 -16
  173. data/test/search_tests/base_test.rb +0 -241
  174. data/test/search_tests/conditions_test.rb +0 -21
  175. data/test/search_tests/ordering_test.rb +0 -167
  176. data/test/search_tests/pagination_test.rb +0 -74
  177. data/test/search_tests/protection_test.rb +0 -26
  178. data/test/test_helper.rb +0 -122
@@ -1,32 +0,0 @@
1
- module Searchlogic
2
- module Search
3
- # = Searchlogic Searching
4
- #
5
- # Implements searching functionality for searchlogic. Searchlogic::Search::Base and Searchlogic::Conditions::Base can both search and include
6
- # this module.
7
- module Searching
8
- # Use these methods just like you would in ActiveRecord
9
- SEARCH_METHODS = [:all, :find, :first]
10
- CALCULATION_METHODS = [:average, :calculate, :count, :maximum, :minimum, :sum]
11
-
12
- (SEARCH_METHODS + CALCULATION_METHODS).each do |method|
13
- class_eval <<-"end_eval", __FILE__, __LINE__
14
- def #{method}(*args)
15
- find_options = {}
16
- options = args.extract_options! # can't pass options, your options are in the search
17
- klass.send(:with_scope, :find => acting_as_filter? ? {} : scope) do
18
- options = sanitize(#{SEARCH_METHODS.include?(method)})
19
- if #{CALCULATION_METHODS.include?(method)}
20
- options[:distinct] = true if #{method == :count} && !joins.blank? && Config.search.remove_duplicates?
21
- args[0] = klass.column_names.first if [nil, :all].include?(args[0])
22
- end
23
- args << options
24
- results = klass.#{method}(*args)
25
- results
26
- end
27
- end
28
- end_eval
29
- end
30
- end
31
- end
32
- end
@@ -1,57 +0,0 @@
1
- module Searchlogic
2
- module Shared
3
- module Utilities # :nodoc:
4
- private
5
- def merge_conditions(*conditions)
6
- options = conditions.extract_options!
7
- conditions.delete_if { |condition| condition.blank? }
8
- return if conditions.blank?
9
- return conditions.first if conditions.size == 1
10
-
11
- conditions_strs = []
12
- conditions_subs = []
13
-
14
- conditions.each do |condition|
15
- next if condition.blank?
16
- arr_condition = condition.is_a?(Array) ? condition : [condition]
17
- conditions_strs << arr_condition.first
18
- conditions_subs += arr_condition[1..-1]
19
- end
20
-
21
- return if conditions_strs.blank?
22
-
23
- join = options[:any] ? " OR " : " AND "
24
- conditions_str = conditions_strs.join(join)
25
-
26
- return conditions_str if conditions_subs.blank?
27
-
28
- [conditions_str, *conditions_subs]
29
- end
30
-
31
- def scope_condition(condition)
32
- return condition if condition.blank?
33
- arr_condition = condition.is_a?(Array) ? condition : [condition]
34
- arr_condition[0] = "(#{arr_condition[0]})"
35
- arr_condition.size == 1 ? arr_condition.first : arr_condition
36
- end
37
-
38
- def merge_joins(*joins)
39
- joins.delete_if { |join| join.blank? }
40
- return if joins.blank?
41
- return joins.first if joins.size == 1
42
-
43
- new_joins = []
44
- joins.each do |join|
45
- case join
46
- when Array
47
- new_joins += join
48
- else
49
- new_joins << join
50
- end
51
- end
52
-
53
- new_joins.compact.uniq
54
- end
55
- end
56
- end
57
- end
@@ -1,39 +0,0 @@
1
- module Searchlogic
2
- module Shared
3
- # = Searchlogic Virtual Classes
4
- #
5
- # Creates virtual classes for each model, to implementing a type of caching. So that object instantiation for searchlogic searches is cached. This is lazy, meaning
6
- # it will only cache when it needs. So the first instantion will be much slow than the following ones. This is cached in the RAM, so if the process is restarted the caching is cleared.
7
- module VirtualClasses
8
- def self.included(klass)
9
- klass.extend ClassMethods
10
- end
11
-
12
- module ClassMethods
13
- # Creates virtual classes for the class passed to it. This is a neccesity for keeping dynamically created method
14
- # names specific to models. It provides caching and helps a lot with performance.
15
- def create_virtual_class(model_class)
16
- class_search_name = "::Searchlogic::Cache::#{model_class.name.gsub("::", "")}" + name.split(/::/)[1]
17
-
18
- begin
19
- eval(class_search_name)
20
- rescue NameError
21
- eval <<-end_eval
22
- class #{class_search_name} < ::#{name}
23
- def self.klass
24
- #{model_class.name}
25
- end
26
-
27
- def klass
28
- #{model_class.name}
29
- end
30
- end
31
-
32
- #{class_search_name}
33
- end_eval
34
- end
35
- end
36
- end
37
- end
38
- end
39
- end
@@ -1,79 +0,0 @@
1
- # (The MIT License)
2
- #
3
- # Copyright (c) 2008 Jamis Buck <jamis@37signals.com>,
4
- # with modifications by Bruce Williams <bruce@fiveruns.com>
5
- #
6
- # Permission is hereby granted, free of charge, to any person obtaining
7
- # a copy of this software and associated documentation files (the
8
- # 'Software'), to deal in the Software without restriction, including
9
- # without limitation the rights to use, copy, modify, merge, publish,
10
- # distribute, sublicense, and/or sell copies of the Software, and to
11
- # permit persons to whom the Software is furnished to do so, subject to
12
- # the following conditions:
13
- #
14
- # The above copyright notice and this permission notice shall be
15
- # included in all copies or substantial portions of the Software.
16
- #
17
- # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
18
- # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
- # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20
- # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21
- # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22
- # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23
- # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
- module Searchlogic
25
- # = Version
26
- #
27
- # A class for describing the current version of a library. The version
28
- # consists of three parts: the +major+ number, the +minor+ number, and the
29
- # +tiny+ (or +patch+) number.
30
- class Version
31
-
32
- include Comparable
33
-
34
- # A convenience method for instantiating a new Version instance with the
35
- # given +major+, +minor+, and +tiny+ components.
36
- def self.[](major, minor, tiny)
37
- new(major, minor, tiny)
38
- end
39
-
40
- attr_reader :major, :minor, :tiny
41
-
42
- # Create a new Version object with the given components.
43
- def initialize(major, minor, tiny)
44
- @major, @minor, @tiny = major, minor, tiny
45
- end
46
-
47
- # Compare this version to the given +version+ object.
48
- def <=>(version)
49
- to_i <=> version.to_i
50
- end
51
-
52
- # Converts this version object to a string, where each of the three
53
- # version components are joined by the '.' character. E.g., 2.0.0.
54
- def to_s
55
- @to_s ||= [@major, @minor, @tiny].join(".")
56
- end
57
-
58
- # Converts this version to a canonical integer that may be compared
59
- # against other version objects.
60
- def to_i
61
- @to_i ||= @major * 1_000_000 + @minor * 1_000 + @tiny
62
- end
63
-
64
- def to_a
65
- [@major, @minor, @tiny]
66
- end
67
-
68
- MAJOR = 1
69
- MINOR = 6
70
- TINY = 6
71
-
72
- # The current version as a Version instance
73
- CURRENT = new(MAJOR, MINOR, TINY)
74
- # The current version as a String
75
- STRING = CURRENT.to_s
76
-
77
- end
78
-
79
- end
@@ -1,94 +0,0 @@
1
- require File.dirname(__FILE__) + '/../test_helper.rb'
2
-
3
- module ActiveRecordTests
4
- class AssociationsTest < ActiveSupport::TestCase
5
- def test_has_many
6
- binary_logic = accounts(:binary_logic)
7
- ben = users(:ben)
8
- jennifer = users(:jennifer)
9
-
10
- search = binary_logic.users.new_search
11
- assert_kind_of Searchlogic::Search::Base, search
12
- assert_equal User, search.klass
13
- assert_equal({:conditions => "\"users\".account_id = #{binary_logic.id}"}, search.scope)
14
-
15
- assert_equal [ben, jennifer], search.all
16
- assert_equal ben, search.first
17
- assert_equal ((ben.id + jennifer.id) / 2.0), search.average("id")
18
- assert_equal 2, search.count
19
-
20
- search.conditions.first_name_contains = "Ben"
21
-
22
- assert_equal [ben], search.all
23
- assert_equal ben, search.first
24
- assert_equal ben.id, search.average("id")
25
- assert_equal 1, search.count
26
-
27
- assert_equal 2, binary_logic.users.count
28
- assert_equal 1, binary_logic.users.all(:conditions => {:first_name_contains => "Ben"}).size
29
- assert_equal 0, binary_logic.users.all(:conditions => {:first_name_contains => "No one"}).size
30
- assert_equal ben.id, binary_logic.users.sum("id", :conditions => {:first_name_contains => "Ben"})
31
- assert_equal 0, binary_logic.users.sum("id", :conditions => {:first_name_contains => "No one"})
32
- assert_equal ben.id, binary_logic.users.average("id", :conditions => {:first_name_contains => "Ben"})
33
- end
34
-
35
- def test_has_many_through
36
- binary_logic = accounts(:binary_logic)
37
-
38
- search = binary_logic.orders.new_search
39
- assert_kind_of Searchlogic::Search::Base, search
40
- assert_equal Order, search.klass
41
- assert_equal({:conditions => "(\"users\".account_id = 431064614)", :joins => "INNER JOIN \"users\" ON \"orders\".user_id = \"users\".id "}, search.scope)
42
-
43
- bens_order = orders(:bens_order)
44
- assert_equal [bens_order], search.all
45
- assert_equal bens_order, search.first
46
- assert_equal bens_order.id, search.average("id")
47
- assert_equal 1, search.count
48
-
49
- search.conditions.total_gt = 100
50
-
51
- assert_equal [bens_order], search.all
52
- assert_equal bens_order, search.first
53
- assert_equal bens_order.id, search.average("id")
54
- assert_equal 1, search.count
55
-
56
- assert_equal 1, binary_logic.orders.count
57
- assert_equal 1, binary_logic.orders.all(:conditions => {:total_gt => 100}).size
58
- assert_equal 0, binary_logic.orders.all(:conditions => {:total_gt => 1000}).size
59
- assert_equal bens_order.id, binary_logic.orders.sum("id", :conditions => {:total_gt => 100})
60
- assert_equal 0, binary_logic.orders.sum("id", :conditions => {:total_gt => 1000})
61
- assert_equal bens_order.id, binary_logic.orders.average("id", :conditions => {:total_gt => 100})
62
- end
63
-
64
- def test_habtm
65
- neco = user_groups(:neco)
66
- ben = users(:ben)
67
- drew = users(:drew)
68
-
69
- search = neco.users.new_search
70
- assert_kind_of Searchlogic::Search::Base, search
71
- assert_equal User, search.klass
72
- 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)
73
- assert_equal [drew, ben], search.all
74
-
75
- assert_equal drew, search.first
76
- assert_equal ((ben.id + drew.id) / 2.0).to_s, search.average("id").to_s
77
- assert_equal 2, search.count
78
-
79
- search.conditions.first_name_contains = "Ben"
80
-
81
- assert_equal [ben], search.all
82
- assert_equal ben, search.first
83
- assert_equal ben.id, search.average("id")
84
- assert_equal 1, search.count
85
-
86
- assert_equal 2, neco.users.count
87
- assert_equal 1, neco.users.all(:conditions => {:first_name_contains => "Ben"}).size
88
- assert_equal 0, neco.users.all(:conditions => {:first_name_contains => "No one"}).size
89
- assert_equal ben.id, neco.users.sum("id", :conditions => {:first_name_contains => "Ben"})
90
- assert_equal 0, neco.users.sum("id", :conditions => {:first_name_contains => "No one"})
91
- assert_equal ben.id, neco.users.average("id", :conditions => {:first_name_contains => "Ben"})
92
- end
93
- end
94
- end
@@ -1,115 +0,0 @@
1
- require File.dirname(__FILE__) + '/../test_helper.rb'
2
-
3
- module ActiveRecordTests
4
- class BaseTest < ActiveSupport::TestCase
5
- def test_standard_find
6
- binary_logic = accounts(:binary_logic)
7
- neco = accounts(:neco)
8
- binary_fun = accounts(:binary_fun)
9
-
10
- assert_equal [binary_logic, binary_fun, neco], Account.all
11
- assert_equal binary_logic, Account.first
12
-
13
- assert_equal [binary_logic, binary_fun, neco], Account.find(:all)
14
- assert_equal [binary_logic], Account.find(:all, :conditions => {:name => "Binary Logic"})
15
- assert_equal [binary_logic], Account.find(:all, :conditions => ["name = ?", "Binary Logic"])
16
- assert_equal [binary_logic], Account.find(:all, :conditions => "name = 'Binary Logic'")
17
- assert_equal binary_logic, Account.find(:first)
18
- assert_equal [binary_logic, binary_fun, neco], Account.find(:all, nil)
19
- assert_equal [binary_logic, binary_fun, neco], Account.find(:all, {})
20
- assert_equal [binary_logic, binary_fun, neco], Account.find(:all, :select => "id, name")
21
- end
22
-
23
- def test_standard_calculations
24
- binary_logic = accounts(:binary_logic)
25
- neco = accounts(:neco)
26
- binary_fun = accounts(:binary_fun)
27
-
28
- assert_equal 3, Account.count({})
29
- assert_equal 3, Account.count(nil)
30
- assert_equal 3, Account.count(:limit => 1)
31
- assert_equal 0, Account.count(:limit => 10, :offset => 10)
32
- assert_equal binary_logic.id + neco.id + binary_fun.id, Account.sum("id")
33
- assert_equal binary_logic.id + neco.id + binary_fun.id, Account.sum("id", {})
34
- assert_equal (binary_logic.id + neco.id + binary_fun.id) / 3.0, Account.average("id")
35
- assert_equal neco.id, Account.maximum("id")
36
- assert_equal binary_logic.id, Account.minimum("id")
37
- end
38
-
39
- def test_valid_ar_options
40
- assert_equal [:conditions, :include, :joins, :limit, :offset, :order, :select, :readonly, :group, :having, :from, :lock], ActiveRecord::Base.valid_find_options
41
- assert_equal [:conditions, :joins, :order, :select, :group, :having, :distinct, :limit, :offset, :include, :from], ActiveRecord::Base.valid_calculations_options
42
- end
43
-
44
- def test_build_search
45
- search = Account.new_search(:conditions => {:name_keywords => "awesome"}, :page => 2, :per_page => 15)
46
- assert_kind_of Searchlogic::Search::Base, search
47
- assert_equal({}, search.scope)
48
- assert_equal Account, search.klass
49
- assert_equal "awesome", search.conditions.name_keywords
50
- assert_equal 2, search.page
51
- assert_equal 15, search.per_page
52
- end
53
-
54
- def test_searchlogic_searching
55
- binary_logic = accounts(:binary_logic)
56
- neco = accounts(:neco)
57
- binary_fun = accounts(:binary_fun)
58
-
59
- assert_equal [binary_logic, binary_fun], Account.all(:conditions => {:name_contains => "Binary"})
60
- assert_equal [binary_logic], Account.all(:conditions => {:name_contains => "Binary", :users => {:first_name_starts_with => "Ben"}})
61
- assert_equal [], Account.all(:conditions => {:name_contains => "Binary", :users => {:first_name_starts_with => "Ben", :last_name => "Mills"}})
62
- assert_equal [binary_logic, neco], Account.all(:conditions => {:users => {:id_gt => 0}}, :include => :users)
63
-
64
- read_only_accounts = Account.all(:conditions => {:name_contains => "Binary"}, :readonly => true)
65
- assert read_only_accounts.first.readonly?
66
-
67
- assert_equal [binary_logic, binary_fun], Account.all(:conditions => {:name_contains => "Binary"}, :page => 2)
68
- assert_equal [], Account.all(:conditions => {:name_contains => "Binary"}, :page => 2, :per_page => 20)
69
-
70
- assert_equal [binary_logic], Account.scope1.all(:conditions => {:users => {:first_name_starts_with => "Ben"}})
71
- end
72
-
73
- def test_searchlogic_counting
74
- assert_equal 2, Account.count(:conditions => {:name_contains => "Binary"})
75
- assert_equal 1, Account.count(:conditions => {:name_contains => "Binary", :users => {:first_name_contains => "Ben"}})
76
- assert_equal 1, Account.count(:conditions => {:name_contains => "Binary", :users => {:first_name_contains => "Ben"}}, :limit => 10, :offset => 10, :order_by => "id", :group => "accounts.id")
77
- end
78
-
79
- def test_scoping
80
- 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) })
81
- 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) })
82
- end
83
-
84
- def test_accessible_conditions
85
- Account.conditions_accessible :name_contains
86
- assert_equal Set.new(["name_contains"]), Account.accessible_conditions
87
- Account.conditions_accessible :id_gt
88
- assert_equal Set.new(["id_gt", "name_contains"]), Account.accessible_conditions
89
- Account.conditions_accessible :id_gt, :name_contains
90
- assert_equal Set.new(["id_gt", "name_contains"]), Account.accessible_conditions
91
- Account.send(:write_inheritable_attribute, :conditions_accessible, nil)
92
- end
93
-
94
- def test_protected_conditions
95
- Account.conditions_protected :name_contains
96
- assert_equal Set.new(["name_contains"]), Account.protected_conditions
97
- Account.conditions_protected :id_gt
98
- assert_equal Set.new(["id_gt", "name_contains"]), Account.protected_conditions
99
- Account.conditions_protected :id_gt, :name_contains
100
- assert_equal Set.new(["id_gt", "name_contains"]), Account.protected_conditions
101
- Account.send(:write_inheritable_attribute, :conditions_protected, nil)
102
- end
103
-
104
- def test_includes
105
- assert_nothing_raised { Account.all(:conditions => {:users => {:first_name_like => "Ben"}}, :include => :users) }
106
- end
107
-
108
- def test_remove_duplicate_joins
109
- query = "SELECT DISTINCT `ticket_groups`.* FROM `ticket_groups` INNER JOIN tickets ON ticket_groups.id = tickets.ticket_group_id LEFT OUTER JOIN `tickets` ON tickets.ticket_group_id = ticket_groups.id WHERE (`tickets`.`id` = 2) AND ((`tickets`.event_id = 810802042)) LIMIT 20"
110
- cleaned_query = ActiveRecord::Base.send(:remove_duplicate_joins, query)
111
- expected_query = "SELECT DISTINCT `ticket_groups`.* FROM `ticket_groups` INNER JOIN tickets ON ticket_groups.id = tickets.ticket_group_id WHERE (`tickets`.`id` = 2) AND ((`tickets`.event_id = 810802042)) LIMIT 20"
112
- assert_equal expected_query, cleaned_query
113
- end
114
- end
115
- end
@@ -1,62 +0,0 @@
1
- require File.dirname(__FILE__) + '/../test_helper.rb'
2
-
3
- module ConditionTests
4
- class BaseTest < ActiveSupport::TestCase
5
- def test_condition_type_name
6
- assert_equal "equals", Searchlogic::Condition::Equals.condition_type_name
7
- assert_equal "keywords", Searchlogic::Condition::Keywords.condition_type_name
8
- assert_equal "greater_than_or_equal_to", Searchlogic::Condition::GreaterThanOrEqualTo.condition_type_name
9
- end
10
-
11
- def test_ignore_meaningless_value?
12
- assert !Searchlogic::Condition::Equals.ignore_meaningless_value?
13
- assert Searchlogic::Condition::Keywords.ignore_meaningless_value?
14
- assert !Searchlogic::Condition::NotEqual.ignore_meaningless_value?
15
- end
16
-
17
- def test_value_type
18
- assert_nil Searchlogic::Condition::Equals.value_type
19
- assert_nil Searchlogic::Condition::Keywords.value_type
20
- assert_equal :boolean, Searchlogic::Condition::Nil.value_type
21
- assert_equal :boolean, Searchlogic::Condition::Blank.value_type
22
- assert_nil Searchlogic::Condition::GreaterThan.value_type
23
- end
24
-
25
- def test_initialize
26
- condition = Searchlogic::Condition::Keywords.new(Account, :column => Account.columns_hash["name"])
27
- assert_equal condition.klass, Account
28
- assert_equal Account.columns_hash["name"], condition.column
29
-
30
- condition = Searchlogic::Condition::GreaterThan.new(Account, :column => "id")
31
- assert_equal Account.columns_hash["id"], condition.column
32
-
33
- condition = Searchlogic::Condition::GreaterThan.new(Account, :column => "id", :column_type => :string, :column_sql_format => "some sql")
34
- assert_equal Account.columns_hash["id"], condition.column
35
- condition.value = "awesome"
36
- assert_equal ["some sql > ?", "awesome"], condition.sanitize
37
- end
38
-
39
- def test_explicitly_set_value
40
- condition = Searchlogic::Condition::Keywords.new(Account, :column => Account.columns_hash["name"])
41
- assert !condition.explicitly_set_value?
42
- condition.value = "test"
43
- assert condition.explicitly_set_value?
44
- end
45
-
46
- def test_sanitize
47
- # This is tested thoroughly in test_condition_types
48
- end
49
-
50
- def test_value
51
- # This is tested thoroughly in test_condition_types
52
- end
53
-
54
- #http://binarylogic.lighthouseapp.com/projects/16601-searchlogic/tickets/60-text-dates-are-incorrectly-converted-into-utc#ticket-60-11
55
- def test_string_to_date
56
- condition = Searchlogic::Condition::GreaterThan.new(Account, :column => "created_at")
57
- condition.value = "03/02/2009"
58
- assert_equal Time.parse("03/02/2009", 0), condition.value
59
- end
60
-
61
- end
62
- end