Skip to content

Commit aeaa90c

Browse files
committed
Tiny refactory for specs of controller of deputados.
[teresinahc#43]
1 parent c9fd14d commit aeaa90c

File tree

9 files changed

+110
-45
lines changed

9 files changed

+110
-45
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
AnoAtual.*
1515
AnoAtual_formated.*
1616
config/database.yml
17+
config/chewy.yml
1718
docker-compose.yml
1819
vendor/bundle
1920
/config/deploy.rb

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ services:
1010

1111
before_script:
1212
- cp config/database.yml.template config/database.yml
13-
- sleep 10
13+
- cp config/chewy.yml.travis config/chewy.yml
1414

1515
script:
1616
- RAILS_ENV=test bundle exec rake db:setup --trace

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,10 @@ Antes de mais nada, tenha certeza de que tenha `docker` e `docker-compose` insta
6868

6969
cp docker-compose.yml.template docker-compose.yml
7070

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

7373
cp config/database.yml.template config/database.yml
74+
cp config/chewy.yml.template config/chewy.yml
7475

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# config/chewy.yml
22
# separate environment configs
33
test:
4-
host: 'localhost:9250'
4+
host: 'es:9200'
55
prefix: 'test'
6+
67
development:
7-
host: 'localhost:9200'
8+
host: 'es:9200'

config/chewy.yml.travis

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
test:
2+
host: 'localhost:9200'
3+
prefix: 'test'

config/initializers/chewy.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
Chewy.root_strategy = :atomic
2+
Chewy.use_after_commit_callbacks = !Rails.env.test?

spec/controllers/deputados_spec.rb

Lines changed: 77 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,112 @@
11
require 'rails_helper'
22

33
describe DeputadosController, type: :controller do
4-
let(:deputado) { Deputado.create! FactoryGirl.attributes_for(:deputado) }
4+
before do
5+
create(:despesa, valor_liquido: 10, valor_documento: 100, deputado_id: deputy.id)
6+
create(:despesa, valor_liquido: 12, valor_documento: 120, deputado_id: deputy.id)
7+
8+
create(:despesa, valor_liquido: 13, valor_documento: 130, deputado_id: john_doe.id)
9+
10+
DeputadosIndex.purge!
11+
DeputadosIndex.import
12+
end
13+
14+
let(:deputy) { create(:deputado, nome: "Deputado") }
15+
let(:john_doe) { create(:john_doe) }
516

617
render_views
718

819
describe 'GET #index' do
9-
context 'via json' do
10-
it 'deve listar os deputados e exibe os atributos corretamente' do
11-
get :index, params: {q: deputado.nome}, format: :json
20+
context 'when using json' do
21+
it 'should list deputies' do
22+
get :index, q: "Deputado", format: :json
1223

1324
json = JSON.parse(response.body)
1425

15-
amostra = json.first
16-
espero_que(amostra).tenha %w(id nome email partido uf url_foto total_despesas total_votos
17-
porcentagem_votos situacao_candidatura)
26+
expect(json.size).to be(1)
1827
end
19-
end
2028

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

24-
it 'deve exibir total de votos' do
25-
expect(response.body).to have_content('votos')
32+
expect(response.body).not_to match("John Doe")
2633
end
2734

28-
it 'deve exibir link Início' do
29-
expect(response.body).to have_link('Início')
35+
context 'should correct return attributes for matching deputies' do
36+
before do
37+
get :index, q: deputy.nome, format: :json
38+
end
39+
40+
it { expect(response.body).to match(%r["id":#{deputy.id}]) }
41+
it { expect(response.body).to match(%r["total_votos":#{deputy.total_votos}]) }
42+
it { expect(response.body).to match(%r["porcentagem_votos":#{deputy.porcentagem_votos}]) }
43+
it { expect(response.body).to match(%r["total_despesas":"R\$ 22,00"]) }
44+
45+
%w(nome email partido uf url_foto situacao_candidatura).each do |attribute|
46+
it { expect(response.body).to match(%r["#{attribute}":"#{deputy.send(attribute)}"]) }
47+
end
3048
end
3149

32-
it 'deve exibir campo de busca no rodapé' do
33-
expect(response.body).to have_selector('footer .search')
50+
%w[nome nome_parlamentar partido matricula email uf].each do |param|
51+
context "should search using #{param} attribute from deputy" do
52+
it 'and returns correct deputy' do
53+
get :index, q: deputy.send(param), format: :json
54+
55+
expect(response.body).to match deputy.nome
56+
end
57+
58+
it 'and does not return incorrect deputy' do
59+
get :index, q: deputy.send(param), format: :json
60+
61+
expect(response.body).not_to match 'John Doe'
62+
end
63+
end
3464
end
3565
end
3666

37-
context 'busca' do
38-
it 'deve retornar resultado para cada parametro' do
39-
[:nome, :nome_parlamentar, :partido, :matricula, :url_foto, :email, :uf].each do |attr|
40-
get :index, params: {q: deputado[attr]}, format: :json
41-
expect(response.body).to_not be_blank
42-
end
67+
context 'when using html' do
68+
before do
69+
get :index, q: deputy.nome
4370
end
4471

45-
it 'nao deve exibir logo no cover' do
46-
get :index, params: {q: deputado.nome}
47-
expect(response.body).to_not have_selector('#logo')
72+
it { expect(response.body).to have_content(deputy.nome_parlamentar) }
73+
it { expect(response.body).to have_content('Partido: PDSK') }
74+
it { expect(response.body).to have_content('Estado: KD') }
75+
it { expect(response.body).to have_content('Total de votos: 123') }
76+
it { expect(response.body).to have_content('Despesas: R$ 22,00') }
77+
78+
it 'should not show info about non-matching deputy' do
79+
expect(response.body).not_to have_content('John Doe')
4880
end
4981
end
5082
end
5183

52-
5384
describe 'GET #show' do
54-
before(:each) { deputado.despesas << FactoryGirl.build(:despesa) }
85+
context 'when using json' do
86+
context 'should return correct information' do
87+
before { get :show, id: deputy.id, format: :json }
5588

56-
context 'via json' do
57-
it 'deve exibir o deputado e os atributos corretamente' do
58-
get :show, id: deputado.id, format: :json
59-
60-
json = JSON.parse(response.body)
61-
62-
amostra_despesa = json.first
63-
espero_que(amostra_despesa).tenha %w(tipo total total_liquido)
89+
it { expect(response.body).to match %q[tipo":"descricao] }
90+
it { expect(response.body).to match %q[total":"R\$ 220,00] }
91+
it { expect(response.body).to match %q[total_liquido":"R\$ 22,00] }
6492
end
6593
end
6694

67-
context 'via html' do
68-
it 'deve exibir o total de votos, votos validos e situacao' do
69-
get :show, id: deputado.id
95+
context 'when using html' do
96+
before { get :show, id: deputy.id }
97+
98+
context 'should return correct infomation' do
99+
it { expect(response.body).to have_content('123 votos') }
100+
it { expect(response.body).to have_content('10,00% votos válidos') }
101+
it { expect(response.body).to have_content('Eleito') }
102+
end
70103

71-
expect(response.body).to have_content('votos')
72-
expect(response.body).to have_content('votos válidos')
73-
expect(response.body).to have_content(deputado.situacao_candidatura)
104+
context 'should correct return @opengraph' do
105+
it { expect(assigns(:opengraph)).to include(title: "Deputado: Deputado") }
106+
it { expect(assigns(:opengraph)).to include(type: "website") }
107+
it { expect(assigns(:opengraph)).to include(url: "http://test.host/deputados/#{deputy.id}") }
108+
it { expect(assigns(:opengraph)).to include(image: "http://url.com/foto") }
109+
it { expect(assigns(:opengraph)).to include(site_name: "Peba") }
74110
end
75111
end
76112
end

spec/factories/deputado.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,20 @@
1212
total_votos 123
1313
porcentagem_votos 10.0
1414
situacao_candidatura "Eleito"
15+
16+
factory :john_doe do
17+
nome "John Doe"
18+
nome_parlamentar "Jonny"
19+
20+
matricula 90
21+
22+
url_foto "http://url.com/foto"
23+
partido "ANOTHER"
24+
uf "AN"
25+
id_cadastro "54321"
26+
total_votos 456
27+
porcentagem_votos 11.0
28+
situacao_candidatura "Eleito"
29+
end
1530
end
1631
end

spec/support/chewy.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require 'chewy/rspec'
2+
3+
RSpec.configure do |config|
4+
config.before(:suite) do
5+
Chewy.strategy(:bypass)
6+
end
7+
end

0 commit comments

Comments
 (0)