From 8ee5acdce3a25104d9f7221842cec847fdb4b09c Mon Sep 17 00:00:00 2001 From: Aayush Karki Date: Tue, 13 May 2025 14:49:10 -0400 Subject: [PATCH 01/12] added test cases for depts --- .../Controllers/CourseControllerTests.hs | 59 +++++++++++++------ 1 file changed, 42 insertions(+), 17 deletions(-) diff --git a/backend-test/Controllers/CourseControllerTests.hs b/backend-test/Controllers/CourseControllerTests.hs index d926d966b..a6ab00ac1 100644 --- a/backend-test/Controllers/CourseControllerTests.hs +++ b/backend-test/Controllers/CourseControllerTests.hs @@ -17,23 +17,23 @@ import qualified Data.Map as Map import Data.Maybe (fromMaybe) import qualified Data.Text as T import Database.Persist.Sqlite (SqlPersistM, insert_) -import Database.Tables (Courses(..)) +import Database.Tables (Courses (..)) import Happstack.Server (rsBody) -import Test.HUnit (Test(..), assertEqual) +import Test.HUnit (Test (..), assertEqual) 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."), @@ -41,16 +41,16 @@ retrieveCourseTestCases = ], "{\"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" ) ] @@ -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 @@ -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", [], "") @@ -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\n"), + ("multiple, diff depts", ["STA237", "CSC236", "MAT237"], "CSC\nMAT\nSTA\n"), + ("multiple, same dept", ["CSC110", "CSC111", "CSC108"], "CSC\n") + ] + +-- | 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) From 2537767157dfc50e48690dbc284184e84b3d6dfe Mon Sep 17 00:00:00 2001 From: Aayush Karki Date: Tue, 13 May 2025 14:51:33 -0400 Subject: [PATCH 02/12] added self to list of contributors --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index e2425926d..cfe474f9e 100644 --- a/README.md +++ b/README.md @@ -112,6 +112,7 @@ Nazanin Ghazitabatabai, Sidharth Gupta, Parker Hutcheson, Jai Joshi, +Aayush Karki, Philip Kukulak, Jaeyong Lee, Ryan Lee, From 00e53ff2cb4742224a0bf7d00402f664fbee1be2 Mon Sep 17 00:00:00 2001 From: Aayush Karki Date: Tue, 13 May 2025 15:09:17 -0400 Subject: [PATCH 03/12] updated changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f817cd1c4..af81e7729 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 From 7afff308344dfdd1a32560f6c3ee4a2c211c8888 Mon Sep 17 00:00:00 2001 From: Aayush Karki Date: Tue, 13 May 2025 17:16:33 -0400 Subject: [PATCH 04/12] added missing import for depts (oops) --- backend-test/Controllers/CourseControllerTests.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend-test/Controllers/CourseControllerTests.hs b/backend-test/Controllers/CourseControllerTests.hs index a6ab00ac1..9c8625c6c 100644 --- a/backend-test/Controllers/CourseControllerTests.hs +++ b/backend-test/Controllers/CourseControllerTests.hs @@ -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) From 52a15b08a457d880482220665b3720537c291bbe Mon Sep 17 00:00:00 2001 From: Aayush Karki Date: Tue, 13 May 2025 17:26:59 -0400 Subject: [PATCH 05/12] fixed typo --- backend-test/Controllers/CourseControllerTests.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend-test/Controllers/CourseControllerTests.hs b/backend-test/Controllers/CourseControllerTests.hs index 9c8625c6c..bf7732f64 100644 --- a/backend-test/Controllers/CourseControllerTests.hs +++ b/backend-test/Controllers/CourseControllerTests.hs @@ -124,7 +124,7 @@ runIndexTests = map (\(label, courses, expected) -> runIndexTest label courses e -- | List of dept test cases; formatted as (test label, input db courses, expected output) deptsTestCases :: [(String, [T.Text], String)] -deptsTestCases :: +deptsTestCases = [ ("empty db", [], ""), ("one course", ["MAT137"], "MAT\n"), From 58d10dc6492c652402f9a49177e04494643fa1ad Mon Sep 17 00:00:00 2001 From: Aayush Karki Date: Tue, 13 May 2025 18:13:20 -0400 Subject: [PATCH 06/12] fixed expected value strings to match JSON formatting --- backend-test/Controllers/CourseControllerTests.hs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/backend-test/Controllers/CourseControllerTests.hs b/backend-test/Controllers/CourseControllerTests.hs index bf7732f64..976e57e24 100644 --- a/backend-test/Controllers/CourseControllerTests.hs +++ b/backend-test/Controllers/CourseControllerTests.hs @@ -126,10 +126,10 @@ runIndexTests = map (\(label, courses, expected) -> runIndexTest label courses e deptsTestCases :: [(String, [T.Text], String)] deptsTestCases = [ - ("empty db", [], ""), - ("one course", ["MAT137"], "MAT\n"), - ("multiple, diff depts", ["STA237", "CSC236", "MAT237"], "CSC\nMAT\nSTA\n"), - ("multiple, same dept", ["CSC110", "CSC111", "CSC108"], "CSC\n") + ("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 From 929a66e48543548088e369c7c25f829bca0f3da9 Mon Sep 17 00:00:00 2001 From: Aayush Karki Date: Fri, 16 May 2025 03:35:45 -0400 Subject: [PATCH 07/12] reverted unintentional spaces From b320dc87e3bba245f7b4b683ca84e1c2f13ce691 Mon Sep 17 00:00:00 2001 From: Aayush Karki Date: Fri, 16 May 2025 04:16:34 -0400 Subject: [PATCH 08/12] reverted extra spaces --- backend-test/Controllers/CourseControllerTests.hs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/backend-test/Controllers/CourseControllerTests.hs b/backend-test/Controllers/CourseControllerTests.hs index 976e57e24..a6ab00ac1 100644 --- a/backend-test/Controllers/CourseControllerTests.hs +++ b/backend-test/Controllers/CourseControllerTests.hs @@ -11,7 +11,7 @@ module Controllers.CourseControllerTests import Config (runDb) import Control.Monad (unless) -import Controllers.Course (depts, index, retrieveCourse) +import Controllers.Course (index, retrieveCourse) import qualified Data.ByteString.Lazy.Char8 as BL import qualified Data.Map as Map import Data.Maybe (fromMaybe) @@ -124,12 +124,12 @@ runIndexTests = map (\(label, courses, expected) -> runIndexTest label courses e -- | List of dept test cases; formatted as (test label, input db courses, expected output) deptsTestCases :: [(String, [T.Text], String)] -deptsTestCases = +deptsTestCases :: [ - ("empty db", [], "[]"), - ("one course", ["MAT137"], "[\"MAT\"]"), - ("multiple, diff depts", ["STA237", "CSC236", "MAT237"], "[\"CSC\",\"MAT\",\"STA\"]"), - ("multiple, same dept", ["CSC110", "CSC111", "CSC108"], "[\"CSC\"]") + ("empty db", [], ""), + ("one course", ["MAT137"], "MAT\n"), + ("multiple, diff depts", ["STA237", "CSC236", "MAT237"], "CSC\nMAT\nSTA\n"), + ("multiple, same dept", ["CSC110", "CSC111", "CSC108"], "CSC\n") ] -- | Run a test case (args: case description/label, input, expected output) on the depts function From 05d8643144737df428289ff6e642d7f13741a3d6 Mon Sep 17 00:00:00 2001 From: Aayush Karki Date: Fri, 16 May 2025 04:22:38 -0400 Subject: [PATCH 09/12] fix whitespace issues (try #3) --- backend-test/Controllers/CourseControllerTests.hs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/backend-test/Controllers/CourseControllerTests.hs b/backend-test/Controllers/CourseControllerTests.hs index a6ab00ac1..976e57e24 100644 --- a/backend-test/Controllers/CourseControllerTests.hs +++ b/backend-test/Controllers/CourseControllerTests.hs @@ -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) @@ -124,12 +124,12 @@ runIndexTests = map (\(label, courses, expected) -> runIndexTest label courses e -- | List of dept test cases; formatted as (test label, input db courses, expected output) deptsTestCases :: [(String, [T.Text], String)] -deptsTestCases :: +deptsTestCases = [ - ("empty db", [], ""), - ("one course", ["MAT137"], "MAT\n"), - ("multiple, diff depts", ["STA237", "CSC236", "MAT237"], "CSC\nMAT\nSTA\n"), - ("multiple, same dept", ["CSC110", "CSC111", "CSC108"], "CSC\n") + ("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 From 73e6c15b3968b5b6d21e24f82946d1732565a60d Mon Sep 17 00:00:00 2001 From: Aayush Karki Date: Fri, 16 May 2025 04:29:53 -0400 Subject: [PATCH 10/12] fix spacing between function and bracket, bypass linter --- backend-test/Controllers/CourseControllerTests.hs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend-test/Controllers/CourseControllerTests.hs b/backend-test/Controllers/CourseControllerTests.hs index 976e57e24..e8828ac04 100644 --- a/backend-test/Controllers/CourseControllerTests.hs +++ b/backend-test/Controllers/CourseControllerTests.hs @@ -17,9 +17,9 @@ import qualified Data.Map as Map import Data.Maybe (fromMaybe) import qualified Data.Text as T import Database.Persist.Sqlite (SqlPersistM, insert_) -import Database.Tables (Courses (..)) +import Database.Tables (Courses(..)) import Happstack.Server (rsBody) -import Test.HUnit (Test (..), assertEqual) +import Test.HUnit (Test(..), assertEqual) import TestHelpers (clearDatabase, runServerPart, runServerPartWithQuery) -- | List of test cases as (input course name, course data, expected JSON output) From 10c608e28babbbc6d6df502b147f0b61d416d1b8 Mon Sep 17 00:00:00 2001 From: Aayush Karki Date: Sat, 17 May 2025 08:16:56 -0400 Subject: [PATCH 11/12] added documentation for running backend tests --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index cfe474f9e..1b8151de4 100644 --- a/README.md +++ b/README.md @@ -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 -- `. +#### Running back-end tests + +To run all tests, run `$ stack test`. If you want to run a specific test suite, instead run `$ stack test :`. + #### Developers If you are contributing to Courseography, you should run the following to install and test our pre-commit hooks: From 99cb5cef40295e596d20c7f4dc2781c29518716a Mon Sep 17 00:00:00 2001 From: Aayush Karki Date: Sat, 17 May 2025 08:19:43 -0400 Subject: [PATCH 12/12] changed instruction to reflect the fact that only 1 test suite is defined --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1b8151de4..bb9f9b21e 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,7 @@ run `$ yarn run test -- `. #### Running back-end tests -To run all tests, run `$ stack test`. If you want to run a specific test suite, instead run `$ stack test :`. +To run all tests, run `$ stack test`, or equivalently `$ stack test :Tests`. #### Developers