<commit_before>require 'spec_helper'

describe Neo4j::Core::Query do

  describe 'options' do
    let(:query) { Neo4j::Core::Query.new(parser: 2.0) }

    it 'should generate a per-query cypher parser version' do
      expect(query.to_cypher).to eq('CYPHER 2.0')
    end

    describe 'subsequent call' do
      let(:query) { super().match('q:Person') }

      it 'should combine the parser version with the rest of the query' do
        expect(query.to_cypher).to eq('CYPHER 2.0 MATCH q:Person')
      end
    end
  end

  class Person
  end

  class Note
    CYPHER_LABEL = 'GreatNote' 
  end

  describe 'batch finding' do
    before(:all) do
      Neo4j::Session.open(:server_db)
    end
    before(:each) do
      Neo4j::Core::Query.new.match(foo: :Foo, bar: :Bar).delete(:foo, :bar).exec

      5.times do
        Neo4j::Node.create({uuid: SecureRandom.uuid}, :Foo)
      end
      2.times do
        Neo4j::Node.create({uuid: SecureRandom.uuid}, :Bar)
      end
    end

    describe 'find_in_batches' do
      {
        1 => 5,
        2 => 3,
        3 => 2,
        4 => 2,
        5 => 1,
        6 => 1
      }.each do |batch_size, expected_yields|
        context "batch_size of #{batch_size}" do
          it 'yields three times for five nodes and a batch size of two' do
            expect do |block|
              Neo4j::Core::Query.new.match(f: :Foo).return(:f).find_in_batches(:f, :uuid, batch_size: batch_size, &block)
            end.to yield_control.exactly(expected_yields).times
          end
        end
      end
    end

    describe 'find_each' do
      {
        1 => 5,
        2 => 5,
        3 => 5,
        4 => 5,
        5 => 5,
        6 => 5
      }.each do |batch_size, expected_yields|
        it 'yields five times for five nodes and a batch size of two' do
          expect do |block|
            Neo4j::Core::Query.new.match(f: :Foo).return(:f).find_each(:f, :uuid, batch_size: 2, &block)
          end.to yield_control.exactly(5).times
        end
      end
    end
  end

  def expects_cypher(cypher, params = nil)
    query = eval("Neo4j::Core::Query.new#{self.class.description}")
    expect(query.to_cypher).to eq(cypher)
    expect(query.send(:merge_params)).to eq(params) if params
  end

  def self.it_generates(cypher, params = nil)
    it "generates #{cypher}" do
      expects_cypher(cypher, params)
    end
  end<commit_msg>Consistentizing the find_in_batches/find_each specs<commit_after>require 'spec_helper'

describe Neo4j::Core::Query do

  describe 'options' do
    let(:query) { Neo4j::Core::Query.new(parser: 2.0) }

    it 'should generate a per-query cypher parser version' do
      expect(query.to_cypher).to eq('CYPHER 2.0')
    end

    describe 'subsequent call' do
      let(:query) { super().match('q:Person') }

      it 'should combine the parser version with the rest of the query' do
        expect(query.to_cypher).to eq('CYPHER 2.0 MATCH q:Person')
      end
    end
  end

  class Person
  end

  class Note
    CYPHER_LABEL = 'GreatNote' 
  end

  describe 'batch finding' do
    before(:all) do
      Neo4j::Session.open(:server_db)
    end
    before(:each) do
      Neo4j::Core::Query.new.match(foo: :Foo, bar: :Bar).delete(:foo, :bar).exec

      5.times do
        Neo4j::Node.create({uuid: SecureRandom.uuid}, :Foo)
      end
      2.times do
        Neo4j::Node.create({uuid: SecureRandom.uuid}, :Bar)
      end
    end

    describe 'find_in_batches' do
      {
        1 => 5,
        2 => 3,
        3 => 2,
        4 => 2,
        5 => 1,
        6 => 1
      }.each do |batch_size, expected_yields|
        context "batch_size of #{batch_size}" do
          it "yields #{expected_yields} times" do
            expect do |block|
              Neo4j::Core::Query.new.match(f: :Foo).return(:f).find_in_batches(:f, :uuid, batch_size: batch_size, &block)
            end.to yield_control.exactly(expected_yields).times
          end
        end
      end
    end

    describe 'find_each' do
      {
        1 => 5,
        2 => 5,
        3 => 5,
        4 => 5,
        5 => 5,
        6 => 5
      }.each do |batch_size, expected_yields|
        context "batch_size of #{batch_size}" do
          it "yields #{expected_yields} times" do
            expect do |block|
              Neo4j::Core::Query.new.match(f: :Foo).return(:f).find_each(:f, :uuid, batch_size: 2, &block)
            end.to yield_control.exactly(5).times
          end
        end
      end
    end
  end

  def expects_cypher(cypher, params = nil)
    query = eval("Neo4j::Core::Query.new#{self.class.description}")
    expect(query.to_cypher).to eq(cypher)
    expect(query.send(:merge_params)).to eq(params) if params
  end

  def self.it_generates(cypher, params = nil)
    it "generates #{cypher}" do
      expects_cypher(cypher, params)
    end
  end
{'fraction_non_alphanumeric': 0.12427265170407314, 'fraction_numerical': 0.017871986699916874, 'mean_word_length': 2.2237106496985932, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, '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': '3849242', 'n_tokens_mistral': 1737, 'n_tokens_neox': 1625, 'n_words': 416}