Skip to content

Commit

Permalink
Tiny refactory for specs of controller of deputados.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitryrck committed Oct 2, 2016
1 parent c9fd14d commit a680b7d
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 43 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
AnoAtual.*
AnoAtual_formated.*
config/database.yml
config/chewy.yml
docker-compose.yml
vendor/bundle
/config/deploy.rb
Expand Down
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ services:
before_script:
- cp config/database.yml.template config/database.yml
- sleep 10
- cp config/chewy.yml.travis config/chewy.yml

script:
- RAILS_ENV=test bundle exec rake db:setup --trace
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ Antes de mais nada, tenha certeza de que tenha `docker` e `docker-compose` insta

cp docker-compose.yml.template docker-compose.yml

2. Copie o arquivo `config/database.yml.template` ele está pronto para ser usado com docker:
2. Copie o arquivo `config/database.yml.template` e `config/chewy.yml.template` eles estão prontos para serem usados com docker:

cp config/database.yml.template config/database.yml
cp config/chewy.yml.template config/chewy.yml

3. Levante o banco de dados (se você não tiver as imagens o primeiro comando pode demorar um pouco):

Expand Down
5 changes: 3 additions & 2 deletions config/chewy.yml → config/chewy.yml.template
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# config/chewy.yml
# separate environment configs
test:
host: 'localhost:9250'
host: 'es:9200'
prefix: 'test'

development:
host: 'localhost:9200'
host: 'es:9200'
3 changes: 3 additions & 0 deletions config/chewy.yml.travis
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test:
host: 'localhost:9200'
prefix: 'test'
1 change: 1 addition & 0 deletions config/initializers/chewy.rb
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Chewy.root_strategy = :atomic
Chewy.use_after_commit_callbacks = !Rails.env.test?
111 changes: 71 additions & 40 deletions spec/controllers/deputados_spec.rb
Original file line number Diff line number Diff line change
@@ -1,76 +1,107 @@
require 'rails_helper'

describe DeputadosController, type: :controller do
let(:deputado) { Deputado.create! FactoryGirl.attributes_for(:deputado) }
before do
create(:despesa, valor_liquido: 10, valor_documento: 100, deputado_id: deputy.id)
create(:despesa, valor_liquido: 12, valor_documento: 120, deputado_id: deputy.id)

create(:despesa, valor_liquido: 13, valor_documento: 130, deputado_id: john_doe.id)

DeputadosIndex.purge!
DeputadosIndex.import
end

let(:deputy) { create(:deputado, nome: "Deputado") }
let(:john_doe) { create(:john_doe) }

render_views

describe 'GET #index' do
context 'via json' do
it 'deve listar os deputados e exibe os atributos corretamente' do
get :index, params: {q: deputado.nome}, format: :json
context 'when using json' do
it 'should list deputies' do
get :index, q: "Deputado", format: :json

json = JSON.parse(response.body)

amostra = json.first
espero_que(amostra).tenha %w(id nome email partido uf url_foto total_despesas total_votos
porcentagem_votos situacao_candidatura)
expect(json.size).to be(1)
end
end

context 'via html' do
before(:each) { get :index, params: {q: deputado.nome}}
it 'should not list non-matching deputy' do
get :index, q: "Deputado", format: :json

it 'deve exibir total de votos' do
expect(response.body).to have_content('votos')
expect(response.body).not_to match("John Doe")
end

it 'deve exibir link Início' do
expect(response.body).to have_link('Início')
context 'should correct return attributes for matching deputies' do
before do
get :index, q: deputy.nome, format: :json
end

it { expect(response.body).to match(%r["id":#{deputy.id}]) }
it { expect(response.body).to match(%r["total_votos":#{deputy.total_votos}]) }
it { expect(response.body).to match(%r["porcentagem_votos":#{deputy.porcentagem_votos}]) }
it { expect(response.body).to match(%r["total_despesas":"R\$ 22,00"]) }

%w(nome email partido uf url_foto situacao_candidatura).each do |attribute|
it { expect(response.body).to match(%r["#{attribute}":"#{deputy.send(attribute)}"]) }
end
end

it 'deve exibir campo de busca no rodapé' do
expect(response.body).to have_selector('footer .search')
%w[nome nome_parlamentar partido matricula email uf].each do |param|
context "should search using #{param} attribute from deputy" do
it 'and returns correct deputy' do
get :index, q: deputy.send(param), format: :json

expect(response.body).to match deputy.nome
end

it 'and does not return incorrect deputy' do
get :index, q: deputy.send(param), format: :json

expect(response.body).not_to match 'John Doe'
end
end
end
end

context 'busca' do
it 'deve retornar resultado para cada parametro' do
[:nome, :nome_parlamentar, :partido, :matricula, :url_foto, :email, :uf].each do |attr|
get :index, params: {q: deputado[attr]}, format: :json
expect(response.body).to_not be_blank
end
context 'when using html' do
before do
get :index, q: deputy.nome
end

it 'nao deve exibir logo no cover' do
get :index, params: {q: deputado.nome}
expect(response.body).to_not have_selector('#logo')
it { expect(response.body).to have_content(deputy.nome_parlamentar) }
it { expect(response.body).to have_content('Partido: PDSK') }
it { expect(response.body).to have_content('Estado: KD') }
it { expect(response.body).to have_content('Total de votos: 123') }
it { expect(response.body).to have_content('Despesas: R$ 22,00') }

it 'should not show info about non-matching deputy' do
expect(response.body).not_to have_content('John Doe')
end
end
end


describe 'GET #show' do
before(:each) { deputado.despesas << FactoryGirl.build(:despesa) }

context 'via json' do
it 'deve exibir o deputado e os atributos corretamente' do
get :show, id: deputado.id, format: :json
before do
end

json = JSON.parse(response.body)
context 'when using json' do
context 'should return correct information' do
before { get :show, id: deputy.id, format: :json }

amostra_despesa = json.first
espero_que(amostra_despesa).tenha %w(tipo total total_liquido)
it { expect(response.body).to match %q[tipo":"descricao] }
it { expect(response.body).to match %q[total":"R\$ 220,00] }
it { expect(response.body).to match %q[total_liquido":"R\$ 22,00] }
end
end

context 'via html' do
it 'deve exibir o total de votos, votos validos e situacao' do
get :show, id: deputado.id
context 'when using html' do
context 'should return correct infomation' do
before { get :show, id: deputy.id }

expect(response.body).to have_content('votos')
expect(response.body).to have_content('votos válidos')
expect(response.body).to have_content(deputado.situacao_candidatura)
it { expect(response.body).to have_content('123 votos') }
it { expect(response.body).to have_content('10,00% votos válidos') }
it { expect(response.body).to have_content('Eleito') }
end
end
end
Expand Down
15 changes: 15 additions & 0 deletions spec/factories/deputado.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,20 @@
total_votos 123
porcentagem_votos 10.0
situacao_candidatura "Eleito"

factory :john_doe do
nome "John Doe"
nome_parlamentar "Jonny"
email "[email protected]"
matricula 90

url_foto "http://url.com/foto"
partido "ANOTHER"
uf "AN"
id_cadastro "54321"
total_votos 456
porcentagem_votos 11.0
situacao_candidatura "Eleito"
end
end
end
7 changes: 7 additions & 0 deletions spec/support/chewy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'chewy/rspec'

RSpec.configure do |config|
config.before(:suite) do
Chewy.strategy(:bypass)
end
end

0 comments on commit a680b7d

Please sign in to comment.