Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System tests #303

Merged
merged 13 commits into from
Mar 17, 2020
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ Layout/DotPosition:
Naming/HeredocDelimiterNaming:
Enabled: false

Style/BracesAroundHashParameters:
EnforcedStyle: context_dependent

Style/SymbolArray:
EnforcedStyle: brackets

Expand Down
13 changes: 9 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,21 @@ gem 'appsignal'

group :development, :test do
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
gem 'capybara'
gem 'database_cleaner-active_record'
gem 'factory_bot_rails'
gem 'rails-controller-testing'
gem 'rspec-collection_matchers'
gem 'rspec-rails', '~> 3.8'
gem 'timecop'
gem 'pry'
end

group :test do
gem 'capybara'
gem 'rails-controller-testing'
gem 'rspec-collection_matchers'
gem 'rspec-rails', '~> 4.0.rc'
gem 'selenium-webdriver'
gem 'webdrivers', '~> 4.0'
end

group :development do
gem 'annotate'
gem 'brakeman', require: false
Expand Down
28 changes: 19 additions & 9 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ GEM
rack-test (>= 0.6.3)
regexp_parser (~> 1.5)
xpath (~> 3.2)
childprocess (3.0.0)
coderay (1.1.2)
concurrent-ruby (1.1.5)
connection_pool (2.2.2)
Expand Down Expand Up @@ -382,14 +383,14 @@ GEM
rspec-mocks (3.9.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.9.0)
rspec-rails (3.9.0)
actionpack (>= 3.0)
activesupport (>= 3.0)
railties (>= 3.0)
rspec-core (~> 3.9.0)
rspec-expectations (~> 3.9.0)
rspec-mocks (~> 3.9.0)
rspec-support (~> 3.9.0)
rspec-rails (4.0.0.rc1)
actionpack (>= 4.2)
activesupport (>= 4.2)
railties (>= 4.2)
rspec-core (~> 3.9)
rspec-expectations (~> 3.9)
rspec-mocks (~> 3.9)
rspec-support (~> 3.9)
rspec-support (3.9.2)
rubocop (0.79.0)
jaro_winkler (~> 1.5.1)
Expand Down Expand Up @@ -425,6 +426,9 @@ GEM
faraday (> 0.8, < 2.0)
select2-rails (4.0.3)
thor (~> 0.14)
selenium-webdriver (3.142.7)
childprocess (>= 0.5, < 4.0)
rubyzip (>= 1.2.2)
signet (0.12.0)
addressable (~> 2.3)
faraday (~> 0.9)
Expand Down Expand Up @@ -472,6 +476,10 @@ GEM
activemodel (>= 6.0.0)
bindex (>= 0.4.0)
railties (>= 6.0.0)
webdrivers (4.2.0)
nokogiri (~> 1.6)
rubyzip (>= 1.3.0)
selenium-webdriver (>= 3.0, < 4.0)
webpacker (4.2.2)
activesupport (>= 4.2)
rack-proxy (>= 0.6.1)
Expand Down Expand Up @@ -533,11 +541,12 @@ DEPENDENCIES
rails-controller-testing
react-rails
rspec-collection_matchers
rspec-rails (~> 3.8)
rspec-rails (~> 4.0.rc)
rubocop
rubocop-performance
rubyzip
sass-rails (>= 6)
selenium-webdriver
simplecov
spring
spring-watcher-listen (~> 2.0.0)
Expand All @@ -546,6 +555,7 @@ DEPENDENCIES
tzinfo-data
uglifier (>= 1.3.0)
web-console (>= 3.3.0)
webdrivers (~> 4.0)
webpacker

RUBY VERSION
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ def index
)
end

# rubocop:disable Metrics/AbcSize
def show
@legislations = CCLOW::LegislationDecorator.decorate_collection(@litigation.legislations)
add_breadcrumb('Litigation cases', cclow_geography_litigation_cases_path(@geography.slug))
add_breadcrumb(@litigation.title, request.path)
@sectors = @litigation.laws_sectors.order(:name)
@keywords = @litigation.keywords.order(:name)
@responses = @litigation.responses.order(:name)
@litigation_sides = @litigation.litigation_sides.map { |ls| CCLOW::LitigationSideDecorator.decorate(ls) }
@litigation_sides = @litigation.litigation_sides
@litigation_events = @litigation.events.order(:date)
@litigation_events_with_links = @litigation_events.map do |e|
::Api::Presenters::Event.call(e, :litigation)
Expand All @@ -34,7 +33,6 @@ def show
admin_litigation_path(@litigation)
)
end
# rubocop:enable Metrics/AbcSize

private

Expand Down
17 changes: 0 additions & 17 deletions app/decorators/cclow/litigation_side_decorator.rb

This file was deleted.

12 changes: 8 additions & 4 deletions app/decorators/cclow/target_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ def link
link_title = model.description || [model.target_type.humanize, model.year].compact.join(', ')

if model.sector
h.link_to(link_title,
h.cclow_geography_geography_climate_targets_sector_path(model.geography.slug,
model.sector.name))
h.link_to(
link_title,
h.cclow_geography_geography_climate_targets_sector_path(
model.geography.slug,
model.sector.name
)
)
else
h.link_to(link_title, h.cclow_geography_climate_targets_path(model.geography))
h.link_to(link_title, h.cclow_geography_climate_targets_path(model.geography.slug))
end
end
# rubocop:enable Metrics/AbcSize
Expand Down
4 changes: 2 additions & 2 deletions app/javascript/components/LawsDropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ const LawsDropdown = () => {
<LawsDropdownCategory title={CATEGORIES.countries} icon={countryFlag}>
{searchGeographiesResults.map(geography => (
<a
href={`/cclow/geographies/${geography.id}`}
onClick={() => setLastSearch(searchValue, CATEGORIES.countries, `/cclow/geographies/${geography.id}`)}
href={`/cclow/geographies/${geography.slug}`}
onClick={() => setLastSearch(searchValue, CATEGORIES.countries, `/cclow/geographies/${geography.slug}`)}
key={geography.slug}
className="laws-dropdown__option"
>
Expand Down
4 changes: 2 additions & 2 deletions app/models/cclow_map_content_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def number_of_climate_laws_and_policies
.joins(:legislations)
.where(legislations: {visibility_status: ::Legislation.visibility_statuses[:published]})
.group(:iso)
.pluck('iso, count(*) as count')
.pluck(Arel.sql('iso, count(*) as count'))
.map do |data|
{
geography_iso: data[0],
Expand All @@ -39,7 +39,7 @@ def number_of_climate_lawsuits
.joins(:litigations)
.where(litigations: {visibility_status: Litigation.visibility_statuses[:published]})
.group(:iso)
.pluck('iso, count(*) as count')
.pluck(Arel.sql('iso, count(*) as count'))
.map do |data|
{
geography_iso: data[0],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<% litigations.each do |litigation| %>
<li>
<h5 class="title">
<%= link_to litigation.title, cclow_geography_litigation_case_path(litigation.geography, litigation.slug) %>
<%= link_to litigation.title, cclow_geography_litigation_case_path(litigation.geography.slug, litigation.slug) %>
</h5>
<p class="meta">
<% if litigation.started_event.present? %>
Expand Down
4 changes: 2 additions & 2 deletions app/views/cclow/geography/climate_targets/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

<% @list.each do |data| %>
<hr class="is-hidden-desktop">
<a href="<%= "/cclow/geographies/#{@geography.id}/climate_targets/#{data[:sector]}" %>" class="item active is-hidden-desktop">
<a href="<%= "/cclow/geographies/#{@geography.slug}/climate_targets/#{data[:sector]}" %>" class="item active is-hidden-desktop">
<div class="sector-name"><%= data[:sector] %></div>
<div class="columns">
<div class="column is-half">Targets in NDC content</div>
Expand Down Expand Up @@ -53,7 +53,7 @@
<hr class="is-hidden-desktop">

<% @list.each do |data| %>
<a href="<%= "/cclow/geographies/#{@geography.id}/climate_targets/#{data[:sector]}" %>" class="columns item active is-hidden-touch">
<a href="<%= "/cclow/geographies/#{@geography.slug}/climate_targets/#{data[:sector]}" %>" class="columns item active is-hidden-touch">
<div class="column is-one-thirds sector-name"><%= data[:sector] %></div>
<div class="column is-one-thirds value">
<% if !data[:ndc_targets_count].zero? %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
<% target.legislations.each do |legislation|%>
<div class="name">
<% path = legislation.policy? ? 'cclow_geography_policy_path' : 'cclow_geography_law_path' %>
<%= link_to legislation.title, send(path, @geography, legislation) %>
<%= link_to legislation.title, send(path, @geography.slug, legislation.slug) %>
</div>
<p class="meta">
<span>
Expand Down
2 changes: 1 addition & 1 deletion app/views/cclow/geography/legislations/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
<% if @legislation.litigations.any? %>
<section>
<h5>Related litigation cases</h5>
<%= render 'releted_litigations_list', litigations: @legislation.litigations %>
<%= render 'related_litigations_list', litigations: @legislation.litigations %>
</section>
<% end %>
</div>
4 changes: 2 additions & 2 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# admin users
# envs: DEV
if Rails.env.development? && !AdminUser.find_by(email: 'admin@example.com')
if (Rails.env.development? || Rails.env.test?) && !AdminUser.find_by(email: 'admin@example.com')
AdminUser.create!(
email: 'admin@example.com',
password: 'password', password_confirmation: 'password',
Expand Down Expand Up @@ -88,7 +88,7 @@ def seed_file(filename)
File.open(Rails.root.join('db', 'seeds', filename), 'r')
end

if Rails.env.development? || ENV['SEED_DATA']
if Rails.env.development? || Rails.env.test? || ENV['SEED_DATA']
# import geographies
TimedLogger.log('Import geographies') do
CSVImport::Geographies.new(seed_file('geographies.csv')).call
Expand Down
Binary file added db/test-dump.psql
Binary file not shown.
34 changes: 34 additions & 0 deletions lib/tasks/test.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace :test do
desc 'Recreate DB dump used in system tests by running seeds and dumping database'
task :db_dump do
abort 'Run recreatedb task only in test env! RAILS_ENV=test bin/rails test:db_dump' unless Rails.env.test?

Rake::Task['db:test:prepare'].invoke
Rails.application.load_seed
system "pg_dump -Fc --no-owner --dbname=#{db_name} > #{dump_path}"
DatabaseCleaner.clean_with(:truncation)
end

task :db_load do
unless File.exist?(dump_path)
puts "Dump file #{dump_path} does not existing. Invoking db_dump task to create it"
Rake::Task['test:db_dump'].invoke
end

system "pg_restore -j 8 --clean --no-owner --dbname=#{db_name} #{dump_path}"
end

private

def dump_path
Rails.root.join('db/test-dump.psql').to_path
end

def db_name
"postgresql://#{db_config['username']}:#{db_config['password']}@127.0.0.1:5432/#{db_config['database']}"
end

def db_config
Rails.configuration.database_configuration[Rails.env]
end
end
16 changes: 16 additions & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
require 'rspec/rails'
require 'cancan/matchers'

require 'rake'
Rails.application.load_tasks

# Add additional requires below this line. Rails is not loaded until this point!

# Requires supporting ruby files with custom matchers and macros, etc, in
Expand Down Expand Up @@ -41,6 +44,7 @@
RSpec.configure do |config|
config.include Devise::Test::ControllerHelpers, type: :controller
config.include FactoryBot::Syntax::Methods
config.include CapybaraHelpers, type: :system

config.render_views
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
Expand Down Expand Up @@ -73,4 +77,16 @@
config.after :each, type: :controller do
ActiveSupport::CurrentAttributes.reset_all
end

config.before(:each, type: :system) do
driven_by :selenium, using: :headless_chrome, screen_size: [1400, 800]
end

config.before(:all, type: :system) do
Rake::Task['test:db_load'].execute
end

config.after(:all, type: :system) do
DatabaseCleaner.clean_with(:truncation)
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'simplecov'
require 'timecop'
require 'capybara/rspec'

SimpleCov.start do
add_filter '/spec/'
Expand Down
5 changes: 5 additions & 0 deletions spec/support/capybara_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module CapybaraHelpers
def contains_class(class_name)
"contains(concat(' ', normalize-space(@class), ' '), ' #{class_name} ')"
end
end
12 changes: 12 additions & 0 deletions spec/system/public/cclow/climate_targets_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'rails_helper'

describe 'Climate Targets search', type: 'system' do
before do
visit 'cclow/climate_targets'
end

it 'loads the page' do
expect(page).to have_text('All Climate Targets')
expect(page).to have_text('Showing 1993 results')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests would need to be updated if we change the test dump, right? Not sure if I'm understanding correctly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's why it's not that great. Although, seeds were not changed for a quite a while

end
end
Loading