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