<commit_before>require "spec_helper"

describe MiqRequest do
  context "CONSTANTS" do
    it "REQUEST_TYPES" do
      expected_request_types = {
        :MiqProvisionRequest             => {:template            => "VM Provision", :clone_to_vm => "VM Clone", :clone_to_template => "VM Publish"},
        :MiqProvisionRequestTemplate     => {:template            => "VM Provision Template"},
        :MiqHostProvisionRequest         => {:host_pxe_install    => "Host Provision"},
        :VmReconfigureRequest            => {:vm_reconfigure      => "VM Reconfigure"},
        :VmMigrateRequest                => {:vm_migrate          => "VM Migrate"},
        :AutomationRequest               => {:automation          => "Automation"},
        :ServiceTemplateProvisionRequest => {:clone_to_service    => "Service Provision"},
        :ServiceReconfigureRequest       => {:service_reconfigure => "Service Reconfigure"},
      }

      expect(described_class::REQUEST_TYPES).to eq(expected_request_types)
    end
  end

  context "Class Methods" do
    let(:fred)   { FactoryGirl.create(:user, :name => '<NAME>', :userid => 'fred') }
    let(:barney) { FactoryGirl.create(:user, :name => '<NAME>',   :userid => 'barney') }

    before do
      @requests_for_fred   = [FactoryGirl.create(:miq_request, :requester => fred), FactoryGirl.create(:miq_request, :requester => fred)]
      @requests_for_barney = [FactoryGirl.create(:miq_request, :requester => barney)]
    end

    it "#requests_for_userid" do
      expect(MiqRequest.requests_for_userid(barney.userid)).to match_array(@requests_for_barney)
      expect(MiqRequest.requests_for_userid(fred.userid)).to   match_array(@requests_for_fred)
    end

    it "#all_requesters" do
      expected_hash = [fred, barney].each_with_object({}) { |user, hash| hash[user.id] = user.name }

      expect(MiqRequest.all_requesters).to eq(expected_hash)

      expected_hash[barney.id] = "#{barney.name} (no longer exists)"
      barney.destroy

      expect(MiqRequest.all_requesters).to eq(expected_hash)

      old_name = expected_hash[fred.id] = fred.name
      fred.update_attributes(:name => "<NAME>, Sr.")

      expect(MiqRequest.all_requesters).to eq(expected_hash)

      fred.update_attributes(:name => old_name)

      expected_hash[fred.id] = "#{fred.name} (no longer exists)"
      fred.destroy

      expect(MiqRequest.all_requesters).to eq(expected_hash)
    end
  end

  context "A new request" do
    let(:event_name) { "hello" }
    let(:fred)       { FactoryGirl.create(:user, :name => '<NAME>',  :userid => 'fred') }<commit_msg>No need to define user factories multiple times<commit_after>require "spec_helper"

describe MiqRequest do
  let(:fred)   { FactoryGirl.create(:user, :name => '<NAME>', :userid => 'fred') }
  let(:barney) { FactoryGirl.create(:user, :name => '<NAME>',   :userid => 'barney') }

  context "CONSTANTS" do
    it "REQUEST_TYPES" do
      expected_request_types = {
        :MiqProvisionRequest             => {:template            => "VM Provision", :clone_to_vm => "VM Clone", :clone_to_template => "VM Publish"},
        :MiqProvisionRequestTemplate     => {:template            => "VM Provision Template"},
        :MiqHostProvisionRequest         => {:host_pxe_install    => "Host Provision"},
        :VmReconfigureRequest            => {:vm_reconfigure      => "VM Reconfigure"},
        :VmMigrateRequest                => {:vm_migrate          => "VM Migrate"},
        :AutomationRequest               => {:automation          => "Automation"},
        :ServiceTemplateProvisionRequest => {:clone_to_service    => "Service Provision"},
        :ServiceReconfigureRequest       => {:service_reconfigure => "Service Reconfigure"},
      }

      expect(described_class::REQUEST_TYPES).to eq(expected_request_types)
    end
  end

  context "Class Methods" do
    before do
      @requests_for_fred   = [FactoryGirl.create(:miq_request, :requester => fred), FactoryGirl.create(:miq_request, :requester => fred)]
      @requests_for_barney = [FactoryGirl.create(:miq_request, :requester => barney)]
    end

    it "#requests_for_userid" do
      expect(MiqRequest.requests_for_userid(barney.userid)).to match_array(@requests_for_barney)
      expect(MiqRequest.requests_for_userid(fred.userid)).to   match_array(@requests_for_fred)
    end

    it "#all_requesters" do
      expected_hash = [fred, barney].each_with_object({}) { |user, hash| hash[user.id] = user.name }

      expect(MiqRequest.all_requesters).to eq(expected_hash)

      expected_hash[barney.id] = "#{barney.name} (no longer exists)"
      barney.destroy

      expect(MiqRequest.all_requesters).to eq(expected_hash)

      old_name = expected_hash[fred.id] = fred.name
      fred.update_attributes(:name => "<NAME>, Sr.")

      expect(MiqRequest.all_requesters).to eq(expected_hash)

      fred.update_attributes(:name => old_name)

      expected_hash[fred.id] = "#{fred.name} (no longer exists)"
      fred.destroy

      expect(MiqRequest.all_requesters).to eq(expected_hash)
    end
  end

  context "A new request" do
    let(:event_name) { "hello" }
{'fraction_non_alphanumeric': 0.13272087568412821, 'fraction_numerical': 0.0, 'mean_word_length': 3.2149917627677103, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 66, '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': '7042192', 'n_tokens_mistral': 1638, 'n_tokens_neox': 1566, 'n_words': 291}