Skip to content

Commit

Permalink
resolves #58
Browse files Browse the repository at this point in the history
  • Loading branch information
amtuannguyen committed Jul 23, 2024
1 parent a7218e4 commit 5738565
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 24 deletions.
38 changes: 19 additions & 19 deletions lib/tasks/load_gem_records_csv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,26 @@ def load_gem_records(filename)
converter = lambda { |header| header.downcase }
CSV.foreach(filename, headers: true, header_converters: converter) do |row|
seqgradevent = row['seqgradevent'].strip
unless GemRecord.find_by_seqgradevent(seqgradevent)
unless gr = GemRecord.find_by_seqgradevent(seqgradevent)
gr = GemRecord.new
gr.seqgradevent = seqgradevent
gr.studentname = row['studentname'].strip
gr.sisid = row['sisid'].strip
gr.emailaddress = row['emailaddress'].strip
gr.eventtype = row['eventtype'].strip
gr.eventdate = row['eventdate'].strip
gr.examresult = row['examresult'].strip
gr.title = row['title'].strip
gr.program = row['program'].strip
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
end
gr.seqgradevent = seqgradevent
gr.studentname = row['studentname'].strip
gr.sisid = row['sisid'].strip
gr.emailaddress = row['emailaddress'].strip
gr.eventtype = row['eventtype'].strip
gr.eventdate = row['eventdate'].strip
gr.examresult = row['examresult'].strip
gr.title = row['title'].strip
gr.program = row['program'].strip
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}")
Expand Down
84 changes: 79 additions & 5 deletions test/lib/load_gem_records_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

require Rails.root.join('lib/tasks/load_gem_records_csv.rb')
class LoadGemRecordsTest < ActiveSupport::TestCase
setup do
gem_load = LoadGemRecordsCSV.new
gem_load.load_gem_records("test/fixtures/files/gem_records.csv")
gem_load.load_committee_members("test/fixtures/files/committee_members.csv")
end

GEM_RECORDS_FILE = "test/fixtures/files/gem_records.csv"
COMMITTEE_MEMBERS_FILE = "test/fixtures/files/committee_members.csv"

should "parse csv" do
gem_load = LoadGemRecordsCSV.new
gem_load.load_gem_records(LoadGemRecordsTest::GEM_RECORDS_FILE)
gem_load.load_committee_members(LoadGemRecordsTest::COMMITTEE_MEMBERS_FILE)

assert_equal 3, GemRecord.count

records = GemRecord.order(:seqgradevent)
Expand Down Expand Up @@ -55,4 +57,76 @@ class LoadGemRecordsTest < ActiveSupport::TestCase
assert_equal 'Black', records[2].committee_members.last.last_name
assert_equal 'Committee Member', records[2].committee_members.last.role
end

should "not delete existing GEM records when loading GEM records from CSV" do
# create 5 GEM records
create_list(:gem_record, 5)
assert_equal 5, GemRecord.count

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

# there should be 8 GEM records at this point
assert_equal 8, GemRecord.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
end

should "update existing and matching GEM records when loading GEM records from CSV" do
# load 3 GEM records from CSV files
gem_load = LoadGemRecordsCSV.new
gem_load.load_gem_records(LoadGemRecordsTest::GEM_RECORDS_FILE)
gem_load.load_committee_members(LoadGemRecordsTest::COMMITTEE_MEMBERS_FILE)

# there should be 3 GEM records at this point
assert_equal 3, GemRecord.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 3 GEM records at this point
assert_equal 3, GemRecord.count


records = GemRecord.order(:seqgradevent)

# verify fields are correct
assert_equal 1, records[0].seqgradevent
assert_equal 'John Doe', records[0].studentname
assert_equal 123456789, records[0].sisid
assert_equal '[email protected]', records[0].emailaddress

# change the fields
records[0].studentname = 'does not matter'
records[0].sisid = 'does not matter'
records[0].emailaddress = 'does not matter'
records[0].save!

# RELOAD 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)

records = GemRecord.order(:seqgradevent)

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

# verify fields are updated from the CSV
assert_equal 1, records[0].seqgradevent
assert_equal 'John Doe', records[0].studentname
assert_equal 123456789, records[0].sisid
assert_equal '[email protected]', records[0].emailaddress

end
end

0 comments on commit 5738565

Please sign in to comment.