<commit_before>require 'spec_helper'

describe Hindsight do
  let(:document) { Document.create }
  let(:company) { Company.create }
  let(:project) { Project.create }
  let(:comment) { Comment.create }
  let(:author) { Author.create }

  describe '#new_version' do
    subject { Document.create }

    context 'setting versioned has_many associations' do
      it 'can assign a collection' do
        expect(project.new_version(:documents => [document]).documents).to contain_exactly(document)
      end

      it 'can persist a collection' do
        expect(Project.find(project.new_version(:documents => [document]).id).documents).to contain_exactly(document)
      end

      it 'creates only a single new version of the association owner' do
        expect { project.new_version(:documents => [document]) }.to change { project.versions.count }.by(1)
      end

      it 'does not affect the association on the previous version' do
        project.update_attributes!(:documents => [Document.create])
        expect { project.new_version(:documents => [Document.create]) }.not_to change { project.documents.to_a }
      end
    end

    context 'setting unversioned has_many associations' do
      it 'can assign a collection' do
        expect(document.new_version(:comments => [comment]).comments).to contain_exactly(comment)
      end

      it 'can persist a collection' do
        expect(Document.find(document.new_version(:comments => [comment]).id).comments).to contain_exactly(comment)
      end

      it 'creates only a single new version of the association owner' do
        expect { document.new_version(:comments => [comment]) }.to change { document.versions.count }.from(1).to(2)
      end

      it 'does not affect the association on the previous version' do
        document.update_attributes!(:comments => [Comment.create])
        expect { document.new_version(:comments => [Comment.create]) }.not_to change { document.comments.to_a }
      end
    end

    context 'setting versioned has_many :through associations' do
      it 'can assign a collection' do
        expect(project.new_version(:companies => [company]).companies).to contain_exactly(company)
      end

      it 'creates only a single new version of the association owner' do
        expect { project.new_version(:companies => [company]) }.to change { project.versions.count }.by(1)
      end

      it 'does not create a new version of the associated record' do
        expect { project.new_version(:companies => [company]) }.not_to change { company.versions.count }
      end

      it 'does not affect the association on the previous version' do
        project.update_attributes!(:companies => [Company.create])
        expect { project.new_version(:companies => [Company.create]) }.not_to change { project.companies.to_a }
      end
    end

    context 'setting unversioned has_many :through associations' do
      it 'can assign a collection' do
        expect(document.new_version(:authors => [author]).authors).to contain_exactly(author)
      end

      it 'creates only a single new version of the association owner' do
        expect { document.new_version(:authors => [author]) }.to change { document.versions.count }.by(1)
      end

      it 'does not affect the association on the previous version' do
        document.update_attributes!(:authors => [Author.create])
        expect { document.new_version(:authors => [Author.create]) }.not_to change { document.authors.to_a }
      end
    end
  end

  describe '#become_current' do
    subject { document }
    let(:new_version) { document.new_version(:comments => [Comment.new] ) }

    it 'updates association to those of the latest version' do
      expect { document.become_current }.to change { document.comments.to_a }.to(new_version.comments.to_a)
    end
  end

  describe "collection<<" do
    context 'on a versioned has_many association' do
      it 'does not create a new version of the record being shovelled' do
        expect { project.documents << document }.not_to change { document.versions.count }
      end
    end

    context 'on a versioned has_many through association' do
      it 'does not create a new version of the record being shovelled' do
        expect { project.companies << company }.not_to change { company.versions.count }
      end

      it 'does not create a new version of the association owner' do
        expect { project.companies << company }.not_to change { project.versions.count }<commit_msg>Expose test failure from not reloading association<commit_after>require 'spec_helper'

describe Hindsight do
  let(:document) { Document.create }
  let(:company) { Company.create }
  let(:project) { Project.create }
  let(:comment) { Comment.create }
  let(:author) { Author.create }

  describe '#new_version' do
    subject { Document.create }

    context 'setting versioned has_many associations' do
      it 'can assign a collection' do
        expect(project.new_version(:documents => [document]).documents).to contain_exactly(document)
      end

      it 'can persist a collection' do
        expect(Project.find(project.new_version(:documents => [document]).id).documents).to contain_exactly(document)
      end

      it 'creates only a single new version of the association owner' do
        expect { project.new_version(:documents => [document]) }.to change { project.versions.count }.by(1)
      end

      it 'does not affect the association on the previous version' do
        project.update_attributes!(:documents => [Document.create])
        expect { project.new_version(:documents => [Document.create]) }.not_to change { project.documents(true).to_a }
      end
    end

    context 'setting unversioned has_many associations' do
      it 'can assign a collection' do
        expect(document.new_version(:comments => [comment]).comments).to contain_exactly(comment)
      end

      it 'can persist a collection' do
        expect(Document.find(document.new_version(:comments => [comment]).id).comments).to contain_exactly(comment)
      end

      it 'creates only a single new version of the association owner' do
        expect { document.new_version(:comments => [comment]) }.to change { document.versions.count }.from(1).to(2)
      end

      it 'does not affect the association on the previous version' do
        document.update_attributes!(:comments => [Comment.create])
        expect { document.new_version(:comments => [Comment.create]) }.not_to change { document.comments(true).to_a }
      end
    end

    context 'setting versioned has_many :through associations' do
      it 'can assign a collection' do
        expect(project.new_version(:companies => [company]).companies).to contain_exactly(company)
      end

      it 'creates only a single new version of the association owner' do
        expect { project.new_version(:companies => [company]) }.to change { project.versions.count }.by(1)
      end

      it 'does not create a new version of the associated record' do
        expect { project.new_version(:companies => [company]) }.not_to change { company.versions.count }
      end

      it 'does not affect the association on the previous version' do
        project.update_attributes!(:companies => [Company.create])
        expect { project.new_version(:companies => [Company.create]) }.not_to change { project.companies(true).to_a }
      end
    end

    context 'setting unversioned has_many :through associations' do
      it 'can assign a collection' do
        expect(document.new_version(:authors => [author]).authors).to contain_exactly(author)
      end

      it 'creates only a single new version of the association owner' do
        expect { document.new_version(:authors => [author]) }.to change { document.versions.count }.by(1)
      end

      it 'does not affect the association on the previous version' do
        document.update_attributes!(:authors => [Author.create])
        expect { document.new_version(:authors => [Author.create]) }.not_to change { document.authors(true).to_a }
      end
    end
  end

  describe '#become_current' do
    subject { document }
    let(:new_version) { document.new_version(:comments => [Comment.new] ) }

    it 'updates association to those of the latest version' do
      expect { document.become_current }.to change { document.comments.to_a }.to(new_version.comments(true).to_a)
    end
  end

  describe "collection<<" do
    context 'on a versioned has_many association' do
      it 'does not create a new version of the record being shovelled' do
        expect { project.documents << document }.not_to change { document.versions.count }
      end
    end

    context 'on a versioned has_many through association' do
      it 'does not create a new version of the record being shovelled' do
        expect { project.companies << company }.not_to change { company.versions.count }
      end

      it 'does not create a new version of the association owner' do
        expect { project.companies << company }.not_to change { project.versions.count }
{'fraction_non_alphanumeric': 0.09941455870981995, 'fraction_numerical': 0.0011046062078868884, 'mean_word_length': 3.232819074333801, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 43, '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': '2483563', 'n_tokens_mistral': 2509, 'n_tokens_neox': 2410, 'n_words': 792}