<commit_before>require 'spec_helper'

class CalculationSet
  def call_me
    #stub, because flexmock doesn't work for new instances during constructor
    @@called=true
  end
  def self.called
    @@called
  end
end

describe CalculationSet do

  before :all do
    CalculationSet.sets.clear
    @calc_set = CalculationSet.find("electricity_and_transport")
  end

  it 'can create an instance' do
    @calc_set.calculations.should be_a ActiveSupport::OrderedHash
    @calc_set.should be_a CalculationSet
  end

  it 'can create an instance and find calcs' do
    @calc_set.calculations.should be_a ActiveSupport::OrderedHash
  end

  it "can access class sets hash" do
    CalculationSet.sets[:electricity_and_transport].should be_a CalculationSet
  end

  it "is included in class sets hash if initialised by find method" do
    CalculationSet.sets[:electricity_and_transport].should be_a CalculationSet
  end

  it "has file attribute if initialised by find method" do
    CalculationSet.sets[:electricity_and_transport].file.should eql "#{Rails.root}/config/calculations/electricity_and_transport.rb"
  end

  it "has name attribute if initialised by find method" do
    CalculationSet.sets[:electricity_and_transport].name.should eql "electricity_and_transport"
  end

  it "is included in class sets hash if initialised manually" do
    CalculationSet.new('my_set') {calculation {label :mycalc}}
    CalculationSet.sets[:my_set].should be_a CalculationSet
  end

  it "has name" do
    CalculationSet.new('my_set') {calculation {label :mycalc}}.name.should eql "my_set"
  end

  it 'can access a calculation by key' do
    @calc_set[:transport].should be_a PrototypeCalculation
  end
  
  describe "initialising from file" do

    after(:each) do
      CalculationSet.sets.clear
      delete_lock_files
    end

    it "should find config file in default Rails location using just file name" do
      CalculationSet.find_config_file("electricity").should eql "#{Rails.root}/config/calculations/electricity.rb"
    end

    it "should find config file in default Rails location using file name and extension" do
      CalculationSet.find_config_file("electricity.rb").should eql "#{Rails.root}/config/calculations/electricity.rb"
    end

    it "should find config file in other Rails location using relative path" do
      CalculationSet.find_config_file("config/electricity.rb").should eql "#{Rails.root}/config/electricity.rb"
    end

    it "should raise error if config file not found" do
      lambda{CalculationSet.find_config_file("fuel")}.should raise_error
    end

    it "should call load_set if no set exists in class hash" do
      CalculationSet.sets[:transport].should be_nil
      flexmock(AMEE::DataAbstraction::CalculationSet) do |mock|
        mock.should_receive(:load_set).once
      end
      set = CalculationSet.find('transport')
    end

    it "should not call load_set if set exists in class hash" do
      CalculationSet.sets[:transport].should be_nil
      set = CalculationSet.find('transport')
      CalculationSet.sets[:transport].should be_a CalculationSet
      flexmock(AMEE::DataAbstraction::CalculationSet) do |mock|
        mock.should_receive(:load_set).never
      end
      set = CalculationSet.find('transport')
    end
<commit_msg>Made double-load test more explicit during AC-206<commit_after>require 'spec_helper'

class CalculationSet
  def call_me
    #stub, because flexmock doesn't work for new instances during constructor
    @@called=true
  end
  def self.called
    @@called
  end
end

describe CalculationSet do

  before :all do
    CalculationSet.sets.clear
    @calc_set = CalculationSet.find("electricity_and_transport")
  end

  it 'can create an instance' do
    @calc_set.calculations.should be_a ActiveSupport::OrderedHash
    @calc_set.should be_a CalculationSet
  end

  it 'can create an instance and find calcs' do
    @calc_set.calculations.should be_a ActiveSupport::OrderedHash
  end

  it "can access class sets hash" do
    CalculationSet.sets[:electricity_and_transport].should be_a CalculationSet
  end

  it "is included in class sets hash if initialised by find method" do
    CalculationSet.sets[:electricity_and_transport].should be_a CalculationSet
  end

  it "has file attribute if initialised by find method" do
    CalculationSet.sets[:electricity_and_transport].file.should eql "#{Rails.root}/config/calculations/electricity_and_transport.rb"
  end

  it "has name attribute if initialised by find method" do
    CalculationSet.sets[:electricity_and_transport].name.should eql "electricity_and_transport"
  end

  it "is included in class sets hash if initialised manually" do
    CalculationSet.new('my_set') {calculation {label :mycalc}}
    CalculationSet.sets[:my_set].should be_a CalculationSet
  end

  it "has name" do
    CalculationSet.new('my_set') {calculation {label :mycalc}}.name.should eql "my_set"
  end

  it 'can access a calculation by key' do
    @calc_set[:transport].should be_a PrototypeCalculation
  end
  
  describe "initialising from file" do

    after(:each) do
      CalculationSet.sets.clear
      delete_lock_files
    end

    it "should find config file in default Rails location using just file name" do
      CalculationSet.find_config_file("electricity").should eql "#{Rails.root}/config/calculations/electricity.rb"
    end

    it "should find config file in default Rails location using file name and extension" do
      CalculationSet.find_config_file("electricity.rb").should eql "#{Rails.root}/config/calculations/electricity.rb"
    end

    it "should find config file in other Rails location using relative path" do
      CalculationSet.find_config_file("config/electricity.rb").should eql "#{Rails.root}/config/electricity.rb"
    end

    it "should raise error if config file not found" do
      lambda{CalculationSet.find_config_file("fuel")}.should raise_error
    end

    it "should call load_set if no set exists in class hash" do
      CalculationSet.sets[:transport].should be_nil
      flexmock(AMEE::DataAbstraction::CalculationSet) do |mock|
        mock.should_receive(:load_set).once
      end
      set = CalculationSet.find('transport')
    end

    it "should only call load_set once for the same set" do
      CalculationSet.sets[:transport].should be_nil
      CalculationSet.sets.count.should == 0
      set_a = CalculationSet.find('transport')
      CalculationSet.sets.count.should == 1
      set_b = CalculationSet.find('transport')
      CalculationSet.sets.count.should == 1
      set_a.should be(set_b)
    end

    it "should not call load_set if set exists in class hash" do
      CalculationSet.sets[:transport].should be_nil
      set = CalculationSet.find('transport')
      CalculationSet.sets[:transport].should be_a CalculationSet
      flexmock(AMEE::DataAbstraction::CalculationSet) do |mock|
        mock.should_receive(:load_set).never
      end
      set = CalculationSet.find('transport')
    end

{'fraction_non_alphanumeric': 0.07958130197877832, 'fraction_numerical': 0.0008603383997705764, 'mean_word_length': 3.881035689293212, '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': '4663682', 'n_tokens_mistral': 2208, 'n_tokens_neox': 2050, 'n_words': 598}