<commit_before>require 'rails_helper'
RSpec.describe StatisticsController, type: :controller do

  describe "GET #metric_percentage for an used metric" do
    
    before :each do
      MetricConfiguration.expects(:all).returns([FactoryGirl.build(:metric_configuration),
        FactoryGirl.build(:metric_configuration),FactoryGirl.build(:metric_configuration)])
      MetricSnapshot.expects(:where).returns([FactoryGirl.build(:metric_snapshot)])
      get :metric_percentage, metric_code: 'loc', format: :json
    end

    it {should respond_with(:success)}

    it "returns the statistics of an existing metric" do  
      expect(JSON.parse(response.body)).to eq(JSON.parse({ metric_percentage: 33.33 }.to_json))
    end
  end

  describe "GET #metric_percentage with no metric configurations in the database" do
    before :each do
      MetricConfiguration.expects(:all).returns([])
      MetricSnapshot.expects(:where).returns([])
      get :metric_percentage, metric_code: 'loc', format: :json
    end

    it {should respond_with(:success)}

    it "returns zero for empty metric configurations database" do  
      expect(JSON.parse(response.body)).to eq(JSON.parse({ metric_percentage: 0.0 }.to_json))
    end
  end

  describe "GET #metric_percentage for a metric that has not been used yet" do
    before :each do
      MetricConfiguration.expects(:all).returns([FactoryGirl.build(:metric_configuration),
        FactoryGirl.build(:metric_configuration),FactoryGirl.build(:metric_configuration)])
      MetricSnapshot.expects(:where).returns([])
      get :metric_percentage, metric_code: 'loc', format: :json
    end

    it {should respond_with(:success)}

    it "returns zero percent for metric not used yet" do  
      expect(JSON.parse(response.body)).to eq(JSON.parse({ metric_percentage: 0.0 }.to_json))
    end
  end
  
end<commit_msg>refactoring statistic test<commit_after>require 'rails_helper'
RSpec.describe StatisticsController, type: :controller do

  describe "#metric_percentage" do
    let(:all_metrics) {[FactoryGirl.build(:metric_configuration),FactoryGirl.build(:metric_configuration),
      FactoryGirl.build(:metric_configuration)]}
    let(:metric_snapshot) {[FactoryGirl.build(:metric_snapshot)]}
    let(:no_metric_configuration) {[]}
    let(:no_metric_snapshot) {[]}

    context "when metric has been used" do
      before :each do
        MetricConfiguration.expects(:all).returns(all_metrics)
        MetricSnapshot.expects(:where).returns(metric_snapshot)
        get :metric_percentage, metric_code: 'loc', format: :json
      end

      it {should respond_with(:success)}

      it "returns use percentage of the selected metric" do
        expect(JSON.parse(response.body)).to eq(JSON.parse({ metric_percentage: 33.33 }.to_json))
      end
    end

    context "when no metric configuration has been created" do
      before :each do
        MetricConfiguration.expects(:all).returns(no_metric_configuration)
        MetricSnapshot.expects(:where).returns(no_metric_snapshot)
        get :metric_percentage, metric_code: 'loc', format: :json
      end

      it {should respond_with(:success)}

      it "returns zero percentage" do
        expect(JSON.parse(response.body)).to eq(JSON.parse({ metric_percentage: 0.0 }.to_json))
      end
    end

    context "when metric was not used yet" do
      before :each do
        MetricConfiguration.expects(:all).returns(all_metrics)
        MetricSnapshot.expects(:where).returns(no_metric_snapshot)
        get :metric_percentage, metric_code: 'loc', format: :json
      end

      it {should respond_with(:success)}

      it "returns zero percentage" do
        expect(JSON.parse(response.body)).to eq(JSON.parse({ metric_percentage: 0.0 }.to_json))
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.10813676119798568, 'fraction_numerical': 0.004240657301881791, 'mean_word_length': 3.907672301690507, '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': '6978626', 'n_tokens_mistral': 1110, 'n_tokens_neox': 1059, 'n_words': 265}