require File.expand_path(File.dirname(__FILE__) + '/spec_helper')

describe ElasticSearch::Api::Hit do
  let(:response) { {"_source" => {"foo" => "bar"}, "_id" => "1"} }

  subject { described_class.new(response) }

  its(:id) { should == response["_id"] }

  it "should set hit attributes" do
    subject.foo.should == response["_source"]["foo"]
  end
end

describe ElasticSearch::Api::Hits do
  context "unpaginated" do
    let(:response) do
      {
        "_shards"=>{"total"=>30, "successful"=>30, "failed"=>0}, 
        "hits"=>{"total"=>2, "max_score"=>1.0, "hits"=>[
          {"_index"=>"test_index", "_type"=>"test_type", "_id"=>"1", "_score"=>1.0, "_source"=>{"foo" => "bar"}},
          {"_index"=>"test_index", "_type"=>"test_type", "_id"=>"2", "_score"=>1.0, "_source"=>{"foo" => "baz"}}
      ]}}
    end

    subject { described_class.new(response) }

    it { should respond_to(:response) }

    its(:total_entries) { should == response["hits"]["hits"].size } # will_paginate
    its(:total_count) { should == response["hits"]["hits"].size } # kaminari

    it "should instantiate hits in order" do
      response["hits"]["hits"].each_with_index do |hit, i|
        subject.hits[i].should == ElasticSearch::Api::Hit.new(hit)
      end
    end

    it { should respond_to(:to_a) }

    it "should delegate array methods" do
      subject.collect { |h| h.id }.should == response["hits"]["hits"].collect { |h| h["_id"] }
    end

  end

  context "paginated" do
    let(:response) do
      {
        "_shards"=>{"total"=>30, "successful"=>30, "failed"=>0}, 
        "hits"=>{"total"=>6, "max_score"=>1.0, "hits"=>[
          {"_index"=>"test_index", "_type"=>"test_type", "_id"=>"3", "_score"=>1.0, "_source"=>{"foo" => "bar"}},
          {"_index"=>"test_index", "_type"=>"test_type", "_id"=>"4", "_score"=>1.0, "_source"=>{"foo" => "baz"}}
      ]}}
    end
    
    let(:per_page) { 2 }
    let(:page) { 2 }
    
    subject { described_class.new(response, {:page => page, :per_page => per_page}) }

    # will_paginate
    its(:total_pages) { should == (response["hits"]["total"] / per_page.to_f).ceil }
    its(:next_page) { should == (page + 1) }
    its(:previous_page) { should == (page - 1) }
    its(:current_page) { should == page }
    its(:per_page) { should == per_page }
    its(:offset) { should == per_page * (page - 1) }

    # kaminari
    its(:limit_value) { should == per_page }
    its(:num_pages) { should == (response["hits"]["total"] / per_page.to_f).ceil }
    its(:offset_value) { should == per_page * (page - 1) }
  end
end
