<commit_before>require 'spec_helper'

describe ProblemMerge do
  let(:problem) { Fabricate(:problem_with_errs) }
  let(:problem_1) { Fabricate(:problem_with_errs) }

  describe "#initialize" do
    it 'failed if less than 2 uniq problem pass in args' do
      expect {
        ProblemMerge.new(problem)
      }.to raise_error(ArgumentError)
    end

    it 'extract first problem like merged_problem' do
      problem_merge = ProblemMerge.new(problem, problem, problem_1)
      expect(problem_merge.merged_problem).to eql problem
    end
    it 'extract other problem like child_problems' do
      problem_merge = ProblemMerge.new(problem, problem, problem_1)
      expect(problem_merge.child_problems).to eql [problem_1]
    end
  end

  describe "#merge" do
    let!(:problem_merge) { ProblemMerge.new(problem, problem_1) }
    let(:first_errs) { problem.errs }
    let(:merged_errs) { problem_1.errs }
    let!(:notice) { Fabricate(:notice, err: first_errs.first) }
    let!(:notice_1) { Fabricate(:notice, err: merged_errs.first) }

    it 'delete one of problem' do
      expect {
        problem_merge.merge
      }.to change(Problem, :count).by(-1)
    end

    it 'move all err in one problem' do
      problem_merge.merge
      expect(problem.reload.errs.map(&:id).sort).to eq (first_errs | merged_errs).map(&:id).sort
    end

    it 'update problem cache' do
      expect(ProblemUpdaterCache).to receive(:new).with(problem).and_return(double(update: true))
      problem_merge.merge
    end

    context "with problem with comment" do
      let!(:comment) { Fabricate(:comment, err: problem.errs.first ) }
      let!(:comment_2) { Fabricate(:comment, err: problem_1.errs.first, user: comment.user ) }
      
      it 'merge comment' do
        problem.comments(true)
        problem_1.comments(true)
        
        expect {
          problem_merge.merge
        }.to change{
          problem.comments.count
        }.from(1).to(2)
        expect(comment_2.reload.problem).to eq problem
      end
    end
  end
end<commit_msg>[refactor] Rewrote the ProblemMerge spec (5m)<commit_after>require 'spec_helper'

describe ProblemMerge do
  let(:problem) { Fabricate(:problem_with_errs, opened_at: 4.weeks.ago) }
  let(:problem_1) { Fabricate(:problem_with_errs, opened_at: 5.weeks.ago) }

  describe "#initialize" do
    it "requires at least 2 unique problems" do
      expect {
        ProblemMerge.new(problem)
      }.to raise_error(ArgumentError)
    end

    it "extracts the first problem as merged_problem" do
      problem_merge = ProblemMerge.new(problem, problem, problem_1)
      expect(problem_merge.merged_problem).to eql problem
    end
    it "extracts the other problems as child_problems" do
      problem_merge = ProblemMerge.new(problem, problem, problem_1)
      expect(problem_merge.child_problems).to eql [problem_1]
    end
  end

  describe "#merge" do
    let!(:problem_merge) { ProblemMerge.new(problem, problem_1) }
    let(:first_errs) { problem.errs }
    let(:merged_errs) { problem_1.errs }
    let!(:notice) { Fabricate(:notice, err: first_errs.first) }
    let!(:notice_1) { Fabricate(:notice, err: merged_errs.first) }
    let!(:comment) { Fabricate(:comment, err: problem.errs.first ) }
    let!(:comment_2) { Fabricate(:comment, err: problem_1.errs.first, user: comment.user ) }

    it "deletes all but one of the problems" do
      expect {
        problem_merge.merge
      }.to change(Problem, :count).by(-1)
    end

    it "associates all errs with the remaining problem" do
      problem_merge.merge
      expect(problem.reload.errs.map(&:id).sort).to eq (first_errs | merged_errs).map(&:id).sort
    end

    it "associates all comments with the remaining problem" do
      problem.comments(true)
      problem_1.comments(true)
      
      expect {
        problem_merge.merge
      }.to change {
        problem.comments.count
      }.from(1).to(2)
      expect(comment_2.reload.problem).to eq problem
    end

    it "updates the problem's cached attributes" do
      expect(ProblemUpdaterCache).to receive(:new).with(problem).and_return(double(update: true))
      problem_merge.merge
    end
  end
end
{'fraction_non_alphanumeric': 0.10940129838903583, 'fraction_numerical': 0.00793459966338062, 'mean_word_length': 3.1683366733466936, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2350796', 'n_tokens_mistral': 1389, 'n_tokens_neox': 1308, 'n_words': 308}