<commit_before>require 'test_helper'
require 'rake'

class LocalTransactionsRakeTest < ActiveSupport::TestCase

  def task_path
    "lib/tasks/local_transactions"
  end

  setup do
    @rake = Rake::Application.new
    Rake.application = @rake
    Rake.application.rake_require(task_path, [Rails.root.to_s], loaded_files_excluding_current_rake_file)
    Rake::Task.define_task(:environment)
  end

  def loaded_files_excluding_current_rake_file
    $".reject {|file| file == Rails.root.join("#{task_path}.rake").to_s }
  end

  context "local_transactions:fetch" do
    setup do
      @task_name = "local_transactions:fetch"
      @rake["local_transactions:import"].clear

      @saved_file = Rails.root.join('data', 'test_local_interactions.csv')
    end

    should "download the CSV file and write to disk" do
      stub_request(:get, "http://local.direct.gov.uk/Data/local_authority_service_details.csv").
        to_return(:status => 200, :body => "Example CSV Content")

      ENV['FILENAME'] = @saved_file.to_s
      Rake::Task[@task_name].execute

      assert File.exists?(@saved_file)
      assert_equal "Example CSV Content", File.open(@saved_file).read
      assert_equal @saved_file.to_s, ENV["SOURCE"]
    end

    teardown do
      File.delete(@saved_file)
    end
  end

  context "local_transactions:import" do
    setup do
      @task_name = "local_transactions:import"
      @source_filename = 'test.csv'
    end

    should "call LocalServiceImporter and LocalInteractionImporter with the correct filenames" do
      ENV['SOURCE'] = @source_filename

      LocalServiceImporter.expects(:new).returns(stub(:run))
      LocalInteractionImporter.expects(:new).returns(stub(:run))

      File.expects(:open).with('data/local_services.csv', 'r:Windows-1252:UTF-8').returns(stub())
      File.expects(:open).with(@source_filename, 'r:Windows-1252:UTF-8').returns(stub())

      Rake::Task[@task_name].execute
    end

    should "exit when no source is provided" do
      ENV['SOURCE'] = nil

      assert_raise SystemExit do
        Rake::Task[@task_name].execute
      end
    end

  end


end<commit_msg>Tightened up local_transaction import rake task tests.<commit_after>require 'test_helper'
require 'rake'

class LocalTransactionsRakeTest < ActiveSupport::TestCase

  setup do
    @rake = Rake::Application.new
    Rake.application = @rake
    Rake.application.rake_require("lib/tasks/local_transactions", [Rails.root.to_s], [])
    Rake::Task.define_task(:environment)
  end

  context "local_transactions:fetch" do
    setup do
      @task_name = "local_transactions:fetch"

      @saved_file = Rails.root.join('data', 'test_local_interactions.csv')

      LocalServiceImporter.stubs(:new).returns(stub(:run))
      LocalInteractionImporter.stubs(:new).returns(stub(:run))
    end

    should "download the service details CSV file and write it to disk" do
      stub_request(:get, "http://local.direct.gov.uk/Data/local_authority_service_details.csv").
        to_return(:status => 200, :body => "Example CSV Content")

      ENV['FILENAME'] = @saved_file.to_s

      silence_stream(STDERR) do
        @rake[@task_name].invoke
      end

      assert File.exists?(@saved_file)
      assert_equal "Example CSV Content", File.open(@saved_file).read
      assert_equal @saved_file.to_s, ENV["SOURCE"]
    end

    teardown do
      @saved_file.delete if @saved_file.exist?
    end
  end

  context "local_transactions:import" do
    setup do
      @task_name = "local_transactions:import"
      @source_filename = 'test.csv'
    end

    should "call LocalServiceImporter and LocalInteractionImporter with the correct filehandles" do
      ENV['SOURCE'] = @source_filename

      services_filehandle = stub()
      interactions_filehandle = stub()

      File.expects(:open).with('data/local_services.csv', 'r:Windows-1252:UTF-8').returns(services_filehandle)
      File.expects(:open).with(@source_filename, 'r:Windows-1252:UTF-8').returns(interactions_filehandle)

      LocalServiceImporter.expects(:new).with(services_filehandle, anything()).returns(stub(:run))
      LocalInteractionImporter.expects(:new).with(interactions_filehandle, anything()).returns(stub(:run))

      silence_stream(STDERR) do
        @rake[@task_name].invoke
      end
    end

    should "exit when no source is provided" do
      ENV['SOURCE'] = nil

      assert_raise SystemExit do
        silence_stream(STDOUT) do
          @rake[@task_name].invoke
        end
      end
    end
  end

end
{'fraction_non_alphanumeric': 0.11612903225806452, 'fraction_numerical': 0.005784204671857619, 'mean_word_length': 3.693110647181628, 'pattern_counts': {'":': 0, '<': 5, '<?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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4931934', 'n_tokens_mistral': 1454, 'n_tokens_neox': 1396, 'n_words': 266}