class TestValidationResult
A unittest.TestCase class that provides comprehensive test coverage for the ValidationResult class, testing initialization, issue addition, string representation, and dictionary conversion.
/tf/active/vicechatdev/invoice_extraction/tests/test_validators.py
13 - 72
simple
Purpose
This test class validates the functionality of the ValidationResult class by testing its ability to track validation state, manage error and warning issues, maintain field-specific issue mappings, and provide different output formats. It ensures that ValidationResult correctly distinguishes between errors (which invalidate results) and warnings (which don't), and that all data structures are properly maintained.
Source Code
class TestValidationResult(unittest.TestCase):
"""Test cases for the ValidationResult class."""
def test_init(self):
"""Test initialization of ValidationResult."""
result = ValidationResult()
self.assertTrue(result.is_valid)
self.assertEqual(len(result.issues), 0)
self.assertEqual(len(result.warnings), 0)
self.assertEqual(len(result.field_issues), 0)
def test_add_issue_error(self):
"""Test adding an error issue."""
result = ValidationResult()
result.add_issue('test_field', 'Test error message', 'error')
# Should mark the result as invalid
self.assertFalse(result.is_valid)
self.assertEqual(len(result.issues), 1)
self.assertEqual(len(result.warnings), 0)
self.assertEqual(len(result.field_issues), 1)
self.assertEqual(result.field_issues['test_field'], 'Test error message')
def test_add_issue_warning(self):
"""Test adding a warning issue."""
result = ValidationResult()
result.add_issue('test_field', 'Test warning message', 'warning')
# Should not mark the result as invalid
self.assertTrue(result.is_valid)
self.assertEqual(len(result.issues), 0)
self.assertEqual(len(result.warnings), 1)
self.assertEqual(len(result.field_issues), 0)
def test_str_representation(self):
"""Test string representation of ValidationResult."""
result = ValidationResult()
result.add_issue('field1', 'Error 1', 'error')
result.add_issue('field2', 'Warning 1', 'warning')
# Check string representation
str_repr = str(result)
self.assertIn('INVALID', str_repr)
self.assertIn('field1: Error 1', str_repr)
self.assertIn('field2: Warning 1', str_repr)
def test_as_dict(self):
"""Test as_dict method of ValidationResult."""
result = ValidationResult()
result.add_issue('field1', 'Error 1', 'error')
result.add_issue('field2', 'Warning 1', 'warning')
# Convert to dict
result_dict = result.as_dict()
# Check dict structure
self.assertFalse(result_dict['is_valid'])
self.assertEqual(len(result_dict['issues']), 1)
self.assertEqual(len(result_dict['warnings']), 1)
self.assertIn('field1', result_dict['field_issues'])
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
bases |
unittest.TestCase | - |
Parameter Details
bases: Inherits from unittest.TestCase, which provides the testing framework infrastructure including assertion methods and test execution capabilities
Return Value
As a test class, it doesn't return values directly. When instantiated and run by a test runner, each test method returns None but uses assertions to validate behavior. Test failures raise AssertionError exceptions.
Class Interface
Methods
test_init(self) -> None
Purpose: Tests that ValidationResult initializes with correct default state: is_valid=True, empty issues, warnings, and field_issues collections
Returns: None - uses assertions to validate behavior
test_add_issue_error(self) -> None
Purpose: Tests that adding an error-level issue correctly marks the result as invalid, adds to issues list, and updates field_issues mapping
Returns: None - uses assertions to validate behavior
test_add_issue_warning(self) -> None
Purpose: Tests that adding a warning-level issue keeps the result valid, adds to warnings list, and doesn't update field_issues
Returns: None - uses assertions to validate behavior
test_str_representation(self) -> None
Purpose: Tests that the string representation of ValidationResult includes validity status and all issues/warnings with their field names
Returns: None - uses assertions to validate behavior
test_as_dict(self) -> None
Purpose: Tests that the as_dict method correctly converts ValidationResult to a dictionary with is_valid, issues, warnings, and field_issues keys
Returns: None - uses assertions to validate behavior
Dependencies
unittestdatetimeloggingvalidators.base_validatorvalidators.uk_validatorvalidators.be_validatorvalidators.au_validator
Required Imports
import unittest
from datetime import datetime
import logging
from validators.base_validator import BaseValidator
from validators.base_validator import ValidationResult
from validators.uk_validator import UKValidator
from validators.be_validator import BEValidator
from validators.au_validator import AUValidator
Usage Example
import unittest
from validators.base_validator import ValidationResult
# Run a specific test
test_suite = unittest.TestLoader().loadTestsFromTestCase(TestValidationResult)
test_runner = unittest.TextTestRunner(verbosity=2)
test_runner.run(test_suite)
# Or run individual test
test = TestValidationResult()
test.test_init()
test.test_add_issue_error()
test.test_add_issue_warning()
test.test_str_representation()
test.test_as_dict()
# Or use pytest
# pytest test_file.py::TestValidationResult
# Or use unittest discovery
# python -m unittest test_file.TestValidationResult
Best Practices
- This is a test class and should be run using a test runner (unittest, pytest, etc.), not instantiated directly in production code
- Each test method is independent and tests a specific aspect of ValidationResult functionality
- Tests follow the Arrange-Act-Assert pattern: create ValidationResult, perform operations, verify outcomes
- Test methods should be run in isolation; they don't depend on each other's state
- The class tests both positive cases (valid operations) and edge cases (error vs warning behavior)
- When adding new ValidationResult features, corresponding test methods should be added to this class
- Test method names follow the convention test_<feature_being_tested> for clarity
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class ValidationResult 72.4% similar
-
class TestUKValidator 59.1% similar
-
class TestBaseValidator 58.2% similar
-
class TestAUValidator 57.0% similar
-
function test_configuration_v2 53.9% similar