Skip to content

Added tests for JSON parsing of Time' data type#1706

Open
r-weng wants to merge 4 commits into
Courseography:masterfrom
r-weng:test-json-parsing
Open

Added tests for JSON parsing of Time' data type#1706
r-weng wants to merge 4 commits into
Courseography:masterfrom
r-weng:test-json-parsing

Conversation

@r-weng
Copy link
Copy Markdown

@r-weng r-weng commented May 16, 2026

Proposed Changes

Created a new file called TablesTests.hs in backend-test/Database, and added test cases for the FromJSON instance of the Time' data type in app/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 X or a brief description next to the type or types that best describe your changes.)

Type Applies?
🚨 Breaking change (fix or feature that would cause existing functionality to change)
New feature (non-breaking change that adds functionality)
🐛 Bug fix (non-breaking change that fixes an issue)
🎨 User interface change (change to user interface; provide screenshots)
♻️ Refactoring (internal change to codebase, without changing functionality)
🚦 Test update (change that only adds or modifies tests) X
📦 Dependency update (change that updates a dependency)
🔧 Internal (change that only affects developers or continuous integration)

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:

  • I have performed a self-review of my changes.
    • Check that all changed files included in this pull request are intentional changes.
    • Check that all changes are relevant to the purpose of this pull request, as described above.
  • I have added tests for my changes, if applicable.
    • This is required for all bug fixes and new features.
  • I have updated the project documentation, if applicable.
    • This is required for new features.
  • If this is my first contribution, I have added myself to the list of contributors.
  • I have updated the project Changelog (this is required for all changes).

After opening your pull request:

  • I have verified that the CircleCI checks have passed.
  • I have requested a review from a project maintainer.

Questions and Comments

(Include any questions or comments you have regarding your changes.)

@coveralls
Copy link
Copy Markdown

coveralls commented May 17, 2026

Coverage Report for CI Build 4

Coverage increased (+0.5%) to 57.873%

Details

  • Coverage increased (+0.5%) from the base build.
  • Patch coverage: No coverable lines changed in this PR.
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 3945
Covered Lines: 2340
Line Coverage: 59.32%
Relevant Branches: 964
Covered Branches: 501
Branch Coverage: 51.97%
Branches in Coverage %: Yes
Coverage Strength: 163.59 hits per line

💛 - Coveralls

@r-weng r-weng marked this pull request as draft May 17, 2026 17:23
@r-weng r-weng marked this pull request as ready for review May 17, 2026 17:23
@r-weng r-weng marked this pull request as draft May 17, 2026 17:26
@r-weng r-weng marked this pull request as ready for review May 17, 2026 17:26
@r-weng r-weng requested a review from david-yz-liu May 17, 2026 19:49
Comment thread backend-test/Database/TablesTests.hs Outdated
-- | Test suite for Tables Module
test_tables :: TestTree
test_tables =
testGroup "Tables tests" runTime'FromJSONTests No newline at end of file
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Comment thread backend-test/Database/TablesTests.hs Outdated
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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

"response body" is an incorrect description; something like "parsing result" is better

Comment thread backend-test/Database/TablesTests.hs Outdated
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'
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Comment thread backend-test/Database/TablesTests.hs Outdated
[ ("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}))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@r-weng r-weng requested a review from david-yz-liu May 18, 2026 17:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants