require 'spec_helper_unit'
require_relative 'geocodings_controller_shared_examples'
require_relative '../../../../app/controllers/api/json/geocodings_controller'

describe Api::Json::GeocodingsController do
  it_behaves_like 'geocoding controllers' do
  end

  include Rack::Test::Methods
  include Warden::Test::Helpers
  include CacheHelper

  describe 'POST api/v1/geocodings' do
    before do
      bypass_named_maps
      @user = create_user
      host! "#{@user.username}.localhost.lan"
      login_as(@user, scope: @user.username)
    end

    let(:params) { { api_key: @user.api_key, kind: 'ipaddress', formatter: '{some_column}' } }

    it 'takes an optional argument force_all_rows and stores it in the model' do
      table = create_table(user_id: @user.id)
      Resque.expects(:enqueue).once

      geocoding_id = nil

      post_json api_v1_geocodings_create_url(params.merge({
            table_name: table.name,
            force_all_rows: true
          })) do |response|
        response.status.should be_success
        response.body[:force_all_rows].should == true
        geocoding_id = response.body[:id]
      end

      Geocoding[geocoding_id].force_all_rows == true
    end

    it 'sets force_all_rows to nil if not present' do
      table = create_table(user_id: @user.id)
      Resque.expects(:enqueue).once

      geocoding_id = nil

      post_json api_v1_geocodings_create_url(params.merge({
            table_name: table.name,
          })) do |response|
        response.status.should be_success
        response.body[:force_all_rows].should == false
        geocoding_id = response.body[:id]
      end

      Geocoding[geocoding_id].force_all_rows == false
    end

  end
end
