Skip to content

Commit

Permalink
Ensure all resource types are shown in programmes dashboard. Fixes #1995
Browse files Browse the repository at this point in the history
  • Loading branch information
fbacall committed Sep 25, 2024
1 parent 53859a5 commit 742f155
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
4 changes: 2 additions & 2 deletions app/models/programme.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Programme < ApplicationRecord
has_many :investigations, -> { distinct }, through: :projects
has_many :studies, -> { distinct }, through: :investigations
has_many :assays, -> { distinct }, through: :studies
%i[data_files documents models sops presentations events publications samples workflows].each do |type|
%i[data_files documents models sops presentations events publications samples workflows collections].each do |type|
has_many type, -> { distinct }, through: :projects
end
has_many :programme_administrator_roles, -> { where(role_type_id: RoleType.find_by_key!(:programme_administrator)) }, as: :scope, class_name: 'Role', inverse_of: :scope
Expand Down Expand Up @@ -73,7 +73,7 @@ def human_diseases
end

def assets
(data_files + models + sops + presentations + events + publications + documents).uniq.compact
data_files | sops | models | publications | presentations | documents | workflows | collections
end

def has_member?(user_or_person)
Expand Down
49 changes: 49 additions & 0 deletions test/unit/dashboard_stats_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,53 @@ class DashboardStatsTest < ActiveSupport::TestCase

assert Rails.cache.exist?(another_project_key)
end

test 'contribution stats' do
Project.delete_all
Programme.delete_all

programme = FactoryBot.create(:min_programme)
project = FactoryBot.create(:project, programme: programme)
person = FactoryBot.create(:person, project: project)
programme.reload # To refresh the `projects` association

assets = [:data_file, :sop, :model, :publication, :presentation, :document, :workflow, :collection].map do |type|
FactoryBot.create(type, projects: [project], contributor: person)
end

investigation = FactoryBot.create(:investigation, projects: [project], contributor: person)
study = FactoryBot.create(:study, investigation: investigation, contributor: person)
assay = FactoryBot.create(:assay, study: study, contributor: person)

assets + [investigation, study, assay]

assets.each do |asset|
FactoryBot.create(:activity_log, action: 'create', activity_loggable: asset, culprit: person)
end

instance_stats = Seek::Stats::DashboardStats.new
project_stats = Seek::Stats::ProjectDashboardStats.new(project)
programme_stats = Seek::Stats::ProgrammeDashboardStats.new(programme)

opts = [2.days.ago, 2.days.from_now, 'month']
types = [DataFile, Sop, Model, Publication, Presentation, Document, Workflow, Collection, Investigation, Study, Assay]

c = instance_stats.contributions(*opts)
(types + [Project, Programme]).each do |t|
assert c[:datasets].key?(t), "#{t.name} missing from instance contributions stats"
assert_equal [1], c[:datasets][t], "#{t.name} unexpected number in instance contributions stats"
end

c = programme_stats.contributions(*opts)
(types + [Project]).each do |t|
assert c[:datasets].key?(t), "#{t.name} missing from programme contributions stats"
assert_equal [1], c[:datasets][t], "#{t.name} unexpected number in instance contributions stats"
end

c = project_stats.contributions(*opts)
types.each do |t|
assert c[:datasets].key?(t), "#{t.name} missing from project contributions stats"
assert_equal [1], c[:datasets][t], "#{t.name} unexpected number in instance contributions stats"
end
end
end

0 comments on commit 742f155

Please sign in to comment.