Skip to content

Latest commit

 

History

History
189 lines (137 loc) · 4.56 KB

File metadata and controls

189 lines (137 loc) · 4.56 KB

Test Suite for DineWise Restaurant Ontology Project

This directory contains comprehensive test cases for all modules in the project.

Test Structure

tests/
├── __init__.py              # Test package initialization
├── conftest.py              # Shared pytest fixtures
├── test_load_ontology.py    # Tests for load_ontology.py
├── test_add_data.py         # Tests for add_data.py
├── test_reasoning_demo.py   # Tests for reasoning_demo.py
├── test_api.py              # Tests for Flask API endpoints
└── README.md                # This file

Running Tests

Install Test Dependencies

pip install -r requirements.txt

This will install:

  • pytest - Test framework
  • pytest-cov - Coverage reporting
  • pytest-mock - Mocking utilities

Run All Tests

pytest

Run Specific Test File

pytest tests/test_load_ontology.py
pytest tests/test_api.py

Run Tests with Coverage

pytest --cov=. --cov-report=html

This generates an HTML coverage report in htmlcov/index.html.

Run Tests Verbosely

pytest -v

Run Specific Test Class or Function

pytest tests/test_api.py::TestAPIRestaurantsEndpoint
pytest tests/test_api.py::TestAPIRestaurantsEndpoint::test_get_restaurants_success

Run Tests by Marker

# Run only unit tests
pytest -m unit

# Run only integration tests
pytest -m integration

# Run ontology-related tests
pytest -m ontology

Test Categories

Unit Tests

  • Test individual functions in isolation
  • Use mocks for external dependencies
  • Fast execution

Integration Tests

  • Test complete workflows
  • Use real ontology files
  • May require add_data.py to be run first

API Tests

  • Test Flask endpoints
  • Use Flask test client
  • Mock ontology data for isolated testing

Test Fixtures

Shared fixtures are defined in conftest.py:

  • base_ontology_path - Path to base ontology file
  • updated_ontology_path - Path to updated ontology file
  • loaded_base_ontology - Loaded base ontology instance
  • loaded_updated_ontology - Loaded updated ontology instance
  • test_ontology - Temporary in-memory ontology for isolated tests
  • sample_restaurant_data - Sample restaurant data
  • sample_dish_data - Sample dish data

Prerequisites for Running Tests

  1. Base Ontology File: restaurant-ontology.owl must exist in project root
  2. Updated Ontology File: For some integration tests, run python add_data.py first to create restaurant-ontology-updated.owl
  3. Python Environment: Python 3.8+ with all dependencies installed

Test Coverage

The test suite covers:

load_ontology.py

  • Ontology file loading
  • Class enumeration
  • Individual inspection
  • Property access
  • Error handling

add_data.py

  • Creating restaurant individuals
  • Creating dish individuals
  • Creating chef individuals
  • Setting properties
  • Creating relationships
  • Saving ontology to file

reasoning_demo.py

  • Running OWL reasoner
  • Automatic classification
  • VegetarianDish classification
  • Querying inferred knowledge

api.py

  • All 5 REST endpoints
  • Request/response formats
  • Error handling (404, 500)
  • Query parameter filtering
  • CORS configuration

Known Limitations

  1. Some integration tests require the ontology files to exist
  2. API tests use mocking - may not catch all real-world issues
  3. Reasoner tests depend on Owlready2's internal implementation

Continuous Integration

To run tests in CI/CD:

# Install dependencies
pip install -r requirements.txt

# Run tests with coverage
pytest --cov=. --cov-report=xml

# Or with HTML report
pytest --cov=. --cov-report=html

Troubleshooting

Issue: "File not found" errors

Solution: Make sure restaurant-ontology.owl exists in the project root. Run python add_data.py to create the updated ontology file.

Issue: Import errors

Solution: Make sure all dependencies are installed: pip install -r requirements.txt

Issue: Tests fail with ontology loading errors

Solution: Verify the ontology files are valid OWL files. Try loading them manually with Owlready2.

Issue: API tests fail

Solution: The API tests use mocking. Make sure pytest-mock is installed and fixtures are set up correctly.

Contributing Tests

When adding new tests:

  1. Follow existing naming conventions (test_*.py, test_* functions)
  2. Use appropriate fixtures from conftest.py
  3. Add docstrings to test functions
  4. Use descriptive test names
  5. Add appropriate markers (@pytest.mark.unit, etc.)
  6. Update this README if adding new test categories