blockpy-server icon indicating copy to clipboard operation
blockpy-server copied to clipboard

Implement comprehensive unit test suite for BlockPy backend with sample data generation

Open Copilot opened this issue 5 months ago • 0 comments

This PR implements a comprehensive unit test suite for the BlockPy education platform backend, addressing the need for thorough testing infrastructure and sample data generation capabilities.

What's Included

🏗️ Test Infrastructure

  • Base test classes with automatic database setup/teardown using in-memory SQLite
  • Factory pattern for creating realistic test data across all models
  • Test runner with selective execution for different test categories
  • Proper test isolation ensuring each test runs independently

📊 Comprehensive Model Coverage (64+ tests)

  • User Model (9 tests): Authentication, password hashing, roles, relationships
  • Course Model (12 tests): Creation, visibility settings, ownership, relationships
  • Assignment Model (12 tests): Different types, status workflows, submissions
  • AssignmentGroup Model (13 tests): Categories, positioning, forking functionality
  • Submission Model (18+ tests): Status transitions, grading workflows, code storage

🌐 API Endpoint Testing (20 tests)

  • Basic endpoints: Root access, static files, health checks
  • BlockPy endpoints: Assignment loading, submission handling, parameter validation
  • Error handling: Graceful handling of invalid parameters and missing data

🔄 End-to-End Workflow Testing (6 scenarios)

Complete user journeys including:

  • Instructor course creation and management
  • Student enrollment and assignment submission
  • Assignment grading and feedback workflows
  • Multi-student collaboration scenarios

🏭 Sample Data Generation

Robust factories for creating test data:

from tests.factories import SampleDataGenerator

# Creates complete course with instructor, students, assignments, submissions
sample_data = SampleDataGenerator.create_sample_course_with_data()
course = sample_data['course']
students = sample_data['students'] 
assignments = sample_data['assignments']

Usage

Run all tests:

python run_tests.py

Run specific categories:

python run_tests.py --models      # Model tests only
python run_tests.py --controllers # API endpoint tests
python run_tests.py --workflows   # Integration tests

Individual test execution:

python -m unittest tests.models.test_user -v

Benefits

  • Regression Detection: Catches breaking changes early in development
  • Documentation: Tests serve as living documentation of expected behavior
  • Refactoring Safety: Enables confident code changes and refactoring
  • Development Support: Realistic sample data for development and debugging
  • Quality Assurance: Ensures API endpoints handle edge cases properly

The test suite provides a solid foundation for maintaining code quality and enables confident development of the BlockPy platform going forward.

Fixes #71.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot avatar Aug 31 '25 13:08 Copilot