Commit f99cc1c9 authored by Zac Wood's avatar Zac Wood

clean up records that shouldn't have been created

parent 108bfda6
Pipeline #5332 passed with stage
in 11 seconds
......@@ -47,11 +47,18 @@ class CourseSection < ApplicationRecord
def self.find_or_update_by!(s)
base_attrs = { crn: s[:crn], course: s[:course], semester: s[:semester] }
section = self.find_by(base_attrs)
if section.nil?
sections = CourseSection.where(base_attrs)
if sections.empty?
section = CourseSection.new(base_attrs)
else
sections = sections.to_a
section = sections.shift
sections.each do |bad_section|
bad_section.destroy
end
end
section.name = s[:name]
section.title = s[:title]
section.start_date = s[:start_date]
section.end_date = s[:end_date]
......
......@@ -43,6 +43,22 @@ class CourseSectionTest < ActiveSupport::TestCase
}
a = CourseSection.create!(initial)
other_initial = {
name: 'CS 211 099',
crn: '99999',
title: 'Bad title',
start_date: '2019-01-01',
end_date: '2019-01-02',
days: 'MWF',
start_time: '1:00pm',
end_time: '2:00pm',
location: 'ENGR',
course: courses(:cs211),
instructor: instructors(:otten),
semester: semesters(:fall2018)
}
b = CourseSection.create!(other_initial)
updated = {
name: 'CS 211 099',
crn: '99999',
......@@ -57,23 +73,25 @@ class CourseSectionTest < ActiveSupport::TestCase
instructor: instructors(:kinga),
semester: semesters(:fall2018)
}
b = CourseSection.find_or_update_by!(updated)
c = CourseSection.find_or_update_by!(updated)
assert_nil CourseSection.find_by(other_initial)
# reload a with the new attributes
a.reload
# ensure each field matches the updated value
updated.each do |key, value|
obj_val = b.method(key).call
obj_val = c.method(key).call
if obj_val.is_a? Date
assert_equal value, obj_val.strftime("%Y-%m-%d")
else
assert_equal value, obj_val
end
assert_equal a.method(key).call, b.method(key).call
assert_equal a.method(key).call, c.method(key).call
end
assert_equal a.id, b.id
assert_equal a.id, c.id
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment