Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

root/branches/2-1-caching/activerecord/test/base_test.rb

Revision 8319, 58.6 kB (checked in by david, 1 year ago)

Fixed that the truncation of strings longer than 50 chars should use inspect so newlines etc are escaped (closes #10385) [norbert]

  • Property svn:executable set to *
Line 
1 require 'abstract_unit'
2 require 'fixtures/topic'
3 require 'fixtures/reply'
4 require 'fixtures/company'
5 require 'fixtures/customer'
6 require 'fixtures/developer'
7 require 'fixtures/project'
8 require 'fixtures/default'
9 require 'fixtures/auto_id'
10 require 'fixtures/column_name'
11 require 'fixtures/subscriber'
12 require 'fixtures/keyboard'
13 require 'fixtures/post'
14 require 'fixtures/minimalistic'
15 require 'rexml/document'
16
17 class Category < ActiveRecord::Base; end
18 class Smarts < ActiveRecord::Base; end
19 class CreditCard < ActiveRecord::Base
20   class PinNumber < ActiveRecord::Base
21     class CvvCode < ActiveRecord::Base; end
22     class SubCvvCode < CvvCode; end
23   end
24   class SubPinNumber < PinNumber; end
25   class Brand < Category; end
26 end
27 class MasterCreditCard < ActiveRecord::Base; end
28 class Post < ActiveRecord::Base; end
29 class Computer < ActiveRecord::Base; end
30 class NonExistentTable < ActiveRecord::Base; end
31 class TestOracleDefault < ActiveRecord::Base; end
32
33 class LoosePerson < ActiveRecord::Base
34   self.table_name = 'people'
35   self.abstract_class = true
36   attr_protected :credit_rating, :administrator
37 end
38
39 class LooseDescendant < LoosePerson
40   attr_protected :phone_number
41 end
42
43 class LooseDescendantSecond< LoosePerson
44   attr_protected :phone_number
45   attr_protected :name
46 end
47
48 class TightPerson < ActiveRecord::Base
49   self.table_name = 'people'
50   attr_accessible :name, :address
51 end
52
53 class TightDescendant < TightPerson
54   attr_accessible :phone_number
55 end
56
57 class ReadonlyTitlePost < Post
58   attr_readonly :title
59 end
60
61 class Booleantest < ActiveRecord::Base; end
62
63 class Task < ActiveRecord::Base
64   attr_protected :starting
65 end
66
67 class TopicWithProtectedContentAndAccessibleAuthorName < ActiveRecord::Base
68   self.table_name = 'topics'
69   attr_accessible :author_name
70   attr_protected  :content
71 end
72
73 class BasicsTest < Test::Unit::TestCase
74   fixtures :topics, :companies, :developers, :projects, :computers, :accounts, :minimalistics
75
76   def test_table_exists
77     assert !NonExistentTable.table_exists?
78     assert Topic.table_exists?
79   end
80  
81   def test_set_attributes
82     topic = Topic.find(1)
83     topic.attributes = { "title" => "Budget", "author_name" => "Jason" }
84     topic.save
85     assert_equal("Budget", topic.title)
86     assert_equal("Jason", topic.author_name)
87     assert_equal(topics(:first).author_email_address, Topic.find(1).author_email_address)
88   end
89
90   def test_integers_as_nil
91     test = AutoId.create('value' => '')
92     assert_nil AutoId.find(test.id).value
93   end
94  
95   def test_set_attributes_with_block
96     topic = Topic.new do |t|
97       t.title       = "Budget"
98       t.author_name = "Jason"
99     end
100
101     assert_equal("Budget", topic.title)
102     assert_equal("Jason", topic.author_name)
103   end
104  
105   def test_respond_to?
106     topic = Topic.find(1)
107     assert topic.respond_to?("title")
108     assert topic.respond_to?("title?")
109     assert topic.respond_to?("title=")
110     assert topic.respond_to?(:title)
111     assert topic.respond_to?(:title?)
112     assert topic.respond_to?(:title=)
113     assert topic.respond_to?("author_name")
114     assert topic.respond_to?("attribute_names")
115     assert !topic.respond_to?("nothingness")
116     assert !topic.respond_to?(:nothingness)
117   end
118  
119   def test_array_content
120     topic = Topic.new
121     topic.content = %w( one two three )
122     topic.save
123
124     assert_equal(%w( one two three ), Topic.find(topic.id).content)
125   end
126
127   def test_hash_content
128     topic = Topic.new
129     topic.content = { "one" => 1, "two" => 2 }
130     topic.save
131
132     assert_equal 2, Topic.find(topic.id).content["two"]
133    
134     topic.content["three"] = 3
135     topic.save
136
137     assert_equal 3, Topic.find(topic.id).content["three"]
138   end
139  
140   def test_update_array_content
141     topic = Topic.new
142     topic.content = %w( one two three )
143
144     topic.content.push "four"
145     assert_equal(%w( one two three four ), topic.content)
146
147     topic.save
148    
149     topic = Topic.find(topic.id)
150     topic.content << "five"
151     assert_equal(%w( one two three four five ), topic.content)
152   end
153  
154   def test_case_sensitive_attributes_hash
155     # DB2 is not case-sensitive
156     return true if current_adapter?(:DB2Adapter)
157
158     assert_equal @loaded_fixtures['computers']['workstation'].to_hash, Computer.find(:first).attributes
159   end
160
161   def test_create
162     topic = Topic.new
163     topic.title = "New Topic"
164     topic.save
165     topic_reloaded = Topic.find(topic.id)
166     assert_equal("New Topic", topic_reloaded.title)
167   end
168  
169   def test_save!
170     topic = Topic.new(:title => "New Topic")
171     assert topic.save!
172    
173     reply = Reply.new
174     assert_raise(ActiveRecord::RecordInvalid) { reply.save! }
175   end
176
177   def test_save_null_string_attributes
178     topic = Topic.find(1)
179     topic.attributes = { "title" => "null", "author_name" => "null" }
180     topic.save!
181     topic.reload
182     assert_equal("null", topic.title)
183     assert_equal("null", topic.author_name)
184   end
185
186   def test_save_nil_string_attributes
187     topic = Topic.find(1)
188     topic.title = nil
189     topic.save!
190     topic.reload
191     assert_nil topic.title
192   end
193
194   def test_save_for_record_with_only_primary_key
195     minimalistic = Minimalistic.new
196     assert_nothing_raised { minimalistic.save }
197   end
198
199   def test_save_for_record_with_only_primary_key_that_is_provided
200     assert_nothing_raised { Minimalistic.create!(:id => 2) }
201   end
202
203   def test_hashes_not_mangled
204     new_topic = { :title => "New Topic" }
205     new_topic_values = { :title => "AnotherTopic" }
206
207     topic = Topic.new(new_topic)
208     assert_equal new_topic[:title], topic.title
209
210     topic.attributes= new_topic_values
211     assert_equal new_topic_values[:title], topic.title
212   end
213  
214   def test_create_many
215     topics = Topic.create([ { "title" => "first" }, { "title" => "second" }])
216     assert_equal 2, topics.size
217     assert_equal "first", topics.first.title
218   end
219
220   def test_create_columns_not_equal_attributes
221     topic = Topic.new
222     topic.title = 'Another New Topic'
223     topic.send :write_attribute, 'does_not_exist', 'test'
224     assert_nothing_raised { topic.save }
225   end
226
227   def test_create_through_factory
228     topic = Topic.create("title" => "New Topic")
229     topicReloaded = Topic.find(topic.id)
230     assert_equal(topic, topicReloaded)
231   end
232
233   def test_update
234     topic = Topic.new
235     topic.title = "Another New Topic"
236     topic.written_on = "2003-12-12 23:23:00"
237     topic.save
238     topicReloaded = Topic.find(topic.id)
239     assert_equal("Another New Topic", topicReloaded.title)
240
241     topicReloaded.title = "Updated topic"
242     topicReloaded.save
243    
244     topicReloadedAgain = Topic.find(topic.id)
245    
246     assert_equal("Updated topic", topicReloadedAgain.title)
247   end
248
249   def test_update_columns_not_equal_attributes
250     topic = Topic.new
251     topic.title = "Still another topic"
252     topic.save
253    
254     topicReloaded = Topic.find(topic.id)
255     topicReloaded.title = "A New Topic"
256     topicReloaded.send :write_attribute, 'does_not_exist', 'test'
257     assert_nothing_raised { topicReloaded.save }
258   end
259
260   def test_update_for_record_with_only_primary_key
261     minimalistic = minimalistics(:first)
262     assert_nothing_raised { minimalistic.save }
263   end
264  
265   def test_write_attribute
266     topic = Topic.new
267     topic.send(:write_attribute, :title, "Still another topic")
268     assert_equal "Still another topic", topic.title
269
270     topic.send(:write_attribute, "title", "Still another topic: part 2")
271     assert_equal "Still another topic: part 2", topic.title
272   end
273
274   def test_read_attribute
275     topic = Topic.new
276     topic.title = "Don't change the topic"
277     assert_equal "Don't change the topic", topic.send(:read_attribute, "title")
278     assert_equal "Don't change the topic", topic["title"]
279
280     assert_equal "Don't change the topic", topic.send(:read_attribute, :title)
281     assert_equal "Don't change the topic", topic[:title]
282   end
283
284   def test_read_attribute_when_false
285     topic = topics(:first)
286     topic.approved = false
287     assert !topic.approved?, "approved should be false"
288     topic.approved = "false"
289     assert !topic.approved?, "approved should be false"
290   end
291
292   def test_read_attribute_when_true
293     topic = topics(:first)
294     topic.approved = true
295     assert topic.approved?, "approved should be true"
296     topic.approved = "true"
297     assert topic.approved?, "approved should be true"
298   end
299
300   def test_read_write_boolean_attribute
301     topic = Topic.new
302     # puts ""
303     # puts "New Topic"
304     # puts topic.inspect
305     topic.approved = "false"
306     # puts "Expecting false"
307     # puts topic.inspect
308     assert !topic.approved?, "approved should be false"
309     topic.approved = "false"
310     # puts "Expecting false"
311     # puts topic.inspect
312     assert !topic.approved?, "approved should be false"
313     topic.approved = "true"
314     # puts "Expecting true"
315     # puts topic.inspect
316     assert topic.approved?, "approved should be true"
317     topic.approved = "true"
318     # puts "Expecting true"
319     # puts topic.inspect
320     assert topic.approved?, "approved should be true"
321     # puts ""
322   end
323  
324   def test_query_attribute_string
325     [nil, "", " "].each do |value|
326       assert_equal false, Topic.new(:author_name => value).author_name?
327     end
328    
329     assert_equal true, Topic.new(:author_name => "Name").author_name?
330   end
331  
332   def test_query_attribute_number
333     [nil, 0, "0"].each do |value|
334       assert_equal false, Developer.new(:salary => value).salary?
335     end
336    
337     assert_equal true, Developer.new(:salary => 1).salary?
338     assert_equal true, Developer.new(:salary => "1").salary?
339   end
340  
341   def test_query_attribute_boolean
342     [nil, "", false, "false", "f", 0].each do |value|
343       assert_equal false, Topic.new(:approved => value).approved?
344     end
345    
346     [true, "true", "1", 1].each do |value|
347       assert_equal true, Topic.new(:approved => value).approved?
348     end
349   end
350
351   def test_query_attribute_with_custom_fields
352     object = Company.find_by_sql(<<-SQL).first
353       SELECT c1.*, c2.ruby_type as string_value, c2.rating as int_value
354         FROM companies c1, companies c2
355        WHERE c1.firm_id = c2.id
356          AND c1.id = 2
357     SQL
358
359     assert_equal "Firm", object.string_value
360     assert object.string_value?
361
362     object.string_value = "  "
363     assert !object.string_value?
364
365     assert_equal 1, object.int_value.to_i
366     assert object.int_value?
367
368     object.int_value = "0"
369     assert !object.int_value?
370   end
371
372
373   def test_reader_for_invalid_column_names
374     Topic.send(:define_read_method, "mumub-jumbo".to_sym, "mumub-jumbo", nil)
375     assert !Topic.generated_methods.include?("mumub-jumbo")
376   end
377
378   def test_non_attribute_access_and_assignment
379     topic = Topic.new
380     assert !topic.respond_to?("mumbo")
381     assert_raises(NoMethodError) { topic.mumbo }
382     assert_raises(NoMethodError) { topic.mumbo = 5 }
383   end
384
385   def test_preserving_date_objects
386     # SQL Server doesn't have a separate column type just for dates, so all are returned as time
387     return true if current_adapter?(:SQLServerAdapter)
388
389     if current_adapter?(:SybaseAdapter, :OracleAdapter)
390       # Sybase ctlib does not (yet?) support the date type; use datetime instead.
391       # Oracle treats all dates/times as Time.
392       assert_kind_of(
393         Time, Topic.find(1).last_read,
394         "The last_read attribute should be of the Time class"
395       )
396     else
397       assert_kind_of(
398         Date, Topic.find(1).last_read,
399         "The last_read attribute should be of the Date class"
400       )
401     end
402   end
403
404   def test_preserving_time_objects
405     assert_kind_of(
406       Time, Topic.find(1).bonus_time,
407       "The bonus_time attribute should be of the Time class"
408     )
409
410     assert_kind_of(
411       Time, Topic.find(1).written_on,
412       "The written_on attribute should be of the Time class"
413     )
414
415     # For adapters which support microsecond resolution.
416     if current_adapter?(:PostgreSQLAdapter)
417       assert_equal 11, Topic.find(1).written_on.sec
418       assert_equal 223300, Topic.find(1).written_on.usec
419       assert_equal 9900, Topic.find(2).written_on.usec
420     end
421   end
422  
423   def test_custom_mutator
424     topic = Topic.find(1)
425     # This mutator is protected in the class definition
426     topic.send(:approved=, true)
427     assert topic.instance_variable_get("@custom_approved")
428   end
429
430   def test_destroy
431     topic = Topic.find(1)
432     assert_equal topic, topic.destroy, 'topic.destroy did not return self'
433     assert topic.frozen?, 'topic not frozen after destroy'
434     assert_raise(ActiveRecord::RecordNotFound) { Topic.find(topic.id) }
435   end
436
437   def test_record_not_found_exception
438     assert_raises(ActiveRecord::RecordNotFound) { topicReloaded = Topic.find(99999) }
439   end
440  
441   def test_initialize_with_attributes
442     topic = Topic.new({
443       "title" => "initialized from attributes", "written_on" => "2003-12-12 23:23"
444     })
445    
446     assert_equal("initialized from attributes", topic.title)
447   end
448  
449   def test_initialize_with_invalid_attribute
450     begin
451       topic = Topic.new({ "title" => "test",
452         "last_read(1i)" => "2005", "last_read(2i)" => "2", "last_read(3i)" => "31"})
453     rescue ActiveRecord::MultiparameterAssignmentErrors => ex
454       assert_equal(1, ex.errors.size)
455       assert_equal("last_read", ex.errors[0].attribute)
456     end
457   end
458  
459   def test_load
460     topics = Topic.find(:all, :order => 'id')   
461     assert_equal(2, topics.size)
462     assert_equal(topics(:first).title, topics.first.title)
463   end
464  
465   def test_load_with_condition
466     topics = Topic.find(:all, :conditions => "author_name = 'Mary'")
467    
468     assert_equal(1, topics.size)
469     assert_equal(topics(:second).title, topics.first.title)
470   end
471
472   def test_table_name_guesses
473     classes = [Category, Smarts, CreditCard, CreditCard::PinNumber, CreditCard::PinNumber::CvvCode, CreditCard::SubPinNumber, CreditCard::Brand, MasterCreditCard]
474
475     assert_equal "topics", Topic.table_name
476
477     assert_equal "categories", Category.table_name
478     assert_equal "smarts", Smarts.table_name
479     assert_equal "credit_cards", CreditCard.table_name
480     assert_equal "credit_card_pin_numbers", CreditCard::PinNumber.table_name
481     assert_equal "credit_card_pin_number_cvv_codes", CreditCard::PinNumber::CvvCode.table_name
482     assert_equal "credit_card_pin_numbers", CreditCard::SubPinNumber.table_name
483     assert_equal "categories", CreditCard::Brand.table_name
484     assert_equal "master_credit_cards", MasterCreditCard.table_name
485
486     ActiveRecord::Base.pluralize_table_names = false
487     classes.each(&:reset_table_name)
488
489     assert_equal "category", Category.table_name
490     assert_equal "smarts", Smarts.table_name
491     assert_equal "credit_card", CreditCard.table_name
492     assert_equal "credit_card_pin_number", CreditCard::PinNumber.table_name
493     assert_equal "credit_card_pin_number_cvv_code", CreditCard::PinNumber::CvvCode.table_name
494     assert_equal "credit_card_pin_number", CreditCard::SubPinNumber.table_name
495     assert_equal "category", CreditCard::Brand.table_name
496     assert_equal "master_credit_card", MasterCreditCard.table_name
497
498     ActiveRecord::Base.pluralize_table_names = true
499     classes.each(&:reset_table_name)
500
501     ActiveRecord::Base.table_name_prefix = "test_"
502     Category.reset_table_name
503     assert_equal "test_categories", Category.table_name
504     ActiveRecord::Base.table_name_suffix = "_test"
505     Category.reset_table_name
506     assert_equal "test_categories_test", Category.table_name
507     ActiveRecord::Base.table_name_prefix = ""
508     Category.reset_table_name
509     assert_equal "categories_test", Category.table_name
510     ActiveRecord::Base.table_name_suffix = ""
511     Category.reset_table_name
512     assert_equal "categories", Category.table_name
513
514     ActiveRecord::Base.pluralize_table_names = false
515     ActiveRecord::Base.table_name_prefix = "test_"
516     Category.reset_table_name
517     assert_equal "test_category", Category.table_name
518     ActiveRecord::Base.table_name_suffix = "_test"
519     Category.reset_table_name
520     assert_equal "test_category_test", Category.