<commit_before>require File.dirname(__FILE__) + '/spec_helper'

describe GoogleContacts::Wrapper do
  describe "fetching" do
    it "should be able to get list of contacts" do
      FakeWeb.register_uri(:get,
        'http://www.google.com/m8/feeds/contacts/default/full',
        :body => asset('contacts_full')
      )

      result = wrapper.contacts.find(:all)
      result.should have(1).contact
      result.first.should be_a GoogleContacts::Contact
    end

    it "should be able to get list of groups" do
      FakeWeb.register_uri(:get,
        'http://www.google.com/m8/feeds/groups/default/full',
        :body => asset('groups_full')
      )

      result = wrapper.groups.find(:all)
      result.should have(2).groups
      result.first.should be_a GoogleContacts::Group
    end
  end

  describe "flushing" do
    it "should not allow nesting of #batch" do
      lambda {
        wrapper.batch { wrapper.batch { } }
      }.should raise_error(/not allowed/i)
    end

    it "should collect operations in a batch" do
      wrapper.expects(:post).never
      document = wrapper.batch(:return_documents => true) do
        wrapper.contacts.build.save
        wrapper.contacts.build.save
      end.first

      document.xpath('.//xmlns:entry').should have(2).entries
      document.xpath('.//batch:operation').each do |operation|
        operation['type'].should == 'insert'
      end
    end

    it "should flush batches in chunks of 100" do
      wrapper.expects(:post).with(regexp_matches(%r!/contacts/!), is_a(String)).twice
      wrapper.batch do
        contact = wrapper.contacts.build
        101.times { contact.save }
      end
    end

    it "should raise when mixing contacts and groups in one batch" do
      lambda {
        wrapper.batch {
          wrapper.contacts.build.save
          wrapper.groups.build.save
        }
      }.should raise_error(/cannot mix/i)
    end

    it "should POST a single-operation batch to contacts when not batching" do
      wrapper.expects(:post).with(regexp_matches(%r!/contacts/!), is_a(String))
      wrapper.contacts.build.save
    end
<commit_msg>make sure nothing is done when there are no operations in a batch<commit_after>require File.dirname(__FILE__) + '/spec_helper'

describe GoogleContacts::Wrapper do
  describe "fetching" do
    it "should be able to get list of contacts" do
      FakeWeb.register_uri(:get,
        'http://www.google.com/m8/feeds/contacts/default/full',
        :body => asset('contacts_full')
      )

      result = wrapper.contacts.find(:all)
      result.should have(1).contact
      result.first.should be_a GoogleContacts::Contact
    end

    it "should be able to get list of groups" do
      FakeWeb.register_uri(:get,
        'http://www.google.com/m8/feeds/groups/default/full',
        :body => asset('groups_full')
      )

      result = wrapper.groups.find(:all)
      result.should have(2).groups
      result.first.should be_a GoogleContacts::Group
    end
  end

  describe "flushing" do
    it "should not allow nesting of #batch" do
      lambda {
        wrapper.batch { wrapper.batch { } }
      }.should raise_error(/not allowed/i)
    end

    it "should collect operations in a batch" do
      wrapper.expects(:post).never
      document = wrapper.batch(:return_documents => true) do
        wrapper.contacts.build.save
        wrapper.contacts.build.save
      end.first

      document.xpath('.//xmlns:entry').should have(2).entries
      document.xpath('.//batch:operation').each do |operation|
        operation['type'].should == 'insert'
      end
    end

    it "should flush batches in chunks of 100" do
      wrapper.expects(:post).with(regexp_matches(%r!/contacts/!), is_a(String)).twice
      wrapper.batch do
        contact = wrapper.contacts.build
        101.times { contact.save }
      end
    end

    it "should not flush when there are no operations to execute" do
      wrapper.expects(:post).never
      wrapper.batch {}
    end

    it "should raise when mixing contacts and groups in one batch" do
      lambda {
        wrapper.batch {
          wrapper.contacts.build.save
          wrapper.groups.build.save
        }
      }.should raise_error(/cannot mix/i)
    end

    it "should POST a single-operation batch to contacts when not batching" do
      wrapper.expects(:post).with(regexp_matches(%r!/contacts/!), is_a(String))
      wrapper.contacts.build.save
    end

{'fraction_non_alphanumeric': 0.10990502035278155, 'fraction_numerical': 0.004975124378109453, 'mean_word_length': 2.5299281723862728, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 2}, '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': '6160340', 'n_tokens_mistral': 1337, 'n_tokens_neox': 1279, 'n_words': 331}