Browse Source

Add unit tests for user scope

pull/467/head
Kat 3 years ago
parent
commit
f48f7eb680
  1. 4
      app/models/case_log.rb
  2. 26
      spec/models/case_log_spec.rb

4
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)) }

26
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

Loading…
Cancel
Save