<commit_before>require 'rails_helper'

describe ModuleResult, :type => :model do
  describe 'associations' do
    it { is_expected.to have_one(:kalibro_module).dependent(:destroy) }
    it { is_expected.to have_many(:tree_metric_results).dependent(:destroy) }
    it { is_expected.to belong_to(:processing) }

    context 'metric_results' do
      describe 'getter' do
        it 'is expected to call the tree_metric_results and print a deprecation warning' do
          subject.expects(:tree_metric_results)
          subject.expects(:warn).with('DEPRECATED: `ModuleResult#metric_results` has been renamed to `ModuleResult#tree_metric_results`')

          subject.metric_results
        end
      end

      describe 'setter' do
        it 'is expected to call the tree_metric_results and print a deprecation warning' do
          value = []

          subject.expects(:tree_metric_results=).with(value)
          subject.expects(:warn).with('DEPRECATED: `ModuleResult#metric_results=` has been renamed to `ModuleResult#tree_metric_results=`')

          subject.metric_results = value
        end
      end
    end

    # Usually we do not touch the database on unit tests. But this is kind of a intricated self-relationship so it's worth the cost.
    context 'with children and parent associations' do
      let(:parent_module_result) { FactoryGirl.create(:module_result) }
      let(:child_module_result) { FactoryGirl.create(:module_result, parent: parent_module_result) }

      describe 'children' do
        it 'the parent should return the children' do
          expect(parent_module_result.children).to eq([child_module_result])
        end

        it 'should add a child' do
          another_child = FactoryGirl.create(:module_result)
          parent_module_result.children << another_child
          parent_module_result.save

          expect(parent_module_result.children).to include(another_child)
          expect(parent_module_result.children).to include(child_module_result)
        end
      end

      describe 'parent' do
        it 'should return the child' do
          expect(child_module_result.parent).to eq(parent_module_result)
        end

        it 'should set the parent' do
          another_parent = FactoryGirl.create(:module_result)
          child_module_result.parent = another_parent
          child_module_result.save

          expect(child_module_result.parent).to eq(another_parent)
        end
      end
    end
  end

  describe 'methods' do
    describe 'find_by_module_and_processing' do
      let!(:kalibro_module) { FactoryGirl.build(:kalibro_module) }
      let!(:processing) { FactoryGirl.build(:processing) }
      let!(:module_result) { FactoryGirl.build(:module_result) }

      before :each do
        name_filtered_results = Object.new
        name_filtered_results.expects(:where).
          with("kalibro_modules.granularity" => kalibro_module.granularity.to_s).
          returns([module_result])<commit_msg>Remove unecessary usage of `let!`<commit_after>require 'rails_helper'

describe ModuleResult, :type => :model do
  describe 'associations' do
    it { is_expected.to have_one(:kalibro_module).dependent(:destroy) }
    it { is_expected.to have_many(:tree_metric_results).dependent(:destroy) }
    it { is_expected.to belong_to(:processing) }

    context 'metric_results' do
      describe 'getter' do
        it 'is expected to call the tree_metric_results and print a deprecation warning' do
          subject.expects(:tree_metric_results)
          subject.expects(:warn).with('DEPRECATED: `ModuleResult#metric_results` has been renamed to `ModuleResult#tree_metric_results`')

          subject.metric_results
        end
      end

      describe 'setter' do
        it 'is expected to call the tree_metric_results and print a deprecation warning' do
          value = []

          subject.expects(:tree_metric_results=).with(value)
          subject.expects(:warn).with('DEPRECATED: `ModuleResult#metric_results=` has been renamed to `ModuleResult#tree_metric_results=`')

          subject.metric_results = value
        end
      end
    end

    # Usually we do not touch the database on unit tests. But this is kind of a intricated self-relationship so it's worth the cost.
    context 'with children and parent associations' do
      let(:parent_module_result) { FactoryGirl.create(:module_result) }
      let(:child_module_result) { FactoryGirl.create(:module_result, parent: parent_module_result) }

      describe 'children' do
        it 'the parent should return the children' do
          expect(parent_module_result.children).to eq([child_module_result])
        end

        it 'should add a child' do
          another_child = FactoryGirl.create(:module_result)
          parent_module_result.children << another_child
          parent_module_result.save

          expect(parent_module_result.children).to include(another_child)
          expect(parent_module_result.children).to include(child_module_result)
        end
      end

      describe 'parent' do
        it 'should return the child' do
          expect(child_module_result.parent).to eq(parent_module_result)
        end

        it 'should set the parent' do
          another_parent = FactoryGirl.create(:module_result)
          child_module_result.parent = another_parent
          child_module_result.save

          expect(child_module_result.parent).to eq(another_parent)
        end
      end
    end
  end

  describe 'methods' do
    describe 'find_by_module_and_processing' do
      let(:kalibro_module) { FactoryGirl.build(:kalibro_module) }
      let(:processing) { FactoryGirl.build(:processing) }
      let(:module_result) { FactoryGirl.build(:module_result) }

      before :each do
        name_filtered_results = Object.new
        name_filtered_results.expects(:where).
          with("kalibro_modules.granularity" => kalibro_module.granularity.to_s).
          returns([module_result])
{'fraction_non_alphanumeric': 0.08186545881563496, 'fraction_numerical': 0.0, 'mean_word_length': 3.1259515570934258, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '5088160', 'n_tokens_mistral': 1697, 'n_tokens_neox': 1626, 'n_words': 395}