searchlogic 1.5.3 → 1.5.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,26 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ module SearchTests
4
+ class ProtectionTest < ActiveSupport::TestCase
5
+ def test_protection
6
+ assert_raise(ArgumentError) { Account.build_search(:conditions => "(DELETE FROM users)", :page => 2, :per_page => 15) }
7
+ Searchlogic::Search::Base::VULNERABLE_FIND_OPTIONS.each { |option| assert_raise(ArgumentError) { Account.build_search(option => "(DELETE FROM users)") } }
8
+
9
+ assert_nothing_raised { Account.build_search!(:conditions => "(DELETE FROM users)", :page => 2, :per_page => 15) }
10
+ Searchlogic::Search::Base::VULNERABLE_FIND_OPTIONS.each { |option| assert_nothing_raised { Account.build_search!(option => "(DELETE FROM users)") } }
11
+
12
+ account = Account.first
13
+
14
+ assert_raise(ArgumentError) { account.users.build_search(:conditions => "(DELETE FROM users)", :page => 2, :per_page => 15) }
15
+ Searchlogic::Search::Base::VULNERABLE_FIND_OPTIONS.each { |option| assert_raise(ArgumentError) { account.users.build_search(option => "(DELETE FROM users)") } }
16
+
17
+ assert_nothing_raised { account.users.build_search!(:conditions => "(DELETE FROM users)", :page => 2, :per_page => 15) }
18
+ Searchlogic::Search::Base::VULNERABLE_FIND_OPTIONS.each { |option| assert_nothing_raised { account.users.build_search!(option => "(DELETE FROM users)") } }
19
+
20
+ assert_raise(ArgumentError) { Account.build_search(:order_by => "unknown_column") }
21
+ assert_nothing_raised { Account.build_search!(:order_by => "unknown_column") }
22
+ assert_raise(ArgumentError) { Account.build_search(:order_by => ["name", "unknown_column"]) }
23
+ assert_nothing_raised { Account.build_search!(:order_by => ["name", "unknown_column"]) }
24
+ end
25
+ end
26
+ end
data/test/test_helper.rb CHANGED
@@ -2,12 +2,61 @@ require "test/unit"
2
2
  require "rubygems"
3
3
  require "ruby-debug"
4
4
  require "active_record"
5
- require File.dirname(__FILE__) + '/../test_libs/acts_as_tree'
6
- require File.dirname(__FILE__) + '/../test_libs/ordered_hash'
7
- require File.dirname(__FILE__) + '/../test_libs/rexml_fix'
8
- require File.dirname(__FILE__) + '/../lib/searchlogic'
5
+ require "active_record/fixtures"
6
+ require File.dirname(__FILE__) + '/libs/acts_as_tree'
7
+ require File.dirname(__FILE__) + '/libs/rexml_fix'
8
+ require File.dirname(__FILE__) + '/../lib/searchlogic' unless defined?(Searchlogic)
9
9
 
10
+ ActiveRecord::Schema.verbose = false
10
11
  ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
12
+ ActiveRecord::Base.configurations = true
13
+ ActiveRecord::Schema.define(:version => 1) do
14
+ create_table :accounts do |t|
15
+ t.datetime :created_at
16
+ t.datetime :updated_at
17
+ t.string :name
18
+ t.boolean :active
19
+ end
20
+
21
+ create_table :user_groups do |t|
22
+ t.datetime :created_at
23
+ t.datetime :updated_at
24
+ t.string :name
25
+ end
26
+
27
+ create_table :user_groups_users, :id => false do |t|
28
+ t.integer :user_group_id
29
+ t.integer :user_id
30
+ end
31
+
32
+ create_table :users do |t|
33
+ t.datetime :created_at
34
+ t.datetime :updated_at
35
+ t.integer :account_id
36
+ t.integer :parent_id
37
+ t.string :first_name
38
+ t.string :last_name
39
+ t.boolean :active
40
+ t.text :bio
41
+ end
42
+
43
+ create_table :orders do |t|
44
+ t.datetime :created_at
45
+ t.datetime :updated_at
46
+ t.integer :user_id
47
+ t.float :total
48
+ t.text :description
49
+ t.binary :receipt
50
+ end
51
+
52
+ create_table :animals do |t|
53
+ t.datetime :created_at
54
+ t.datetime :updated_at
55
+ t.string :type
56
+ t.text :description
57
+ end
58
+ end
59
+
11
60
 
12
61
  class Account < ActiveRecord::Base
13
62
  has_one :admin, :class_name => "User", :conditions => {:first_name => "Ben"}
@@ -45,90 +94,37 @@ class Cat < Animal
45
94
  end
46
95
 
47
96
  class Test::Unit::TestCase
48
- def setup_db
49
- ActiveRecord::Schema.define(:version => 1) do
50
- create_table :accounts do |t|
51
- t.datetime :created_at
52
- t.datetime :updated_at
53
- t.string :name
54
- t.boolean :active
55
- end
56
-
57
- create_table :user_groups do |t|
58
- t.datetime :created_at
59
- t.datetime :updated_at
60
- t.string :name
61
- end
62
-
63
- create_table :user_groups_users, :id => false do |t|
64
- t.integer :user_group_id
65
- t.integer :user_id
66
- end
67
-
68
- create_table :users do |t|
69
- t.datetime :created_at
70
- t.datetime :updated_at
71
- t.integer :account_id
72
- t.integer :parent_id
73
- t.string :first_name
74
- t.string :last_name
75
- t.boolean :active
76
- t.text :bio
77
- end
78
-
79
- create_table :orders do |t|
80
- t.datetime :created_at
81
- t.datetime :updated_at
82
- t.integer :user_id
83
- t.float :total
84
- t.text :description
85
- t.binary :receipt
86
- end
87
-
88
- create_table :animals do |t|
89
- t.datetime :created_at
90
- t.datetime :updated_at
91
- t.string :type
92
- t.text :description
93
- end
94
- end
95
- end
97
+ self.fixture_path = File.dirname(__FILE__) + "/fixtures"
98
+ self.use_transactional_fixtures = true
99
+ self.use_instantiated_fixtures = false
100
+ self.pre_loaded_fixtures = true
101
+ fixtures :all
96
102
 
97
- def load_fixtures
98
- fixtures = [:accounts, :orders, :users, :user_groups, :dogs, :cats]
99
- fixtures.each do |fixture|
100
- records = YAML.load(File.read(File.dirname(__FILE__) + "/fixtures/#{fixture.to_s}.yml"))
101
- records.each do |name, attributes|
102
- record = fixture.to_s.singularize.classify.constantize.new
103
- attributes.each { |k, v| record.send("#{k}=", v) }
104
- record.save!
103
+ private
104
+ def assert_equal_find_options(find_options, result)
105
+ find_options_conditions = find_options.delete(:conditions)
106
+ result_conditions = result.delete(:conditions)
107
+
108
+ assert_equal find_options, result
109
+ if find_options_conditions.blank? || result_conditions.blank?
110
+ assert_equal find_options_conditions, result_conditions
111
+ else
112
+ assert_equal_sql find_options_conditions, result_conditions
105
113
  end
106
114
  end
107
115
 
108
- # Not sure why I have to do this, but sick of dealing with it
109
- UserGroup.all.each do |user_group|
110
- user_ids = user_group.user_ids.uniq!
111
- user_group.users.clear
112
- user_group.user_ids = user_ids
113
- user_group.save!
116
+ def assert_equal_sql(sql, result)
117
+ sql_parts = breakdown_sql(sql)
118
+ result_parts = breakdown_sql(sql)
119
+
120
+ assert_equal sql_parts.size, result_parts.size
121
+ sql_parts.each { |part| assert result_parts.include?(part) }
114
122
  end
115
123
 
116
- # Create the cached virtual classes
117
- fixtures.each { |fixture| fixture.to_s.classify.constantize.new_search }
118
- end
119
-
120
- def teardown_db
121
- ActiveRecord::Base.connection.tables.each do |table|
122
- ActiveRecord::Base.connection.drop_table(table)
124
+ def breakdown_sql(sql)
125
+ sanitized_sql = ActiveRecord::Base.send(:sanitize_sql, sql)
126
+ sanitized_sql.gsub!(/(\(|\))/, "")
127
+ sql_parts = sanitized_sql.split(/or/i)
128
+ sql_parts.collect { |part| part.split(/ and /i) }.flatten
123
129
  end
124
- end
125
-
126
- def setup
127
- setup_db
128
- load_fixtures
129
- end
130
-
131
- def teardown
132
- teardown_db
133
- end
134
130
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: searchlogic
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.3
4
+ version: 1.5.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Johnson of Binary Logic
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-11-03 00:00:00 -05:00
12
+ date: 2008-11-16 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -147,7 +147,6 @@ extra_rdoc_files:
147
147
  - TODO.rdoc
148
148
  files:
149
149
  - CHANGELOG.rdoc
150
- - examples/README.rdoc
151
150
  - init.rb
152
151
  - lib/searchlogic/active_record/associations.rb
153
152
  - lib/searchlogic/active_record/base.rb
@@ -246,28 +245,48 @@ files:
246
245
  - MIT-LICENSE
247
246
  - Rakefile
248
247
  - README.rdoc
248
+ - test/active_record_tests/associations_test.rb
249
+ - test/active_record_tests/base_test.rb
250
+ - test/condition_tests/base_test.rb
251
+ - test/condition_tests/begins_with_test.rb
252
+ - test/condition_tests/blank_test.rb
253
+ - test/condition_tests/child_of_test.rb
254
+ - test/condition_tests/descendant_of_test.rb
255
+ - test/condition_tests/ends_with_test.rb
256
+ - test/condition_tests/equals_test.rb
257
+ - test/condition_tests/greater_than_or_equal_to_test.rb
258
+ - test/condition_tests/greater_than_test.rb
259
+ - test/condition_tests/inclusive_descendant_of_test.rb
260
+ - test/condition_tests/keyswords_test.rb
261
+ - test/condition_tests/less_than_or_equal_to_test.rb
262
+ - test/condition_tests/less_than_test.rb
263
+ - test/condition_tests/like_test.rb
264
+ - test/condition_tests/nil_test.rb
265
+ - test/condition_tests/not_begin_with_test.rb
266
+ - test/condition_tests/not_blank_test.rb
267
+ - test/condition_tests/not_end_with_test.rb
268
+ - test/condition_tests/not_equal_test.rb
269
+ - test/condition_tests/not_have_keywords_test.rb
270
+ - test/condition_tests/not_like_test.rb
271
+ - test/condition_tests/not_nil_test.rb
272
+ - test/condition_tests/sibling_of_test.rb
273
+ - test/conditions_tests/base_test.rb
274
+ - test/conditions_tests/protection_test.rb
275
+ - test/config_test.rb
249
276
  - test/fixtures/accounts.yml
250
- - test/fixtures/cats.yml
251
- - test/fixtures/dogs.yml
277
+ - test/fixtures/animals.yml
252
278
  - test/fixtures/orders.yml
253
279
  - test/fixtures/user_groups.yml
254
280
  - test/fixtures/users.yml
255
- - test/test_active_record_associations.rb
256
- - test/test_active_record_base.rb
257
- - test/test_condition_base.rb
258
- - test/test_condition_types.rb
259
- - test/test_conditions_base.rb
260
- - test/test_conditions_protection.rb
261
- - test/test_config.rb
281
+ - test/libs/acts_as_tree.rb
282
+ - test/libs/rexml_fix.rb
283
+ - test/modifier_tests/day_of_month_test.rb
284
+ - test/search_tests/base_test.rb
285
+ - test/search_tests/conditions_test.rb
286
+ - test/search_tests/ordering_test.rb
287
+ - test/search_tests/pagination_test.rb
288
+ - test/search_tests/protection_test.rb
262
289
  - test/test_helper.rb
263
- - test/test_search_base.rb
264
- - test/test_search_conditions.rb
265
- - test/test_search_ordering.rb
266
- - test/test_search_pagination.rb
267
- - test/test_search_protection.rb
268
- - test_libs/acts_as_tree.rb
269
- - test_libs/ordered_hash.rb
270
- - test_libs/rexml_fix.rb
271
290
  - TODO.rdoc
272
291
  - searchlogic.gemspec
273
292
  has_rdoc: true
@@ -302,16 +321,38 @@ signing_key:
302
321
  specification_version: 2
303
322
  summary: Object based ActiveRecord searching, ordering, pagination, and more!
304
323
  test_files:
305
- - test/test_active_record_associations.rb
306
- - test/test_active_record_base.rb
307
- - test/test_condition_base.rb
308
- - test/test_condition_types.rb
309
- - test/test_conditions_base.rb
310
- - test/test_conditions_protection.rb
311
- - test/test_config.rb
324
+ - test/active_record_tests/associations_test.rb
325
+ - test/active_record_tests/base_test.rb
326
+ - test/condition_tests/base_test.rb
327
+ - test/condition_tests/begins_with_test.rb
328
+ - test/condition_tests/blank_test.rb
329
+ - test/condition_tests/child_of_test.rb
330
+ - test/condition_tests/descendant_of_test.rb
331
+ - test/condition_tests/ends_with_test.rb
332
+ - test/condition_tests/equals_test.rb
333
+ - test/condition_tests/greater_than_or_equal_to_test.rb
334
+ - test/condition_tests/greater_than_test.rb
335
+ - test/condition_tests/inclusive_descendant_of_test.rb
336
+ - test/condition_tests/keyswords_test.rb
337
+ - test/condition_tests/less_than_or_equal_to_test.rb
338
+ - test/condition_tests/less_than_test.rb
339
+ - test/condition_tests/like_test.rb
340
+ - test/condition_tests/nil_test.rb
341
+ - test/condition_tests/not_begin_with_test.rb
342
+ - test/condition_tests/not_blank_test.rb
343
+ - test/condition_tests/not_end_with_test.rb
344
+ - test/condition_tests/not_equal_test.rb
345
+ - test/condition_tests/not_have_keywords_test.rb
346
+ - test/condition_tests/not_like_test.rb
347
+ - test/condition_tests/not_nil_test.rb
348
+ - test/condition_tests/sibling_of_test.rb
349
+ - test/conditions_tests/base_test.rb
350
+ - test/conditions_tests/protection_test.rb
351
+ - test/config_test.rb
352
+ - test/modifier_tests/day_of_month_test.rb
353
+ - test/search_tests/base_test.rb
354
+ - test/search_tests/conditions_test.rb
355
+ - test/search_tests/ordering_test.rb
356
+ - test/search_tests/pagination_test.rb
357
+ - test/search_tests/protection_test.rb
312
358
  - test/test_helper.rb
313
- - test/test_search_base.rb
314
- - test/test_search_conditions.rb
315
- - test/test_search_ordering.rb
316
- - test/test_search_pagination.rb
317
- - test/test_search_protection.rb
data/examples/README.rdoc DELETED
@@ -1,4 +0,0 @@
1
- === Live example
2
-
3
- * Please see the live example: http://searchlogic_example.binarylogic.com
4
- * Source / Github project for the example: http://github.com/binarylogic/searchlogic_example.binarylogic.com
@@ -1,3 +0,0 @@
1
- pepper:
2
- id: 1
3
- description: pepper meows
@@ -1,3 +0,0 @@
1
- harry:
2
- id: 2
3
- description: harry is hairy
@@ -1,81 +0,0 @@
1
- require File.dirname(__FILE__) + '/test_helper.rb'
2
-
3
- class TestActiveRecordAssociations < Test::Unit::TestCase
4
- def test_has_many
5
- search = Account.find(1).users.new_search
6
- assert_kind_of Searchlogic::Search::Base, search
7
- assert_equal User, search.klass
8
- assert_equal({:conditions => "\"users\".account_id = 1"}, search.scope)
9
-
10
- assert_equal User.find(1, 3), search.all
11
- assert_equal User.find(1), search.first
12
- assert_equal 2, search.average("id")
13
- assert_equal 2, search.count
14
-
15
- search.conditions.first_name_contains = "Ben"
16
-
17
- assert_equal [User.find(1)], search.all
18
- assert_equal User.find(1), search.first
19
- assert_equal 1, search.average("id")
20
- assert_equal 1, search.count
21
-
22
- assert_equal 2, Account.find(1).users.count
23
- assert_equal 1, Account.find(1).users.all(:conditions => {:first_name_contains => "Ben"}).size
24
- assert_equal 0, Account.find(1).users.all(:conditions => {:first_name_contains => "No one"}).size
25
- assert_equal 1, Account.find(1).users.sum("id", :conditions => {:first_name_contains => "Ben"})
26
- assert_equal 0, Account.find(1).users.sum("id", :conditions => {:first_name_contains => "No one"})
27
- assert_equal 1, Account.find(1).users.average("id", :conditions => {:first_name_contains => "Ben"})
28
- end
29
-
30
- def test_has_many_through
31
- search = Account.find(1).orders.new_search
32
- assert_kind_of Searchlogic::Search::Base, search
33
- assert_equal Order, search.klass
34
- assert_equal({:joins => "INNER JOIN users ON orders.user_id = users.id ", :conditions => "(\"users\".account_id = 1)"}, search.scope)
35
-
36
- assert_equal [Order.find(1)], search.all
37
- assert_equal Order.find(1), search.first
38
- assert_equal 1, search.average("id")
39
- assert_equal 1, search.count
40
-
41
- search.conditions.total_gt = 100
42
-
43
- assert_equal [Order.find(1)], search.all
44
- assert_equal Order.find(1), search.first
45
- assert_equal 1, search.average("id")
46
- assert_equal 1, search.count
47
-
48
- assert_equal 1, Account.find(1).orders.count
49
- assert_equal 1, Account.find(1).orders.all(:conditions => {:total_gt => 100}).size
50
- assert_equal 0, Account.find(1).orders.all(:conditions => {:total_gt => 1000}).size
51
- assert_equal 1, Account.find(1).orders.sum("id", :conditions => {:total_gt => 100})
52
- assert_equal 0, Account.find(1).orders.sum("id", :conditions => {:total_gt => 1000})
53
- assert_equal 1, Account.find(1).orders.average("id", :conditions => {:total_gt => 100})
54
- end
55
-
56
- def test_habtm
57
- search = UserGroup.find(1).users.new_search
58
- assert_kind_of Searchlogic::Search::Base, search
59
- assert_equal User, search.klass
60
- 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)
61
-
62
- assert_equal User.find(1, 2), search.all
63
- assert_equal User.find(1), search.first
64
- assert_equal (1.5).to_s, search.average("id").to_s
65
- assert_equal 2, search.count
66
-
67
- search.conditions.first_name_contains = "Ben"
68
-
69
- assert_equal [User.find(1)], search.all
70
- assert_equal User.find(1), search.first
71
- assert_equal 1, search.average("id")
72
- assert_equal 1, search.count
73
-
74
- assert_equal 2, UserGroup.find(1).users.count
75
- assert_equal 1, UserGroup.find(1).users.all(:conditions => {:first_name_contains => "Ben"}).size
76
- assert_equal 0, UserGroup.find(1).users.all(:conditions => {:first_name_contains => "No one"}).size
77
- assert_equal 1, UserGroup.find(1).users.sum("id", :conditions => {:first_name_contains => "Ben"})
78
- assert_equal 0, UserGroup.find(1).users.sum("id", :conditions => {:first_name_contains => "No one"})
79
- assert_equal 1, UserGroup.find(1).users.average("id", :conditions => {:first_name_contains => "Ben"})
80
- end
81
- end