-
Notifications
You must be signed in to change notification settings - Fork 70
Test cases for retrieveProgram #1609
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
Japleen-Kaur2409
wants to merge
4
commits into
Courseography:master
Choose a base branch
from
Japleen-Kaur2409:retrieveprogram-tests
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
dd594c8
Added backend tests for retrieveProgram
Japleen-Kaur2409 e6c7313
Added CHANGELOG.md and removed test checker.
Japleen-Kaur2409 e953bd8
updated retrieveprogramTestCases to include the full JSON response.
Japleen-Kaur2409 d35686c
parsed json to not include the timestamp
Japleen-Kaur2409 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,17 @@ | ||
{-| | ||
Description: Program Controller module tests. | ||
|
||
Module that contains the tests for the functions in the Program Controller module. | ||
-} | ||
|
||
module Controllers.ProgramControllerTests ( | ||
test_programController | ||
) where | ||
|
||
import Config (runDb) | ||
import Controllers.Program (index, retrieveProgram) | ||
import Control.Monad.IO.Class (liftIO) | ||
import Controllers.Program (index) | ||
import Data.Aeson (FromJSON (parseJSON), decode, withObject, (.:)) | ||
import qualified Data.ByteString.Lazy.Char8 as BL | ||
import qualified Data.Text as T | ||
import Data.Time (getCurrentTime) | ||
|
@@ -13,8 +20,74 @@ import Database.Persist.Sqlite (SqlPersistM, insert_) | |
import Database.Tables (Post (..)) | ||
import Happstack.Server (rsBody) | ||
import Test.Tasty (TestTree) | ||
import Test.Tasty.HUnit (assertEqual, testCase) | ||
import TestHelpers (clearDatabase, runServerPart, withDatabase) | ||
import Test.Tasty.HUnit (assertBool, assertEqual, testCase) | ||
import TestHelpers (clearDatabase, runServerPart, runServerPartWithProgramQuery, withDatabase) | ||
|
||
-- | A Post response without timestamps (for comparison purposes) | ||
data PostResponseNoTime = PostResponseNoTime | ||
{ postCode :: T.Text | ||
, postDepartment :: T.Text | ||
, postDescription :: T.Text | ||
, postName :: T.Text | ||
, postRequirements :: T.Text | ||
} deriving (Show, Eq) | ||
|
||
instance FromJSON PostResponseNoTime where | ||
parseJSON = withObject "Expected Object for Post" $ \o -> do | ||
code <- o .: "postCode" | ||
dept <- o .: "postDepartment" | ||
desc <- o .: "postDescription" | ||
name <- o .: "postName" | ||
reqs <- o .: "postRequirements" | ||
return $ PostResponseNoTime code dept desc name reqs | ||
|
||
-- | List of test cases as (label, programs to insert, query params, expected output) | ||
retrieveprogramTestCases :: [(String, [T.Text], T.Text, String)] | ||
retrieveprogramTestCases = | ||
[ ("Valid program code returns JSON" | ||
, ["ASMAJ1689"] | ||
, "ASMAJ1689" | ||
, "{\"postCode\":\"ASMAJ1689\",\"postDepartment\":\"test\",\"postDescription\":\"test\",\"postName\":\"Other\",\"postRequirements\":\"test\"}" | ||
) | ||
, ("Invalid program code returns null JSON" | ||
, [] | ||
, "INVALID123" | ||
, "null" | ||
) | ||
, ("Empty code parameter returns null" | ||
, [] | ||
, "" | ||
, "null" | ||
) | ||
] | ||
|
||
-- | Parse response and extract non-timestamp fields | ||
parsePostResponse :: String -> Maybe PostResponseNoTime | ||
parsePostResponse jsonStr = decode (BL.pack jsonStr) | ||
|
||
-- | Run a test case (case, input, expected output) on the retrieveProgram function. | ||
runRetrieveProgramTest :: String -> [T.Text] -> T.Text -> String -> TestTree | ||
runRetrieveProgramTest label posts queryParam expected = | ||
testCase label $ do | ||
runDb $ do | ||
clearDatabase | ||
insertPrograms posts | ||
|
||
response <- runServerPartWithProgramQuery Controllers.Program.retrieveProgram (T.unpack queryParam) | ||
let actual = BL.unpack $ rsBody response | ||
let parsedActual = parsePostResponse actual | ||
let parsedExpected = parsePostResponse expected | ||
|
||
assertBool | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can still use |
||
("JSON mismatch for " ++ label ++ | ||
"\nExpected: " ++ show parsedExpected ++ | ||
"\nActual: " ++ show parsedActual ++ | ||
"\nRaw JSON: " ++ actual) | ||
(parsedActual == parsedExpected) | ||
|
||
-- | Run all the retrieveProgram test cases | ||
runRetrieveProgramTests :: [TestTree] | ||
runRetrieveProgramTests = map (\(label, programs, params, expected) -> runRetrieveProgramTest label programs params expected) retrieveprogramTestCases | ||
|
||
-- | List of test cases as (label, input programs, expected output) | ||
indexTestCases :: [(String, [T.Text], String)] | ||
|
@@ -51,4 +124,4 @@ runIndexTests = map (\(label, programs, expected) -> runIndexTest label programs | |
|
||
-- | Test suite for Program Controller Module | ||
test_programController :: TestTree | ||
test_programController = withDatabase "Program Controller tests" runIndexTests | ||
test_programController = withDatabase "Program Controller tests" (runIndexTests ++ runRetrieveProgramTests) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's more explicit to encode the expected value directly as a
Maybe PostResponseNoTime
rather than aString
that's going to be parsed.(This applies to all test cases in this list; the
"null"
values can be converted toNothing
s.)