<commit_before>require 'test_helper'

module Tire

  class TireTest < Test::Unit::TestCase

    context "Tire" do

      should "have the DSL methods available" do
        assert_respond_to Tire, :search
        assert_respond_to Tire, :index
        assert_respond_to Tire, :configure
      end

      context "DSL" do

        should "allow searching with a block" do
          Search::Search.expects(:new).returns( stub(:perform => true) )

          Tire.search 'dummy' do
            query 'foo'
          end
        end

        should "allow searching with a Ruby Hash" do
          payload = { :query => { :query_string => { :query => 'foo' } } }
          Search::Search.expects(:new).with('dummy', :payload => payload).returns( stub(:perform => true) )

          Tire.search 'dummy', payload
        end

        should "allow searching with a JSON string" do
          payload = '{"query":{"query_string":{"query":"foo"}}}'
          Search::Search.expects(:new).with('dummy', :payload => payload).returns( stub(:perform => true) )

          Tire.search 'dummy', payload
        end

        should "raise an error when passed incorrect payload" do
          assert_raise(ArgumentError) do
            Tire.search 'dummy', 1
          end
        end

        should "raise SearchRequestFailed when receiving bad response from backend" do
          assert_raise(Search::SearchRequestFailed) do
            Tire::Configuration.client.expects(:get).returns( mock_response('INDEX DOES NOT EXIST', 404) )
            Tire.search('not-existing', :query => { :query_string => { :query => 'foo' }}).results
          end
        end

        context "when retrieving results" do

          should "not call the #perform method immediately" do
            s = Tire.search('dummy') { query { string 'foo' } }
            s.expects(:perform).never
          end

          should "call #perform from #results" do
            s = Tire.search('dummy') { query { string 'foo' } }
            s.expects(:perform).once
            s.results
          end

        end

        context "when scanning an index" do
          should "initiate the scan" do
            Search::Scan.expects(:new).with { |index| index == 'dummy' }

            Tire.scan('dummy')
          end

          should "allow to pass the query as a block to scan" do
            Search::Scan.expects(:new).with { |index| index == 'dummy' }

            Tire.scan('dummy') { query { string 'foo' } }
          end

          should "allow to pass the query as a hash to scan" do
            payload = { :query => { :query_string => { :query => 'foo' } } }
            Search::Scan.expects(:new).with('dummy', payload)

            Tire.scan 'dummy', payload
          end

        end

      end

      context "utils" do

        should "encode a string for URL" do
          assert_equal 'foo+bar',   Utils.escape('foo bar')
          assert_equal 'foo%2Fbar', Utils.escape('foo/bar')
          assert_equal 'foo%21',    Utils.escape('foo!')
        end

        should "encode a string from URL" do
          assert_equal 'foo bar', Utils.unescape('foo+bar')
          assert_equal 'foo/bar', Utils.unescape('foo%2Fbar')
          assert_equal 'foo!',    Utils.unescape('foo%21')
        end

      end
    end

  end

end
<commit_msg>[TESTS] Fixed incorrect expectations on Ruby 1.8<commit_after>require 'test_helper'

module Tire

  class TireTest < Test::Unit::TestCase

    context "Tire" do

      should "have the DSL methods available" do
        assert_respond_to Tire, :search
        assert_respond_to Tire, :index
        assert_respond_to Tire, :configure
      end

      context "DSL" do

        should "allow searching with a block" do
          Search::Search.expects(:new).returns( stub(:perform => true) )

          Tire.search 'dummy' do
            query 'foo'
          end
        end

        should "allow searching with a Ruby Hash" do
          payload = { :query => { :query_string => { :query => 'foo' } } }
          Search::Search.expects(:new).with('dummy', :payload => payload).returns( stub(:perform => true) )

          Tire.search 'dummy', payload
        end

        should "allow searching with a JSON string" do
          payload = '{"query":{"query_string":{"query":"foo"}}}'
          Search::Search.expects(:new).with('dummy', :payload => payload).returns( stub(:perform => true) )

          Tire.search 'dummy', payload
        end

        should "raise an error when passed incorrect payload" do
          assert_raise(ArgumentError) do
            Tire.search 'dummy', 1
          end
        end

        should "raise SearchRequestFailed when receiving bad response from backend" do
          assert_raise(Search::SearchRequestFailed) do
            Tire::Configuration.client.expects(:get).returns( mock_response('INDEX DOES NOT EXIST', 404) )
            Tire.search('not-existing', :query => { :query_string => { :query => 'foo' }}).results
          end
        end

        context "when retrieving results" do

          should "not call the #perform method immediately" do
            s = Tire.search('dummy') { query { string 'foo' } }
            s.expects(:perform).never
          end

          should "call #perform from #results" do
            s = Tire.search('dummy') { query { string 'foo' } }
            s.expects(:perform).once
            s.results
          end

        end

        context "when scanning an index" do
          should "initiate the scan" do
            Search::Scan.expects(:new).with { |index,options| index == 'dummy' }

            Tire.scan('dummy')
          end

          should "allow to pass the query as a block to scan" do
            Search::Scan.expects(:new).with do |index,options|
              index == 'dummy'
            end

            Tire.scan('dummy') { query { string 'foo' } }
          end

          should "allow to pass the query as a hash to scan" do
            payload = { :query => { :query_string => { :query => 'foo' } } }
            Search::Scan.expects(:new).with('dummy', payload)

            Tire.scan 'dummy', payload
          end

        end

      end

      context "utils" do

        should "encode a string for URL" do
          assert_equal 'foo+bar',   Utils.escape('foo bar')
          assert_equal 'foo%2Fbar', Utils.escape('foo/bar')
          assert_equal 'foo%21',    Utils.escape('foo!')
        end

        should "encode a string from URL" do
          assert_equal 'foo bar', Utils.unescape('foo+bar')
          assert_equal 'foo/bar', Utils.unescape('foo%2Fbar')
          assert_equal 'foo!',    Utils.unescape('foo%21')
        end

      end
    end

  end

end

{'fraction_non_alphanumeric': 0.11607409620555721, 'fraction_numerical': 0.0032865252464893933, 'mean_word_length': 1.9808548530721282, 'pattern_counts': {'":': 6, '<': 5, '<?xml version=': 0, '>': 31, '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': '3706287', 'n_tokens_mistral': 1900, 'n_tokens_neox': 1816, 'n_words': 550}