Skip to content

Commit

Permalink
rspec has Line Coverage: 92.96 (251 / 270)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kingofikings441 committed Oct 28, 2024
1 parent 760f787 commit e3edf2b
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 0 deletions.
118 changes: 118 additions & 0 deletions spec/controllers/courses_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe CoursesController, type: :controller do
render_views
let(:schedule) { Schedule.create!(schedule_name: 'Fall Semester', semester_name: '2024') }

describe 'GET #index' do
before do
@user = User.create!(uid: '12345', provider: 'google_oauth2', email: '[email protected]', first_name: 'John',
last_name: 'Doe')
allow(controller).to receive(:logged_in?).and_return(true)
controller.instance_variable_set(:@current_user, @user)
end

it 'incorrect schedule id' do
get :index, params: { schedule_id: (schedule.id + 1) }
expect(flash[:alert]).to eq('Schedule not found.')
end

context 'without added courses ' do
before do
get :index, params: { schedule_id: schedule.id }
end

it 'try seeing the courses' do
expect(assigns(:courses)).to be_empty
end

it 'displays a message saying no courses added to this schedule' do
expect(response.body).to include('No courses added to this schedule!')
end
end
context 'with added courses ' do
let!(:course1) do
create(:course, course_number: '120', max_seats: 90, lecture_type: 'Hybrid', num_labs: 3, schedule_id: schedule.id)
end
let!(:section11) do
create(:section, section_number: '500', seats_alloted: 30, course_id: course1.id)
end
let!(:section12) do
create(:section, section_number: '501', seats_alloted: 30, course_id: course1.id)
end
let!(:section13) do
create(:section, section_number: '502', seats_alloted: 30, course_id: course1.id)
end
let!(:course2) do
create(:course, course_number: '320', max_seats: 120, lecture_type: 'Online', num_labs: 2, schedule_id: schedule.id)
end
let!(:section21) do
create(:section, section_number: '500', seats_alloted: 60, course_id: course2.id)
end
let!(:section22) do
create(:section, section_number: '501', seats_alloted: 60, course_id: course2.id)
end
let!(:course3) do
create(:course, course_number: '400', max_seats: 150, lecture_type: 'F2F', num_labs: 1, schedule_id: schedule.id)
end
let!(:section31) do
create(:section, section_number: '500', seats_alloted: 150, course_id: course3.id)
end


context 'without any sorting' do
it 'assigns all courses to @courses' do
get :index, params: { schedule_id: schedule.id }
expect(assigns(:courses)).to match_array([course1, course2, course3])
end
end


context 'with sorting by course_number' do
it 'assigns courses sorted by course_number ascending to @courses' do
get :index, params: { schedule_id: schedule.id, sort: 'course_number', direction: 'asc' }
expect(assigns(:courses)).to eq([course1, course2, course3])
end
it 'assigns courses sorted by course_number descending to @courses' do
get :index, params: { schedule_id: schedule.id, sort: 'course_number', direction: 'desc' }
expect(assigns(:courses)).to eq([course3, course2, course1])
end
end

context 'with sorting by max_seats' do
it 'assigns courses sorted by max_seats ascending to @courses' do
get :index, params: { schedule_id: schedule.id, sort: 'max_seats', direction: 'asc' }
expect(assigns(:courses)).to eq([course1, course2, course3])
end
it 'assigns courses sorted by max_seats descending to @courses' do
get :index, params: { schedule_id: schedule.id, sort: 'max_seats', direction: 'desc' }
expect(assigns(:courses)).to eq([course3, course2, course1])
end
end

context 'with sorting by lecture_type' do
it 'assigns courses sorted by lecture_type ascending to @courses' do
get :index, params: { schedule_id: schedule.id, sort: 'lecture_type', direction: 'asc' }
expect(assigns(:courses)).to eq([course3, course1, course2])
end
it 'assigns courses sorted by lecture_type descending to @courses' do
get :index, params: { schedule_id: schedule.id, sort: 'lecture_type', direction: 'desc' }
expect(assigns(:courses)).to eq([course2, course1, course3])
end
end

context 'with sorting by num_labs' do
it 'assigns courses sorted by num_labs ascending to @courses' do
get :index, params: { schedule_id: schedule.id, sort: 'num_labs', direction: 'asc' }
expect(assigns(:courses)).to eq([course3, course2, course1])
end
it 'assigns courses sorted by num_labs descending to @courses' do
get :index, params: { schedule_id: schedule.id, sort: 'num_labs', direction: 'desc' }
expect(assigns(:courses)).to eq([course1, course2, course3])
end
end
end
end
end
10 changes: 10 additions & 0 deletions spec/fixtures/courses/Course_list_invalid.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
110,96,F2F,4,"500,501,502,503","24,24,24,24",None,,, ,,,
110,96,F2F,4,"504,505,506, 507","24,24,24,24",None,,,,,,
111,96,F2F,4,"500,501,502,503","24,24,24,24",None,,,,,,
111/708 (86/10),96,F2F,5,"504, 505, 506, 507","24, 24, 24, 24",None,,,,,,
120,96,F2F,4,"500,501,502,503","24,24,24,24",None,,,,,,
120,96,F2F,4,"504,505,506,507","24,24,24,24",None,,,,,,
120,96,F2F,4,"508,509,510,511","24,24,24,24",None,,,,,,
120,96,F2F,4,"512,513,545,515","24,24,24,24",None,,,,,,
120,96,F2F,4,"516,517,518,519","24,24,24,24",None,,,,,,
435/735/735D,135,F2F,2,"435-500, 735-600/700","95,40","CPSC, CECN, COMP, CEEN, CPEN",,,,,,
29 changes: 29 additions & 0 deletions spec/services/csv_handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
let(:invalid_room_csv) { File.read(Rails.root.join('spec', 'fixtures', 'rooms', 'rooms_invalid.csv')) }
let(:valid_instructor_csv) { File.read(Rails.root.join('spec', 'fixtures', 'instructors', 'instructors_valid.csv')) }
let(:invalid_instructor_csv) { File.read(Rails.root.join('spec', 'fixtures', 'instructors', 'instructors_missing_headers.csv')) }
let(:valid_course_csv) { File.read(Rails.root.join('spec', 'fixtures', 'courses', 'Course_list_valid.csv')) }
let(:invalid_course_csv) { File.read(Rails.root.join('spec', 'fixtures', 'courses', 'Course_list_invalid.csv')) }

describe '#initialize' do
it 'initializes with a file' do
Expand Down Expand Up @@ -71,4 +73,31 @@
end
end
end

describe '#parse_course_csv' do
context 'with valid data' do
it 'creates course records' do
handler = CsvHandler.new
handler.upload(StringIO.new(valid_course_csv))
handler.parse_course_csv(schedule.id)
expect(Course.count).to eq(10)
end

it 'returns a success message' do
handler = CsvHandler.new
handler.upload(StringIO.new(valid_course_csv))
result = handler.parse_course_csv(schedule.id)
expect(result[:notice]).to eq('Courses successfully uploaded.')
end
end

context 'with invalid data' do
it 'prints an alert' do
handler = CsvHandler.new
handler.upload(StringIO.new(invalid_course_csv))
result = handler.parse_course_csv(schedule.id)
expect(result[:alert]).to include('Missing required headers: Class, Max. Seats, Lecture Type, #Labs, Section number, Seat Split')
end
end
end
end

0 comments on commit e3edf2b

Please sign in to comment.