<commit_before>require 'unit/spec_helper'
require 'ashikawa-core/cursor'

describe Ashikawa::Core::Cursor do
  subject { Ashikawa::Core::Cursor }

  before :each do
    @database = double()
    mock Ashikawa::Core::Document
  end

  it "should create a cursor for a non-complete batch" do
    my_cursor = subject.new @database, server_response("cursor/26011191")
    my_cursor.id.should        == "26011191"
    my_cursor.length.should    == 5
  end

  it "should create a cursor for the last batch" do
    my_cursor = subject.new @database, server_response("cursor/26011191-3")
    my_cursor.id.should be_nil
    my_cursor.length.should == 5
  end

  describe "existing cursor" do
    subject { Ashikawa::Core::Cursor.new @database,
      server_response("cursor/26011191")
    }

    it "should iterate over all documents of a cursor when given a block" do
      first = true

      @database.stub(:send_request).with("cursor/26011191", :put => {}) do
        if first
          first = false
          server_response("cursor/26011191-2")
        else
          server_response("cursor/26011191-3")
        end
      end
      @database.should_receive(:send_request).twice

      Ashikawa::Core::Document.stub(:new)
      Ashikawa::Core::Document.should_receive(:new).exactly(5).times

      subject.each { |document| }
    end

    it "should return an enumerator to go over all documents of a cursor when given no block" do
      first = true

      @database.stub(:send_request).with("cursor/26011191", :put => {}) do
        if first
          first = false<commit_msg>each has an odd behavior on 1.8.7, pending on this version for now<commit_after>require 'unit/spec_helper'
require 'ashikawa-core/cursor'

describe Ashikawa::Core::Cursor do
  subject { Ashikawa::Core::Cursor }

  before :each do
    @database = double()
    mock Ashikawa::Core::Document
  end

  it "should create a cursor for a non-complete batch" do
    my_cursor = subject.new @database, server_response("cursor/26011191")
    my_cursor.id.should        == "26011191"
    my_cursor.length.should    == 5
  end

  it "should create a cursor for the last batch" do
    my_cursor = subject.new @database, server_response("cursor/26011191-3")
    my_cursor.id.should be_nil
    my_cursor.length.should == 5
  end

  describe "existing cursor" do
    subject { Ashikawa::Core::Cursor.new @database,
      server_response("cursor/26011191")
    }

    it "should iterate over all documents of a cursor when given a block" do
      first = true

      @database.stub(:send_request).with("cursor/26011191", :put => {}) do
        if first
          first = false
          server_response("cursor/26011191-2")
        else
          server_response("cursor/26011191-3")
        end
      end
      @database.should_receive(:send_request).twice

      Ashikawa::Core::Document.stub(:new)
      Ashikawa::Core::Document.should_receive(:new).exactly(5).times

      subject.each { |document| }
    end

    it "should return an enumerator to go over all documents of a cursor when given no block" do
      pending "This fails on 1.8.7 for unknown reasons. Investigating." if RUBY_VERSION == "1.8.7"

      first = true

      @database.stub(:send_request).with("cursor/26011191", :put => {}) do
        if first
          first = false
{'fraction_non_alphanumeric': 0.10741301059001512, 'fraction_numerical': 0.04508320726172466, 'mean_word_length': 2.9310344827586206, 'pattern_counts': {'":': 0, '<': 3, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5291313', 'n_tokens_mistral': 1099, 'n_tokens_neox': 993, 'n_words': 271}