<commit_before>require 'pavlov_helper'
require_relative '../../../../app/interactors/interactors/facts/create.rb'

describe Interactors::Facts::Create do
  include PavlovSupport

  it '.new' do
    interactor = Interactors::Facts::Create.new 'displaystring','url','title', mock, current_user: mock
    interactor.should_not be_nil
  end

  describe '.validate' do
    it 'calls the correct validation methods' do
      displaystring = 'displaystring'
      url = 'url'
      title = 'title'
      sharing_options = mock

      described_class.any_instance.should_receive(:validate_nonempty_string)
                                  .with(:displaystring, displaystring)
      described_class.any_instance.should_receive(:validate_string)
                                  .with(:url, url)
      described_class.any_instance.should_receive(:validate_string)
                                  .with(:title, title)
      described_class.any_instance.should_receive(:validate_not_nil)
                                  .with(:sharing_options, sharing_options)

      described_class.new displaystring, url, title, sharing_options, current_user: mock
    end
  end

  it '.authorized raises when not logged in' do
    expect{ Interactors::Facts::Create.new 'displaystring', 'url', 'title', mock, current_user: nil }.
      to raise_error Pavlov::AccessDenied, "Unauthorized"
  end

  describe '#call' do
    before do
      stub_classes 'Fact'
    end

    it 'correctly when site doesn''t exist' do
      url = 'www.fmf.nl'
      displaystring = 'this is the annotated text'
      title = 'this is the title'
      site = mock
      fact_data = mock(persisted?: true)
      fact = mock(id: mock(to_i: mock), errors: [], data: fact_data)
      user = mock(id: mock)
      sharing_options = mock

      pavlov_options = {current_user: user, ability: mock(can?: true)}
      interactor = Interactors::Facts::Create.new displaystring, url, title, sharing_options, pavlov_options

      interactor.should_receive(:query).with(:'sites/for_url', url).and_return(nil)
      interactor.should_receive(:command).with(:'sites/create', url).and_return(site)
      interactor.should_receive(:command).with(:'facts/create', displaystring, title, user, site).and_return(fact)
      interactor.should_receive(:command).with(:'facts/share_new', fact.id.to_s, sharing_options)
      interactor.should_receive(:command).with(:'facts/add_to_recently_viewed', fact.id.to_i, user.id.to_s)

      expect(interactor.call).to eq fact
    end

    it 'correctly when site exists' do
      url = 'www.fmf.nl'
      displaystring = 'this is the annotated text'
      title = 'this is the title'
      site = mock
      fact_data = mock(persisted?: true)
      fact = mock(id: mock(to_i: mock), errors: [], data: fact_data)
      user = mock(id: mock)
      sharing_options = mock

      pavlov_options = {current_user: user, ability: mock(can?: true)}
      interactor = Interactors::Facts::Create.new displaystring, url, title, sharing_options, pavlov_options

      interactor.should_receive(:query).with(:'sites/for_url',url).and_return(site)
      interactor.should_receive(:command).with(:'facts/create', displaystring, title, user, site).and_return(fact)
      interactor.should_receive(:command).with(:'facts/share_new', fact.id.to_s, sharing_options)
      interactor.should_receive(:command).with(:'facts/add_to_recently_viewed', fact.id.to_i, user.id.to_s)

      expect(interactor.call).to eq fact
    end
  end
end
<commit_msg>facts/create using more of the new style<commit_after>require 'pavlov_helper'
require_relative '../../../../app/interactors/interactors/facts/create.rb'

describe Interactors::Facts::Create do
  include PavlovSupport

  describe '.validate' do
    it 'calls the correct validation methods' do
      displaystring = 'displaystring'
      url = 'url'
      title = 'title'
      sharing_options = mock

      described_class.any_instance.should_receive(:validate_nonempty_string)
                                  .with(:displaystring, displaystring)
      described_class.any_instance.should_receive(:validate_string)
                                  .with(:url, url)
      described_class.any_instance.should_receive(:validate_string)
                                  .with(:title, title)
      described_class.any_instance.should_receive(:validate_not_nil)
                                  .with(:sharing_options, sharing_options)

      described_class.new displaystring, url, title, sharing_options, current_user: mock
    end
  end

  it '.authorized raises when not logged in' do
    expect{ described_class.new 'displaystring', 'url', 'title', mock, current_user: nil }.
      to raise_error Pavlov::AccessDenied, "Unauthorized"
  end

  describe '#call' do
    before do
      stub_classes 'Fact'
    end

    it 'correctly when site doesn''t exist' do
      url = 'www.fmf.nl'
      displaystring = 'this is the annotated text'
      title = 'this is the title'
      site = mock
      fact_data = mock(persisted?: true)
      fact = mock(id: mock(to_i: mock), errors: [], data: fact_data)
      user = mock(id: mock)
      sharing_options = mock

      pavlov_options = {current_user: user, ability: mock(can?: true)}
      interactor = described_class.new displaystring, url, title, sharing_options, pavlov_options

      Pavlov.stub(:query)
            .with(:'sites/for_url', url, pavlov_options)
            .and_return(nil)

      Pavlov.should_receive(:command)
            .with(:'sites/create', url, pavlov_options)
            .and_return(site)

      Pavlov.should_receive(:command)
            .with(:'facts/create', displaystring, title, user, site, pavlov_options)
            .and_return(fact)

      Pavlov.should_receive(:command)
            .with(:'facts/share_new', fact.id.to_s, sharing_options, pavlov_options)

      Pavlov.should_receive(:command)
            .with(:'facts/add_to_recently_viewed', fact.id.to_i, user.id.to_s, pavlov_options)

      expect(interactor.call).to eq fact
    end

    it 'correctly when site exists' do
      url = 'www.fmf.nl'
      displaystring = 'this is the annotated text'
      title = 'this is the title'
      site = mock
      fact_data = mock(persisted?: true)
      fact = mock(id: mock(to_i: mock), errors: [], data: fact_data)
      user = mock(id: mock)
      sharing_options = mock

      pavlov_options = {current_user: user, ability: mock(can?: true)}
      interactor = described_class.new displaystring, url, title, sharing_options, pavlov_options

      Pavlov.stub(:query)
            .with(:'sites/for_url',url, pavlov_options)
            .and_return(site)

      Pavlov.should_receive(:command)
            .with(:'facts/create', displaystring, title, user, site, pavlov_options)
            .and_return(fact)

      Pavlov.should_receive(:command)
            .with(:'facts/share_new', fact.id.to_s, sharing_options, pavlov_options)

      Pavlov.should_receive(:command)
            .with(:'facts/add_to_recently_viewed', fact.id.to_i, user.id.to_s, pavlov_options)


      expect(interactor.call).to eq fact
    end
  end
end

{'fraction_non_alphanumeric': 0.11214035087719298, 'fraction_numerical': 0.0, 'mean_word_length': 2.967706013363029, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2401650', 'n_tokens_mistral': 2233, 'n_tokens_neox': 2122, 'n_words': 467}