Skip to content

Tests for Graph controller index method #1548

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

Merged
merged 11 commits into from
May 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
- Switched connection for timetable parsing from `RequireEMS` to `AllowEMS`
- Fixed dependency caching in CircleCI configuration
- Added dependency groups to Dependabot configuration
- Added tests for the index function in `Controllers/Graph`

## [0.6.0] - 2024-06-24

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ Ross Gatih,
Nazanin Ghazitabatabai,
Sidharth Gupta,
Parker Hutcheson,
Yoonie Jang,
Jai Joshi,
Aayush Karki,
Philip Kukulak,
Expand Down
4 changes: 3 additions & 1 deletion backend-test/Controllers/ControllerTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ module Controllers.ControllerTests
( controllerTests ) where

import Test.HUnit (Test (..))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like if I don't add this space here, then the import statements get ordered alphabetically upon commit

import Controllers.CourseControllerTests (courseControllerTestSuite)
import Controllers.GraphControllerTests (graphControllerTestSuite)

-- Single test encompassing all controller test suites
controllerTests :: Test
controllerTests = TestList [courseControllerTestSuite]
controllerTests = TestList [courseControllerTestSuite, graphControllerTestSuite]
63 changes: 63 additions & 0 deletions backend-test/Controllers/GraphControllerTests.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{-|
Description: Graph Controller module tests.

Module that contains the tests for the functions in the Graph Controller module.

-}

module Controllers.GraphControllerTests
( graphControllerTestSuite
) where

import Config (runDb)
import Controllers.Graph (index)
import qualified Data.ByteString.Lazy.Char8 as BL
import qualified Data.Text as T
import Database.Persist.Sqlite (SqlPersistM, insert_)
import Database.Tables (Graph (..))
import Happstack.Server (rsBody)
import Test.HUnit (Test (..), assertEqual)
import TestHelpers (clearDatabase, runServerPart)

-- | List of test cases as (label, input graphs, expected output)
indexTestCases :: [(String, [T.Text], String)]
indexTestCases =
[ ("No graphs",
[],
"[]"
),

("One graph",
["Statistics"],
"[{\"dynamic\":false,\"height\":0,\"id\":1,\"title\":\"Statistics\",\"width\":0}]"
),

("Multiple graphs",
["Computer Science", "Statistics", "Physics"],
"[{\"dynamic\":false,\"height\":0,\"id\":1,\"title\":\"Computer Science\",\"width\":0},{\"dynamic\":false,\"height\":0,\"id\":3,\"title\":\"Physics\",\"width\":0},{\"dynamic\":false,\"height\":0,\"id\":2,\"title\":\"Statistics\",\"width\":0}]" )
]

-- | Helper function to insert graphs into the database
insertGraphs :: [T.Text] -> SqlPersistM ()
insertGraphs = mapM_ insertGraph
where
insertGraph title = insert_ (Graph title 0 0 False )

-- | Run a test case (case, input, expected output) on the index function.
runIndexTest :: String -> [T.Text] -> String -> Test
runIndexTest label graphs expected =
TestLabel label $ TestCase $ do
runDb $ do
clearDatabase
insertGraphs graphs
response <- runServerPart Controllers.Graph.index
let actual = BL.unpack $ rsBody response
assertEqual ("Unexpected response body for " ++ label) expected actual

-- | Run all the index test cases
runIndexTests :: [Test]
runIndexTests = map (\(label, graphs, expected) -> runIndexTest label graphs expected) indexTestCases

-- | Test suite for Graph Controller Module
graphControllerTestSuite :: Test
graphControllerTestSuite = TestLabel "Graph Controller tests" $ TestList runIndexTests
20 changes: 20 additions & 0 deletions courseography.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ library
exposed-modules:
Config,
Controllers.Course,
Controllers.Graph,
Css.Constants,
Database.CourseInsertion,
Database.CourseQueries,
Database.CourseVideoSeed,
Expand All @@ -32,9 +34,17 @@ library
Database.Requirement,
Database.Tables,
DynamicGraphs.GraphNodeUtils,
Export.GetImages,
Export.ImageConversion,
Export.TimetableImageCreator,
MasterTemplate,
Response.Image,
Scripts,
Svg.Builder,
Svg.Database,
Svg.Generator,
Svg.Parser,
Util.Blaze,
Util.Happstack,
WebParsing.ArtSciParser,
WebParsing.ParsecCombinators,
Expand All @@ -43,23 +53,32 @@ library
build-depends:
aeson,
base >=4.14,
base64-bytestring,
blaze-html,
blaze-markup,
blaze-svg,
bytestring,
Cabal >= 1.16.0,
containers,
crypton-connection,
data-default-class,
directory,
diagrams-lib,
diagrams-svg,
happstack-server,
hlint,
hslogger,
http-conduit,
http-types,
MissingH,
markdown,
monad-logger,
mtl,
parsec,
persistent,
persistent-sqlite,
process,
random,
resourcet,
split,
system-filepath,
Expand All @@ -83,6 +102,7 @@ test-suite Tests
other-modules:
Controllers.ControllerTests,
Controllers.CourseControllerTests,
Controllers.GraphControllerTests,
RequirementTests.ModifierTests,
RequirementTests.PostParserTests,
RequirementTests.PreProcessingTests,
Expand Down