Browse Source

Add additional tests (#108)

* Spec case logs admin table

* Spec admin user admin index

* Dashboard controller panel specs

* Spec panel contents

* Add create specs

* Don't assign non db ids if we don't need them

* Fix specs for new section

* Fix up fields
pull/111/head
Daniel Baark 3 years ago committed by GitHub
parent
commit
50c50806be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      app/constants/db_enums.rb
  2. 2
      app/models/bulk_upload.rb
  3. 13
      app/models/case_log.rb
  4. 12
      app/models/form/question.rb
  5. 9
      db/migrate/20211124174732_remove_old_fields.rb
  6. 7
      db/schema.rb
  7. 30
      spec/controllers/admin/admin_users_controller_spec.rb
  8. 29
      spec/controllers/admin/case_logs_controller_spec.rb
  9. 41
      spec/controllers/admin/dashboard_controller_spec.rb
  10. 8
      spec/factories/admin_user.rb
  11. 5
      spec/factories/case_log.rb
  12. 1
      spec/factories/user.rb
  13. 9
      spec/models/case_log_spec.rb
  14. 8
      spec/support/controller_macros.rb

2
app/constants/db_enums.rb

@ -22,7 +22,7 @@ module DbEnums
def self.builtype
{
"Purpose built" => 1,
"Conversion" => 2
"Conversion" => 2,
}
end

2
app/models/bulk_upload.rb

@ -159,7 +159,7 @@ class BulkUpload
# property_reference: row[100],
beds: row[101],
unittype_gn: row[102],
property_building_type: row[103],
builtype: row[103],
wchair: row[104],
property_relet: row[105],
rsnvac: row[106],

13
app/models/case_log.rb

@ -112,9 +112,12 @@ class CaseLog < ApplicationRecord
enum armedforces: DbEnums.armed_forces, _suffix: true
enum first_time_property_let_as_social_housing: DbEnums.polar, _suffix: true
enum unitletas: DbEnums.unitletas, _suffix: true
enum builtype: DbEnums.builtype, _suffix: true
AUTOGENERATED_FIELDS = %w[id status created_at updated_at discarded_at].freeze
OPTIONAL_FIELDS = %w[do_you_know_the_postcode do_you_know_the_local_authority].freeze
OPTIONAL_FIELDS = %w[do_you_know_the_postcode
do_you_know_the_local_authority
first_time_property_let_as_social_housing].freeze
def self.editable_fields
attribute_names - AUTOGENERATED_FIELDS
@ -236,6 +239,14 @@ private
dynamically_not_required << "incfreq"
end
if sale_or_letting == "Letting"
dynamically_not_required << "sale_completion_date"
end
if la.present?
dynamically_not_required << "why_dont_you_know_la"
end
if tenancy == "Secure (including flexible)"
dynamically_not_required << "tenancylength"
end

12
app/models/form/question.rb

@ -33,12 +33,6 @@ class Form::Question
!!readonly
end
def conditional_on
@conditional_on ||= form.conditional_question_conditions.select do |condition|
condition[:to] == id
end
end
def enabled?(case_log)
return true if conditional_on.blank?
@ -61,6 +55,12 @@ private
answer.join(", ")
end
def conditional_on
@conditional_on ||= form.conditional_question_conditions.select do |condition|
condition[:to] == id
end
end
def evaluate_condition(condition, case_log)
case page.questions.find { |q| q.id == condition[:from] }.type
when "numeric"

9
db/migrate/20211124174732_remove_old_fields.rb

@ -0,0 +1,9 @@
class RemoveOldFields < ActiveRecord::Migration[6.1]
def up
remove_column :case_logs, :property_building_type
end
def down
add_column :case_logs, :property_building_type, :string
end
end

7
db/schema.rb

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2021_11_24_152204) do
ActiveRecord::Schema.define(version: 2021_11_24_174732) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -35,7 +35,6 @@ ActiveRecord::Schema.define(version: 2021_11_24_152204) do
t.integer "ethnic"
t.integer "national"
t.integer "prevten"
t.string "armed_forces"
t.integer "ecstat1"
t.integer "hhmemb"
t.string "relat2"
@ -70,7 +69,6 @@ ActiveRecord::Schema.define(version: 2021_11_24_152204) do
t.integer "underoccupation_benefitcap"
t.integer "leftreg"
t.integer "reservist"
t.string "armed_forces_partner"
t.integer "illness"
t.integer "preg_occ"
t.string "accessibility_requirements"
@ -84,7 +82,6 @@ ActiveRecord::Schema.define(version: 2021_11_24_152204) do
t.string "previous_postcode"
t.integer "rsnvac"
t.integer "unittype_gn"
t.string "property_building_type"
t.integer "beds"
t.string "property_void_date"
t.integer "offered"
@ -100,6 +97,7 @@ ActiveRecord::Schema.define(version: 2021_11_24_152204) do
t.integer "tcharge"
t.integer "layear"
t.integer "lawaitlist"
t.string "property_postcode"
t.integer "reasonpref"
t.string "reasonable_preference_reason"
t.integer "cbl"
@ -164,7 +162,6 @@ ActiveRecord::Schema.define(version: 2021_11_24_152204) do
t.datetime "sale_completion_date"
t.datetime "startdate"
t.integer "armedforces"
t.string "property_postcode"
t.integer "first_time_property_let_as_social_housing"
t.string "why_dont_you_know_la"
t.integer "unitletas"

30
spec/controllers/admin/admin_users_controller_spec.rb

@ -0,0 +1,30 @@
require "rails_helper"
require_relative "../../support/devise"
describe Admin::AdminUsersController, type: :controller do
render_views
let(:page) { Capybara::Node::Simple.new(response.body) }
let(:resource_title) { "Admin Users" }
let(:valid_session) { {} }
login_admin_user
describe "Get admin users" do
before do
get :index, session: valid_session
end
it "returns a table of admin users" do
expect(page).to have_content(resource_title)
expect(page).to have_table("index_table_admin_users")
expect(page).to have_link(AdminUser.first.id.to_s)
end
end
describe "Create admin users" do
let(:params) { { admin_user: { email: "test2@example.com", password: "pAssword1" } } }
it "creates a new admin users" do
expect { post :create, session: valid_session, params: params }.to change(AdminUser, :count).by(1)
end
end
end

29
spec/controllers/admin/case_logs_controller_spec.rb

@ -0,0 +1,29 @@
require "rails_helper"
require_relative "../../support/devise"
describe Admin::CaseLogsController, type: :controller do
render_views
let(:page) { Capybara::Node::Simple.new(response.body) }
let(:resource_title) { "Case Logs" }
let(:valid_session) { {} }
login_admin_user
describe "Get case logs" do
let!(:case_log) { FactoryBot.create(:case_log, :in_progress) }
before do
get :index, session: valid_session
end
it "returns a table of case logs" do
expect(page).to have_content(resource_title)
expect(page).to have_table("index_table_case_logs")
expect(page).to have_link(case_log.id.to_s)
end
end
describe "Create case logs" do
it "creates a new case log" do
expect { post :create, session: valid_session }.to change(CaseLog, :count).by(1)
end
end
end

41
spec/controllers/admin/dashboard_controller_spec.rb

@ -0,0 +1,41 @@
require "rails_helper"
require_relative "../../support/devise"
describe Admin::DashboardController, type: :controller do
render_views
let(:page) { Capybara::Node::Simple.new(response.body) }
let(:resource_title) { "Dashboard" }
let!(:case_log) { FactoryBot.create(:case_log, :in_progress) }
let!(:case_log_2) { FactoryBot.create(:case_log, :in_progress) }
let!(:completed_case_log) { FactoryBot.create(:case_log, :completed) }
let(:valid_session) { {} }
login_admin_user
describe "Get case logs" do
before do
get :index, session: valid_session
end
it "returns a dashboard page" do
expect(page).to have_content(resource_title)
end
it "returns a panel of recent case logs" do
expect(page).to have_xpath("//div[contains(@class, 'panel') and contains(//h3, 'Recent Case Logs')]")
end
it "returns a panel of in progress case logs" do
panel_xpath = "//div[@class='panel' and .//h3[contains(., 'Total case logs in progress')]]"
panel_content_xpath = "#{panel_xpath}//div[@class='panel_contents' and .//p[contains(., 2)]]"
expect(page).to have_xpath(panel_xpath)
expect(page).to have_xpath(panel_content_xpath)
end
it "returns a panel of completed case logs" do
panel_xpath = "//div[@class='panel' and .//h3[contains(., 'Total case logs completed')]]"
panel_content_xpath = "#{panel_xpath}//div[@class='panel_contents' and .//p[contains(., 1)]]"
expect(page).to have_xpath(panel_xpath)
expect(page).to have_xpath(panel_content_xpath)
end
end
end

8
spec/factories/admin_user.rb

@ -0,0 +1,8 @@
FactoryBot.define do
factory :admin_user do
email { "admin@example.com" }
password { "pAssword1" }
created_at { Time.zone.now }
updated_at { Time.zone.now }
end
end

5
spec/factories/case_log.rb

@ -56,7 +56,6 @@ FactoryBot.define do
previous_postcode { "SE2 6RT" }
rsnvac { "Tenant abandoned property" }
unittype_gn { "House" }
property_building_type { "dummy" }
beds { 3 }
property_void_date { "03/11/2019" }
offered { 2 }
@ -133,8 +132,10 @@ FactoryBot.define do
mrcyear { 2020 }
incref { 554_355 }
sale_completion_date { nil }
startdate { nil }
startdate { Time.zone.now }
armedforces { 1 }
builtype { 1 }
unitletas { 2 }
end
created_at { Time.zone.now }
updated_at { Time.zone.now }

1
spec/factories/user.rb

@ -1,6 +1,5 @@
FactoryBot.define do
factory :user do
sequence(:id) { |i| i }
email { "test@example.com" }
password { "pAssword1" }
created_at { Time.zone.now }

9
spec/models/case_log_spec.rb

@ -330,6 +330,7 @@ RSpec.describe Form, type: :model do
describe "status" do
let!(:empty_case_log) { FactoryBot.create(:case_log) }
let!(:in_progress_case_log) { FactoryBot.create(:case_log, :in_progress) }
let!(:completed_case_log) { FactoryBot.create(:case_log, :completed) }
it "is set to not started for an empty case log" do
expect(empty_case_log.not_started?).to be(true)
@ -337,11 +338,17 @@ RSpec.describe Form, type: :model do
expect(empty_case_log.completed?).to be(false)
end
it "is set to not started for an empty case log" do
it "is set to in progress for a started case log" do
expect(in_progress_case_log.in_progress?).to be(true)
expect(in_progress_case_log.not_started?).to be(false)
expect(in_progress_case_log.completed?).to be(false)
end
it "is set to completed for a completed case log" do
expect(completed_case_log.in_progress?).to be(false)
expect(completed_case_log.not_started?).to be(false)
expect(completed_case_log.completed?).to be(true)
end
end
describe "weekly_net_income" do

8
spec/support/controller_macros.rb

@ -13,4 +13,12 @@ module ControllerMacros
sign_in user
end
end
def login_admin_user
before(:each) do
@request.env["devise.mapping"] = Devise.mappings[:admin_user]
admin_user = FactoryBot.create(:admin_user)
sign_in admin_user
end
end
end

Loading…
Cancel
Save