searchlogic 1.5.3 → 1.5.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. data/CHANGELOG.rdoc +6 -0
  2. data/Manifest +37 -18
  3. data/README.rdoc +13 -17
  4. data/TODO.rdoc +1 -3
  5. data/lib/searchlogic.rb +6 -6
  6. data/lib/searchlogic/active_record/connection_adapters/sqlite_adapter.rb +9 -9
  7. data/lib/searchlogic/conditions/base.rb +20 -6
  8. data/lib/searchlogic/search/base.rb +0 -5
  9. data/lib/searchlogic/search/searching.rb +3 -1
  10. data/lib/searchlogic/version.rb +1 -1
  11. data/searchlogic.gemspec +4 -4
  12. data/test/active_record_tests/associations_test.rb +95 -0
  13. data/test/active_record_tests/base_test.rb +108 -0
  14. data/test/condition_tests/base_test.rb +54 -0
  15. data/test/condition_tests/begins_with_test.rb +11 -0
  16. data/test/condition_tests/blank_test.rb +31 -0
  17. data/test/condition_tests/child_of_test.rb +17 -0
  18. data/test/condition_tests/descendant_of_test.rb +16 -0
  19. data/test/condition_tests/ends_with_test.rb +11 -0
  20. data/test/condition_tests/equals_test.rb +19 -0
  21. data/test/condition_tests/greater_than_or_equal_to_test.rb +11 -0
  22. data/test/condition_tests/greater_than_test.rb +11 -0
  23. data/test/condition_tests/inclusive_descendant_of_test.rb +16 -0
  24. data/test/condition_tests/keyswords_test.rb +19 -0
  25. data/test/condition_tests/less_than_or_equal_to_test.rb +11 -0
  26. data/test/condition_tests/less_than_test.rb +11 -0
  27. data/test/condition_tests/like_test.rb +11 -0
  28. data/test/condition_tests/nil_test.rb +31 -0
  29. data/test/condition_tests/not_begin_with_test.rb +8 -0
  30. data/test/condition_tests/not_blank_test.rb +8 -0
  31. data/test/condition_tests/not_end_with_test.rb +8 -0
  32. data/test/condition_tests/not_equal_test.rb +19 -0
  33. data/test/condition_tests/not_have_keywords_test.rb +8 -0
  34. data/test/condition_tests/not_like_test.rb +8 -0
  35. data/test/condition_tests/not_nil_test.rb +13 -0
  36. data/test/condition_tests/sibling_of_test.rb +15 -0
  37. data/test/conditions_tests/base_test.rb +221 -0
  38. data/test/conditions_tests/protection_test.rb +18 -0
  39. data/test/{test_config.rb → config_test.rb} +1 -1
  40. data/test/fixtures/accounts.yml +0 -3
  41. data/test/fixtures/animals.yml +7 -0
  42. data/test/fixtures/orders.yml +2 -4
  43. data/test/fixtures/user_groups.yml +3 -9
  44. data/test/fixtures/users.yml +6 -12
  45. data/{test_libs → test/libs}/acts_as_tree.rb +0 -0
  46. data/{test_libs → test/libs}/rexml_fix.rb +0 -0
  47. data/test/modifier_tests/day_of_month_test.rb +16 -0
  48. data/test/search_tests/base_test.rb +237 -0
  49. data/test/search_tests/conditions_test.rb +21 -0
  50. data/test/search_tests/ordering_test.rb +167 -0
  51. data/test/search_tests/pagination_test.rb +74 -0
  52. data/test/search_tests/protection_test.rb +26 -0
  53. data/test/test_helper.rb +79 -83
  54. metadata +73 -32
  55. data/examples/README.rdoc +0 -4
  56. data/test/fixtures/cats.yml +0 -3
  57. data/test/fixtures/dogs.yml +0 -3
  58. data/test/test_active_record_associations.rb +0 -81
  59. data/test/test_active_record_base.rb +0 -93
  60. data/test/test_condition_base.rb +0 -52
  61. data/test/test_condition_types.rb +0 -143
  62. data/test/test_conditions_base.rb +0 -242
  63. data/test/test_conditions_protection.rb +0 -16
  64. data/test/test_search_base.rb +0 -227
  65. data/test/test_search_conditions.rb +0 -19
  66. data/test/test_search_ordering.rb +0 -165
  67. data/test/test_search_pagination.rb +0 -72
  68. data/test/test_search_protection.rb +0 -24
  69. data/test_libs/ordered_hash.rb +0 -9
@@ -0,0 +1,19 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ module ConditionTests
4
+ class NotEqualTest < ActiveSupport::TestCase
5
+ def test_sanitize
6
+ condition = Searchlogic::Condition::NotEqual.new(Account, :column => Account.columns_hash["id"])
7
+ condition.value = 12
8
+ assert_equal ["\"accounts\".\"id\" != ?", 12], condition.sanitize
9
+
10
+ condition = Searchlogic::Condition::NotEqual.new(Account, :column => Account.columns_hash["id"])
11
+ condition.value = [1,2,3,4]
12
+ assert_equal ["\"accounts\".\"id\" NOT IN (?)", [1, 2, 3, 4]], condition.sanitize
13
+
14
+ condition = Searchlogic::Condition::NotEqual.new(Account, :column => Account.columns_hash["id"])
15
+ condition.value = (1..10)
16
+ assert_equal ["\"accounts\".\"id\" NOT BETWEEN ? AND ?", 1, 10], condition.sanitize
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,8 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ module ConditionTests
4
+ class NotHaveKeywordsTest < ActiveSupport::TestCase
5
+ def test_sanitize
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ module ConditionTests
4
+ class NotLikeTest < ActiveSupport::TestCase
5
+ def test_sanitize
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,13 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ module ConditionTests
4
+ class NotNilTest < ActiveSupport::TestCase
5
+ def test_sanitize
6
+ condition = Searchlogic::Condition::NotNil.new(Account, :column => Account.columns_hash["created_at"])
7
+ condition.value = "1"
8
+ assert_equal "\"accounts\".\"created_at\" IS NOT NULL", condition.sanitize
9
+ condition.value = "false"
10
+ assert_equal "\"accounts\".\"created_at\" IS NULL", condition.sanitize
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,15 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ module ConditionTests
4
+ class SiblingOfTest < ActiveSupport::TestCase
5
+ def test_sanitize
6
+ ben = users(:ben)
7
+ drew = users(:drew)
8
+ jennifer = users(:jennifer)
9
+
10
+ condition = Searchlogic::Condition::SiblingOf.new(User)
11
+ condition.value = drew
12
+ assert_equal ["(\"users\".\"id\" != ?) AND (\"users\".\"parent_id\" = ?)", drew.id, ben.id], condition.sanitize
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,221 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ module ConditionsTests
4
+ class BaseTest < ActiveSupport::TestCase
5
+ def test_register_conditions
6
+ Searchlogic::Conditions::Base.register_condition(Searchlogic::Condition::Keywords)
7
+ assert [Searchlogic::Condition::Keywords], Searchlogic::Conditions::Base.conditions
8
+
9
+ Searchlogic::Conditions::Base.register_condition(Searchlogic::Condition::Like)
10
+ assert [Searchlogic::Condition::Keywords, Searchlogic::Condition::Like], Searchlogic::Conditions::Base.conditions
11
+ end
12
+
13
+ def test_association_names
14
+ ["dogs", "children", "user_groups", "orders", "account", "parent", "cats"].each do |name|
15
+ assert Searchlogic::Cache::UserConditions.association_names.include? name
16
+ end
17
+ ["admin", "orders", "users"].each do |name|
18
+ Searchlogic::Cache::AccountConditions.association_names.include? name
19
+ end
20
+ end
21
+
22
+ def test_needed
23
+ assert (not Searchlogic::Conditions::Base.needed?(User, {}))
24
+ assert (not Searchlogic::Conditions::Base.needed?(User, {:first_name => "Ben"}))
25
+ assert Searchlogic::Conditions::Base.needed?(User, {:first_name_contains => "Awesome"})
26
+ assert (not Searchlogic::Conditions::Base.needed?(User, {"orders.id" => 2}))
27
+ end
28
+
29
+ def test_initialize
30
+ conditions = Searchlogic::Cache::AccountConditions.new(:name_contains => "Binary")
31
+ assert_equal conditions.klass, Account
32
+ assert_equal conditions.name_contains, "Binary"
33
+ end
34
+
35
+ def test_any
36
+ conditions = Searchlogic::Cache::AccountConditions.new
37
+ assert (not conditions.any?)
38
+ conditions = Searchlogic::Cache::AccountConditions.new(:any => true)
39
+ assert conditions.any?
40
+ conditions.any = "false"
41
+ assert (not conditions.any?)
42
+ conditions = Searchlogic::Cache::AccountConditions.new
43
+ conditions.name_contains = "Binary"
44
+ assert_equal_sql ["\"accounts\".\"name\" LIKE ?", "%Binary%"], conditions.sanitize
45
+ conditions.id = 1
46
+ assert_equal_sql ["(\"accounts\".\"id\" = ?) AND (\"accounts\".\"name\" LIKE ?)", 1, "%Binary%"], conditions.sanitize
47
+ conditions.any = true
48
+ assert_equal_sql ["(\"accounts\".\"id\" = ?) OR (\"accounts\".\"name\" LIKE ?)", 1, "%Binary%"], conditions.sanitize
49
+ conditions.any = false
50
+ assert_equal_sql ["(\"accounts\".\"id\" = ?) AND (\"accounts\".\"name\" LIKE ?)", 1, "%Binary%"], conditions.sanitize
51
+ end
52
+
53
+ def test_auto_joins
54
+ conditions = Searchlogic::Cache::AccountConditions.new
55
+ assert_equal conditions.auto_joins, nil
56
+
57
+ conditions.name_like = "Binary"
58
+ assert_equal conditions.auto_joins, nil
59
+
60
+ conditions.users.first_name_like = "Ben"
61
+ assert_equal conditions.auto_joins, :users
62
+
63
+ conditions.users.orders.description_like = "apple"
64
+ assert_equal conditions.auto_joins, {:users => :orders}
65
+ end
66
+
67
+ def test_inspect
68
+ conditions = Searchlogic::Cache::AccountConditions.new
69
+ assert_nothing_raised { conditions.inspect }
70
+ end
71
+
72
+ def test_sanitize
73
+ conditions = Searchlogic::Cache::AccountConditions.new
74
+ conditions.name_contains = "Binary"
75
+ conditions.id_gt = 5
76
+ now = Time.now
77
+ conditions.created_after = now
78
+ assert_equal_sql ["(\"accounts\".\"created_at\" > ?) AND (\"accounts\".\"id\" > ?) AND (\"accounts\".\"name\" LIKE ?)", now, 5, "%Binary%"], conditions.sanitize
79
+
80
+ # test out associations
81
+ conditions.users.first_name_like = "Ben"
82
+ conditions.users.id_gt = 10
83
+ conditions.users.orders.total_lt = 500
84
+ 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
85
+
86
+ # test that raw sql is returned
87
+ conditions.conditions = "awesome"
88
+ assert_equal "awesome", conditions.sanitize
89
+ end
90
+
91
+ def test_conditions
92
+ conditions = Searchlogic::Cache::AccountConditions.new
93
+ now = Time.now
94
+ v = {:name_like => "Binary", :created_at_greater_than => now}
95
+ conditions.conditions = v
96
+ assert_equal v, conditions.conditions
97
+
98
+ sql = "id in (1,2,3,4)"
99
+ conditions.conditions = sql
100
+ assert_equal sql, conditions.conditions
101
+ assert_equal({}, conditions.send(:objects))
102
+
103
+ v2 = {:id_less_than => 5, :name_begins_with => "Beginning of string"}
104
+ conditions.conditions = v2
105
+ assert_equal v2, conditions.conditions
106
+
107
+ v = {:name_like => "Binary", :created_at_greater_than => now}
108
+ conditions.conditions = v
109
+ assert_equal v2.merge(v), conditions.conditions
110
+
111
+ sql2 = "id > 5 and name = 'Test'"
112
+ conditions.conditions = sql2
113
+ assert_equal sql2, conditions.conditions
114
+ assert_equal({}, conditions.send(:objects))
115
+
116
+ conditions.name_contains = "awesome"
117
+ assert_equal({:name_like => "awesome"}, conditions.conditions)
118
+
119
+ now = Time.now
120
+ conditions.conditions = {:id_gt => "", :id => "", :name => ["", "", ""], :created_at => ["", now], :name_starts_with => "Ben"}
121
+ assert_equal({:name_like => "awesome", :name_begins_with => "Ben", :created_at_equals => now}, conditions.conditions)
122
+ end
123
+
124
+ # Other general usage tests, need to clean these up
125
+
126
+ def test_setting_conditions
127
+ [Account, User, Order].each do |klass|
128
+ conditions = "Searchlogic::Cache::#{klass}Conditions".constantize.new
129
+ conditions.class.condition_names.each do |condition_name|
130
+ conditions.send("#{condition_name}=", 1)
131
+ assert_equal 1, conditions.send(condition_name)
132
+ end
133
+ end
134
+ end
135
+
136
+ def test_accessible_protected_conditions
137
+ Account.conditions_accessible << :name_contains
138
+ conditions = Searchlogic::Cache::AccountConditions.new
139
+ conditions.conditions = {:created_after => Time.now, :name_contains => "Binary"}
140
+ assert({:name_contains => "Binary"}, conditions.conditions)
141
+ Account.send(:write_inheritable_attribute, :conditions_accessible, nil)
142
+
143
+ Account.conditions_protected << :name_contains
144
+ conditions = Searchlogic::Cache::AccountConditions.new
145
+ now = Time.now
146
+ conditions.conditions = {:created_after => now, :name_contains => "Binary"}
147
+ assert({:created_after => now}, conditions.conditions)
148
+ Account.send(:write_inheritable_attribute, :conditions_protected, nil)
149
+ end
150
+
151
+ def test_assert_valid_values
152
+ conditions = Searchlogic::Cache::UserConditions.new
153
+ assert_raise(NoMethodError) { conditions.conditions = {:unknown => "blah"} }
154
+ assert_nothing_raised { conditions.conditions = {:first_name => "blah"} }
155
+ assert_nothing_raised { conditions.conditions = {:first_name_contains => "blah"} }
156
+ end
157
+
158
+ def test_setting_associations
159
+ conditions = Searchlogic::Cache::AccountConditions.new(:users => {:first_name_like => "Ben"})
160
+ assert_equal conditions.users.first_name_like, "Ben"
161
+
162
+ conditions.users.last_name_begins_with = "Ben"
163
+ assert_equal conditions.users.last_name_begins_with, "Ben"
164
+ end
165
+
166
+ def test_virtual_columns
167
+ conditions = Searchlogic::Cache::AccountConditions.new
168
+ conditions.hour_of_created_gt = 2
169
+ assert_equal_sql ["strftime('%H', \"accounts\".\"created_at\") > ?", 2], conditions.sanitize
170
+ conditions.dow_of_created_at_most = 5
171
+ assert_equal_sql ["(strftime('%w', \"accounts\".\"created_at\") <= ?) AND (strftime('%H', \"accounts\".\"created_at\") > ?)", 5, 2], conditions.sanitize
172
+ conditions.month_of_created_at_nil = true
173
+ 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
174
+ conditions.min_of_hour_of_month_of_created_at_nil = true
175
+ 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
176
+ end
177
+
178
+ def test_objects
179
+ conditions = Searchlogic::Cache::AccountConditions.new
180
+ assert_equal({}, conditions.send(:objects))
181
+
182
+ conditions.name_contains = "Binary"
183
+ assert_equal 1, conditions.send(:objects).size
184
+
185
+ conditions.users.first_name_contains = "Ben"
186
+ assert_equal 2, conditions.send(:objects).size
187
+ end
188
+
189
+ def test_reset
190
+ conditions = Searchlogic::Cache::AccountConditions.new
191
+
192
+ conditions.name_contains = "Binary"
193
+ assert_equal 1, conditions.send(:objects).size
194
+
195
+ conditions.reset_name_like!
196
+ conditions.reset_name_contains! # should set up aliases for reset
197
+ assert_equal({}, conditions.send(:objects))
198
+
199
+ conditions.users.first_name_like = "Ben"
200
+ assert_equal 1, conditions.send(:objects).size
201
+
202
+ conditions.reset_users!
203
+ assert_equal({}, conditions.send(:objects))
204
+
205
+ conditions.name_begins_with ="Binary"
206
+ conditions.users.orders.total_gt = 200
207
+ assert_equal 2, conditions.send(:objects).size
208
+
209
+ conditions.reset_name_begins_with!
210
+ assert_equal 1, conditions.send(:objects).size
211
+
212
+ conditions.reset_users!
213
+ assert_equal({}, conditions.send(:objects))
214
+ end
215
+
216
+ def test_method_conflicts
217
+ conditions = Searchlogic::Cache::AccountConditions.new
218
+ assert_nil conditions.id
219
+ end
220
+ end
221
+ end
@@ -0,0 +1,18 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ module ConditionsTests
4
+ class ProtectionTest < ActiveSupport::TestCase
5
+ def test_protection
6
+ assert_raise(ArgumentError) { Account.new_search(:conditions => "(DELETE FROM users)") }
7
+ assert_nothing_raised { Account.new_search!(:conditions => "(DELETE FROM users)") }
8
+
9
+ account = Account.first
10
+
11
+ assert_raise(ArgumentError) { account.users.new_search(:conditions => "(DELETE FROM users)") }
12
+ assert_nothing_raised { account.users.new_search!(:conditions => "(DELETE FROM users)") }
13
+
14
+ search = Account.new_search
15
+ assert_raise(ArgumentError) { search.conditions = "(DELETE FROM users)" }
16
+ end
17
+ end
18
+ end
@@ -1,6 +1,6 @@
1
1
  require File.dirname(__FILE__) + '/test_helper.rb'
2
2
 
3
- class TestConfig < Test::Unit::TestCase
3
+ class ConfigTest < ActiveSupport::TestCase
4
4
  def test_per_page
5
5
  Searchlogic::Config.search.per_page = 1
6
6
 
@@ -1,15 +1,12 @@
1
1
  binary_logic:
2
- id: 1
3
2
  name: "Binary Logic"
4
3
  active: true
5
4
 
6
5
  neco:
7
- id: 2
8
6
  name: "NECO"
9
7
  active: false
10
8
 
11
9
  binary_fun:
12
- id: 3
13
10
  name: "Binary Fun"
14
11
  active: true
15
12
 
@@ -0,0 +1,7 @@
1
+ pepper:
2
+ type: Cat
3
+ description: pepper meows
4
+
5
+ harry:
6
+ type: Doc
7
+ description: harry is hairy
@@ -1,13 +1,11 @@
1
1
  bens_order:
2
- id: 1
3
- user_id: 1
2
+ user: ben
4
3
  total: 500.23
5
4
  description: A bunch of apple products, etc.
6
5
  receipt: some binary text
7
6
 
8
7
  drews_order:
9
- id: 2
10
- user_id: 2
8
+ user: drew
11
9
  total: 2.12
12
10
  description: Some more apple projects, ipod, etc
13
11
  receipt: some more binary text
@@ -1,13 +1,7 @@
1
1
  neco:
2
- id: 1
3
2
  name: NECO
4
- user_ids:
5
- - 1
6
- - 2
7
-
3
+ users: ben, drew
4
+
8
5
  johnsons:
9
- id: 2
10
6
  name: Johnsons
11
- user_ids:
12
- - 1
13
- - 3
7
+ users: ben, jennifer
@@ -1,34 +1,28 @@
1
1
  ben:
2
- id: 1
3
- account_id: 1
4
- parent_id: null
2
+ account: binary_logic
5
3
  first_name: Ben
6
4
  last_name: Johnson
7
5
  active: true
8
6
  bio: Totally awesome!
9
7
 
10
8
  drew:
11
- id: 2
12
- account_id: 2
13
- parent_id: 1
9
+ account: neco
10
+ parent: ben
14
11
  first_name: Drew
15
12
  last_name: Mills
16
13
  active: false
17
14
  bio: Totally not awesome!
18
15
 
19
16
  jennifer:
20
- id: 3
21
- account_id: 1
22
- parent_id: 2
17
+ account: binary_logic
18
+ parent: drew
23
19
  first_name: Jennifer
24
20
  last_name: Hopkins
25
21
  active: false
26
22
  bio: Totally not awesome at all!
27
23
 
28
24
  tren:
29
- id: 4
30
- account_id:
31
- parent_id:
25
+ parent: ben
32
26
  first_name: Tren
33
27
  last_name: Garfield
34
28
  active: false
File without changes
File without changes
@@ -0,0 +1,16 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ module ConditionsTests
4
+ class DayOfMonthTest < ActiveSupport::TestCase
5
+ def test_modifier_names
6
+
7
+ end
8
+
9
+ def test_usage
10
+ search = User.new_search
11
+ search.conditions.dom_of_created_at_gt = "1"
12
+ assert_equal({:conditions => ["(strftime('%d', \"users\".\"created_at\") * 1) > ?", 1], :limit => 25}, search.sanitize)
13
+ search.all
14
+ end
15
+ end
16
+ end