From 04ae0187e95471cf81a8c2814503b36b14463e0a Mon Sep 17 00:00:00 2001 From: Dmitry Rocha Date: Sat, 1 Oct 2016 18:23:15 -0300 Subject: [PATCH] Tiny refactory for specs of controller of deputados. [#43] --- .gitignore | 1 + .travis.yml | 2 +- README.md | 3 +- config/{chewy.yml => chewy.yml.template} | 5 +- config/chewy.yml.travis | 3 + config/database.yml.template | 2 +- config/initializers/chewy.rb | 1 + spec/controllers/deputados_spec.rb | 118 +++++++++++++++-------- spec/factories/deputado.rb | 15 +++ spec/support/chewy.rb | 7 ++ 10 files changed, 111 insertions(+), 46 deletions(-) rename config/{chewy.yml => chewy.yml.template} (63%) create mode 100644 config/chewy.yml.travis create mode 100644 spec/support/chewy.rb diff --git a/.gitignore b/.gitignore index cd4cfdd..478afd4 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,7 @@ AnoAtual.* AnoAtual_formated.* config/database.yml +config/chewy.yml docker-compose.yml vendor/bundle /config/deploy.rb diff --git a/.travis.yml b/.travis.yml index ac4720d..4736e02 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,7 +10,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 diff --git a/README.md b/README.md index dfea6b9..ad8382b 100644 --- a/README.md +++ b/README.md @@ -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 já está pronto para ser usado com docker: +2. Copie os arquivos `config/database.yml.template` e `config/chewy.yml.template`, eles já 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): diff --git a/config/chewy.yml b/config/chewy.yml.template similarity index 63% rename from config/chewy.yml rename to config/chewy.yml.template index 88e987c..0d78af4 100644 --- a/config/chewy.yml +++ b/config/chewy.yml.template @@ -1,7 +1,8 @@ # config/chewy.yml # separate environment configs test: - host: 'localhost:9250' + host: 'es:9200' prefix: 'test' + development: - host: 'localhost:9200' \ No newline at end of file + host: 'es:9200' diff --git a/config/chewy.yml.travis b/config/chewy.yml.travis new file mode 100644 index 0000000..8511347 --- /dev/null +++ b/config/chewy.yml.travis @@ -0,0 +1,3 @@ +test: + host: 'localhost:9200' + prefix: 'test' diff --git a/config/database.yml.template b/config/database.yml.template index 62f38ea..98b6c15 100644 --- a/config/database.yml.template +++ b/config/database.yml.template @@ -15,7 +15,7 @@ default: &default pool: 5 username: root password: - host: + host: db port: 3306 development: diff --git a/config/initializers/chewy.rb b/config/initializers/chewy.rb index 5193010..d4e01f3 100644 --- a/config/initializers/chewy.rb +++ b/config/initializers/chewy.rb @@ -1 +1,2 @@ Chewy.root_strategy = :atomic +Chewy.use_after_commit_callbacks = !Rails.env.test? diff --git a/spec/controllers/deputados_spec.rb b/spec/controllers/deputados_spec.rb index 5c4f4f3..da1455b 100644 --- a/spec/controllers/deputados_spec.rb +++ b/spec/controllers/deputados_spec.rb @@ -1,76 +1,112 @@ 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 'when using json' do + context 'should return correct information' do + before { get :show, id: deputy.id, format: :json } - context 'via json' do - it 'deve exibir o deputado e os atributos corretamente' do - get :show, id: deputado.id, format: :json - - json = JSON.parse(response.body) - - 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 + before { get :show, id: deputy.id } + + context 'should return correct infomation' do + 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 - 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) + context 'should correct return @opengraph' do + it { expect(assigns(:opengraph)).to include(title: "Deputado: Deputado") } + it { expect(assigns(:opengraph)).to include(type: "website") } + it { expect(assigns(:opengraph)).to include(url: "http://test.host/deputados/#{deputy.id}") } + it { expect(assigns(:opengraph)).to include(image: "http://url.com/foto") } + it { expect(assigns(:opengraph)).to include(site_name: "Peba") } end end end diff --git a/spec/factories/deputado.rb b/spec/factories/deputado.rb index e56dd33..af0b2be 100644 --- a/spec/factories/deputado.rb +++ b/spec/factories/deputado.rb @@ -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 "john.doe@example.com" + 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 diff --git a/spec/support/chewy.rb b/spec/support/chewy.rb new file mode 100644 index 0000000..2430278 --- /dev/null +++ b/spec/support/chewy.rb @@ -0,0 +1,7 @@ +require 'chewy/rspec' + +RSpec.configure do |config| + config.before(:suite) do + Chewy.strategy(:bypass) + end +end