require 'test_helper'

class PfClientTest < Minitest::Test
  def setup
    @pf_params = {
      name: 'JOÃO DA SILVA',
      cpf_cnpj: '68247905752',
      pf_data: {
        rg: '03 232.115-06',
        rg_issuer: 'SSP',
        rg_uf: 'MG',
        civil_status: 'Solteiro(a)',
        gender: 'M',
        father_name: 'José da Silva',
        mother_name: 'Maria da Silva',
        birthday: '29/05/1985'
      },
      address_data: {
        street: 'Rua Coração Eucarítico de Jesus',
        number: '75a',
        complement: 'casa',
        neighborhood: 'Coração Eucarístico',
        city: 'Belo Horizonte',
        state: 'MG',
        zipcode: '31814030'
      },
      bank_data: {
        code: 237,
        agency: '36102',
        account: '103608'
      },
      contact_data: {
        phone: '(31) 3319-1900',
        mobile_phone: '(31) 98819-1988'
      },
      other_data: {
        salary: 5000,
        job_role: 'Engenheiro',
        admission_date: '20/06/2016'
      },
      personal_reference: {
        name: 'Pedro Paulo Da Silva',
        phone: '(31)8818-1888',
        relationship: 'Primo'
      },
      informations: 'Informações adicionais'
    }
  end

  def test_has_pf_required_params_method_for_complete_pf_client
    c = NexoosSocinal::PfClient.new(@pf_params)
    assert c.has_required_params?
    assert c.errors.blank?
  end

  def test_has_pf_required_params_method_for_non_passed_pf_name
    name = @pf_params.delete(:name)
    assert_not_has_pf_required_params(@pf_params)
    assert_pf_error_message(@pf_params, "name is required")
  end

  def test_has_pf_required_params_method_for_empty_pf_name
    @pf_params[:name] = ''
    assert_not_has_pf_required_params(@pf_params)
    assert_pf_error_message(@pf_params, "name is required")
  end

  def test_has_pf_required_params_method_for_non_passed_pf_cpf_cnpj
    @pf_params.delete(:cpf_cnpj)
    assert_not_has_pf_required_params(@pf_params)
    assert_pf_error_message(@pf_params, "cpf_cnpj is required")
  end

  def test_has_pf_required_params_method_for_empty_pf_cpf_cnpj
    @pf_params[:cpf_cnpj] = ''
    assert_not_has_pf_required_params(@pf_params)
    assert_pf_error_message(@pf_params, "cpf_cnpj is required")
  end

  def test_has_pf_required_params_method_for_unexistent_pf_data
    @pf_params.delete(:pf_data)
    assert_not_has_pf_required_params(@pf_params)
    assert_pf_error_message(@pf_params, "pf_data is required")
  end

  def test_has_pf_required_params_method_for_empty_pf_data
    @pf_params[:pf_data] = {}
    assert_not_has_pf_required_params(@pf_params)
    assert_pf_error_message(@pf_params, "pf_data is required")
  end

  def test_has_pf_required_params_method_for_nil_pf_data
    @pf_params[:pf_data] = nil
    assert_not_has_pf_required_params(@pf_params)
    assert_pf_error_message(@pf_params, "pf_data is required")
  end

  def test_has_pf_required_params_method_for_non_passed_pf_data_rg
    @pf_params[:pf_data].delete(:rg)
    assert_not_has_pf_required_params(@pf_params)
    assert_pf_error_message(@pf_params, "pf_data#rg is required")
  end

  def test_has_pf_required_params_method_for_empty_pf_data_rg
    @pf_params[:pf_data][:rg] = ''
    assert_not_has_pf_required_params(@pf_params)
    assert_pf_error_message(@pf_params, "pf_data#rg is required")
  end

  def test_has_pf_required_params_method_for_non_passed_pf_data_civil_status
    @pf_params[:pf_data].delete(:civil_status)
    assert_not_has_pf_required_params(@pf_params)
    assert_pf_error_message(@pf_params, "pf_data#civil_status is required")
  end

  def test_has_pf_required_params_method_for_empty_pf_data_civil_status
    @pf_params[:pf_data][:civil_status] = ''
    assert_not_has_pf_required_params(@pf_params)
    assert_pf_error_message(@pf_params, "pf_data#civil_status is required")
  end

  def test_has_pf_required_params_method_for_non_passed_pf_data_gender
    @pf_params[:pf_data].delete(:gender)
    assert_not_has_pf_required_params(@pf_params)
    assert_pf_error_message(@pf_params, "pf_data#gender is required")
  end

  def test_has_pf_required_params_method_for_empty_pf_data_gender
    @pf_params[:pf_data][:gender] = ''
    assert_not_has_pf_required_params(@pf_params)
    assert_pf_error_message(@pf_params, "pf_data#gender is required")
  end

  def test_has_pf_required_params_method_for_non_passed_pf_data_mother_name
    @pf_params[:pf_data].delete(:mother_name)
    assert_not_has_pf_required_params(@pf_params)
    assert_pf_error_message(@pf_params, "pf_data#mother_name is required")
  end

  def test_has_pf_required_params_method_for_empty_pf_data_mother_name
    @pf_params[:pf_data][:mother_name] = ''
    assert_not_has_pf_required_params(@pf_params)
    assert_pf_error_message(@pf_params, "pf_data#mother_name is required")
  end

  def test_has_pf_required_params_method_for_non_passed_pf_data_birthday
    @pf_params[:pf_data].delete(:birthday)
    assert_not_has_pf_required_params(@pf_params)
    assert_pf_error_message(@pf_params, "pf_data#birthday is required")
  end

  def test_has_pf_required_params_method_for_empty_pf_data_birthday
    @pf_params[:pf_data][:birthday] = ''
    assert_not_has_pf_required_params(@pf_params)
    assert_pf_error_message(@pf_params, "pf_data#birthday is required")
  end

  def test_has_pf_required_params_method_for_unexistent_address_data
    @pf_params.delete(:address_data)
    assert_not_has_pf_required_params(@pf_params)
    assert_pf_error_message(@pf_params, "address_data is required")
  end

  def test_has_pf_required_params_method_for_empty_address_data
    @pf_params[:address_data] = {}
    assert_not_has_pf_required_params(@pf_params)
    assert_pf_error_message(@pf_params, "address_data is required")
  end

  def test_has_pf_required_params_method_for_nil_address_data
    @pf_params[:address_data] = nil
    assert_not_has_pf_required_params(@pf_params)
    assert_pf_error_message(@pf_params, "address_data is required")
  end

  def test_has_pf_required_params_method_for_non_passed_address_data_street
    @pf_params[:address_data].delete(:street)
    assert_not_has_pf_required_params(@pf_params)
    assert_pf_error_message(@pf_params, "address_data#street is required")
  end

  def test_has_pf_required_params_method_for_empty_address_data_street
    @pf_params[:address_data][:street] = ''
    assert_not_has_pf_required_params(@pf_params)
    assert_pf_error_message(@pf_params, "address_data#street is required")
  end

  def test_has_pf_required_params_method_for_non_passed_address_data_number
    @pf_params[:address_data].delete(:number)
    assert_not_has_pf_required_params(@pf_params)
    assert_pf_error_message(@pf_params, "address_data#number is required")
  end

  def test_has_pf_required_params_method_for_empty_address_data_number
    @pf_params[:address_data][:number] = ''
    assert_not_has_pf_required_params(@pf_params)
    assert_pf_error_message(@pf_params, "address_data#number is required")
  end

  def test_has_pf_required_params_method_for_non_passed_address_data_neighborhood
    @pf_params[:address_data].delete(:neighborhood)
    assert_not_has_pf_required_params(@pf_params)
    assert_pf_error_message(@pf_params, "address_data#neighborhood is required")
  end

  def test_has_pf_required_params_method_for_empty_address_data_neighborhood
    @pf_params[:address_data][:neighborhood] = ''
    assert_not_has_pf_required_params(@pf_params)
    assert_pf_error_message(@pf_params, "address_data#neighborhood is required")
  end

  def test_has_pf_required_params_method_for_non_passed_address_data_city
    @pf_params[:address_data].delete(:city)
    assert_not_has_pf_required_params(@pf_params)
    assert_pf_error_message(@pf_params, "address_data#city is required")
  end

  def test_has_pf_required_params_method_for_empty_address_data_city
    @pf_params[:address_data][:city] = ''
    assert_not_has_pf_required_params(@pf_params)
    assert_pf_error_message(@pf_params, "address_data#city is required")
  end

  def test_has_pf_required_params_method_for_non_passed_address_data_state
    @pf_params[:address_data].delete(:state)
    assert_not_has_pf_required_params(@pf_params)
    assert_pf_error_message(@pf_params, "address_data#state is required")
  end

  def test_has_pf_required_params_method_for_empty_address_data_state
    @pf_params[:address_data][:state] = ''
    assert_not_has_pf_required_params(@pf_params)
    assert_pf_error_message(@pf_params, "address_data#state is required")
  end

  def test_has_pf_required_params_method_for_non_passed_address_data_zipcode
    @pf_params[:address_data].delete(:zipcode)
    assert_not_has_pf_required_params(@pf_params)
    assert_pf_error_message(@pf_params, "address_data#zipcode is required")
  end

  def test_has_pf_required_params_method_for_empty_address_data_zipcode
    @pf_params[:address_data][:zipcode] = ''
    assert_not_has_pf_required_params(@pf_params)
    assert_pf_error_message(@pf_params, "address_data#zipcode is required")
  end

  def test_has_pf_required_params_method_for_unexistent_contact_data
    @pf_params.delete(:contact_data)
    assert_not_has_pf_required_params(@pf_params)
    assert_pf_error_message(@pf_params, "contact_data is required")
  end

  def test_has_pf_required_params_method_for_empty_contact_data
    @pf_params[:contact_data] = {}
    assert_not_has_pf_required_params(@pf_params)
    assert_pf_error_message(@pf_params, "contact_data is required")
  end

  def test_has_pf_required_params_method_for_nil_contact_data
    @pf_params[:contact_data] = nil
    assert_not_has_pf_required_params(@pf_params)
    assert_pf_error_message(@pf_params, "contact_data is required")
  end

  def test_has_pf_required_params_method_for_non_passed_contact_data_phone
    @pf_params[:contact_data].delete(:phone)
    assert_not_has_pf_required_params(@pf_params)
    assert_pf_error_message(@pf_params, "contact_data#phone is required")
  end

  def test_has_pf_required_params_method_for_empty_contact_data_phone
    @pf_params[:contact_data][:phone] = ''
    assert_not_has_pf_required_params(@pf_params)
    assert_pf_error_message(@pf_params, "contact_data#phone is required")
  end

  def test_has_pf_required_params_method_for_unexistent_other_data
    @pf_params.delete(:other_data)
    assert_not_has_pf_required_params(@pf_params)
    assert_pf_error_message(@pf_params, "other_data is required")
  end

  def test_has_pf_required_params_method_for_empty_other_data
    @pf_params[:other_data] = {}
    assert_not_has_pf_required_params(@pf_params)
    assert_pf_error_message(@pf_params, "other_data is required")
  end

  def test_has_pf_required_params_method_for_nil_other_data
    @pf_params[:other_data] = nil
    assert_not_has_pf_required_params(@pf_params)
    assert_pf_error_message(@pf_params, "other_data is required")
  end

  def test_has_pf_required_params_method_for_non_passed_other_data_salary
    @pf_params[:other_data].delete(:salary)
    assert_not_has_pf_required_params(@pf_params)
    assert_pf_error_message(@pf_params, "other_data#salary is required")
  end

  def test_has_pf_required_params_method_for_empty_other_data_salary
    @pf_params[:other_data][:salary] = ''
    assert_not_has_pf_required_params(@pf_params)
    assert_pf_error_message(@pf_params, "other_data#salary is required")
  end

  def test_compact_pf_data_method
    c = NexoosSocinal::PfClient.new(@pf_params)
    expected = '03 232.115-06;SSP;MG;S;M;JOSÉ DA SILVA;MARIA DA SILVA;29/05/1985'
    assert_respond_to c, :compact_pf_data
    assert_equal c.compact_pf_data, expected
  end

  def test_compact_address_data_method
    c = NexoosSocinal::PfClient.new(@pf_params)
    assert_respond_to c, :compact_address_data
    expected = 'RUA CORAÇÃO EUCARÍTICO DE JESUS;75A;CASA;CORAÇÃO EUCARÍSTICO;BELO HORIZONTE;MG;31814-030'
    assert_equal c.compact_address_data, expected
  end

  def test_compact_bank_data_method
    c = NexoosSocinal::PfClient.new(@pf_params)
    assert_respond_to c, :compact_bank_data
    expected = '237;36102;10360-8'
    assert_equal c.compact_bank_data, expected
  end

  def test_compact_contact_data_method
    c = NexoosSocinal::PfClient.new(@pf_params)
    assert_respond_to c, :compact_contact_data
    expected = '31-3319-1900;;31-98819-1988'
    assert_equal c.compact_contact_data, expected
  end

  def test_compact_other_data_method
    c = NexoosSocinal::PfClient.new(@pf_params)
    assert_respond_to c, :compact_other_data
    expected = '5000.00;ENGENHEIRO;20/06/2016'
    assert_equal c.compact_other_data, expected
  end

  def test_informations_method
    c = NexoosSocinal::PfClient.new(@pf_params)
    assert_respond_to c, :informations
    expected = 'INFORMAÇÕES ADICIONAIS'
    assert_equal c.informations, expected
  end

  def test_informations_method_for_nil_informations
    @pf_params.delete(:informations)
    c = NexoosSocinal::PfClient.new(@pf_params)
    assert_empty c.informations
  end

  def test_compact_personal_reference_method
    c = NexoosSocinal::PfClient.new(@pf_params)
    assert_respond_to c, :compact_personal_reference
    expected = 'PEDRO PAULO DA SILVA;31-8818-1888;3'
    assert_equal c.compact_personal_reference, expected
  end

  def test_request_message
    c = NexoosSocinal::PfClient.new(@pf_params)
    assert_equal c.request_message, expected_register_pf
  end
end
