Skip to content

Test cases for depts #1545

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
- Removed `privacy` route and codes related to it
- Improved general testing infrastructure
- Added test cases for the index function in `Controllers/Course`
- Added test cases for the depts function in `Controllers/Course`
- Refactored the class components in `/grid` folder to function components
- Update stylelint integration with development environment and fix existing stylelint violations
- Use `magick` command instead of `convert` when serving graph images
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ This will take longer but results in smaller asset files.
To run all tests, run `$ yarn run test`. However, if you need to run a specific file or folder of tests,
run `$ yarn run test -- <path>`.

#### Running back-end tests

To run all tests, run `$ stack test`, or equivalently `$ stack test :Tests`.

#### Developers

If you are contributing to Courseography, you should run the following to install and test our pre-commit hooks:
Expand Down Expand Up @@ -112,6 +116,7 @@ Nazanin Ghazitabatabai,
Sidharth Gupta,
Parker Hutcheson,
Jai Joshi,
Aayush Karki,
Philip Kukulak,
Jaeyong Lee,
Ryan Lee,
Expand Down
57 changes: 41 additions & 16 deletions backend-test/Controllers/CourseControllerTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Controllers.CourseControllerTests

import Config (runDb)
import Control.Monad (unless)
import Controllers.Course (index, retrieveCourse)
import Controllers.Course (depts, index, retrieveCourse)
import qualified Data.ByteString.Lazy.Char8 as BL
import qualified Data.Map as Map
import Data.Maybe (fromMaybe)
Expand All @@ -25,32 +25,32 @@ import TestHelpers (clearDatabase, runServerPart, runServerPartWithQuery)
-- | List of test cases as (input course name, course data, expected JSON output)
retrieveCourseTestCases :: [(String, T.Text, Map.Map T.Text T.Text, String)]
retrieveCourseTestCases =
[ ("Course exists",
"STA238",
[ ("Course exists",
"STA238",
Map.fromList [
("name", "STA238H1"),
("title", "Probability, Statistics and Data Analysis II"),
("description", "An introduction to statistical inference and practice. Statistical models and parameters, estimators of parameters and their statistical properties, methods of estimation, confidence intervals, hypothesis testing, likelihood function, the linear model. Use of statistical computation for data analysis and simulation."),
("prereqs", "STA237H1/ STA247H1/ STA257H1/ STAB52H3/ STA256H5"),
("exclusions", "ECO220Y1/ ECO227Y1/ GGR270H1/ PSY201H1/ SOC300H1/ SOC202H1/ SOC252H1/ STA220H1/ STA221H1/ STA255H1/ STA248H1/ STA261H1/ STA288H1/ EEB225H1/ STAB22H3/ STAB27H3/ STAB57H3/ STA220H5/ STA221H5/ STA258H5/ STA260H5/ ECO220Y5/ ECO227Y5"),
("breadth", "The Physical and Mathematical Universes (5)"),
("breadth", "The Physical and Mathematical Universes (5)"),
("distribution", "null"),
("prereqString", "STA237H1/ STA247H1/ STA257H1/ STAB52H3/ STA256H5"),
("coreqs", "CSC108H1/ CSC110Y1/ CSC148H1 *Note: the corequisite may be completed either concurrently or in advance."),
("videoUrls", "https://example.com/video1, https://example.com/video2")
],
"{\"allMeetingTimes\":[],\"breadth\":null,\"coreqs\":\"CSC108H1/ CSC110Y1/ CSC148H1 *Note: the corequisite may be completed either concurrently or in advance.\",\"description\":\"An introduction to statistical inference and practice. Statistical models and parameters, estimators of parameters and their statistical properties, methods of estimation, confidence intervals, hypothesis testing, likelihood function, the linear model. Use of statistical computation for data analysis and simulation.\",\"distribution\":null,\"exclusions\":\"ECO220Y1/ ECO227Y1/ GGR270H1/ PSY201H1/ SOC300H1/ SOC202H1/ SOC252H1/ STA220H1/ STA221H1/ STA255H1/ STA248H1/ STA261H1/ STA288H1/ EEB225H1/ STAB22H3/ STAB27H3/ STAB57H3/ STA220H5/ STA221H5/ STA258H5/ STA260H5/ ECO220Y5/ ECO227Y5\",\"name\":\"STA238H1\",\"prereqString\":\"STA237H1/ STA247H1/ STA257H1/ STAB52H3/ STA256H5\",\"title\":\"Probability, Statistics and Data Analysis II\",\"videoUrls\":[\"https://example.com/video1\",\"https://example.com/video2\"]}"
),
("Course does not exist",
"STA238",
Map.empty,

("Course does not exist",
"STA238",
Map.empty,
"null"
),
("No course provided",
"",
Map.empty,

("No course provided",
"",
Map.empty,
"null"
)
]
Expand All @@ -60,12 +60,12 @@ runRetrieveCourseTest :: String -> T.Text -> Map.Map T.Text T.Text -> String ->
runRetrieveCourseTest label courseName courseData expected =
TestLabel label $ TestCase $ do
let currCourseName = fromMaybe "" $ Map.lookup "name" courseData

let videoUrls = case Map.lookup "videoUrls" courseData of
Just urlsText -> map T.strip (T.splitOn "," urlsText)
Nothing -> []

let courseToInsert =
let courseToInsert =
Courses
{ coursesCode = currCourseName
, coursesTitle = Map.lookup "title" courseData
Expand Down Expand Up @@ -98,7 +98,7 @@ insertCourses = mapM_ insertCourse
where
insertCourse code = insert_ (Courses code Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing [])

-- | List of test cases as (input courses, label, expected output)
-- | List of test cases as (label, input courses, expected output)
indexTestCases :: [(String, [T.Text], String)]
indexTestCases =
[ ("Empty database", [], "")
Expand All @@ -122,6 +122,31 @@ runIndexTest label courses expected =
runIndexTests :: [Test]
runIndexTests = map (\(label, courses, expected) -> runIndexTest label courses expected) indexTestCases

-- | List of dept test cases; formatted as (test label, input db courses, expected output)
deptsTestCases :: [(String, [T.Text], String)]
deptsTestCases =
[
("empty db", [], "[]"),
("one course", ["MAT137"], "[\"MAT\"]"),
("multiple, diff depts", ["STA237", "CSC236", "MAT237"], "[\"CSC\",\"MAT\",\"STA\"]"),
("multiple, same dept", ["CSC110", "CSC111", "CSC108"], "[\"CSC\"]")
]

-- | Run a test case (args: case description/label, input, expected output) on the depts function
runDeptsTest :: String -> [T.Text] -> String -> Test
runDeptsTest label courses expected =
TestLabel label $ TestCase $ do
runDb $ do
clearDatabase
insertCourses courses
response <- runServerPart Controllers.Course.depts
let actual = BL.unpack $ rsBody response
assertEqual ("Unexpected output for test: " ++ label) expected actual

-- | Run all test cases for depts
runDeptsTests :: [Test]
runDeptsTests = map (\(label, courses, expected) -> runDeptsTest label courses expected) deptsTestCases

-- | Test suite for Course Controller Module
courseControllerTestSuite :: Test
courseControllerTestSuite = TestLabel "Course Controller tests" $ TestList (runRetrieveCourseTests ++ runIndexTests)
courseControllerTestSuite = TestLabel "Course Controller tests" $ TestList (runRetrieveCourseTests ++ runIndexTests ++ runDeptsTests)