base_test.rb 20.5 KB
Newer Older
D
Initial  
David Heinemeier Hansson 已提交
1 2 3 4
require 'abstract_unit'
require 'fixtures/topic'
require 'fixtures/reply'
require 'fixtures/company'
5
require 'fixtures/project'
D
Initial  
David Heinemeier Hansson 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
require 'fixtures/default'
require 'fixtures/auto_id'
require 'fixtures/column_name'

class Category < ActiveRecord::Base; end
class Smarts < ActiveRecord::Base; end
class CreditCard < ActiveRecord::Base; end
class MasterCreditCard < ActiveRecord::Base; end

class LoosePerson < ActiveRecord::Base
  attr_protected :credit_rating, :administrator
end

class TightPerson < ActiveRecord::Base
  attr_accessible :name, :address
end

class TightDescendent < TightPerson
  attr_accessible :phone_number
end

class Booleantest < ActiveRecord::Base; end

class BasicsTest < Test::Unit::TestCase
30
  fixtures :topics, :companies, :projects
D
Initial  
David Heinemeier Hansson 已提交
31 32 33 34 35 36 37

  def test_set_attributes
    topic = Topic.find(1)
    topic.attributes = { "title" => "Budget", "author_name" => "Jason" }
    topic.save
    assert_equal("Budget", topic.title)
    assert_equal("Jason", topic.author_name)
38
    assert_equal(@topics["first"]["author_email_address"], Topic.find(1).author_email_address)
D
Initial  
David Heinemeier Hansson 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
  end
  
  def test_integers_as_nil
    Topic.update(1, "approved" => "")
    assert_nil Topic.find(1).approved
  end
  
  def test_set_attributes_with_block
    topic = Topic.new do |t|
      t.title       = "Budget"
      t.author_name = "Jason"
    end

    assert_equal("Budget", topic.title)
    assert_equal("Jason", topic.author_name)
  end
  
  def test_respond_to?
    topic = Topic.find(1)
    assert topic.respond_to?("title")
    assert topic.respond_to?("title?")
    assert topic.respond_to?("title=")
    assert topic.respond_to?(:title)
    assert topic.respond_to?(:title?)
    assert topic.respond_to?(:title=)
    assert topic.respond_to?("author_name")
    assert topic.respond_to?("attribute_names")
    assert !topic.respond_to?("nothingness")
    assert !topic.respond_to?(:nothingness)
  end
  
  def test_array_content
    topic = Topic.new
    topic.content = %w( one two three )
    topic.save

    assert_equal(%w( one two three ), Topic.find(topic.id).content)
  end

  def test_hash_content
    topic = Topic.new
    topic.content = { "one" => 1, "two" => 2 }
    topic.save

    assert_equal 2, Topic.find(topic.id).content["two"]
    
    topic.content["three"] = 3
    topic.save

    assert_equal 3, Topic.find(topic.id).content["three"]
  end
  
  def test_update_array_content
    topic = Topic.new
    topic.content = %w( one two three )

    topic.content.push "four"
    assert_equal(%w( one two three four ), topic.content)

    topic.save
    
    topic = Topic.find(topic.id)
    topic.content << "five"
    assert_equal(%w( one two three four five ), topic.content)
  end
104 105 106 107
 
  def test_attributes_hash
    assert_equal @projects["action_controller"].to_hash, @action_controller.attributes
  end
D
Initial  
David Heinemeier Hansson 已提交
108 109 110 111 112 113 114 115 116
  
  def test_create
    topic = Topic.new
    topic.title = "New Topic"
    topic.save
    id = topic.id
    topicReloaded = Topic.find(id)
    assert_equal("New Topic", topicReloaded.title)
  end
117 118 119 120 121 122
  
  def test_create_many
    topics = Topic.create([ { "title" => "first" }, { "title" => "second" }])
    assert_equal 2, topics.size
    assert_equal "first", topics.first.title
  end
123 124 125 126 127 128 129 130

  def test_create_columns_not_equal_attributes
    topic = Topic.new
    topic.title = 'Another New Topic'
    topic.send :write_attribute, 'does_not_exist', 'test'
    assert_nothing_raised { topic.save }
  end

D
Initial  
David Heinemeier Hansson 已提交
131 132 133 134 135 136 137 138 139
  def test_create_through_factory
    topic = Topic.create("title" => "New Topic")
    topicReloaded = Topic.find(topic.id)
    assert_equal(topic, topicReloaded)
  end

  def test_update
    topic = Topic.new
    topic.title = "Another New Topic"
140
    topic.written_on = "2003-12-12 23:23:00"
D
Initial  
David Heinemeier Hansson 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
    topic.save
    id = topic.id
    assert_equal(id, topic.id)
    
    topicReloaded = Topic.find(id)
    assert_equal("Another New Topic", topicReloaded.title)

    topicReloaded.title = "Updated topic"
    topicReloaded.save
    
    topicReloadedAgain = Topic.find(id)
    
    assert_equal("Updated topic", topicReloadedAgain.title)
  end

156 157 158 159 160 161 162 163 164 165 166 167
  def test_update_columns_not_equal_attributes
    topic = Topic.new
    topic.title = "Still another topic"
    topic.save
    id = topic.id
    
    topicReloaded = Topic.find(id)
    topicReloaded.title = "A New Topic"
    topicReloaded.send :write_attribute, 'does_not_exist', 'test'
    assert_nothing_raised { topicReloaded.save }
  end

D
Initial  
David Heinemeier Hansson 已提交
168 169 170 171 172 173 174 175 176 177
  def test_preserving_date_objects
    # SQL Server doesn't have a separate column type just for dates, so all are returned as time
    if ActiveRecord::ConnectionAdapters.const_defined? :SQLServerAdapter
      return true if ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters::SQLServerAdapter)
    end

    assert_kind_of(
      Date, Topic.find(1).last_read, 
      "The last_read attribute should be of the Date class"
    )
178 179 180 181 182

    assert_kind_of(
      Time, Topic.find(1).bonus_time,
      "The bonus_time attribute should be of the Time class"
    )
D
Initial  
David Heinemeier Hansson 已提交
183 184 185 186 187 188 189 190 191 192 193 194
  end

  def test_preserving_time_objects
    assert_kind_of(
      Time, Topic.find(1).written_on,
      "The written_on attribute should be of the Time class"
    )
  end
  
  def test_destroy
    topic = Topic.new
    topic.title = "Yet Another New Topic"
195
    topic.written_on = "2003-12-12 23:23:00"
D
Initial  
David Heinemeier Hansson 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
    topic.save
    id = topic.id
    topic.destroy
    
    assert_raises(ActiveRecord::RecordNotFound) { topicReloaded = Topic.find(id) }
  end
  
  def test_record_not_found_exception
    assert_raises(ActiveRecord::RecordNotFound) { topicReloaded = Topic.find(id) }
  end
  
  def test_initialize_with_attributes
    topic = Topic.new({ 
      "title" => "initialized from attributes", "written_on" => "2003-12-12 23:23"
    })
    
    assert_equal("initialized from attributes", topic.title)
  end
  
  def test_load
    topics = Topic.find_all nil, "id"    
    assert_equal(2, topics.size)
218
    assert_equal(@topics["first"]["title"], topics.first.title)
D
Initial  
David Heinemeier Hansson 已提交
219 220 221 222 223 224
  end
  
  def test_load_with_condition
    topics = Topic.find_all "author_name = 'Mary'"
    
    assert_equal(1, topics.size)
225
    assert_equal(@topics["second"]["title"], topics.first.title)
D
Initial  
David Heinemeier Hansson 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
  end

  def test_table_name_guesses
    assert_equal "topics", Topic.table_name
    
    assert_equal "categories", Category.table_name
    assert_equal "smarts", Smarts.table_name
    assert_equal "credit_cards", CreditCard.table_name
    assert_equal "master_credit_cards", MasterCreditCard.table_name

    ActiveRecord::Base.pluralize_table_names = false
    assert_equal "category", Category.table_name
    assert_equal "smarts", Smarts.table_name
    assert_equal "credit_card", CreditCard.table_name
    assert_equal "master_credit_card", MasterCreditCard.table_name
    ActiveRecord::Base.pluralize_table_names = true

    ActiveRecord::Base.table_name_prefix = "test_"
    assert_equal "test_categories", Category.table_name
    ActiveRecord::Base.table_name_suffix = "_test"
    assert_equal "test_categories_test", Category.table_name
    ActiveRecord::Base.table_name_prefix = ""
    assert_equal "categories_test", Category.table_name
    ActiveRecord::Base.table_name_suffix = ""
    assert_equal "categories", Category.table_name

    ActiveRecord::Base.pluralize_table_names = false
    ActiveRecord::Base.table_name_prefix = "test_"
    assert_equal "test_category", Category.table_name
    ActiveRecord::Base.table_name_suffix = "_test"
    assert_equal "test_category_test", Category.table_name
    ActiveRecord::Base.table_name_prefix = ""
    assert_equal "category_test", Category.table_name
    ActiveRecord::Base.table_name_suffix = ""
    assert_equal "category", Category.table_name
    ActiveRecord::Base.pluralize_table_names = true
  end
  
  def test_destroy_all
265
    assert_equal 2, Topic.find_all.size
D
Initial  
David Heinemeier Hansson 已提交
266 267

    Topic.destroy_all "author_name = 'Mary'"
268
    assert_equal 1, Topic.find_all.size
D
Initial  
David Heinemeier Hansson 已提交
269
  end
270 271 272 273 274 275 276 277 278 279 280

  def test_destroy_many
    Client.destroy([2, 3])
    assert_equal 0, Client.count
  end

  def test_delete_many
    Topic.delete([1, 2])
    assert_equal 0, Topic.count
  end

D
Initial  
David Heinemeier Hansson 已提交
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
  def test_boolean_attributes
    assert ! Topic.find(1).approved?
    assert Topic.find(2).approved?
  end
  
  def test_increment_counter
    Topic.increment_counter("replies_count", 1)
    assert_equal 1, Topic.find(1).replies_count

    Topic.increment_counter("replies_count", 1)
    assert_equal 2, Topic.find(1).replies_count
  end
  
  def test_decrement_counter
    Topic.decrement_counter("replies_count", 2)
    assert_equal 1, Topic.find(2).replies_count

    Topic.decrement_counter("replies_count", 2)
    assert_equal 0, Topic.find(1).replies_count
  end
  
  def test_update_all
303
    assert_equal 2, Topic.update_all("content = 'bulk updated!'")
D
Initial  
David Heinemeier Hansson 已提交
304 305
    assert_equal "bulk updated!", Topic.find(1).content
    assert_equal "bulk updated!", Topic.find(2).content
306 307 308
    assert_equal 2, Topic.update_all(['content = ?', 'bulk updated again!']);
    assert_equal "bulk updated again!", Topic.find(1).content
    assert_equal "bulk updated again!", Topic.find(2).content
D
Initial  
David Heinemeier Hansson 已提交
309
  end
310

311 312 313
  def test_update_many
    topic_data = { "1" => { "content" => "1 updated" }, "2" => { "content" => "2 updated" } }
    updated = Topic.update(topic_data.keys, topic_data.values)
314 315 316 317 318 319

    assert_equal 2, updated.size
    assert_equal "1 updated", Topic.find(1).content
    assert_equal "2 updated", Topic.find(2).content
  end

320 321 322 323
  def test_delete_all
    assert_equal 2, Topic.delete_all
  end

D
Initial  
David Heinemeier Hansson 已提交
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
  def test_update_by_condition
    Topic.update_all "content = 'bulk updated!'", "approved = 1"
    assert_equal "Have a nice day", Topic.find(1).content
    assert_equal "bulk updated!", Topic.find(2).content
  end
    
  def test_attribute_present
    t = Topic.new
    t.title = "hello there!"
    t.written_on = Time.now
    assert t.attribute_present?("title")
    assert t.attribute_present?("written_on")
    assert !t.attribute_present?("content")
  end
  
  def test_attribute_keys_on_new_instance
    t = Topic.new
    assert_equal nil, t.title, "The topics table has a title column, so it should be nil"
    assert_raises(NoMethodError) { t.title2 }
  end
  
  def test_class_name
    assert_equal "Firm", ActiveRecord::Base.class_name("firms")
    assert_equal "Category", ActiveRecord::Base.class_name("categories")
    assert_equal "AccountHolder", ActiveRecord::Base.class_name("account_holder")

    ActiveRecord::Base.pluralize_table_names = false
    assert_equal "Firms", ActiveRecord::Base.class_name( "firms" )
    ActiveRecord::Base.pluralize_table_names = true

    ActiveRecord::Base.table_name_prefix = "test_"
    assert_equal "Firm", ActiveRecord::Base.class_name( "test_firms" )
    ActiveRecord::Base.table_name_suffix = "_tests"
    assert_equal "Firm", ActiveRecord::Base.class_name( "test_firms_tests" )
    ActiveRecord::Base.table_name_prefix = ""
    assert_equal "Firm", ActiveRecord::Base.class_name( "firms_tests" )
    ActiveRecord::Base.table_name_suffix = ""
    assert_equal "Firm", ActiveRecord::Base.class_name( "firms" )
  end
  
  def test_null_fields
    assert_nil Topic.find(1).parent_id
    assert_nil Topic.create("title" => "Hey you").parent_id
  end
  
  def test_default_values
    topic = Topic.new
    assert_equal 1, topic.approved
    assert_nil topic.written_on
373
    assert_nil topic.bonus_time
D
Initial  
David Heinemeier Hansson 已提交
374 375 376 377 378 379 380 381
    assert_nil topic.last_read
    
    topic.save

    topic = Topic.find(topic.id)
    assert_equal 1, topic.approved
    assert_nil topic.last_read
  end
382 383 384 385 386 387 388 389 390 391

  def test_utc_as_time_zone
    Topic.default_timezone = :utc
    attributes = { "bonus_time" => "5:42:00AM" }
    topic = Topic.find(1)
    topic.attributes = attributes
    assert_equal Time.utc(2000, 1, 1, 5, 42, 0), topic.bonus_time
    Topic.default_timezone = :local
  end

D
Initial  
David Heinemeier Hansson 已提交
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
  def test_default_values_on_empty_strings
    topic = Topic.new
    topic.approved  = nil
    topic.last_read = nil

    topic.save

    topic = Topic.find(topic.id)
    assert_nil topic.last_read
    assert_nil topic.approved
  end
  
  def test_equality
    assert_equal Topic.find(1), Topic.find(2).parent
  end
  
  def test_hashing
    assert_equal [ Topic.find(1) ], [ Topic.find(2).parent ] & [ Topic.find(1) ]
  end
  
  def test_destroy_new_record
    client = Client.new
    client.destroy
    assert client.frozen?
  end
  
  def test_update_attribute
    assert !Topic.find(1).approved?
    Topic.find(1).update_attribute("approved", true)
    assert Topic.find(1).approved?
422 423 424

    Topic.find(1).update_attribute(:approved, false)
    assert !Topic.find(1).approved?
D
Initial  
David Heinemeier Hansson 已提交
425 426 427 428 429 430 431 432
  end
  
  def test_mass_assignment_protection
    firm = Firm.new
    firm.attributes = { "name" => "Next Angle", "rating" => 5 }
    assert_equal 1, firm.rating
  end
  
433 434 435 436 437 438 439
  def test_mass_assignment_protection_on_defaults
    firm = Firm.new
    firm.attributes = { "id" => 5, "type" => "Client" }
    assert_nil firm.id
    assert_equal "Firm", firm[:type]
  end
  
D
Initial  
David Heinemeier Hansson 已提交
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
  def test_mass_assignment_accessible
    reply = Reply.new("title" => "hello", "content" => "world", "approved" => 0)
    reply.save
    
    assert_equal 1, reply.approved
    
    reply.approved = 0
    reply.save

    assert_equal 0, reply.approved
  end
  
  def test_mass_assignment_protection_inheritance
    assert_equal [ :credit_rating, :administrator ], LoosePerson.protected_attributes
    assert_nil TightPerson.protected_attributes
  end

  def test_multiparameter_attributes_on_date
    # SQL Server doesn't have a separate column type just for dates, so all are returned as time
    if ActiveRecord::ConnectionAdapters.const_defined? :SQLServerAdapter
      return true if ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters::SQLServerAdapter)
    end

    attributes = { "last_read(1i)" => "2004", "last_read(2i)" => "6", "last_read(3i)" => "24" }
    topic = Topic.find(1)
    topic.attributes = attributes
    assert_equal Date.new(2004, 6, 24).to_s, topic.last_read.to_s
  end

  def test_multiparameter_attributes_on_date_with_empty_date
    # SQL Server doesn't have a separate column type just for dates, so all are returned as time
    if ActiveRecord::ConnectionAdapters.const_defined? :SQLServerAdapter
      return true if ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters::SQLServerAdapter)
    end

    attributes = { "last_read(1i)" => "2004", "last_read(2i)" => "6", "last_read(3i)" => "" }
    topic = Topic.find(1)
    topic.attributes = attributes
    assert_equal Date.new(2004, 6, 1).to_s, topic.last_read.to_s
  end

  def test_multiparameter_attributes_on_date_with_all_empty
    attributes = { "last_read(1i)" => "", "last_read(2i)" => "", "last_read(3i)" => "" }
    topic = Topic.find(1)
    topic.attributes = attributes
    assert_nil topic.last_read
  end

  def test_multiparameter_attributes_on_time
    attributes = { 
      "written_on(1i)" => "2004", "written_on(2i)" => "6", "written_on(3i)" => "24", 
      "written_on(4i)" => "16", "written_on(5i)" => "24", "written_on(6i)" => "00"
    }
    topic = Topic.find(1)
    topic.attributes = attributes
    assert_equal Time.local(2004, 6, 24, 16, 24, 0), topic.written_on
  end

  def test_multiparameter_attributes_on_time_with_empty_seconds
    attributes = { 
      "written_on(1i)" => "2004", "written_on(2i)" => "6", "written_on(3i)" => "24", 
      "written_on(4i)" => "16", "written_on(5i)" => "24", "written_on(6i)" => ""
    }
    topic = Topic.find(1)
    topic.attributes = attributes
    assert_equal Time.local(2004, 6, 24, 16, 24, 0), topic.written_on
  end

508 509 510 511 512 513 514 515 516
  def test_attributes_on_dummy_time
    attributes = {
      "bonus_time" => "5:42:00AM"
    }
    topic = Topic.find(1)
    topic.attributes = attributes
    assert_equal Time.local(2000, 1, 1, 5, 42, 0), topic.bonus_time
  end

D
Initial  
David Heinemeier Hansson 已提交
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
  def test_boolean
    b_false = Booleantest.create({ "value" => false })
    false_id = b_false.id
    b_true = Booleantest.create({ "value" => true })
    true_id = b_true.id

    b_false = Booleantest.find(false_id)
    assert !b_false.value?
    b_true = Booleantest.find(true_id)
    assert b_true.value?
  end
  
  def test_clone
    topic = Topic.find(1)
    cloned_topic = topic.clone
    assert_equal topic.title, cloned_topic.title
    assert cloned_topic.new_record?

    # test if the attributes have been cloned
    topic.title = "a" 
    cloned_topic.title = "b" 
    assert_equal "a", topic.title
    assert_equal "b", cloned_topic.title

    # test if the attribute values have been cloned
    topic.title = {"a" => "b"}
    cloned_topic = topic.clone
    cloned_topic.title["a"] = "c" 
    assert_equal "b", topic.title["a"]
  end
  
  def test_bignum
    company = Company.find(1)
    company.rating = 2147483647
    company.save
    company = Company.find(1)
    assert_equal 2147483647, company.rating
  end

  def test_default
    if Default.connection.class.name == 'ActiveRecord::ConnectionAdapters::PostgreSQLAdapter'
      default = Default.new
  
      # dates / timestampts
      time_format = "%m/%d/%Y %H:%M"
      assert_equal Time.now.strftime(time_format), default.modified_time.strftime(time_format)
      assert_equal Date.today, default.modified_date
  
      # fixed dates / times
      assert_equal Date.new(2004, 1, 1), default.fixed_date
      assert_equal Time.local(2004, 1,1,0,0,0,0), default.fixed_time
  
      # char types
      assert_equal 'Y', default.char1
      assert_equal 'a varchar field', default.char2
      assert_equal 'a text field', default.char3
    end
  end

  def test_auto_id
    auto = AutoId.new
    auto.save
    assert (auto.id > 0)
  end
  
  def quote_column_name(name)
    "<#{name}>"
  end

  def test_quote_keys
    ar = AutoId.new
    source = {"foo" => "bar", "baz" => "quux"}
    actual = ar.send(:quote_columns, self, source)
    inverted = actual.invert
    assert_equal("<foo>", inverted["bar"])
    assert_equal("<baz>", inverted["quux"])
  end

  def test_column_name_properly_quoted
    col_record = ColumnName.new
    col_record.references = 40
    col_record.save
    col_record.references = 41
    col_record.save
    c2 = ColumnName.find(col_record.id)
    assert_equal(41, c2.references)
  end

  MyObject = Struct.new :attribute1, :attribute2
  
  def test_serialized_attribute
    myobj = MyObject.new('value1', 'value2')
    topic = Topic.create("content" => myobj)  
    Topic.serialize("content", MyObject)
    assert_equal(myobj, topic.content)
  end

  def test_serialized_attribute_with_class_constraint
    myobj = MyObject.new('value1', 'value2')
    topic = Topic.create("content" => myobj)
    Topic.serialize(:content, Hash)

    assert_raises(ActiveRecord::SerializationTypeMismatch) { Topic.find(topic.id).content }

    settings = { "color" => "blue" }
    Topic.find(topic.id).update_attribute("content", settings)
    assert_equal(settings, Topic.find(topic.id).content)
    Topic.serialize(:content)
  end

  def test_quote
    content = "\\ \001 ' \n \\n \""
    topic = Topic.create('content' => content)
    assert_equal content, Topic.find(topic.id).content
  end
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
  
  def test_class_level_destroy
    should_be_destroyed_reply = Reply.create("title" => "hello", "content" => "world")
    @first.replies << should_be_destroyed_reply

    Topic.destroy(1)
    assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1) }
    assert_raises(ActiveRecord::RecordNotFound) { Reply.find(should_be_destroyed_reply.id) }
  end

  def test_class_level_delete
    should_be_destroyed_reply = Reply.create("title" => "hello", "content" => "world")
    @first.replies << should_be_destroyed_reply

    Topic.delete(1)
    assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1) }
    assert_nothing_raised { Reply.find(should_be_destroyed_reply.id) }
  end
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681

  def test_increment_attribute
    assert_equal 0, @topics["first"].find.replies_count
    @topics["first"].find.increment! :replies_count
    assert_equal 1, @topics["first"].find.replies_count
    
    @topics["first"].find.increment(:replies_count).increment!(:replies_count)
    assert_equal 3, @topics["first"].find.replies_count
  end
  
  def test_increment_nil_attribute
    assert_nil @topics["first"].find.parent_id
    @topics["first"].find.increment! :parent_id
    assert_equal 1, @topics["first"].find.parent_id
  end
  
  def test_decrement_attribute
    @topics["first"].find.increment(:replies_count).increment!(:replies_count)
    assert_equal 2, @topics["first"].find.replies_count
    
    @topics["first"].find.decrement!(:replies_count)
    assert_equal 1, @topics["first"].find.replies_count

    @topics["first"].find.decrement(:replies_count).decrement!(:replies_count)
    assert_equal -1, @topics["first"].find.replies_count
  end
  
  def test_toggle_attribute
    assert !@topics["first"].find.approved?
    @topics["first"].find.toggle!(:approved)
    assert @topics["first"].find.approved?
  end
682 683 684 685 686 687 688 689 690 691

  def test_reload
    t1 = Topic.find(1)
    t2 = Topic.find(1)
    t1.title = "something else"
    t1.save
    t2.reload
    assert_equal t1.title, t2.title
  end
end