From f48f7eb680c921dc68f8fd4834602de5d734d91c Mon Sep 17 00:00:00 2001 From: Kat Date: Tue, 12 Apr 2022 14:56:21 +0100 Subject: [PATCH] Add unit tests for user scope --- app/models/case_log.rb | 4 ++-- spec/models/case_log_spec.rb | 26 ++++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 7274ecc29..f3f7648f0 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -34,8 +34,8 @@ class CaseLog < ApplicationRecord belongs_to :managing_organisation, class_name: "Organisation" scope :for_organisation, ->(org) { where(owning_organisation: org).or(where(managing_organisation: org)) } - scope :filter_by_status, ->(status, user = nil) { where status: status } - scope :filter_by_years, lambda { |years, user = nil| + scope :filter_by_status, ->(status, _user = nil) { where status: status } + scope :filter_by_years, lambda { |years, _user = nil| first_year = years.shift query = filter_by_year(first_year) years.each { |year| query = query.or(filter_by_year(year)) } diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index 971b75056..8a04fc1e9 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -1827,9 +1827,10 @@ RSpec.describe CaseLog do end describe "scopes" do + let!(:case_log_1) { FactoryBot.create(:case_log, :in_progress, startdate: Time.utc(2021, 5, 3)) } + let!(:case_log_2) { FactoryBot.create(:case_log, :completed, startdate: Time.utc(2021, 5, 3)) } + before do - FactoryBot.create(:case_log, :in_progress, startdate: Time.utc(2021, 5, 3)) - FactoryBot.create(:case_log, :completed, startdate: Time.utc(2021, 5, 3)) FactoryBot.create(:case_log, startdate: Time.utc(2022, 6, 3)) end @@ -1856,5 +1857,26 @@ RSpec.describe CaseLog do expect(described_class.filter_by_status(%w[in_progress completed]).count).to eq(3) end end + + context "when filtering by user" do + let!(:user) { FactoryBot.create(:user) } + + before do + PaperTrail::Version.find_by(item_id: case_log_1.id, event: "create").update!(whodunnit: user.to_global_id.uri.to_s) + PaperTrail::Version.find_by(item_id: case_log_2.id, event: "create").update!(whodunnit: user.to_global_id.uri.to_s) + end + + it "allows filtering on current user" do + expect(described_class.filter_by_user(%w[yours], user).count).to eq(2) + end + + it "returns all logs when all logs selected" do + expect(described_class.filter_by_user(%w[all], user).count).to eq(3) + end + + it "returns all logs when all and your users selected" do + expect(described_class.filter_by_user(%w[all yours], user).count).to eq(3) + end + end end end