PropertyDamage.Suggestions (PropertyDamage v0.2.0)
View SourceProperty and invariant suggestions for PropertyDamage models.
Analyzes a model's commands, events, and projections to suggest missing checks and invariants that would improve test coverage.
Overview
This module helps answer the question: "What invariants should my model check?"
It examines:
- Event fields: Numeric fields that should be non-negative, currency fields that should be consistent, reference fields that should exist, etc.
- Command patterns: Operations that commonly need specific checks
- Existing checks: What's already covered vs what's missing
Usage
# Analyze a model and get suggestions
suggestions = PropertyDamage.Suggestions.analyze(MyModel)
# Print suggestions
IO.puts(PropertyDamage.Suggestions.format(suggestions))
# Get suggestions as structured data
suggestions.missing_checks # List of suggested checks
suggestions.unchecked_fields # Fields with no apparent validation
suggestions.coverage_gaps # Areas lacking coverageHow It Works
Event Analysis: Extracts all events produced by commands and analyzes their field types to suggest appropriate checks.
Pattern Detection: Identifies common patterns that warrant invariants:
- Numeric fields (balance, amount, count) → non-negative checks
- Currency/type fields → consistency checks across operations
- Reference fields (account_ref, user_id) → existence checks
- Status fields → valid state transition checks
- Timestamp fields → ordering checks
Check Coverage: Compares detected patterns against existing checks to find gaps in test coverage.
Suggestion Generation: Produces actionable suggestions with example code for implementing missing checks.
Summary
Functions
Analyzes a model and returns suggestions for missing checks.
Returns suggestions for a specific event type.
Returns suggestions for a specific field.
Formats suggestions for display.
Generates example check code for a suggestion.
Returns only high-priority suggestions.
Returns a quick summary of the analysis.
Types
@type analysis() :: %{ model: module(), suggestions: [suggestion()], detected_patterns: [PropertyDamage.Suggestions.Patterns.pattern()], existing_checks: [map()], field_coverage: map(), summary: String.t() }
Functions
Analyzes a model and returns suggestions for missing checks.
Options
:include_low_priority- Include low priority suggestions (default: true):max_suggestions- Maximum suggestions to return (default: 20):focus- Focus on specific areas::all,:numeric,:references,:consistency(default::all)
Example
suggestions = PropertyDamage.Suggestions.analyze(MyModel)
IO.puts(PropertyDamage.Suggestions.format(suggestions))
@spec for_event(analysis(), module()) :: [suggestion()]
Returns suggestions for a specific event type.
@spec for_field(analysis(), atom()) :: [suggestion()]
Returns suggestions for a specific field.
Formats suggestions for display.
Formats
:terminal- ASCII box output for console (default):markdown- Markdown tables for documentation:json- JSON for programmatic analysis
Example
suggestions = PropertyDamage.Suggestions.analyze(MyModel)
IO.puts(PropertyDamage.Suggestions.format(suggestions, :terminal))
@spec generate_check_code(suggestion()) :: String.t()
Generates example check code for a suggestion.
@spec high_priority(analysis()) :: [suggestion()]
Returns only high-priority suggestions.
Returns a quick summary of the analysis.