<commit_before>require 'formhub_ruby'
require 'spec_helper'
require 'yaml'

describe FormhubRuby::ApiConnector do
  before :each do
    credentials = YAML.load_file('spec/fixtures/test_credentials.yml')
    FormhubRuby.configure do |config|
      config.username = credentials['username']
      config.password = credentials['password']
    end
  end
  

  context 'when connecting to the API' do
    

    it 'successfully connects to the FormHub API and retrieve JSON Data' do
      VCR.use_cassette 'successful_connection' do 
        connection = FormhubRuby::ApiConnector.new(formname: 'survey')
        connection.fetch
        connection.data.should be_a_kind_of(Array)
        connection.data[0].should be_a_kind_of(Object)
      end
    end

    it 'displays appropriate message if the JSON data is not successfully retrieved' do
      stub_request(:any, "http://formhub.org/#{FormhubRuby.configuration.username}/forms/survey/api").to_return(:body => 'NO JSON HERE')
      connection = FormhubRuby::ApiConnector.new(formname: 'fake')
      expect {connection.fetch}.to raise_error('API connection error')
    end
  end

  context 'when formulating a more complex query string' do

    let(:connection) {FormhubRuby::ApiConnector.new(formname: 'survey')}
    let(:username) {FormhubRuby.configuration.username}

    it "does not add any extraneaous query" do
       connection = FormhubRuby::ApiConnector.new(formname: 'survey')
       connection.api_uri.should == "http://formhub.org/#{username}/forms/survey/api"
    end

    it "does form a simple query" do
      connection.query = {age: 12}
      connection.api_uri.should == "http://formhub.org/#{username}/forms/survey/api?query=%7B%22age%22%3A%2212%22%7D"
      VCR.use_cassette 'age_query' do
        connection.fetch
        connection.data.length.should == 1
      end

    end

    it "formulates a query with a start" do
      connection.start = 1
      connection.api_uri.should == "http://formhub.org/#{username}/forms/survey/api?start=1"
      VCR.use_cassette 'query_start' do
        connection.fetch
        connection.data.length.should == 1
      end
    end

    it "formulates a query with a limit" do
      connection.limit = 1
      connection.api_uri.should == "http://formhub.org/#{username}/forms/survey/api?limit=1"
      VCR.use_cassette 'query_limit' do
        connection.fetch
        connection.data.length.should == 1
      end
    end
    
  end

end
<commit_msg>Replaces "should" with "Expect" statements<commit_after>require 'formhub_ruby'
require 'spec_helper'
require 'yaml'

describe FormhubRuby::ApiConnector do
  before :each do
    credentials = YAML.load_file('spec/fixtures/test_credentials.yml')
    FormhubRuby.configure do |config|
      config.username = credentials['username']
      config.password = credentials['password']
    end
  end
  

  context 'when connecting to the API' do
    

    it 'successfully connects to the FormHub API and retrieve JSON Data' do
      VCR.use_cassette 'successful_connection' do 
        connection = FormhubRuby::ApiConnector.new(formname: 'survey')
        connection.fetch
        expect(connection.data).to be_a_kind_of(Array)
        expect(connection.data[0]).to be_a_kind_of(Object)
      end
    end

    it 'displays appropriate message if the JSON data is not successfully retrieved' do
      stub_request(:any, "http://formhub.org/#{FormhubRuby.configuration.username}/forms/survey/api").to_return(:body => 'NO JSON HERE')
      connection = FormhubRuby::ApiConnector.new(formname: 'fake')
      expect {connection.fetch}.to raise_error('API connection error')
    end
  end

  context 'when formulating a more complex query string' do

    let(:connection) {FormhubRuby::ApiConnector.new(formname: 'survey')}
    let(:username) {FormhubRuby.configuration.username}

    it "does not add any extraneaous query" do
       connection = FormhubRuby::ApiConnector.new(formname: 'survey')
       expect(connection.api_uri).to eq("http://formhub.org/#{username}/forms/survey/api")
    end

    it "does form a simple query" do
      connection.query = {age: 12}
      expect(connection.api_uri).to eq("http://formhub.org/#{username}/forms/survey/api?query=%7B%22age%22%3A%2212%22%7D")
      VCR.use_cassette 'age_query' do
        connection.fetch
        expect(connection.data.length).to eq(1)
      end

    end

    it "formulates a query with a start" do
      connection.start = 1
      expect(connection.api_uri).to eq("http://formhub.org/#{username}/forms/survey/api?start=1")
      VCR.use_cassette 'query_start' do
        connection.fetch
        expect(connection.data.length).to eq(1)
      end
    end

    it "formulates a query with a limit" do
      connection.limit = 1
      expect(connection.api_uri).to eq("http://formhub.org/#{username}/forms/survey/api?limit=1")
      VCR.use_cassette 'query_limit' do
        connection.fetch
        expect(connection.data.length).to eq(1)
      end
    end
    
  end

end

{'fraction_non_alphanumeric': 0.11070998796630566, 'fraction_numerical': 0.009225832330525471, 'mean_word_length': 3.2406462585034013, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '4982453', 'n_tokens_mistral': 1604, 'n_tokens_neox': 1527, 'n_words': 350}