Testing Suite ============== This section outlines the testing suite for the 5D Neural Network Interpolator application. All tests are located in the ``backend/tests/`` folder and use the pytest framework. Overview -------- The testing suite is organised into separate test files, each focusing on a specific component of the application. A ``conftest.py`` file provides shared test fixtures and data to ensure consistent test setup across all test files. Running the Tests ----------------- To run the entire test suite: .. code-block:: bash cd backend pytest tests/ -v To run a specific test file: .. code-block:: bash pytest tests/test_api.py -v Test Files ---------- test_api.py ~~~~~~~~~~~ Tests for the FastAPI backend endpoints. **Purpose:** This module validates all API endpoints and their error handling. **Test Coverage:** - **Health Check Endpoint** (``TestHealthEndpoint``) - Verifies the health endpoint returns a success status - **File Upload Endpoint** (``TestUploadEndpoint``) - Tests uploading valid pickle files - Validates rejection of invalid file types - Tests handling of missing file parameters - **Training Endpoint** (``TestTrainEndpoint``) - Tests that training fails without uploaded data - Validates successful training with uploaded data - Checks response structure and metrics (MSE, R², training time) - **Prediction Endpoint** (``TestPredictEndpoint``) - Tests that prediction fails without a trained model - Validates successful prediction with a trained model - Tests input validation (correct number of inputs) - **Reset Endpoint** (``TestResetEndpoint``) - Verifies the reset endpoint clears application state **Key Features:** - Uses FastAPI's ``TestClient`` for HTTP request testing - Creates temporary pickle files for testing - Ensures test isolation by clearing application state between tests test_data.py ~~~~~~~~~~~~ Tests for the ``DataLoader`` class. **Purpose:** This module validates data loading, preprocessing, and validation functionality. **Test Coverage:** - **DataLoader Initialization** (``TestDataLoaderInitialization``) - Tests correct initialization with file paths - Verifies scaler creation - **Dataset Loading** (``TestDataLoaderLoadDataset``) - Tests loading valid pickle files - Validates data structure and column names (x1-x5, y) - Checks that loaded data matches original format - **Data Inspection and Validation** (``TestDataLoaderInspectData``) - Tests data inspection with valid datasets - Validates error handling for missing features - Tests error handling for missing target columns - Tests handling of missing values (NaN) - Validates correct train/validation/test split ratios (70%/15%/15%) - Verifies feature standardization (mean ~0, std ~1) **Key Features:** - Tests the new dictionary-based dataset format (X, y, metadata) - Validates data splitting and preprocessing - Ensures proper error handling for invalid data test_neuralnetwork.py ~~~~~~~~~~~~~~~~~~~~~ Tests for the ``NeuralNetwork`` class. **Purpose:** This module validates neural network initialization, training, evaluation, and prediction. **Test Coverage:** - **Initialization** (``TestNeuralNetworkInitialization``) - Tests initialization with default parameters - Tests initialization with custom parameters (layers, learning rate, epochs) - Validates model architecture (input/output layers, hidden layers) - **Training** (``TestNeuralNetworkTraining``) - Tests that training completes without errors - Validates loss history recording - Checks that losses are positive numbers - **Evaluation** (``TestNeuralNetworkEvaluation``) - Tests that evaluation returns MSE and R² metrics - Validates metric types and ranges - Checks that predictions are stored - **Prediction** (``TestNeuralNetworkPrediction``) - Tests that prediction returns numeric values - Validates predictions vary with different inputs - Tests input validation **Key Features:** - Tests PyTorch model creation and architecture - Validates training and evaluation workflows - Ensures predictions are numeric and reasonable test_plotter.py ~~~~~~~~~~~~~~~ Tests for the ``Plotter`` class. **Purpose:** This module validates plot generation and file saving functionality. **Test Coverage:** - **Learning Curve Plotting** (``TestPlotterLearningCurve``) - Tests creation of learning curve plots - Tests plotting without validation history - Tests handling of empty history - **Predictions vs Actual Plotting** (``TestPlotterPredictionsVsActual``) - Tests creation of predictions vs actual plots - Tests handling of different array shapes (1D, 2D) - Validates file creation and size - **Error Handling** (``TestPlotterErrorHandling``) - Tests handling of invalid file paths - Validates graceful error handling **Key Features:** - Tests matplotlib plot generation - Validates file saving functionality - Ensures proper error handling for invalid paths test_logger.py ~~~~~~~~~~~~~~~ Tests for the ``Logger`` class. **Purpose:** This module validates logging functionality. **Test Coverage:** - **Logger Initialization** (``TestLogger``) - Tests that Logger can be initialized - Validates logger instance creation - **Logging Methods** - Tests ``info`` method - Tests ``error`` method - Tests ``warning`` method **Key Features:** - Simple validation of logging interface - Ensures all logging methods are callable conftest.py ~~~~~~~~~~~ Shared test fixtures and data. **Purpose:** This file provides reusable test fixtures and sample data for all test files. **Key Features:** - Creates temporary pickle files with sample data - Provides sample training/validation/test data splits - Creates temporary output directories for plot testing - Ensures consistent test setup across all test files Test Structure -------------- The test suite follows a clear structure: - Each test file focuses on a single component - Test classes group related test methods - Descriptive test method names explain what is being tested - Fixtures in ``conftest.py`` provide reusable test data - Tests use pytest's assertion syntax for clarity Best Practices -------------- The test suite follows these best practices: - **Isolation:** Each test is independent and doesn't rely on other tests - **Fixtures:** Shared test data is provided through pytest fixtures - **Clear Names:** Test methods have descriptive names explaining their purpose - **Error Handling:** Tests validate both success and error cases - **Temporary Files:** Tests use temporary files that are cleaned up automatically - **State Management:** Application state is cleared between tests to prevent interference