course_section_test.rb 2.04 KB
Newer Older
Zac Wood's avatar
Zac Wood committed
1 2
require 'test_helper'

Zac Wood's avatar
Zac Wood committed
3
class CourseSectionTest < ActiveSupport::TestCase
4 5
  test 'fails with improper data' do
    assert_raise do
Zac Wood's avatar
Zac Wood committed
6
      CourseSection.create! name: nil,
7
                            crn: nil
8 9 10 11
    end
  end

  test 'succeeds with proper data' do
Zac Wood's avatar
Zac Wood committed
12
    CourseSection.create! name: 'Test section',
Zac Wood's avatar
Zac Wood committed
13
                          crn: '12345',
14 15 16
                          course: courses(:cs211),
                          instructor: instructors(:luke),
                          semester: semesters(:fall2018)
17 18 19 20 21
  end

  test '#with_instructor filters correctly' do
    section = CourseSection.with_instructor.first
    assert section.instructor_name != ""
22
  end
23 24 25

  test '#latest_by_crn sorts correctly' do
    s = CourseSection.latest_by_crn(70192)
26
    assert_equal semesters(:fall2018).id, s.semester.id
27
  end
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

  test '#find_or_update_by! correctly updates' do
    initial = {
      name: 'CS 211 099',
      crn: '99999',
      title: 'Test 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(:luke),
      semester: semesters(:fall2018)
    }
    a = CourseSection.create!(initial)

    updated = {
      name: 'CS 211 099',
      crn: '99999',
      title: 'New title',
      start_date: '2019-02-01',
      end_date: '2019-02-02',
      days: 'TR',
      start_time: '4:00pm',
      end_time: '5:00pm',
      location: 'SUB 1',
      course: courses(:cs211),
      instructor: instructors(:kinga),
      semester: semesters(:fall2018)
    }
    b = CourseSection.find_or_update_by!(updated)

    # 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
      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
    end

    assert_equal a.id, b.id
  end
Zac Wood's avatar
Zac Wood committed
79
end