<commit_before>require File.expand_path(File.dirname(__FILE__) + '/spec_helper')

describe CollectiveIdea::Acts::Audited do
  
  it "should include instance methods" do
    User.new.should be_kind_of(CollectiveIdea::Acts::Audited::InstanceMethods)
  end
  
  it "should extend singleton methods" do
    User.should be_kind_of(CollectiveIdea::Acts::Audited::SingletonMethods)
  end

  it "should know which attributes are audited" do
    attrs = {'name' => nil, 'username' => nil, 'logins' => 0, 'activated' => nil}
    User.new.audited_attributes.should == attrs
  end
  
  ['created_at', 'updated_at', 'lock_version', 'id', 'password'].each do |column|
    it "should not audit #{column}" do
      User.non_audited_columns.should include(column)
    end
  end

  it "should not save non-audited columns" do
    create_user.audits.first.changes.keys.any?{|col| ['created_at', 'updated_at', 'password'].include? col}.should be_false
  end
  
  describe "on create" do
    it "should save an audit" do
      lambda {
        create_user.should have(1).audit
      }.should change { Audit.count }.by(1)
    end
    
    it "should set the action to 'create'" do
      audit = create_user.audits.first
      audit.action.should == 'create'
    end
    
    it "should store all the audited attributes" do
      audit = create_user.audits.first
      audit.changes.should == audit.auditable.audited_attributes
    end
  end
  
  describe "on update" do
    before do
      @user = create_user
    end
    
    it "should save an audit on update" do
      lambda { @user.update_attribute(:name, "Someone") }.should change { @user.audits.count }.by(1)
      lambda { @user.update_attribute(:name, "Someone else") }.should change { @user.audits.count }.by(1)
    end

    it "should not save an audit if the record is not changed" do
      lambda { @user.save! }.should_not change { Audit.count }
    end
    
    it "should set the action to 'update'" do
      @user.update_attributes :name => 'Changed'
      @user.audits.first.action.should == 'update'
    end
    
    it "should store the changed attributes" do
      @user.update_attributes :name => 'Changed'
      @user.audits.first.changes.should == {'name' => 'Changed'}
    end
    
    it "should not save an audit if the value doesn't change after type casting" do
      pending "Dirty tracking doesn't seem to account for type casting"
      @user.update_attributes! :logins => 0, :activated => true
      lambda { @user.update_attribute :logins, '0' }.should_not change { Audit.count }
      lambda { @user.update_attribute :activated, 1 }.should_not change { Audit.count }
      lambda { @user.update_attribute :activated, '1' }.should_not change { Audit.count }
    end
    
  end
  
  describe "on destroy" do
    before do
      @user = create_user
    end
    
    it "should save an audit" do
      lambda { @user.destroy }.should change { Audit.count }.by(1)
      @user.should have(2).audits
    end
    
    it "should set the action to 'destroy'" do
      @user.destroy
      @user.audits.first.action.should == 'destroy'
    end
    
    it "should not store any changes" do
      @user.destroy
      @user.audits.first.changes.should be_nil
    end
  end
  
  describe "dirty tracking" do
    before do
      @user = create_user
    end
    
    it "should not be changed when the record is saved" do
      u = User.new(:name => 'Brandon')
      u.should be_changed
      u.save
      u.should_not be_changed
    end
    
    it "should be changed when an attribute has been changed" do
      @user.name = "Bobby"
      @user.should be_changed
      @user.name_changed?.should be_true
      @user.username_changed?.should be_false
    end
    
    it "should not be changed if the value doesn't change after type casting" do
      pending "Dirty tracking doesn't seem to account for type casting"
      @user.update_attributes! :logins => 0, :activated => true
      @user.logins = '0'
      @user.should_not be_changed
    end
    
  end
  
  describe "revisions" do
    before do
      @user = create_versions
    end
    
    it "should be an Array of Users" do
      @user.revisions.should be_an_instance_of(Array)
      @user.revisions.each {|version| version.should be_an_instance_of(User) }
    end
    
    it "should have one revision for a new record" do
      create_user.revisions.size.should == 1
    end
    
    it "should have one revision for each audit" do
      @user.should have(@user.audits.size).revisions
    end

    it "should set the attributes for each revision" do
      u = User.create(:name => 'Brandon')
      u.revisions.size.should == 1
      u.revisions[0].name.should == 'Brandon'

      u.update_attribute :name, 'Foobar'
      u.revisions.size.should == 2
      u.revisions[0].name.should == 'Foobar'
      u.revisions[1].name.should == 'Brandon'
    end
    
    it "should ignore attributes that have been deleted" do
      @user.audits.last.update_attributes :changes => {:old_attribute => 'old value'}
      lambda { @user.revisions }.should_not raise_error(ActiveRecord::UnknownAttributeError)
    end

  end<commit_msg>Failing spec to show that old revisions are not properly populated<commit_after>require File.expand_path(File.dirname(__FILE__) + '/spec_helper')

describe CollectiveIdea::Acts::Audited do
  
  it "should include instance methods" do
    User.new.should be_kind_of(CollectiveIdea::Acts::Audited::InstanceMethods)
  end
  
  it "should extend singleton methods" do
    User.should be_kind_of(CollectiveIdea::Acts::Audited::SingletonMethods)
  end

  it "should know which attributes are audited" do
    attrs = {'name' => nil, 'username' => nil, 'logins' => 0, 'activated' => nil}
    User.new.audited_attributes.should == attrs
  end
  
  ['created_at', 'updated_at', 'lock_version', 'id', 'password'].each do |column|
    it "should not audit #{column}" do
      User.non_audited_columns.should include(column)
    end
  end

  it "should not save non-audited columns" do
    create_user.audits.first.changes.keys.any?{|col| ['created_at', 'updated_at', 'password'].include? col}.should be_false
  end
  
  describe "on create" do
    it "should save an audit" do
      lambda {
        create_user.should have(1).audit
      }.should change { Audit.count }.by(1)
    end
    
    it "should set the action to 'create'" do
      audit = create_user.audits.first
      audit.action.should == 'create'
    end
    
    it "should store all the audited attributes" do
      audit = create_user.audits.first
      audit.changes.should == audit.auditable.audited_attributes
    end
  end
  
  describe "on update" do
    before do
      @user = create_user
    end
    
    it "should save an audit on update" do
      lambda { @user.update_attribute(:name, "Someone") }.should change { @user.audits.count }.by(1)
      lambda { @user.update_attribute(:name, "Someone else") }.should change { @user.audits.count }.by(1)
    end

    it "should not save an audit if the record is not changed" do
      lambda { @user.save! }.should_not change { Audit.count }
    end
    
    it "should set the action to 'update'" do
      @user.update_attributes :name => 'Changed'
      @user.audits.first.action.should == 'update'
    end
    
    it "should store the changed attributes" do
      @user.update_attributes :name => 'Changed'
      @user.audits.first.changes.should == {'name' => 'Changed'}
    end
    
    it "should not save an audit if the value doesn't change after type casting" do
      pending "Dirty tracking doesn't seem to account for type casting"
      @user.update_attributes! :logins => 0, :activated => true
      lambda { @user.update_attribute :logins, '0' }.should_not change { Audit.count }
      lambda { @user.update_attribute :activated, 1 }.should_not change { Audit.count }
      lambda { @user.update_attribute :activated, '1' }.should_not change { Audit.count }
    end
    
  end
  
  describe "on destroy" do
    before do
      @user = create_user
    end
    
    it "should save an audit" do
      lambda { @user.destroy }.should change { Audit.count }.by(1)
      @user.should have(2).audits
    end
    
    it "should set the action to 'destroy'" do
      @user.destroy
      @user.audits.first.action.should == 'destroy'
    end
    
    it "should not store any changes" do
      @user.destroy
      @user.audits.first.changes.should be_nil
    end
  end
  
  describe "dirty tracking" do
    before do
      @user = create_user
    end
    
    it "should not be changed when the record is saved" do
      u = User.new(:name => 'Brandon')
      u.should be_changed
      u.save
      u.should_not be_changed
    end
    
    it "should be changed when an attribute has been changed" do
      @user.name = "Bobby"
      @user.should be_changed
      @user.name_changed?.should be_true
      @user.username_changed?.should be_false
    end
    
    it "should not be changed if the value doesn't change after type casting" do
      pending "Dirty tracking doesn't seem to account for type casting"
      @user.update_attributes! :logins => 0, :activated => true
      @user.logins = '0'
      @user.should_not be_changed
    end
    
  end
  
  describe "revisions" do
    before do
      @user = create_versions
    end
    
    it "should be an Array of Users" do
      @user.revisions.should be_an_instance_of(Array)
      @user.revisions.each {|version| version.should be_an_instance_of(User) }
    end
    
    it "should have one revision for a new record" do
      create_user.revisions.size.should == 1
    end
    
    it "should have one revision for each audit" do
      @user.should have(@user.audits.size).revisions
    end

    it "should set the attributes for each revision" do
      u = User.create(:name => 'Brandon', :username => 'brandon')
      u.update_attributes :name => 'Foobar'
      u.update_attributes :name => 'Awesome', :username => 'keepers'
      
      u.revisions.size.should == 3
      u.revisions[0].name.should == 'Awesome'
      u.revisions[0].username.should == 'keepers'
      
      u.revisions[1].name.should == 'Foobar'
      u.revisions[1].username.should == 'brandon'

      u.revisions[2].name.should == 'Brandon'
      u.revisions[2].username.should == 'brandon'
    end
    
    it "should ignore attributes that have been deleted" do
      @user.audits.last.update_attributes :changes => {:old_attribute => 'old value'}
      lambda { @user.revisions }.should_not raise_error(ActiveRecord::UnknownAttributeError)
    end

  end
{'fraction_non_alphanumeric': 0.10502846299810246, 'fraction_numerical': 0.003795066413662239, 'mean_word_length': 2.741924032658857, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '942510', 'n_tokens_mistral': 3359, 'n_tokens_neox': 3266, 'n_words': 978}