<commit_before>require 'cases/helper'
require 'models/developer'
require 'models/owner'
require 'models/pet'
require 'models/toy'
require 'models/car'
require 'models/task'

class TimestampTest < ActiveRecord::TestCase
  fixtures :developers, :owners, :pets, :toys, :cars, :tasks

  def setup
    @developer = Developer.first
    @previously_updated_at = @developer.updated_at
  end

  def test_saving_a_changed_record_updates_its_timestamp
    @developer.name = "<NAME>"
    @developer.save!

    assert_not_equal @previously_updated_at, @developer.updated_at
  end

  def test_saving_a_unchanged_record_doesnt_update_its_timestamp
    @developer.save!

    assert_equal @previously_updated_at, @developer.updated_at
  end

  def test_touching_a_record_updates_its_timestamp
    previous_salary = @developer.salary
    @developer.salary = previous_salary + 10000
    @developer.touch

    assert_not_equal @previously_updated_at, @developer.updated_at
    assert_equal previous_salary + 10000, @developer.salary
    assert @developer.salary_changed?, 'developer salary should have changed'
    assert @developer.changed?, 'developer should be marked as changed'
    @developer.reload
    assert_equal previous_salary, @developer.salary
  end

  def test_saving_when_record_timestamps_is_false_doesnt_update_its_timestamp
    Developer.record_timestamps = false
    @developer.name = "<NAME>"
    @developer.save!

    assert_equal @previously_updated_at, @developer.updated_at
  ensure
    Developer.record_timestamps = true
  end

  def test_touching_an_attribute_updates_timestamp
    previously_created_at = @developer.created_at
    @developer.touch(:created_at)

    assert !@developer.created_at_changed? , 'created_at should not be changed'
    assert !@developer.changed?, 'record should not be changed'
    assert_not_equal previously_created_at, @developer.created_at
    assert_not_equal @previously_updated_at, @developer.updated_at
  end

  def test_touching_an_attribute_updates_it
    task = Task.first
    previous_value = task.ending
    task.touch(:ending)
    assert_not_equal previous_value, task.ending
    assert_in_delta Time.now, task.ending, 1
  end

  def test_touching_a_record_without_timestamps_is_unexceptional
    assert_nothing_raised { Car.first.touch }
  end

  def test_saving_a_record_with_a_belongs_to_that_specifies_touching_the_parent_should_update_the_parent_updated_at
    pet   = Pet.first
    owner = pet.owner
    previously_owner_updated_at = owner.updated_at

    pet.name = "<NAME>"
    pet.save

    assert_not_equal previously_owner_updated_at, pet.owner.updated_at
  end

  def test_destroying_a_record_with_a_belongs_to_that_specifies_touching_the_parent_should_update_the_parent_updated_at
    pet   = Pet.first
    owner = pet.owner
    previously_owner_updated_at = owner.updated_at

    pet.destroy

    assert_not_equal previously_owner_updated_at, pet.owner.updated_at
  end

  def test_saving_a_record_with_a_belongs_to_that_specifies_touching_a_specific_attribute_the_parent_should_update_that_attribute
    Pet.belongs_to :owner, :touch => :happy_at

    pet   = Pet.first
    owner = pet.owner
    previously_owner_happy_at = owner.happy_at

    pet.name = "<NAME>"
    pet.save

    assert_not_equal previously_owner_happy_at, pet.owner.happy_at
  ensure
    Pet.belongs_to :owner, :touch => true
  end

  def test_touching_a_record_with_a_belongs_to_that_uses_a_counter_cache_should_update_the_parent
    Pet.belongs_to :owner, :counter_cache => :use_count, :touch => true

    pet = Pet.first
    owner = pet.owner
    owner.update_attribute(:happy_at, 3.days.ago)
    previously_owner_updated_at = owner.updated_at

    pet.name = "I'<NAME>"
    pet.save

    assert_not_equal previously_owner_updated_at, pet.owner.updated_at
  ensure
    Pet.belongs_to :owner, :touch => true
  end

  def test_touching_a_record_touches_parent_record_and_grandparent_record
    Toy.belongs_to :pet, :touch => true
    Pet.belongs_to :owner, :touch => true

    toy = Toy.first
    pet = toy.pet
    owner = pet.owner

    owner.update_attribute(:updated_at, (time = 3.days.ago))
    toy.touch
    owner.reload

    assert_not_equal time, owner.updated_at
  ensure
    Toy.belongs_to :pet
  end

  def test_timestamp_attributes_for_create
    toy = Toy.first
    assert_equal toy.send(:timestamp_attributes_for_create), [:created_at, :created_on]
  end

  def test_timestamp_attributes_for_update
    toy = Toy.first
    assert_equal toy.send(:timestamp_attributes_for_update), [:updated_at, :updated_on]
  end

  def test_all_timestamp_attributes
    toy = Toy.first
    assert_equal toy.send(:all_timestamp_attributes), [:created_at, :created_on, :updated_at, :updated_on]
  end

  def test_timestamp_attributes_for_create_in_model
    toy = Toy.first
    assert_equal toy.send(:timestamp_attributes_for_create_in_model), [:created_at]
  end
end<commit_msg>Test timestamp_attributes_for_update_in_model that was already in place<commit_after>require 'cases/helper'
require 'models/developer'
require 'models/owner'
require 'models/pet'
require 'models/toy'
require 'models/car'
require 'models/task'

class TimestampTest < ActiveRecord::TestCase
  fixtures :developers, :owners, :pets, :toys, :cars, :tasks

  def setup
    @developer = Developer.first
    @previously_updated_at = @developer.updated_at
  end

  def test_saving_a_changed_record_updates_its_timestamp
    @developer.name = "<NAME>"
    @developer.save!

    assert_not_equal @previously_updated_at, @developer.updated_at
  end

  def test_saving_a_unchanged_record_doesnt_update_its_timestamp
    @developer.save!

    assert_equal @previously_updated_at, @developer.updated_at
  end

  def test_touching_a_record_updates_its_timestamp
    previous_salary = @developer.salary
    @developer.salary = previous_salary + 10000
    @developer.touch

    assert_not_equal @previously_updated_at, @developer.updated_at
    assert_equal previous_salary + 10000, @developer.salary
    assert @developer.salary_changed?, 'developer salary should have changed'
    assert @developer.changed?, 'developer should be marked as changed'
    @developer.reload
    assert_equal previous_salary, @developer.salary
  end

  def test_saving_when_record_timestamps_is_false_doesnt_update_its_timestamp
    Developer.record_timestamps = false
    @developer.name = "<NAME>"
    @developer.save!

    assert_equal @previously_updated_at, @developer.updated_at
  ensure
    Developer.record_timestamps = true
  end

  def test_touching_an_attribute_updates_timestamp
    previously_created_at = @developer.created_at
    @developer.touch(:created_at)

    assert !@developer.created_at_changed? , 'created_at should not be changed'
    assert !@developer.changed?, 'record should not be changed'
    assert_not_equal previously_created_at, @developer.created_at
    assert_not_equal @previously_updated_at, @developer.updated_at
  end

  def test_touching_an_attribute_updates_it
    task = Task.first
    previous_value = task.ending
    task.touch(:ending)
    assert_not_equal previous_value, task.ending
    assert_in_delta Time.now, task.ending, 1
  end

  def test_touching_a_record_without_timestamps_is_unexceptional
    assert_nothing_raised { Car.first.touch }
  end

  def test_saving_a_record_with_a_belongs_to_that_specifies_touching_the_parent_should_update_the_parent_updated_at
    pet   = Pet.first
    owner = pet.owner
    previously_owner_updated_at = owner.updated_at

    pet.name = "<NAME>"
    pet.save

    assert_not_equal previously_owner_updated_at, pet.owner.updated_at
  end

  def test_destroying_a_record_with_a_belongs_to_that_specifies_touching_the_parent_should_update_the_parent_updated_at
    pet   = Pet.first
    owner = pet.owner
    previously_owner_updated_at = owner.updated_at

    pet.destroy

    assert_not_equal previously_owner_updated_at, pet.owner.updated_at
  end

  def test_saving_a_record_with_a_belongs_to_that_specifies_touching_a_specific_attribute_the_parent_should_update_that_attribute
    Pet.belongs_to :owner, :touch => :happy_at

    pet   = Pet.first
    owner = pet.owner
    previously_owner_happy_at = owner.happy_at

    pet.name = "<NAME>"
    pet.save

    assert_not_equal previously_owner_happy_at, pet.owner.happy_at
  ensure
    Pet.belongs_to :owner, :touch => true
  end

  def test_touching_a_record_with_a_belongs_to_that_uses_a_counter_cache_should_update_the_parent
    Pet.belongs_to :owner, :counter_cache => :use_count, :touch => true

    pet = Pet.first
    owner = pet.owner
    owner.update_attribute(:happy_at, 3.days.ago)
    previously_owner_updated_at = owner.updated_at

    pet.name = "<NAME>"
    pet.save

    assert_not_equal previously_owner_updated_at, pet.owner.updated_at
  ensure
    Pet.belongs_to :owner, :touch => true
  end

  def test_touching_a_record_touches_parent_record_and_grandparent_record
    Toy.belongs_to :pet, :touch => true
    Pet.belongs_to :owner, :touch => true

    toy = Toy.first
    pet = toy.pet
    owner = pet.owner

    owner.update_attribute(:updated_at, (time = 3.days.ago))
    toy.touch
    owner.reload

    assert_not_equal time, owner.updated_at
  ensure
    Toy.belongs_to :pet
  end

  def test_timestamp_attributes_for_create
    toy = Toy.first
    assert_equal toy.send(:timestamp_attributes_for_create), [:created_at, :created_on]
  end

  def test_timestamp_attributes_for_update
    toy = Toy.first
    assert_equal toy.send(:timestamp_attributes_for_update), [:updated_at, :updated_on]
  end

  def test_all_timestamp_attributes
    toy = Toy.first
    assert_equal toy.send(:all_timestamp_attributes), [:created_at, :created_on, :updated_at, :updated_on]
  end

  def test_timestamp_attributes_for_create_in_model
    toy = Toy.first
    assert_equal toy.send(:timestamp_attributes_for_create_in_model), [:created_at]
  end

  def test_timestamp_attributes_for_update_in_model
    toy = Toy.first
    assert_equal toy.send(:timestamp_attributes_for_update_in_model), [:updated_at]
  end
end
{'fraction_non_alphanumeric': 0.0696423234034074, 'fraction_numerical': 0.0025904154627876858, 'mean_word_length': 4.693703913783324, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5513689', 'n_tokens_mistral': 3440, 'n_tokens_neox': 3297, 'n_words': 589}