Skip to content
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
5 changes: 4 additions & 1 deletion src/studyflow/platforms/ucampus/course_progress.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import base64
import json
import math
import re
from typing import Any, TYPE_CHECKING
from urllib.parse import quote
Expand Down Expand Up @@ -137,7 +138,9 @@ def parse_tasks(rt: dict[str, Any], unit_id: str) -> list[dict[str, Any]]:
state = leaf.get("state")
required = strategies.get("required") if isinstance(strategies, dict) else None
passed = state.get("pass") if isinstance(state, dict) else None
if passed is True or passed == 1 or isinstance(passed, (int, float)) and passed > 1:
if (passed is True or
(type(passed) is int or type(passed) is float and math.isfinite(passed))
and passed >= 1):
completed = True
elif passed in (0, "0") and passed is not None:
completed = False
Expand Down
24 changes: 24 additions & 0 deletions tests/test_ucampus_progress.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,27 @@ class Response:
lambda *a, **kw: Response())
with pytest.raises(UCampusProgressError, match=expected):
UCampusProgressReader(credentials_loader=lambda: cfg).list_units("course-v2:real")


@pytest.mark.parametrize("invalid", [float("nan"), float("inf"), float("-inf")])
def test_nonfinite_server_pass_is_unknown_not_completed(invalid):
tasks = parse_tasks({"leafs": {
"video1": {"tab_type": "video", "state": {"pass": invalid}},
}}, "unit1")
assert len(tasks) == 1
assert tasks[0]["completed"] is None


def test_numeric_server_pass_keeps_previous_valid_behavior():
for value, expected in ((True, True), (1, True), (2, True), (1.0, True),
(0, False), (False, False), (-1, None)):
tasks = parse_tasks({"leafs": {
"video1": {"tab_type": "video", "state": {"pass": value}},
}}, "unit1")
assert tasks[0]["completed"] is expected
# Large integer counters should not pass through float(), which may raise
# OverflowError for a JSON integer with more digits than a float can hold.
huge = parse_tasks({"leafs": {
"video1": {"tab_type": "video", "state": {"pass": 10 ** 400}},
}}, "unit1")
assert huge[0]["completed"] is True
Loading