cbaclig-ar-extensions 0.9.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (112) hide show
  1. data/ChangeLog +148 -0
  2. data/Manifest +111 -0
  3. data/README +167 -0
  4. data/Rakefile +65 -0
  5. data/ar-extensions.gemspec +19 -0
  6. data/benchmarks/README +32 -0
  7. data/benchmarks/benchmark.rb +61 -0
  8. data/benchmarks/boot.rb +21 -0
  9. data/benchmarks/lib/base.rb +121 -0
  10. data/benchmarks/lib/cli_parser.rb +103 -0
  11. data/benchmarks/lib/float.rb +15 -0
  12. data/benchmarks/lib/mysql_benchmark.rb +27 -0
  13. data/benchmarks/lib/output_to_csv.rb +18 -0
  14. data/benchmarks/lib/output_to_html.rb +69 -0
  15. data/cbaclig-ar-extensions.gemspec +30 -0
  16. data/config/database.yml +7 -0
  17. data/config/database.yml.template +7 -0
  18. data/config/mysql.schema +72 -0
  19. data/config/postgresql.schema +39 -0
  20. data/db/migrate/generic_schema.rb +97 -0
  21. data/db/migrate/mysql_schema.rb +32 -0
  22. data/db/migrate/oracle_schema.rb +5 -0
  23. data/db/migrate/version.rb +4 -0
  24. data/init.rb +31 -0
  25. data/lib/ar-extensions.rb +5 -0
  26. data/lib/ar-extensions/adapters/abstract_adapter.rb +139 -0
  27. data/lib/ar-extensions/adapters/mysql.rb +10 -0
  28. data/lib/ar-extensions/adapters/oracle.rb +14 -0
  29. data/lib/ar-extensions/adapters/postgresql.rb +9 -0
  30. data/lib/ar-extensions/adapters/sqlite.rb +7 -0
  31. data/lib/ar-extensions/create_and_update.rb +509 -0
  32. data/lib/ar-extensions/create_and_update/mysql.rb +7 -0
  33. data/lib/ar-extensions/csv.rb +309 -0
  34. data/lib/ar-extensions/delete.rb +143 -0
  35. data/lib/ar-extensions/delete/mysql.rb +3 -0
  36. data/lib/ar-extensions/extensions.rb +506 -0
  37. data/lib/ar-extensions/finder_options.rb +275 -0
  38. data/lib/ar-extensions/finder_options/mysql.rb +6 -0
  39. data/lib/ar-extensions/finders.rb +94 -0
  40. data/lib/ar-extensions/foreign_keys.rb +70 -0
  41. data/lib/ar-extensions/fulltext.rb +62 -0
  42. data/lib/ar-extensions/fulltext/mysql.rb +44 -0
  43. data/lib/ar-extensions/import.rb +362 -0
  44. data/lib/ar-extensions/import/mysql.rb +50 -0
  45. data/lib/ar-extensions/import/postgresql.rb +7 -0
  46. data/lib/ar-extensions/import/sqlite.rb +22 -0
  47. data/lib/ar-extensions/insert_select.rb +178 -0
  48. data/lib/ar-extensions/insert_select/mysql.rb +7 -0
  49. data/lib/ar-extensions/synchronize.rb +30 -0
  50. data/lib/ar-extensions/temporary_table.rb +131 -0
  51. data/lib/ar-extensions/temporary_table/mysql.rb +3 -0
  52. data/lib/ar-extensions/union.rb +204 -0
  53. data/lib/ar-extensions/union/mysql.rb +6 -0
  54. data/lib/ar-extensions/util/sql_generation.rb +27 -0
  55. data/lib/ar-extensions/util/support_methods.rb +32 -0
  56. data/lib/ar-extensions/version.rb +9 -0
  57. data/tests/README +68 -0
  58. data/tests/boot.rb +23 -0
  59. data/tests/database.yml +31 -0
  60. data/tests/database.yml.sample +28 -0
  61. data/tests/fixtures/addresses.yml +25 -0
  62. data/tests/fixtures/books.yml +46 -0
  63. data/tests/fixtures/developers.yml +20 -0
  64. data/tests/fixtures/unit/active_record_base_finders/addresses.yml +25 -0
  65. data/tests/fixtures/unit/active_record_base_finders/books.yml +64 -0
  66. data/tests/fixtures/unit/active_record_base_finders/developers.yml +20 -0
  67. data/tests/fixtures/unit/synchronize/books.yml +16 -0
  68. data/tests/fixtures/unit/to_csv_headers/addresses.yml +8 -0
  69. data/tests/fixtures/unit/to_csv_headers/developers.yml +6 -0
  70. data/tests/fixtures/unit/to_csv_with_common_options/addresses.yml +40 -0
  71. data/tests/fixtures/unit/to_csv_with_common_options/developers.yml +13 -0
  72. data/tests/fixtures/unit/to_csv_with_common_options/languages.yml +29 -0
  73. data/tests/fixtures/unit/to_csv_with_common_options/teams.yml +3 -0
  74. data/tests/fixtures/unit/to_csv_with_default_options/developers.yml +7 -0
  75. data/tests/models/address.rb +4 -0
  76. data/tests/models/animal.rb +2 -0
  77. data/tests/models/book.rb +3 -0
  78. data/tests/models/cart_item.rb +4 -0
  79. data/tests/models/developer.rb +8 -0
  80. data/tests/models/group.rb +3 -0
  81. data/tests/models/language.rb +5 -0
  82. data/tests/models/mysql/book.rb +3 -0
  83. data/tests/models/mysql/test_innodb.rb +3 -0
  84. data/tests/models/mysql/test_memory.rb +3 -0
  85. data/tests/models/mysql/test_myisam.rb +3 -0
  86. data/tests/models/project.rb +2 -0
  87. data/tests/models/shopping_cart.rb +4 -0
  88. data/tests/models/team.rb +4 -0
  89. data/tests/models/topic.rb +13 -0
  90. data/tests/mysql/test_create_and_update.rb +290 -0
  91. data/tests/mysql/test_delete.rb +142 -0
  92. data/tests/mysql/test_finder_options.rb +121 -0
  93. data/tests/mysql/test_finders.rb +29 -0
  94. data/tests/mysql/test_import.rb +354 -0
  95. data/tests/mysql/test_insert_select.rb +173 -0
  96. data/tests/mysql/test_mysql_adapter.rb +45 -0
  97. data/tests/mysql/test_union.rb +81 -0
  98. data/tests/oracle/test_adapter.rb +14 -0
  99. data/tests/postgresql/test_adapter.rb +14 -0
  100. data/tests/prepare.rb +9 -0
  101. data/tests/run.rb +13 -0
  102. data/tests/run_from_gem.rb +17 -0
  103. data/tests/test_activerecord_compatability.rb +71 -0
  104. data/tests/test_finders.rb +543 -0
  105. data/tests/test_helper.rb +70 -0
  106. data/tests/test_import.rb +339 -0
  107. data/tests/test_synchronize.rb +31 -0
  108. data/tests/test_temporary_tables.rb +93 -0
  109. data/tests/test_to_csv_headers.rb +204 -0
  110. data/tests/test_to_csv_with_common_options.rb +670 -0
  111. data/tests/test_to_csv_with_default_options.rb +34 -0
  112. metadata +211 -0
@@ -0,0 +1,543 @@
1
+ require File.expand_path( File.join( File.dirname( __FILE__ ), 'test_helper' ) )
2
+
3
+ class FindersTest< TestCaseSuperClass
4
+ include ActiveRecord::ConnectionAdapters
5
+ self.fixture_path = File.join( File.dirname( __FILE__ ), 'fixtures/unit/active_record_base_finders' )
6
+ self.fixtures 'developers', 'books'
7
+
8
+ def setup
9
+ @connection = ActiveRecord::Base.connection
10
+ end
11
+
12
+ def teardown
13
+ Developer.delete_all
14
+ Book.delete_all
15
+ end
16
+
17
+ def setup_time
18
+ Book.destroy_all
19
+ 3.times do |i|
20
+ book = Book.create! :title=>"a#{i}", :publisher=>"b#{i}", :author_name=>"c#{i}"
21
+ assert book.update_attribute(:created_at, Time.local(2007, 01, 01, 21, 38, i))
22
+ end
23
+ book = Book.create! :title=>"nope", :publisher=>"not i", :author_name=>"not me"
24
+ assert book.update_attribute(:created_at, Time.local(2007, 01, 01, 20, 38, 04))
25
+ end
26
+
27
+ def test_find_by_array1
28
+ developers = Developer.find( :all, :conditions=>[ 'ID IN(?)', [1,2] ] )
29
+ assert_equal( 2, developers.size )
30
+ end
31
+
32
+ def test_find_by_array_with_reserved_words1
33
+ Group.destroy_all
34
+ Group.create!(:order => "a")
35
+ Group.create!(:order => "b")
36
+ Group.create!(:order => "c")
37
+ groups = Group.find(:all, :conditions => {:order => ["a", "b", "c"]})
38
+ assert_equal 3, groups.size
39
+ end
40
+
41
+ def test_find_by_array2
42
+ developers = Developer.find_all_by_id( [ 1, 2 ] )
43
+ assert_equal( 2, developers.size )
44
+ end
45
+
46
+ def test_find_by_array_with_reserved_words2
47
+ Group.destroy_all
48
+ Group.create!(:order => "a")
49
+ Group.create!(:order => "b")
50
+ Group.create!(:order => "c")
51
+ groups = Group.find_all_by_order(["a", "b", "c"])
52
+ assert_equal 3, groups.size
53
+ end
54
+
55
+ def test_find_by_array3
56
+ developers = Developer.find_all_by_name( [ 'Zach Dennis', 'John Doe', "The Second Topic's of the day" ] )
57
+ assert_equal( 2, developers.size )
58
+ end
59
+
60
+ def test_find_by_array4
61
+ developers = Developer.find( :all, :conditions=>{ :id=>[1,2] } )
62
+ assert_equal( 2, developers.size )
63
+ end
64
+
65
+ def test_find_by_range
66
+ # ( x...z ) == (x..(z-1))
67
+ developers = Developer.find( :all, :conditions=>{ :id=>(1..2) } )
68
+ assert_equal( 2, developers.size )
69
+ developers = Developer.find( :all, :conditions=>{ :id=>(1...2) } )
70
+ assert_equal( 1, developers.size )
71
+ end
72
+
73
+ def test_find_by_range_with_reserved_words
74
+ Group.destroy_all
75
+ Group.create!(:order => "a")
76
+ Group.create!(:order => "b")
77
+ Group.create!(:order => "c")
78
+ groups = Group.find(:all, :conditions => {:order => ("a".."c")})
79
+ assert_equal 3, groups.size
80
+ groups = Group.find(:all, :conditions => {:order => ("b".."c")})
81
+ assert_equal 2, groups.size
82
+ groups = Group.find(:all, :conditions => {:order => ("d".."z")})
83
+ assert_equal 0, groups.size
84
+ end
85
+
86
+ def test_find_with_like
87
+ developers = Developer.find( :all, :conditions=>{ :name_like=>'ach' } )
88
+ assert_equal( 1, developers.size )
89
+
90
+ developers = Developer.find( :all, :conditions=>{ :name_like=>'Zach' } )
91
+ assert_equal( 1, developers.size )
92
+
93
+ developers = Developer.find( :all, :conditions=>{ :name_like=>['ach', 'oe'] } )
94
+ assert_equal( 2, developers.size )
95
+ end
96
+
97
+ def test_find_with_like_with_reserved_words
98
+ Group.destroy_all
99
+ g1 = Group.create!(:order => "a")
100
+ g2 = Group.create!(:order => "b")
101
+ groups = Group.find(:all, :conditions => {:order_like => 'a'})
102
+ assert_equal g1, groups.first
103
+
104
+ groups = Group.find(:all, :conditions => {:order_like => ['a', 'b']})
105
+ assert_equal [g1, g2], groups
106
+ end
107
+
108
+ def test_find_with_contains
109
+ developers = Developer.find( :all, :conditions=>{ :name_contains=>'ach' } )
110
+ assert_equal( 1, developers.size )
111
+
112
+ developers = Developer.find( :all, :conditions=>{ :name_contains=>'Zach' } )
113
+ assert_equal( 1, developers.size )
114
+
115
+ developers = Developer.find( :all, :conditions=>{ :name_contains=>['ach', 'oe'] } )
116
+ assert_equal( 2, developers.size )
117
+ end
118
+
119
+ def test_find_with_contains_with_reserved_words
120
+ Group.destroy_all
121
+ g1 = Group.create!(:order => "a")
122
+ g2 = Group.create!(:order => "b")
123
+ groups = Group.find(:all, :conditions => {:order_contains => 'a'})
124
+ assert_equal g1, groups.first
125
+
126
+ groups = Group.find(:all, :conditions => {:order_contains => ['a', 'b']})
127
+ assert_equal [g1, g2], groups
128
+ end
129
+
130
+
131
+ def test_find_with_starts_with
132
+ developers = Developer.find( :all, :conditions=>{ :name_starts_with=>'Zach' } )
133
+ assert_equal( 1, developers.size )
134
+
135
+ # we shouldn't find a record which starts with the last name Dennis
136
+ developers = Developer.find( :all, :conditions=>{ :name_starts_with=>'Dennis' } )
137
+ assert_equal( 0, developers.size )
138
+
139
+ developers = Developer.find( :all, :conditions=>{ :name_starts_with=>['Za', 'Jo'] } )
140
+ assert_equal( 2, developers.size )
141
+ end
142
+
143
+ def test_find_with_starts_with_with_reserved_words
144
+ Group.destroy_all
145
+ g1 = Group.create!(:order => "a")
146
+ g2 = Group.create!(:order => "b")
147
+ groups = Group.find(:all, :conditions => {:order_starts_with => 'a'})
148
+ assert_equal g1, groups.first
149
+
150
+ groups = Group.find(:all, :conditions => {:order_starts_with => ['a', 'b']})
151
+ assert_equal [g1, g2], groups
152
+
153
+ groups = Group.find(:all, :conditions => {:order_starts_with => ['z']})
154
+ assert groups.empty?
155
+ end
156
+
157
+ def test_find_with_ends_with
158
+ developers = Developer.find( :all, :conditions=>{ :name_ends_with=>'Dennis' } )
159
+ assert_equal( 1, developers.size )
160
+
161
+ # we shouldn't find an issue which ends with the first name Zach
162
+ developers = Developer.find( :all, :conditions=>{ :name_ends_with=>'Zach' } )
163
+ assert_equal( 0, developers.size )
164
+
165
+ developers = Developer.find( :all, :conditions=>{ :name_ends_with=>['is', 'oe'] } )
166
+ assert_equal( 2, developers.size )
167
+ end
168
+
169
+ def test_find_with_ends_with_with_reserved_words
170
+ Group.destroy_all
171
+ g1 = Group.create!(:order => "a")
172
+ g2 = Group.create!(:order => "b")
173
+ groups = Group.find(:all, :conditions => {:order_ends_with => 'a'})
174
+ assert_equal g1, groups.first
175
+
176
+ groups = Group.find(:all, :conditions => {:order_ends_with => ['a', 'b']})
177
+ assert_equal [g1, g2], groups
178
+
179
+ groups = Group.find(:all, :conditions => {:order_ends_with => ['z']})
180
+ assert groups.empty?
181
+ end
182
+
183
+ def test_find_with_regex
184
+ developers = Developer.find( :all, :conditions=>{ :name=>/^Zach/ } )
185
+ assert_equal( 1, developers.size )
186
+
187
+ developers = Developer.find( :all, :conditions=>{ :name=>/Dennis$/ } )
188
+ assert_equal( 1, developers.size )
189
+ end
190
+
191
+ def test_find_using_regexp_with_reserved_words
192
+ Group.destroy_all
193
+ g1 = Group.create!(:order => "abc")
194
+ group = Group.find(:first, :conditions => {:order => /abc/})
195
+ assert_equal g1, group
196
+
197
+ g2 = Group.create!(:order => "adf")
198
+ groups = Group.find(:all, :conditions => {:order => /a/})
199
+ assert_equal [g1, g2], groups
200
+
201
+ groups = Group.find(:all, :conditions => {:order => /^a/})
202
+ assert_equal g1, groups.first
203
+
204
+ groups = Group.find(:all, :conditions => {:order => /df$/})
205
+ assert_equal g2, groups.first
206
+ end
207
+
208
+ def test_find_with_less_than
209
+ developers = Developer.find( :all, :conditions=>{ :id_lt=>2 } )
210
+ assert_equal( 1, developers.size )
211
+ end
212
+
213
+ def test_find_with_greater_than
214
+ developers = Developer.find( :all, :conditions=>{ :id_gt=>1 } )
215
+ assert_equal( 2, developers.size )
216
+ end
217
+
218
+ def test_find_with_less_than_or_equal_to
219
+ developers = Developer.find( :all, :conditions=>{ :id_lte=>2 } )
220
+ assert_equal( 2, developers.size )
221
+ end
222
+
223
+ def test_find_with_greater_than_or_equal_to
224
+ developers = Developer.find( :all, :conditions=>{ :id_gte=>1 } )
225
+ assert_equal( 3, developers.size )
226
+ end
227
+
228
+ def test_find_not_equal_to
229
+ developers = Developer.find( :all, :conditions=>{ :id_ne=>9999 } )
230
+ assert_equal( Developer.count, developers.size )
231
+
232
+ developers = Developer.find( :all, :conditions=>{ :id_not=>9999 } )
233
+ assert_equal( Developer.count, developers.size )
234
+ end
235
+
236
+ def test_find_greater_than_date
237
+ Book.destroy_all
238
+ books = [
239
+ Book.create!(:publish_date => Date.parse("2009-01-01"), :title=>"a", :publisher=>"x", :author_name=>"j"),
240
+ Book.create!(:publish_date => Date.parse("2009-01-15"), :title=>"b", :publisher=>"y", :author_name=>"k"),
241
+ Book.create!(:publish_date => Date.parse("2009-01-30"), :title=>"c", :publisher=>"z", :author_name=>"l")]
242
+ assert_equal books[1..2], Book.find( :all, :conditions=>{ :publish_date_gt => Date.parse("2009-01-01") } )
243
+ end
244
+
245
+ def test_find_less_than_date
246
+ Book.destroy_all
247
+ books = [
248
+ Book.create!(:publish_date => Date.parse("2009-01-01"), :title=>"a", :publisher=>"x", :author_name=>"j"),
249
+ Book.create!(:publish_date => Date.parse("2009-01-15"), :title=>"b", :publisher=>"y", :author_name=>"k"),
250
+ Book.create!(:publish_date => Date.parse("2009-01-30"), :title=>"c", :publisher=>"z", :author_name=>"l")]
251
+ assert_equal books[0..1], Book.find( :all, :conditions=>{ :publish_date_lt => Date.parse("2009-01-30") } )
252
+ end
253
+
254
+ def test_find_greater_than_time
255
+ setup_time
256
+ books = Book.find( :all, :conditions=>{ :created_at_gt => Time.local(2007, 01, 01, 21, 38, 0) } )
257
+ assert_equal 2, books.size
258
+ end
259
+
260
+ def test_find_greater_than_time_with_reserved_words
261
+ now = Time.now
262
+ Group.destroy_all
263
+ g1 = Group.create! :created_at => now - 1
264
+ g2 = Group.create! :created_at => now
265
+ g3 = Group.create! :created_at => now + 1
266
+ groups = Group.find( :all, :conditions=>{ :created_at_gt => now } )
267
+ assert_equal [g3], groups
268
+ end
269
+
270
+ def test_find_less_than_time
271
+ setup_time
272
+ books = Book.find( :all, :conditions=>{ :created_at_lt => Time.local(2007, 01, 01, 21, 38, 0) } )
273
+ assert_equal 1, books.size
274
+ end
275
+
276
+ def test_find_less_than_time_with_reserved_words
277
+ now = Time.now
278
+ Group.destroy_all
279
+ g1 = Group.create! :created_at => now - 1
280
+ g2 = Group.create! :created_at => now
281
+ g3 = Group.create! :created_at => now + 1
282
+ groups = Group.find( :all, :conditions=>{ :created_at_lt => now } )
283
+ assert_equal [g1], groups
284
+ end
285
+
286
+ def test_find_greater_than_or_equal_to_time
287
+ time = setup_time
288
+ books = Book.find( :all, :conditions=>{ :created_at_gte => Time.local(2007, 01, 01, 21, 38, 0) } )
289
+ assert_equal 3, books.size
290
+ end
291
+
292
+ def test_find_greater_than_or_equal_to_time_with_reserved_words
293
+ now = Time.now
294
+ Group.destroy_all
295
+ g1 = Group.create! :created_at => now - 1
296
+ g2 = Group.create! :created_at => now
297
+ g3 = Group.create! :created_at => now + 1
298
+ groups = Group.find( :all, :conditions=>{ :created_at_gte => now } )
299
+ assert_equal [g2, g3], groups
300
+ end
301
+
302
+ def test_find_less_than_or_equal_to_time
303
+ setup_time
304
+
305
+ books = Book.find( :all, :conditions=>{ :created_at_lte => Time.local(2007, 01, 01, 21, 38, 0) } )
306
+ assert_equal 2, books.size
307
+ end
308
+
309
+ def test_find_less_than_or_equal_to_time
310
+ setup_time
311
+
312
+ books = Book.find( :all, :conditions=>{ :created_at_lte => Time.local(2007, 01, 01, 21, 38, 0) } )
313
+ assert_equal 2, books.size
314
+ end
315
+
316
+ def test_find_less_than_or_equal_to_time_with_reserved_words
317
+ now = Time.now
318
+ Group.destroy_all
319
+ g1 = Group.create! :created_at => now - 1
320
+ g2 = Group.create! :created_at => now
321
+ g3 = Group.create! :created_at => now + 1
322
+ groups = Group.find( :all, :conditions=>{ :created_at_lte => now } )
323
+ assert_equal [g1, g2], groups
324
+ end
325
+
326
+ def test_find_not_equal_to_time
327
+ setup_time
328
+ books = Book.find( :all, :conditions=>{ :created_at_ne => Time.local(2007, 01, 01, 21, 38, 0) } )
329
+ assert_equal 3, books.size
330
+ end
331
+
332
+ def test_find_greater_than_or_equal_to_time_with_reserved_words
333
+ now = Time.now
334
+ Group.destroy_all
335
+ g1 = Group.create! :created_at => now - 1
336
+ g2 = Group.create! :created_at => now
337
+ g3 = Group.create! :created_at => now + 1
338
+ groups = Group.find( :all, :conditions=>{ :created_at_ne => now } )
339
+ assert_equal [g1, g3], groups
340
+ end
341
+
342
+ def test_find_not_in_array
343
+ developers = Developer.find( :all, :conditions=>{ :id_ne=>[ 9999 ] } )
344
+ assert_equal( Developer.count, developers.size )
345
+
346
+ developers = Developer.find( :all, :conditions=>{ :id_not=>[ 9999 ] } )
347
+ assert_equal( Developer.count, developers.size )
348
+
349
+ developers = Developer.find( :all, :conditions=>{ :id_not_in=>[ 9999 ] } )
350
+ assert_equal( Developer.count, developers.size )
351
+ end
352
+
353
+ def test_find_not_in_array_with_reserved_words
354
+ Group.destroy_all
355
+ g1 = Group.create! :order => "a"
356
+ g2 = Group.create! :order => "b"
357
+ g3 = Group.create! :order => "c"
358
+ groups = Group.find( :all, :conditions=>{ :order_not_in => %w(a c) } )
359
+ assert_equal [g2], groups
360
+ end
361
+
362
+ def test_find_not_in_range
363
+ developers = Developer.find( :all, :conditions=>{ :id_ne=>( 9998..9999 ) } )
364
+ assert_equal( Developer.count, developers.size )
365
+
366
+ developers = Developer.find( :all, :conditions=>{ :id_not=>( 9998..9999 ) } )
367
+ assert_equal( Developer.count, developers.size )
368
+
369
+ developers = Developer.find( :all, :conditions=>{ :id_not_in=>( 9998..9999 ) } )
370
+ assert_equal( Developer.count, developers.size )
371
+
372
+ developers = Developer.find( :all, :conditions=>{ :id_not_between=>( 9998..9999 ) } )
373
+ assert_equal( Developer.count, developers.size )
374
+ end
375
+
376
+ def test_find_not_in_range_with_reserved_words
377
+ now = Time.now
378
+ Group.destroy_all
379
+ g1 = Group.create! :created_at => now - 1
380
+ g2 = Group.create! :created_at => now
381
+ g3 = Group.create! :created_at => now + 1
382
+ groups = Group.find( :all, :conditions=>{ :created_at_not_between => (now .. now+1) } )
383
+ assert_equal [g1], groups
384
+ end
385
+
386
+ def test_find_not_matching_regex
387
+ developers = Developer.find( :all, :conditions=>{ :id_ne=>/9999/ } )
388
+ assert_equal( Developer.count, developers.size )
389
+
390
+ developers = Developer.find( :all, :conditions=>{ :id_not=>/9999/ } )
391
+ assert_equal( Developer.count, developers.size )
392
+
393
+ developers = Developer.find( :all, :conditions=>{ :id_does_not_match=>/9999/ } )
394
+ assert_equal( Developer.count, developers.size )
395
+ end
396
+
397
+ def test_find_not_matching_regex_with_reserved_words
398
+ now = Time.now
399
+ Group.destroy_all
400
+ g1 = Group.create! :order => "abc"
401
+ g2 = Group.create! :order => "def"
402
+ g3 = Group.create! :order => "abcdef"
403
+ groups = Group.find( :all, :conditions=>{ :order_ne => /abc/ } )
404
+ assert_equal [g2], groups
405
+
406
+ groups = Group.find( :all, :conditions=>{ :order_not => /def/ } )
407
+ assert_equal [g1], groups
408
+
409
+ groups = Group.find( :all, :conditions=>{ :order_does_not_match => /^a/ } )
410
+ assert_equal [g2], groups
411
+ end
412
+
413
+ def test_find_with_hash_containing_normal_and_arext_components
414
+ developers = Developer.find( :all,
415
+ :conditions=>{ :id=>1, :name_starts_with=>'Zach' } )
416
+ assert_equal( 1, developers.size )
417
+ end
418
+
419
+ def test_find_with_string_and_hash
420
+ developers = Developer.find( :all,
421
+ :conditions=>[ "name = 'Zach Dennis'", { :id=>1 } ] )
422
+ assert_equal( 1, developers.size )
423
+ end
424
+
425
+ def test_find_with_string_and_hash_where_none_match
426
+ developers = Developer.find( :all,
427
+ :conditions=>[ "id = 1", { :id=>2 } ] )
428
+ assert_equal( 0, developers.size )
429
+ end
430
+
431
+ def test_find_with_string_and_hash_where_string_uses_hash_values
432
+ developers = Developer.find( :all,
433
+ :conditions=>[ "id = :id", { :id=>1 } ] )
434
+ assert_equal( 1, developers.size )
435
+ end
436
+
437
+ def test_find_where_value_is_null
438
+ developers = Developer.find( :all,
439
+ :conditions=>{ :name=>nil } )
440
+ assert_equal( 1, developers.size )
441
+ end
442
+
443
+
444
+ def test_find_with_duck_typing_to_sql_for_an_id
445
+ search_object = Object.new
446
+ class << search_object
447
+ def to_sql( caller )
448
+ 'id=1'
449
+ end
450
+ end
451
+
452
+ developers = Developer.find( :all,
453
+ :conditions=>search_object )
454
+ assert_equal( 1, developers.size )
455
+ end
456
+
457
+ def test_find_with_duck_typing_to_sql_for_multiple_conditions
458
+ name = Object.new
459
+ class << name
460
+ def to_sql( caller )
461
+ "name='Zach Dennis'"
462
+ end
463
+ end
464
+
465
+ salary = Object.new
466
+ class << salary
467
+ def to_sql( caller )
468
+ "salary='1'"
469
+ end
470
+ end
471
+
472
+ developers = Developer.find( :all,
473
+ :conditions=>{
474
+ :name=>name,
475
+ :salary=>salary }
476
+ )
477
+ assert_equal( 1, developers.size )
478
+ end
479
+
480
+ def test_find_with_duck_typing_to_sql_for_multiple_conditions_find_nothing
481
+ name = Object.new
482
+ class << name
483
+ def to_sql( caller )
484
+ "name='Zach Dennis'"
485
+ end
486
+ end
487
+
488
+ salary = Object.new
489
+ class << salary
490
+ def to_sql( caller )
491
+ "salary='0'"
492
+ end
493
+ end
494
+
495
+ developers = Developer.find( :all,
496
+ :conditions=>{
497
+ :name=>name,
498
+ :salary=>salary }
499
+ )
500
+ assert_equal( 0, developers.size )
501
+ end
502
+
503
+ def test_find_should_not_break_proper_string_escapes
504
+ assert Book.find_or_create_by_title_and_publisher_and_author_name( "Book1%20Something", "Publisher%20", "%20Author%20" )
505
+ end
506
+
507
+ def test_find_should_not_break_boolean_searches
508
+ Book.destroy_all
509
+ book = Book.create! :title=>"Blah", :publisher=>"Del Rey", :author_name=>"Terry Brooks", :for_sale => false
510
+
511
+ record = Book.find_by_author_name_and_for_sale('Terry Brooks', false)
512
+ assert_equal book, record, "wrong record"
513
+ end
514
+
515
+ def test_find_should_not_break_with_question_marks
516
+ Book.destroy_all
517
+ book = Book.create! :title=>"Where's Waldo?", :publisher=>"Candlewick", :author_name=>"Martin Handford", :for_sale => false
518
+
519
+ record = Book.find :first, :conditions => { :title_contains => '?' }
520
+ assert_equal book, record, "wrong record"
521
+
522
+ record = Book.find :first, :conditions => { :title_contains => 'Waldo?' }
523
+ assert_equal book, record, "wrong record 2"
524
+ end
525
+
526
+ def test_find_should_not_break_with_percent_signs
527
+ Book.destroy_all
528
+ book = Book.create! :title=>"Where's % Waldo", :publisher=>"Candlewick", :author_name=>"Martin Handford", :for_sale => false
529
+
530
+ record = Book.find :first, :conditions => { :title_contains => '%' }
531
+ assert_equal book, record, "wrong record"
532
+
533
+ record = Book.find :first, :conditions => { :title_contains => '% Waldo' }
534
+ assert_equal book, record, "wrong record 2"
535
+ end
536
+
537
+ def test_find_should_not_break_with_blank_conditions
538
+ assert_equal Book.find(:first), Book.find(:first, :conditions => "")
539
+ assert_equal Book.find(:first), Book.find(:first, :conditions => [""])
540
+ assert_equal Book.find(:first), Book.find(:first, :conditions => ["",{}])
541
+ end
542
+
543
+ end