website/PRICING_TESTS_SUMMARY.md
Tobias Brunner 78f52ea7f4
Some checks failed
Pricing Tests / Pricing Model Tests (push) Failing after 4s
Pricing Tests / Pricing Documentation Check (push) Failing after 3s
tests and actions
2025-06-20 10:46:11 +02:00

6.4 KiB

Pricing Model Test Suite Summary

Overview

I've created a comprehensive test suite for the Django pricing models in the Servala project. The test suite ensures that all price calculations work correctly and provides protection against regressions when making future changes to the pricing logic.

Test Files Created

1. hub/services/tests/test_pricing.py (639 lines)

Core pricing model tests with 29 test methods:

ComputePlanTestCase (6 tests)

  • String representation
  • Price creation and retrieval
  • Non-existent price handling
  • Unique constraint validation

StoragePlanTestCase (4 tests)

  • String representation
  • Price creation and retrieval
  • Non-existent price handling

ProgressiveDiscountModelTestCase (6 tests)

  • String representation
  • Discount calculations for single and multiple tiers
  • Discount breakdown analysis
  • Tier representation

VSHNAppCatPriceTestCase (8 tests)

  • String representation
  • Base fee and unit rate management
  • Final price calculations with and without discounts
  • Error handling for negative values and missing data
  • Price calculations without discount models

VSHNAppCatAddonTestCase (5 tests)

  • Base fee and unit rate addon types
  • Error handling for missing service levels
  • Final price calculations with mandatory and optional addons
  • Addon string representations

2. hub/services/tests/test_pricing_edge_cases.py (8 tests)

Edge cases and error conditions:

  • Overlapping discount tier handling
  • Zero unit calculations
  • Very large number handling
  • Inactive discount model behavior
  • Missing addon price data
  • Validity date ranges
  • Decimal precision edge cases
  • Unique constraint enforcement
  • Addon ordering and filtering

3. hub/services/tests/test_pricing_integration.py (8 tests)

Integration tests for complex scenarios:

  • Complete pricing setup across all models
  • Multi-currency pricing (CHF, EUR, USD)
  • Complex AppCat services with all features
  • External price comparisons
  • Service availability based on pricing
  • Model relationship verification
  • Comprehensive real-world scenarios

4. hub/services/tests/test_utils.py

Test utilities and helpers:

  • PricingTestMixin for common setup
  • Helper functions for expected price calculations
  • Test data factory methods

5. hub/services/tests/README.md

Comprehensive documentation covering:

  • Test structure and organization
  • How to run tests
  • Test coverage details
  • Key test scenarios
  • Best practices for adding new tests
  • Maintenance guidelines

6. run_pricing_tests.sh

Test runner script for easy execution

Key Features Tested

Price Calculation Logic

Progressive Discount Models: Multi-tier discount calculations with proper tier handling
Final Price Calculations: Base fees + unit rates + addons with discounts
Multi-Currency Support: CHF, EUR, USD pricing
Addon Pricing: Both base fee and unit rate addon types
Service Level Pricing: Different rates for Best Effort vs Guaranteed service levels

Business Logic

Mandatory vs Optional Addons: Proper inclusion in price calculations
Discount Model Activation: Active/inactive discount model handling
Public Display Settings: Service availability based on pricing configuration
External Price Comparisons: Integration with competitor pricing data

Error Handling

Negative Values: Proper error handling for invalid inputs
Missing Data: Graceful handling of missing price configurations
Decimal Precision: Accurate monetary calculations
Constraint Validation: Database constraint enforcement

Edge Cases

Zero Units: Calculations with zero quantity
Large Numbers: Performance with high unit counts
Boundary Conditions: Discount tier boundaries
Data Integrity: Relationship and constraint validation

Test Coverage Statistics

  • Total Test Methods: 45 test methods across all test files
  • Models Covered: All pricing-related models (ComputePlan, StoragePlan, VSHNAppCatPrice, Progressive Discounts, Addons, etc.)
  • Scenarios Covered: Basic CRUD, complex calculations, error conditions, integration scenarios
  • Edge Cases: Comprehensive coverage of boundary conditions and error states

Real-World Test Scenarios

PostgreSQL Service Pricing

The integration tests include a complete PostgreSQL service setup with:

  • 16 GiB RAM requirement with progressive discounts
  • Mandatory automated backup addon
  • Optional monitoring and SSL certificate addons
  • Expected total: CHF 186.20/month

Multi-Tier Discount Example

For 60 units with progressive discount:

  • First 10 units: 100% of base rate (no discount)
  • Next 40 units: 90% of base rate (10% discount)
  • Next 10 units: 80% of base rate (20% discount)

External Price Comparison

Tests include AWS RDS comparison scenarios to verify competitive pricing.

Usage Instructions

Run All Tests

cd /home/tobru/src/servala/website
uv run --extra dev manage.py test hub.services.tests --verbosity=2

Run Specific Test Categories

# Basic pricing tests
uv run --extra dev manage.py test hub.services.tests.test_pricing

# Edge case tests
uv run --extra dev manage.py test hub.services.tests.test_pricing_edge_cases

# Integration tests
uv run --extra dev manage.py test hub.services.tests.test_pricing_integration

Use Test Runner Script

./run_pricing_tests.sh

Benefits

Regression Protection

The comprehensive test suite protects against breaking changes when:

  • Modifying discount calculation algorithms
  • Adding new pricing features
  • Refactoring pricing models
  • Updating business logic

Documentation

Tests serve as living documentation of how the pricing system should work, including:

  • Expected calculation logic
  • Error handling behavior
  • Integration patterns
  • Business rules

Confidence in Changes

Developers can make changes to the pricing system with confidence, knowing that the test suite will catch any regressions or unexpected behavior changes.

Maintenance

  • Tests are organized into logical groups for easy maintenance
  • Helper utilities reduce code duplication
  • Comprehensive documentation guides future development
  • Test runner script simplifies execution

The test suite follows Django best practices and provides comprehensive coverage of the pricing models and calculations, ensuring the reliability and correctness of the pricing system.