Skip to content

Commit

Permalink
Allowing Affliations to be loaded from the department csv file (#1214)
Browse files Browse the repository at this point in the history
  • Loading branch information
carolyncole authored Jan 9, 2025
1 parent ad0da73 commit 80ca3fc
Show file tree
Hide file tree
Showing 15 changed files with 93 additions and 24 deletions.
19 changes: 10 additions & 9 deletions app/models/affiliation.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# frozen_string_literal: true
class Affiliation
def self.all
data = []
data << { code: "23100", name: "Astrophysical Sciences" }
data << { code: "HPC", name: "High Performance Computing" }
data << { code: "RDSS", name: "Research Data and Scholarship Services" }
data << { code: "PRDS", name: "Princeton Research Data Service" }
data << { code: "PPPL", name: "Princeton Plasma Physics Laboratory" }
data
class Affiliation < ApplicationRecord
# Affiliation file is loaded onto the servers via pransible into the shared folder
def self.load_from_file(file)
affiliations = CSV.read(file, headers: true, skip_lines: /^JT_CF.*/)
affiliations.each do |data|
code = data["Dept"]
if Affiliation.where(code: ).count == 0
self.create(code: code, name: data["Department Descr"])
end
end
end
end
1 change: 1 addition & 0 deletions config/deploy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
on roles(:rake) do
within release_path do
execute("cd #{release_path} && bundle exec rake load_users:from_registration_list")
execute("cd #{release_path} && bundle exec rake load_affiliations:from_file[#{deploy_to}/shared/departments.csv]")
end
end
end
Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20250108202203_create_affiliations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateAffiliations < ActiveRecord::Migration[7.0]
def change
create_table :affiliations do |t|
t.string :code, null: false
t.string :name, null: false
t.timestamps
end
end
end
9 changes: 8 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions lib/tasks/load_departments.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true
namespace :load_affiliations do
desc "Load in users from the registration list"
task :from_file, [:department_file] => [:environment] do |_, args|
Affiliation.load_from_file(args[:department_file])
end
end
1 change: 1 addition & 0 deletions lib/tasks/servers.rake
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace :servers do
system("rake servers:initialize")
system("rake servers:initialize RAILS_ENV=test")
system("rake load_users:from_registration_list")
system("rake load_affiliations:from_file[spec/fixtures/departments.csv]")
end

desc "Stop development dependencies"
Expand Down
19 changes: 12 additions & 7 deletions spec/controllers/projects_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@

RSpec.describe ProjectsController, type: ["controller", "feature"] do
let(:project) { FactoryBot.create :project }

before do
Affiliation.load_from_file(Rails.root.join("spec", "fixtures", "departments.csv"))
end

describe "#details" do
it "renders an error when requesting json" do
get :details, params: { id: project.id, format: :json }
Expand All @@ -24,11 +29,11 @@

it "shows affiliation code as being saved to the project" do
get :details, params: { id: project.id, format: :json }
expect(JSON.parse(response.body)["departments"]).to include("23100")
.or(include("HPC"))
.or(include("RDSS"))
.or(include("PRDS"))
.or(include("PPPL"))
expect(JSON.parse(response.body)["departments"]).to include("55555")
.or(include("66666"))
.or(include("77777"))
.or(include("88888"))
.or(include("99999"))
end

it "renders the project metadata as xml" do
Expand All @@ -50,8 +55,8 @@
" <Status>#{project.metadata[:status]}</Status>\n" \
" <DataSponsor>#{project.metadata[:data_sponsor]}</DataSponsor>\n" \
" <DataManager>#{project.metadata[:data_manager]}</DataManager>\n" \
" <Department>RDSS</Department>\n" \
" <Department>PRDS</Department>\n" \
" <Department>77777</Department>\n" \
" <Department>88888</Department>\n" \
" <CreatedOn>#{Mediaflux::Time.format_date_for_mediaflux(project.metadata[:created_on])}</CreatedOn>\n" \
" <CreatedBy>#{project.metadata[:created_by]}</CreatedBy>\n" \
" <ProjectID>abc-123</ProjectID>\n" \
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
data_manager: data_manager,
data_user_read_only: data_user_read_only,
data_user_read_write: data_user_read_write,
departments: ["RDSS", "PRDS"],
departments: ["77777", "88888"],
project_directory: project_directory,
title: title,
description: "a random description",
Expand Down
7 changes: 7 additions & 0 deletions spec/fixtures/departments.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
JT_CF_DEPT_TBL,2573,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
Dept,Eff Date,Status,Department Descr,SetID,Short Desc,Descr,Co,Location SetID,Location,Tax Loc,MgrID,Mgr Posn,Yr End Dt,Budget Lvl,GL-Expense,EEO4 Fcn,Ind Sector,Accnt_ins,SI_accdnt#,Hazard,Estab ID,RSK CD,Descr,Sub-Agency,PAR Line 2,PAR Line 3,PAR Line 4,PAR Line 5,PAR2 Descr,PAR3 Descr,PAR4 Descr,PAR5 Descr,Class Unit,Org. Unit,Work Sector,Agency Code,Indus Committee,NACE,FTE Edit,Can Grant,Use TL Distrib?,Use Budgets?,Use Encumb?,Use Distrib?,Budget Dept,Dist Optn,Stats Can Dept,Faculty Code,Manager,Acct Owner,Country Group,Bud. Only,RTBL Data,Sync ID,Sync Date Time
55555,1/1/01,A,AST-Astrophysical Sciences,XXXX,55555,,,,,,,,0,A,,,,,,,,,,,,,,,,,,,,,,0,,,B,C,D,E,F,G,,H,,,,,,I,,9999,1/1/01 12:01
66666,1/1/01,A,HPC-High Performance Computing,XXXX,66666,,,,,,,,0,A,,,,,,,,,,,,,,,,,,,,,,0,,,B,C,D,E,F,G,,H,,,,,,I,,9999,1/1/01 12:01
77777,1/1/01,A,RDSS-Research Data and Scholarship Services,XXXX,77777,,,,,,,,0,A,,,,,,,,,,,,,,,,,,,,,,0,,,B,C,D,E,F,G,,H,,,,,,I,,9999,1/1/01 12:01
88888,1/1/01,A,PRDS-Princeton Research Data Service,XXXX,88888,,,,,,,,0,A,,,,,,,,,,,,,,,,,,,,,,0,,,B,C,D,E,F,G,,H,,,,,,I,,9999,1/1/01 12:01
99999,1/1/01,A,PPPL-Princeton Plasma Physics Laboratory,XXXX,99999,,,,,,,,0,A,,,,,,,,,,,,,,,,,,,,,,0,,,B,C,D,E,F,G,,H,,,,,,I,,9999,1/1/01 12:01
25 changes: 25 additions & 0 deletions spec/models/affiliation_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true
require "rails_helper"

RSpec.describe Affiliation, type: :model do
describe "#load_from_file" do
it "reads the departments from the file" do
expect(Affiliation.count).to eq 0
Affiliation.load_from_file(Rails.root.join("spec", "fixtures", "departments.csv"))
expect(Affiliation.count).to eq 5
expect(Affiliation.where(code: "55555").first.name).to eq("AST-Astrophysical Sciences")
expect(Affiliation.where(code: "66666").first.name).to eq("HPC-High Performance Computing")
expect(Affiliation.where(code: "77777").first.name).to eq("RDSS-Research Data and Scholarship Services")
expect(Affiliation.where(code: "88888").first.name).to eq("PRDS-Princeton Research Data Service")
expect(Affiliation.where(code: "99999").first.name).to eq("PPPL-Princeton Plasma Physics Laboratory")
end

it "only loads the departments once" do
expect(Affiliation.count).to eq 0
Affiliation.load_from_file(Rails.root.join("spec", "fixtures", "departments.csv"))
expect(Affiliation.count).to eq 5
Affiliation.load_from_file(Rails.root.join("spec", "fixtures", "departments.csv"))
expect(Affiliation.count).to eq 5
end
end
end
6 changes: 3 additions & 3 deletions spec/models/mediaflux/project_create_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@
" <Status>#{project.metadata[:status]}</Status>\n" \
" <DataSponsor>#{project.metadata[:data_sponsor]}</DataSponsor>\n" \
" <DataManager>#{project.metadata[:data_manager]}</DataManager>\n" \
" <Department>RDSS</Department>\n" \
" <Department>PRDS</Department>\n" \
" <Department>77777</Department>\n" \
" <Department>88888</Department>\n" \
" <CreatedOn>#{Mediaflux::Time.format_date_for_mediaflux(project.metadata[:created_on])}</CreatedOn>\n" \
" <CreatedBy>#{project.metadata[:created_by]}</CreatedBy>\n" \
" <ProjectID>abc-123</ProjectID>\n" \
Expand Down Expand Up @@ -93,7 +93,7 @@
"<meta><tigerdata:project><ProjectDirectory>#{project.project_directory}</ProjectDirectory><Title>#{project.metadata[:title]}</Title>" \
"<Description>#{project.metadata[:description]}</Description><Status>#{project.metadata[:status]}</Status>" \
"<DataSponsor>#{project.metadata[:data_sponsor]}</DataSponsor><DataManager>#{project.metadata[:data_manager]}</DataManager>" \
"<Department>RDSS</Department><Department>PRDS</Department><CreatedOn>#{Mediaflux::Time.format_date_for_mediaflux(project.metadata[:created_on])}</CreatedOn>" \
"<Department>77777</Department><Department>88888</Department><CreatedOn>#{Mediaflux::Time.format_date_for_mediaflux(project.metadata[:created_on])}</CreatedOn>" \
"<CreatedBy>#{project.metadata[:created_by]}</CreatedBy><ProjectID>abc-123</ProjectID><StorageCapacity><Size>500</Size><Unit>GB</Unit></StorageCapacity>" \
"<Performance Requested='standard'>standard</Performance><Submission><RequestedBy>#{project.metadata[:created_by]}</RequestedBy>" \
"<RequestDateTime>#{Mediaflux::Time.format_date_for_mediaflux(project.metadata[:created_on])}</RequestDateTime></Submission>" \
Expand Down
4 changes: 2 additions & 2 deletions spec/models/mediaflux/project_update_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@
" <SchemaVersion>0.6.1</SchemaVersion>\n" \
" <DataSponsor>#{project.metadata[:data_sponsor]}</DataSponsor>\n" \
" <DataManager>#{project.metadata[:data_manager]}</DataManager>\n" \
" <Department>RDSS</Department>\n" \
" <Department>PRDS</Department>\n" \
" <Department>77777</Department>\n" \
" <Department>88888</Department>\n" \
" <CreatedBy>uid1</CreatedBy>\n" \
" <CreatedOn>17-JUN-2024 20:32:37</CreatedOn>\n" \
" <UpdatedBy>#{project.metadata[:updated_by]}</UpdatedBy>\n" \
Expand Down
4 changes: 4 additions & 0 deletions spec/services/test_project_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
let(:subject) { described_class.new(user:, number: 1, project_prefix: 'test-project') }
let(:user) { FactoryBot.create :user, mediaflux_session: SystemUser.mediaflux_session}

before do
Affiliation.load_from_file(Rails.root.join("spec", "fixtures", "departments.csv"))
end

describe "#generate" do
it "creates a project in mediaflux" do
subject.generate
Expand Down
1 change: 1 addition & 0 deletions spec/system/project_roles_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
let(:read_write) { FactoryBot.create :user }
before do
sign_in sponsor_user
Affiliation.load_from_file(Rails.root.join("spec", "fixtures", "departments.csv"))

# make sure the users exist before the page loads
data_manager
Expand Down
3 changes: 2 additions & 1 deletion spec/system/project_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
data_manager: data_manager.uid,
project_directory: "project-123",
title: "project 123",
departments: ["RDSS"],
departments: ["77777"], # RDSS test code in fixture data
description: "hello world",
data_user_read_only: [read_only.uid],
data_user_read_write: [read_write.uid],
Expand All @@ -41,6 +41,7 @@

before do
sign_in sponsor_user
Affiliation.load_from_file(Rails.root.join("spec", "fixtures", "departments.csv"))
end

context "Show page" do
Expand Down

0 comments on commit 80ca3fc

Please sign in to comment.