Added tests for JSON parsing of Time' data type#1706
Conversation
Coverage Report for CI Build 4Coverage increased (+0.5%) to 57.873%Details
Uncovered ChangesNo uncovered changes found. Coverage RegressionsNo coverage regressions found. Coverage Stats
💛 - Coveralls |
| -- | Test suite for Tables Module | ||
| test_tables :: TestTree | ||
| test_tables = | ||
| testGroup "Tables tests" runTime'FromJSONTests No newline at end of file |
There was a problem hiding this comment.
Make sure this file ends with exactly one new line. You can configure your text editor to ensure this happens automatically when you save your file.
| runTime'FromJSONTest (label, input, expected) = | ||
| testCase label $ do | ||
| let decoded = decode (BL.fromStrict (TE.encodeUtf8 input)) :: Maybe Time' | ||
| assertEqual ("Unexpected response body for " ++ label) expected decoded |
There was a problem hiding this comment.
"response body" is an incorrect description; something like "parsing result" is better
| runTime'FromJSONTest :: (String, T.Text, Maybe Time') -> TestTree | ||
| runTime'FromJSONTest (label, input, expected) = | ||
| testCase label $ do | ||
| let decoded = decode (BL.fromStrict (TE.encodeUtf8 input)) :: Maybe Time' |
There was a problem hiding this comment.
There is some unnecessary text manipulation here. Instead of decode you can use the function listed here: https://hackage-content.haskell.org/package/aeson-2.2.5.0/docs/Data-Aeson.html#g:12.
| [ ("Empty JSON string", "", Nothing) | ||
| , ("Valid JSON string", "{ \"start\": { \"day\": 1, \"millisofday\": 36000000 }, \"end\": { \"millisofday\": 39600000 }, \"building\": { \"buildingCode\": \"BA\", \"buildingRoomNumber\": \"1130\" }, \"assignedRoom2\": \"MP102\" }", Just (Time' {weekDay' = 0.0, startHour' = 10.0, endHour' = 11.0, firstLocation' = Just "BA1130", secondLocation' = Just "MP102"})) | ||
| , ("Valid JSON string with no second location", "{ \"start\": { \"day\": 2, \"millisofday\": 39600000 }, \"end\": { \"millisofday\": 43200000 }, \"building\": { \"buildingCode\": \"MP\", \"buildingRoomNumber\": \"203\" } }", Just (Time' {weekDay' = 1.0, startHour' = 11.0, endHour' = 12.0, firstLocation' = Just "MP203", secondLocation' = Nothing})) | ||
| , ("Invalid JSON string (no day value)", "{ \"start\": { \"millisofday\": 43200000 }, \"end\": { \"millisofday\": 50400000 }, \"building\": { \"buildingCode\": \"MY\", \"buildingRoomNumber\": \"150\" } }", Just (Time' {weekDay' = 5.0, startHour' = 25.0, endHour' = 25.0, firstLocation' = Just "MY150", secondLocation' = Nothing})) |
There was a problem hiding this comment.
Don't use the term "invalid" when then parsing succeeds. Note that there are cases of invalid JSON: when start and end themselves are missing (and these would be good test cases to add). You can tell this in the code by the use of (.:) vs. (.:?) (see https://hackage-content.haskell.org/package/aeson-2.2.5.0/docs/Data-Aeson.html#v:.:).
For the other fields the label should reflect that when the field isn't provided, a default value is used instead.
Proposed Changes
Created a new file called
TablesTests.hsinbackend-test/Database, and added test cases for theFromJSONinstance of theTime'data type inapp/Database/Tables.hs.The test cases check the following: empty JSON string, valid JSON string, valid JSON string with no second location, and invalid JSON string, either with no day value, no start millisofday value, no end millisofday value, no buildingCode value, or no buildingRoomNumber value.
Type of Change
(Write an
Xor a brief description next to the type or types that best describe your changes.)Checklist
(Complete each of the following items for your pull request. Indicate that you have completed an item by changing the
[ ]into a[x]in the raw text, or by clicking on the checkbox in the rendered description on GitHub.)Before opening your pull request:
After opening your pull request:
Questions and Comments
(Include any questions or comments you have regarding your changes.)