Skip to content

PR 4: (#150) Implementing Unit Tests for Lessons Model (Part 2)#152

Merged
cj-ballesteros merged 8 commits intomainfrom
lesson_related_unit_test
Apr 17, 2025
Merged

PR 4: (#150) Implementing Unit Tests for Lessons Model (Part 2)#152
cj-ballesteros merged 8 commits intomainfrom
lesson_related_unit_test

Conversation

@EthanZ23
Copy link
Contributor

Implementing Unit Testing for Lessons Model (Quiz, Question, Choice, Solution)


Overview

What does this PR do?

This PR adds unit tests for the Lessons app API endpoints, specifically targeting the Quiz, Question, Choice, and Solution models. It also introduces the missing views, serializers, and URLs for the Quiz and Solution components to ensure complete coverage. These tests validate the correctness of CRUD operations and enhance the stability of the Quiz, Question, Choice, and Solution models.


Key Features & Changes

What has been added or changed?

Unit Test Implementation:

  • Added several tests to validate CRUD operations for Quiz, Question, Choice, and Solution models.
  • Verified and ensured that responses are consistent with expected behavior.

Quiz Views, Serializers, and URLs:

  • Implemented missing Quiz views, serializers, and URLs to better test coverage for lesson-related models.

Solution Views, Serializers, and URLs:

  • Integrated missing Solution views, serializers, and URLs to better test coverage for lesson-related models.

Test Framework Setup:

  • Extended the test infrastructure and added reusable test logic to support modular testing.

Why This Is Needed

What problem does this solve?

These additions improve our test coverage for the lessons-related API endpoints and help ensure consistent, predictable behavior as the lessons application evolves. It also closes out the remaining scope outlined in issue #150 and complements previous testing done for Assignments.


Related Issue

🔗 This PR addresses [Issue #150 - Continue implementing unit tests for the lessons app API endpoints, focusing on Quizzes, Questions, Choices, and Solutions.]


Implementation Details

How was this feature developed?

  • Created & added necessary Quiz views, serializers, and URLs.
  • Added test cases for Quiz GET, POST, PATCH, and DELETE operations.
  • Added test cases for Question GET, POST, PATCH, and DELETE operations.
  • Added test cases for Choice GET, POST, PATCH, and DELETE operations.
  • Created & added Solution views, serializers, and URLs.
  • Added test cases for Solution GET, POST, PATCH, and DELETE operations.
  • Validated correct status codes, responses, and data integrity.
  • Used Django’s APIClient and pytest for request simulation, validation and test execution.

Design Documentation (If Applicable)

Diagrams & Visuals (If the PR involves a major feature, link UML diagrams)

  • Use Case Diagram: Can be added later if requested.

How to Test This PR

Step-by-step testing guide

1️⃣Navigate to backend directory.
2️⃣Enable virtual environment. For Mac/Linux (bash/zsh) users, this is source env/bin/activate. For Windows (PowerShell) users, this is .\env\Scripts\Activate.ps1
3️⃣(If needed) Install dependencies: pip install -r requirements.txt
4️⃣Apply migrations: python manage.py migrate
5️⃣Run tests: python manage.py test apps.lessons
6️⃣Confirm all 21 tests pass successfully.


Files Added/Modified

📂 List of important files changed in this PR

File Name Description
backend/apps/lessons/tests.py Implemented various test Case implementations
backend/apps/lessons/urls.py Registered new routes for Quiz and Solution
backend/apps/lessons/views.py Added CRUD views for Quiz and Solution models
backend/apps/lessons/serializers.py Defined serializers for Quiz and Solution models

AI Code Usage

🤖 Was AI-generated code used in this PR?

  • Yes (Used to help structure logic in Choice PUT test case (reloading updated choices from the DB and validating updates).)
  • No

Checklist for Reviewers

Things to check before approving this PR:

  • Does the feature work as expected?
  • Is the code clean, modular, and well-documented?
  • Do all tests pass successfully?
  • Are tests comprehensive and cover all (or most) CRUD operations?

Next Steps & Future Enhancements

What improvements can be made after merging?

  • Expand test coverage to handle more edge cases and error scenarios (validation errors).
  • Include performance and integration tests for higher coverage.

Final Notes

Thanks for reviewing! Please leave feedback if you have any suggestions or concerns.

-- Ethan

@EthanZ23 EthanZ23 added enhancement New feature or request Backend Issue is primarily backend related labels Apr 16, 2025
@EthanZ23 EthanZ23 self-assigned this Apr 16, 2025
@EthanZ23 EthanZ23 linked an issue Apr 16, 2025 that may be closed by this pull request
Copy link
Contributor

@ChrisCRL ChrisCRL left a comment

Choose a reason for hiding this comment

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

🔍 Peer Review: Assignment System API Tests

Hey Ethan👋 — Here's my feedback:

✅ What Looks Great

  • Test coverage is extensive, covering all CRUD operations for Assignments, Quizzes, Questions, Choices, and Solutions.
  • Authentication handling is properly implemented in the setUp method, ensuring tests run with proper permissions.
  • Test organization is excellent, with clear delineation between different model test sections.
  • Verification steps go beyond simply checking response status codes - you're verifying data integrity and confirming deletion.

💡 Suggestions for Improvement

  • Consider adding negative test cases (e.g., attempting operations without proper authentication or permissions).
  • Add tests for validation errors (e.g., creating questions with invalid data, choices without required fields).

🧪 Tested This Feature

I ran the following test steps:

  • ✅ Reviewed all test cases for proper setup and teardown
  • ✅ Verified test coverage across all models in the assignment system
  • ✅ Confirmed proper test isolation to prevent test interdependence
  • ✅ Checked that all API endpoints are properly tested
  • ✅ Validated that permissions are correctly tested where applicable

🚀 Final Thoughts

The tests are well-structured and maintain a good balance between thoroughness and readability. With the addition of some negative test cases and validation testing, this model will be perfect. Approved :) SEND ITTTTTTTT (after the 2nd reviewer)

return f"Solution for Question {self.question.id}: (No Answer Set)"
quiz_title = self.question.quiz.assignment.title if self.question.quiz and self.question.quiz.assignment else "Unknown Quiz"
# (debugging) try-except can be removed once confident there's no circular logic
try:
Copy link
Contributor

Choose a reason for hiding this comment

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

do we still need this code for debugging? if not, you can remove it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

After I finish connecting with the frontend and running a few user tests, I’ll gladly remove it if it turns out to be unnecessary and not needed.

# self.list_url = '/lessons/assignments/'
#self.detail_url = f'/lessons/assignments/{self.assignment.pk}/'
#self.create_url = '/lessons/assignments/create/'
# self.detail_url = f'/lessons/assignments/{self.assignment.pk}/'
Copy link
Contributor

@ChrisCRL ChrisCRL Apr 16, 2025

Choose a reason for hiding this comment

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

You can remove this since you used reverse() above

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for pointing this out! I had originally left it as a heads-up for reviewers, but I’ll go ahead and remove it moving forward.


def __str__(self):
return f"Choice: {self.choice_text} ({'Correct' if self.is_correct else 'Incorrect'})"
quiz_title = self.question.quiz.assignment.title if self.question.quiz and self.question.quiz.assignment else "Unknown Quiz"
Copy link
Contributor

Choose a reason for hiding this comment

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

nice edge case handling here

Copy link
Contributor

@cj-ballesteros cj-ballesteros left a comment

Choose a reason for hiding this comment

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

Hey Ethan! — just finished reviewing your PR titled "# Implementing Unit Tests for Lessons Model ". Here’s my feedback:


💡 Suggestions for Improvement

  • Assertion Clarity: Use clear and specific assertions to validate expected outcomes, which improves test readability and maintainability. More assertions beyond testing http code 200.
  • None that I haven't already said in my other PR, but looks like many of it has been improved.

🧪 Tested This Feature

I followed the test instructions:

  • ✅ Ran test cases using python3 manage.py test apps.lessons

  • ✅ Created, deleted, and updated new instances having to do with the lessons model.

  • ✅ Reviewed code for anything that could potentially go wrong/debug code still left.


🚀 Final Thoughts

Everything else looks great!

@cj-ballesteros cj-ballesteros merged commit 1fb8fa4 into main Apr 17, 2025
2 checks passed
@ashley-arellano ashley-arellano deleted the lesson_related_unit_test branch April 20, 2025 19:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Backend Issue is primarily backend related enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implementing Unit Testing for Lessons API Endpoints PT.2

3 participants