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

describe Daywalker::Legislator do

  before :each do
    FakeWeb.clean_registry
  end

  before :all do
    Daywalker.api_key = 'redacted'
  end

  after :all do
    Daywalker.api_key = nil
  end

  describe 'find_all_by_zip' do

    describe 'happy path' do
      setup do
        # curl -i "http://services.sunlightlabs.com/api/legislators.allForZip.xml?zip=27511&apikey=redacted" > legislators_by_zip.xml
        register_uri_with_response 'legislators.allForZip.xml?zip=27511&apikey=redacted', 'legislators_by_zip.xml'

        @legislators = Daywalker::Legislator.find_all_by_zip 27511
      end

      it 'return 4 legislators' do
        @legislators.size.should == 4
      end
      
      it 'should have first legislator with votesmart id 119' do
        @legislators[0].votesmart_id.should == 119
      end

      it 'should have second legislator with votesmart id 21082' do
        @legislators[1].votesmart_id.should == 21082
      end

      it 'should have third legislator with votesmart id 21787' do
        @legislators[2].votesmart_id.should == 21787
      end

      it 'should have fourth legislator with votesmart id 10205' do
        @legislators[3].votesmart_id.should == 10205
      end
    end

    describe 'with a bad API key' do
      setup do
        #  curl -i "http://services.sunlightlabs.com/api/legislators.allForZip.xml?zip=27511&apikey=redacted" > legislators_by_zip
        register_uri_with_response 'legislators.allForZip.xml?zip=27511&apikey=redacted', 'legislators_by_zip_bad_api.xml'
      end

      it 'should raise bad API key error' do
        lambda {
          Daywalker::Legislator.find_all_by_zip 27511
        }.should raise_error(Daywalker::BadApiKey) 
      end
    end

    describe 'without a zip code' do
      it 'should raise a missing parameter error for zip' do
        lambda {
          Daywalker::Legislator.find_all_by_zip nil
        }.should raise_error(Daywalker::MissingParameter, 'zip') 
      end
    end
  end

  describe 'found with find(:only)' do
    describe 'by state and district, with one result,' do
      describe 'happy path' do
        before do
          register_uri_with_response 'legislators.get?apikey=redacted&state=NY&district=4', 'legislators_find_by_ny_district_4.xml'
          @legislator = Daywalker::Legislator.find(:only, :state => 'NY', :district => 4)
        end


        it 'should have votesmart id 119' do
          @legislator.votesmart_id.should == 119
        end
      end

      describe 'by state and district, with bad API key' do
        before do
          register_uri_with_response 'legislators.get?apikey=redacted&state=NY&district=4', 'legislators_find_by_ny_district_4_bad_api.xml'
        end

        it 'should raise a missing parameter error for zip' do
          lambda {
            Daywalker::Legislator.find(:only, :state => 'NY', :district => 4)
          }.should raise_error(Daywalker::BadApiKey) 
        end
      end
    end

  end

  describe 'found with find(:all)' do
    describe 'by state and title, with multiple results,' do
      describe 'happy path' do
        before do
          # curl -i "http://services.sunlightlabs.com/api/legislators.getList.xml?state=NY&title=Sen&apikey=redacted" > legislators_find_ny_senators.xml
          register_uri_with_response 'legislators.getList?state=NY&apikey=redacted&title=senator', 'legislators_find_ny_senators.xml'
          @legislators = Daywalker::Legislator.find(:all, :state => 'NY', :title => :senator)
        end

        it 'should have 2 results' do
          @legislators.size.should == 2
        end

        it 'should have first legislator with votesmart id 55463' do
          @legislators[0].votesmart_id.should == 55463
        end

        it 'should have second legislator with votesmart_id id 26976' do
          @legislators[1].votesmart_id.should == 26976
        end

      end

      describe 'with bad API key' do
        before do
          register_uri_with_response 'legislators.getList?state=NY&apikey=redacted&title=senator', 'legislators_find_ny_senators_bad_api.xml'
        end

        it 'should raise BadApiKey error' do
          lambda {
            Daywalker::Legislator.find(:all, :state => 'NY', :title => :senator)
          }.should raise_error(Daywalker::BadApiKey)

        end

      end
    end

  end


  describe 'parsed from XML' do
    setup do
      @xml = <<-XML
        <legislator>
          <district>4</district>
          <title>Rep</title>
          <eventful_id>P0-001-000016562-5</eventful_id>
          <in_office>1</in_office>
          <state>NC</state>
          <votesmart_id>119</votesmart_id><commit_msg>Refactored code example based on feedback from presentation.<commit_after>require File.dirname(__FILE__) + '/../spec_helper'

describe Daywalker::Legislator do

  before :each do
    FakeWeb.clean_registry
  end

  before :all do
    Daywalker.api_key = 'redacted'
  end

  after :all do
    Daywalker.api_key = nil
  end

  describe 'find_all_by_zip' do

    describe 'happy path' do
      setup do
        # curl -i "http://services.sunlightlabs.com/api/legislators.allForZip.xml?zip=27511&apikey=redacted" > legislators_by_zip.xml
        register_uri_with_response 'legislators.allForZip.xml?zip=27511&apikey=redacted', 'legislators_by_zip.xml'

        @legislators = Daywalker::Legislator.find_all_by_zip 27511
      end

      it 'return legislators identified by votersmart ids 119, 21082, 21787, and 10205' do
        @legislators.map{|each| each.votesmart_id}.should == [119, 21082, 21787, 10205]
      end
    end

    describe 'with a bad API key' do
      setup do
        #  curl -i "http://services.sunlightlabs.com/api/legislators.allForZip.xml?zip=27511&apikey=redacted" > legislators_by_zip
        register_uri_with_response 'legislators.allForZip.xml?zip=27511&apikey=redacted', 'legislators_by_zip_bad_api.xml'
      end

      it 'should raise bad API key error' do
        lambda {
          Daywalker::Legislator.find_all_by_zip 27511
        }.should raise_error(Daywalker::BadApiKey) 
      end
    end

    describe 'without a zip code' do
      it 'should raise a missing parameter error for zip' do
        lambda {
          Daywalker::Legislator.find_all_by_zip nil
        }.should raise_error(Daywalker::MissingParameter, 'zip') 
      end
    end
  end

  describe 'found with find(:only)' do
    describe 'by state and district, with one result,' do
      describe 'happy path' do
        before do
          register_uri_with_response 'legislators.get?apikey=redacted&state=NY&district=4', 'legislators_find_by_ny_district_4.xml'
          @legislator = Daywalker::Legislator.find(:only, :state => 'NY', :district => 4)
        end


        it 'should have votesmart id 119' do
          @legislator.votesmart_id.should == 119
        end
      end

      describe 'by state and district, with bad API key' do
        before do
          register_uri_with_response 'legislators.get?apikey=redacted&state=NY&district=4', 'legislators_find_by_ny_district_4_bad_api.xml'
        end

        it 'should raise a missing parameter error for zip' do
          lambda {
            Daywalker::Legislator.find(:only, :state => 'NY', :district => 4)
          }.should raise_error(Daywalker::BadApiKey) 
        end
      end
    end

  end

  describe 'found with find(:all)' do
    describe 'by state and title, with multiple results,' do
      describe 'happy path' do
        before do
          # curl -i "http://services.sunlightlabs.com/api/legislators.getList.xml?state=NY&title=Sen&apikey=redacted" > legislators_find_ny_senators.xml
          register_uri_with_response 'legislators.getList?state=NY&apikey=redacted&title=senator', 'legislators_find_ny_senators.xml'
          @legislators = Daywalker::Legislator.find(:all, :state => 'NY', :title => :senator)
        end

        it 'should have 2 results' do
          @legislators.size.should == 2
        end

        it 'should have first legislator with votesmart id 55463' do
          @legislators[0].votesmart_id.should == 55463
        end

        it 'should have second legislator with votesmart_id id 26976' do
          @legislators[1].votesmart_id.should == 26976
        end

      end

      describe 'with bad API key' do
        before do
          register_uri_with_response 'legislators.getList?state=NY&apikey=redacted&title=senator', 'legislators_find_ny_senators_bad_api.xml'
        end

        it 'should raise BadApiKey error' do
          lambda {
            Daywalker::Legislator.find(:all, :state => 'NY', :title => :senator)
          }.should raise_error(Daywalker::BadApiKey)

        end
      end
    end

  end


  describe 'parsed from XML' do
    setup do
      @xml = <<-XML
        <legislator>
          <district>4</district>
          <title>Rep</title>
          <eventful_id>P0-001-000016562-5</eventful_id>
          <in_office>1</in_office>
          <state>NC</state>
          <votesmart_id>119</votesmart_id>
{'fraction_non_alphanumeric': 0.09257218426272867, 'fraction_numerical': 0.02733083535375799, 'mean_word_length': 2.6785569517632752, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 28}, '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': '7200534', 'n_tokens_mistral': 3169, 'n_tokens_neox': 2828, 'n_words': 678}