Commit 5836be61 authored by Zac Wood's avatar Zac Wood

Major refactor. Changed API structure and added much more comprehensive tests.

parent 8b0cf18d
......@@ -3,8 +3,8 @@
class CourseSectionsController < ApplicationController
# Render JSON of all Sections belonging to a given Course.
def index
@course = Course.find(params[:course_id])
@sections = @course.course_sections
@sections = CourseSection.where(course_id: params[:course_id])
render json: @sections
end
end
# Contains all actions having to do with Courses.
class CoursesController < ApplicationController
# Renders JSON of all courses.
# Renders JSON of courses matching params.
def index
@courses = Course.all
@courses = @courses.where(subject: params[:subject].upcase) if params.key?(:subject)
@courses = @courses.where(course_number: params[:course_number]) if params.key?(:course_number)
render json: @courses
end
# Renders JSON of details of a singluar course, such as its sections
def show
@sections = CourseSection.where(course_id: params[:id])
render json: @sections
end
end
......@@ -4,8 +4,9 @@
class Course < ApplicationRecord
# Each course belongs to a +Semester+
belongs_to :semester
has_many :course_sections
# Ensure all necessary fields are present.
# Ensure all necessary are fields present.
validates :course_number, presence: true
validates :subject, presence: true
validates :semester_id, presence: true
......
......@@ -2,7 +2,13 @@
#
# A +Semester+ is a simple model that consists of a +year+ and a +season+, e.g. "Fall 2018".
class Semester < ApplicationRecord
has_many :courses
# Ensure necessary fields are present.
validates :year, presence: true
validates :season, presence: true
def courses
Course.where semester_id: id
end
end
# Registers all routes for the app.
Rails.application.routes.draw do
scope :api do # Register /api routes
resources :courses, only: [:index] do # GET /api/courses
resources :course_sections, only: [:index] # GET /api/courses/:course_id/sections
end
resources :courses, only: [:index, :show]
resources :course_sections, only: [:index]
get 'search', controller: 'search', action: 'index'
post 'generate', controller: 'calendar_generator', action: 'generate'
end
......
......@@ -2,8 +2,12 @@ require 'test_helper'
class CourseSectionsControllerTest < ActionDispatch::IntegrationTest
test 'should get index' do
# get url_for controller: 'course_sections', action: 'index', course_id: 1
get url_for controller: 'course_sections', action: 'index', course_id: 1
get course_sections_url course_id: courses(:cs112).id
assert_response :success
sections_returned = JSON.parse @response.body
num_sections = CourseSection.where(course_id: courses(:cs112).id).count
assert_equal num_sections, sections_returned.count
end
end
require 'test_helper'
class CoursesControllerTest < ActionDispatch::IntegrationTest
test 'should get index' do
get url_for controller: 'courses', action: 'index'
test '#index should return all courses' do
get courses_url
assert_response :success
courses_returned = JSON.parse @response.body
courses_count = Course.all.count
assert_equal courses_count, courses_returned.count
end
test '#index should return filtered by subject' do
get courses_url subject: "CS"
assert_response :success
courses_returned = JSON.parse @response.body
courses_count = Course.where(subject: "CS").count
assert_equal courses_count, courses_returned.count
end
test '#index should return filtered by subject and course number' do
get courses_url subject: "CS", course_number: "112"
assert_response :success
courses_returned = JSON.parse @response.body
courses_count = Course.where(subject: "CS", course_number: "112").count
assert_equal courses_count, courses_returned.count
end
test '#show should return course_sections for course' do
cs_112_id = courses(:cs112).id
get course_url id: cs_112_id
assert_response :success
sections_returned = JSON.parse @response.body
cs_112_sections = CourseSection.where(course_id: cs_112_id)
assert_equal cs_112_sections.count, sections_returned.count
end
end
require 'test_helper'
class SearchControllerTest < ActionDispatch::IntegrationTest
test "should get index and search by crn" do
get url_for controller: 'search', action: 'index', crn: 'MyString'
assert_response :success
end
test "should 404 without crn" do
get url_for controller: 'search', action: 'index'
assert_response :missing
end
end
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
date: 2018-05-05
may1st: # tuesday
date: 2018-05-01
semester: one
two:
date: 2018-05-05
may2nd: # wednesday
date: 2018-05-02
semester: two
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
id: 1
cs112001:
name: MyString
crn: MyString
section_type: MyString
......@@ -17,11 +16,29 @@ one:
campus: MyString
notes: MyString
size_limit: 1
course: one
course: cs112
two:
cs112002:
name: MyString2
crn: MyString2
section_type: MyString
title: MyString
instructor: MyString
start_date: 2018-04-07
end_date: 2018-04-07
days: MyString
start_time: MyString
end_time: MyString
location: MyString
status: MyString
campus: MyString
notes: MyString
size_limit: 1
course: cs112
cs211001:
name: MyString
crn: MyString
crn: MyString3
section_type: MyString
title: MyString
instructor: MyString
......@@ -35,4 +52,4 @@ two:
campus: MyString
notes: MyString
size_limit: 1
course: two
course: cs211
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
id: 1
subject: MyString
course_number: MyString
semester: one
two:
subject: MyString
course_number: MyString
semester: two
cs112:
subject: CS
course_number: 112
semester: fall2018
cs211:
subject: CS
course_number: 211
semester: fall2018
acct110:
subject: ACCT
course_number: 110
semester: spring2018
cs110:
subject: CS
course_number: 110
semester: spring2018
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
id: 1
season: MyString
year: MyString
fall2018:
season: Fall
year: 2018
two:
season: MyString
year: MyString
spring2018:
season: Spring
year: 2018
......@@ -5,10 +5,7 @@ class CourseSectionTest < ActiveSupport::TestCase
assert_raise do
CourseSection.create! name: nil,
crn: nil,
title: nil,
start_date: nil,
end_date: nil,
days: nil
title: nil
end
end
......@@ -16,9 +13,6 @@ class CourseSectionTest < ActiveSupport::TestCase
CourseSection.create! name: 'Test section',
crn: '12345',
title: 'Test title',
start_date: Time.zone.today,
end_date: Time.zone.today,
days: 'MWF',
course_id: 1
course_id: courses(:cs211).id
end
end
......@@ -8,6 +8,10 @@ class CourseTest < ActiveSupport::TestCase
end
test 'creates with proper data' do
Course.create! course_number: '112', subject: 'CS', semester_id: 1
Course.create! course_number: '112', subject: 'CS', semester_id: semesters(:fall2018).id
end
test 'has correct number of sections' do
assert_equal 2, courses(:cs112).course_sections.count
end
end
......@@ -10,4 +10,8 @@ class SemesterTest < ActiveSupport::TestCase
test 'create successful' do
Semester.create!(season: 'Test', year: 'Test')
end
test 'semester has correct number of courses' do
assert_equal 2, semesters(:fall2018).courses.count
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