Skip to content

Commit

Permalink
resolves #59
Browse files Browse the repository at this point in the history
  • Loading branch information
amtuannguyen committed Jul 23, 2024
1 parent bd37c3c commit 788ec7f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
34 changes: 16 additions & 18 deletions lib/tasks/load_gem_records_csv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
class LoadGemRecordsCSV

def load_gem_records(filename)
count = 0

converter = lambda { |header| header.downcase }
CSV.foreach(filename, headers: true, header_converters: converter) do |row|
seqgradevent = row['seqgradevent'].strip
Expand All @@ -29,15 +27,7 @@ def load_gem_records(filename)
gr.superv = row['superv'].strip
gr.examdate = row['examdate'].strip

if gr.save!(validate: false)
count += 1
else
warn('Error: Load Gem Records Save Failed!')
warn("Error: #{gr.errors.inspect}")
end
rescue StandardError => e
warn("ERROR: #{e}")
warn('Hint: Possible Bad File if strip nil')
gr.save!
end
end

Expand All @@ -47,13 +37,21 @@ def load_committee_members(filename)
converter = lambda { |header| header.downcase }
CSV.foreach(filename, headers: true, header_converters: converter) do |row|
seqgradevent = row['seqgradevent'].strip
gr = GemRecord.find_by_seqgradevent(seqgradevent)
cm = CommitteeMember.new
cm.gem_record = gr
cm.first_name = row['firstname'].strip
cm.last_name = row['surname'].strip
cm.role = row['role'].strip
cm.save!
sisid = row['sisid'].strip
first_name = row['firstname'].strip
last_name = row['surname'].strip
role = row['role'].strip

if gr = GemRecord.find_by_seqgradevent(seqgradevent)
unless cm = CommitteeMember.find_by(gem_record_id: gr.id, first_name: first_name, last_name: last_name, role: role)
cm = CommitteeMember.new
cm.gem_record = gr
cm.first_name = first_name
cm.last_name = last_name
cm.role = role
cm.save!
end
end
end
end
end
9 changes: 9 additions & 0 deletions test/lib/load_gem_records_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,19 @@ class LoadGemRecordsTest < ActiveSupport::TestCase
# there should be 8 GEM records at this point
assert_equal 8, GemRecord.count

# there should be 6 CommitteeMember
assert_equal 6, CommitteeMember.count

# load 3 GEM records from the exact same CSV files AGAIN
gem_load = LoadGemRecordsCSV.new
gem_load.load_gem_records(LoadGemRecordsTest::GEM_RECORDS_FILE)
gem_load.load_committee_members(LoadGemRecordsTest::COMMITTEE_MEMBERS_FILE)

# there should STILL be 8 GEM records at this point
assert_equal 8, GemRecord.count

# there should STILL be 6 CommitteeMember
assert_equal 6, CommitteeMember.count
end

should "update existing and matching GEM records when loading GEM records from CSV" do
Expand Down Expand Up @@ -121,6 +127,9 @@ class LoadGemRecordsTest < ActiveSupport::TestCase

# there should STILL be 3 GEM records at this point
assert_equal 3, GemRecord.count

# there should STILL be 6 CommitteeMember
assert_equal 6, CommitteeMember.count

# verify fields are updated from the CSV
assert_equal 1, records[0].seqgradevent
Expand Down

0 comments on commit 788ec7f

Please sign in to comment.