feat: improve Canvas grade sync and add test coverage#804
Conversation
9907c22 to
19d42f7
Compare
tecoholic
left a comment
There was a problem hiding this comment.
@xitij2000 I tested the changes with an actualy Canvas instance and Open edX instance. They work as expected and I can verify it from the logs as well.
I think we can improve the tests a bit and then it should be ready.
| USER_MODEL = get_user_model() | ||
|
|
||
|
|
||
| @override_settings(BULK_EMAIL_DEFAULT_RETRY_DELAY=10, BULK_EMAIL_MAX_RETRIES=5) |
There was a problem hiding this comment.
These settings are added in conftest.py for compatibility and are not actually used in any of the tested functionality, right? Do we need to override them here?
There was a problem hiding this comment.
Nope, this is from before i added them to the test config.
| def test_assignment_not_synced( | ||
| self, | ||
| mock_client_class, | ||
| ): | ||
| mock_client = mock_client_class.return_value | ||
| mock_client.get_canvas_assignments.return_value = { | ||
| "dummy-key": { | ||
| "id": self.canvas_assignment_id, | ||
| "due_at": "", | ||
| } | ||
| } | ||
|
|
||
| _sync_user_grade_with_canvas(self.grade_id) | ||
|
|
||
| mock_client.update_assignment_grades.assert_not_called() |
There was a problem hiding this comment.
Why is this not synced? It would be clear if the reason is in the test case name itself, or at least a docstring explaining it. From the test, it looks like canvas returns an assignment with no due date, which should ideally be syncing the grades. Why is it not?
There was a problem hiding this comment.
I've updated the test. I too forgot why this shouldn't sync, so it definitely needs clarification!
|
|
||
| _sync_user_grade_with_canvas(self.grade_id) | ||
|
|
||
| mock_client.update_assignment_grades.assert_not_called() |
There was a problem hiding this comment.
Not really sure what's going on in this test. Test function name says, it's "sync error", the docstring says "test successful grade sync", finally it's asserted as "not called". What's this test actually checking?
I am guessing it's this block?
try:
grade = grade_dict[grade_instance.full_usage_key][openedx_user]
payload = dict([update_grade_payload_kv(canvas_user_id, grade.percent_graded)])
except (KeyError, AttributeError):
Can we make the docstring a bit more informative about why this condition occurs?
There was a problem hiding this comment.
Sorry about that! I basically copied the success case and then started introducing failures to test all possible failures which left in docstring. I've updated this.
To be honest, I don't know why this would occur. This code is only run if the a grade is generated, so why would it not be returned from here, I'm not sure of the whole grading mechanism to know why we'd generate an object but still have the grading API not return a grade. If you know why this could happen, do tell and I'll update the docstring.
19d42f7 to
7ef8445
Compare
- Added validation to skip grade sync for assignments past their due date. - Added missing test suite for Canvas grade sync tasks under `tests/`. - Minor code cleanup in `api.py` and `client.py`.
7ef8445 to
701bf00
Compare
Description (What does it do?)
This PR enhances the Canvas grade synchronization functionality with improved validation logic and
comprehensive test coverage:
Key Changes:
Assignment Due Date Validation - Added a new validation check to prevent grade
synchronization for assignments that have already passed their due date. When attempting to sync
grades for an overdue assignment, the system will now log a warning and skip the synchronization.
Refactored Task Structure - Split the
sync_user_grade_with_canvasshared task into twocomponents:
sync_user_grade_with_canvas()- The Celery shared task decorator_sync_user_grade_with_canvas()- The internal implementation functionThis separation improves testability and allows the implementation logic to be tested
independently without Celery.
New Test Suite - Added a new test module (
tests/test_tasks.py) with 8 test casescovering:
Test Configuration - Added pytest configuration:
conftest.py- Test setup with Django settings initialization for bulk email configurationpyproject.tomlpytest settings - Configured with database reuse and deprecation warningfilters
How Can This Be Tested?
Unit Tests
Run the new test suite using the standard test process. Or you can mount this pacakge in the Tutor
openedx container, cd into the directory and run pytest.
Manual Testing
To manually test the functionality:
warning log