""" Tests for the normalization module. """ import pytest import math from src.normalization.signal_normalization import ( normalize_logic_check, normalize_cross_check, normalize_corroboration_signal, min_max_scale, calculate_trust_delta, SignalTracker ) from src.utils.common import CrossCheckType class TestNormalizeLogicCheck: """Tests for normalize_logic_check function.""" def test_valid_values(self): """Test with valid binary values.""" test_cases = [ (0, 0.0), (1, 1.0), (True, 1.0), (False, 0.0), ("1", 1.0), ("0", 0.0), ([], 0.0), # Empty collections evaluate to False ([1, 2], 1.0) # Non-empty collections evaluate to True ] for input_val, expected in test_cases: result = normalize_logic_check(input_val) assert result == expected assert isinstance(result, float) def test_invalid_values(self): """Test with values that cannot be interpreted as binary.""" invalid_values = [ "invalid", None, object() ] for value in invalid_values: with pytest.raises(ValueError): normalize_logic_check(value) class TestNormalizeCrossCheck: """Tests for normalize_cross_check function.""" def test_cosine_similarity(self): """Test normalization of cosine similarity values.""" test_cases = [ (-1.0, 0.0), # Minimum similarity -> 0 (-0.5, 0.25), # Negative similarity -> value in [0, 0.5) (0.0, 0.5), # No similarity -> 0.5 (0.5, 0.75), # Positive similarity -> value in (0.5, 1] (1.0, 1.0) # Maximum similarity -> 1 ] for sim, expected in test_cases: result = normalize_cross_check(sim, CrossCheckType.COSINE_SIMILARITY) assert pytest.approx(result) == expected def test_general_score(self): """Test normalization of general scores.""" max_value = 100 test_cases = [ (0, 0.0), # Minimum score -> 0 (50, 0.5), # Half of max -> 0.5 (100, 1.0), # Maximum score -> 1 (150, 1.0) # Beyond maximum should be clipped to 1 ] for score, expected in test_cases: result = normalize_cross_check(score, CrossCheckType.GENERAL_SCORE, max_value) assert pytest.approx(result) == expected def test_missing_max_value(self): """Test that error is raised when max_value is missing for GENERAL_SCORE.""" with pytest.raises(ValueError): normalize_cross_check(50, CrossCheckType.GENERAL_SCORE) def test_invalid_max_value(self): """Test that error is raised when max_value is invalid.""" invalid_max_values = [0, -10] for max_val in invalid_max_values: with pytest.raises(ValueError): normalize_cross_check(50, CrossCheckType.GENERAL_SCORE, max_val) def test_unknown_type(self): """Test that error is raised for unknown CrossCheckType.""" # Create a dummy type class DummyType: pass with pytest.raises(ValueError): normalize_cross_check(0.5, DummyType()) class TestNormalizeCorroborationSignal: """Tests for normalize_corroboration_signal function.""" def test_valid_values(self): """Test with valid inputs.""" kappa = 0.5 test_cases = [ (0, 0.0), # No corroborators -> 0 (1, 1.0 - math.exp(-kappa * 1)), # One corroborator (5, 1.0 - math.exp(-kappa * 5)), # Five corroborators (10, 1.0 - math.exp(-kappa * 10)) # Ten corroborators ] for num_corr, expected in test_cases: result = normalize_corroboration_signal(num_corr, kappa) assert pytest.approx(result) == expected def test_saturation_behavior(self): """Test that the function approaches 1.0 as number of corroborators increases.""" kappa = 0.5 medium_number = 10 # Use a more reasonable starting point large_number = 100 # Still large but not causing precision issues # Test with medium sized number first result1 = normalize_corroboration_signal(medium_number, kappa) assert result1 < 1.0 assert result1 > 0.9 # With a larger number, should be closer to 1 result2 = normalize_corroboration_signal(large_number, kappa) assert result2 > result1 # Should be closer to 1.0 assert result2 < 1.0 # Also verify mathematical properties directly # but use smaller numbers to avoid floating point precision issues math_result1 = 1.0 - math.exp(-kappa * medium_number/10) # Scaled down math_result2 = 1.0 - math.exp(-kappa * large_number/10) # Scaled down assert math_result2 > math_result1 def test_kappa_effect(self): """Test how different values of kappa affect the result.""" num_corr = 5 kappa_values = [0.1, 0.5, 1.0, 2.0] results = [normalize_corroboration_signal(num_corr, k) for k in kappa_values] # Results should increase as kappa increases for i in range(1, len(results)): assert results[i] > results[i-1] def test_invalid_inputs(self): """Test with invalid inputs.""" # Negative number of corroborators with pytest.raises(ValueError): normalize_corroboration_signal(-1, 0.5) # Non-positive kappa with pytest.raises(ValueError): normalize_corroboration_signal(5, 0) with pytest.raises(ValueError): normalize_corroboration_signal(5, -0.5) class TestMinMaxScale: """Tests for min_max_scale function.""" def test_valid_ranges(self): """Test with valid ranges.""" test_cases = [ (5, 0, 10, 0.5), # Middle of range (0, 0, 10, 0.0), # Minimum value (10, 0, 10, 1.0), # Maximum value (-5, -10, 0, 0.5), # Negative range (0, -10, 10, 0.5) # Range crossing zero ] for value, min_val, max_val, expected in test_cases: result = min_max_scale(value, min_val, max_val) assert pytest.approx(result) == expected def test_clipping(self): """Test clipping of out-of-range values.""" test_cases = [ (-5, 0, 10, 0.0), # Below range -> 0 (15, 0, 10, 1.0) # Above range -> 1 ] for value, min_val, max_val, expected in test_cases: result = min_max_scale(value, min_val, max_val) assert pytest.approx(result) == expected def test_equal_min_max(self): """Test case where min and max are equal.""" # Should return the default value (0.5) when min == max result = min_max_scale(5, 5, 5) assert pytest.approx(result) == 0.5 class TestCalculateTrustDelta: """Tests for calculate_trust_delta function.""" def test_valid_inputs(self): """Test with valid inputs.""" test_cases = [ # L, C, R, alpha, beta, gamma, expected (1.0, 1.0, 1.0, 0.3, 0.4, 0.3, 1.0), # All signals max, weights sum to 1 (0.0, 0.0, 0.0, 0.3, 0.4, 0.3, 0.0), # All signals min, weights sum to 1 (0.5, 0.5, 0.5, 0.3, 0.4, 0.3, 0.5), # All signals mid, weights sum to 1 (1.0, 0.0, 0.5, 0.2, 0.3, 0.4, 0.4), # Mixed signals, weights sum to 0.9 (1.0, 1.0, 1.0, 0.1, 0.1, 0.1, 0.3) # All signals max, weights sum to 0.3 ] for L, C, R, a, b, g, expected in test_cases: result = calculate_trust_delta(L, C, R, a, b, g) assert pytest.approx(result) == expected def test_weights_exceeding_one(self): """Test with weights summing to more than 1.""" # If weights sum to > 1, result should be clipped to 1.0 result = calculate_trust_delta(1.0, 1.0, 1.0, 0.5, 0.5, 0.5) assert pytest.approx(result) == 1.0 # Mixed case with clipping result = calculate_trust_delta(0.5, 0.5, 0.5, 1.0, 1.0, 1.0) assert pytest.approx(result) == 1.0 def test_invalid_signals(self): """Test with signal values outside [0,1].""" invalid_cases = [ # L, C, R, alpha, beta, gamma (-0.1, 0.5, 0.5, 0.3, 0.4, 0.3), # L < 0 (0.5, 1.1, 0.5, 0.3, 0.4, 0.3), # C > 1 (0.5, 0.5, -0.1, 0.3, 0.4, 0.3) # R < 0 ] for L, C, R, a, b, g in invalid_cases: with pytest.raises(ValueError): calculate_trust_delta(L, C, R, a, b, g) def test_negative_weights(self): """Test with negative weights.""" invalid_cases = [ # L, C, R, alpha, beta, gamma (0.5, 0.5, 0.5, -0.1, 0.4, 0.3), # alpha < 0 (0.5, 0.5, 0.5, 0.3, -0.1, 0.3), # beta < 0 (0.5, 0.5, 0.5, 0.3, 0.4, -0.1) # gamma < 0 ] for L, C, R, a, b, g in invalid_cases: with pytest.raises(ValueError): calculate_trust_delta(L, C, R, a, b, g) class TestSignalTracker: """Tests for SignalTracker class.""" def test_initialize_and_update(self): """Test initialization and updating with values.""" tracker = SignalTracker() # Initial state assert tracker.count == 0 assert tracker.min_value == float('inf') assert tracker.max_value == float('-inf') # Update with one value tracker.update(5.0) assert tracker.count == 1 assert tracker.min_value == 5.0 assert tracker.max_value == 5.0 # Update with more values tracker.update(3.0) # New min assert tracker.min_value == 3.0 assert tracker.max_value == 5.0 tracker.update(7.0) # New max assert tracker.min_value == 3.0 assert tracker.max_value == 7.0 assert tracker.count == 3 def test_get_normalized(self): """Test the get_normalized method.""" tracker = SignalTracker() # Add some values values = [3.0, 5.0, 7.0] for val in values: tracker.update(val) # Test normalization test_cases = [ (3.0, 0.0), # Min value -> 0 (5.0, 0.5), # Middle value -> 0.5 (7.0, 1.0), # Max value -> 1 (2.0, 0.0), # Below min -> clipped to 0 (8.0, 1.0) # Above max -> clipped to 1 ] for value, expected in test_cases: result = tracker.get_normalized(value) assert pytest.approx(result) == expected def test_empty_tracker(self): """Test behavior with no values.""" tracker = SignalTracker() # Should raise an error if trying to normalize with no values with pytest.raises(ValueError): tracker.get_normalized(5.0) def test_reset(self): """Test the reset method.""" tracker = SignalTracker() # Add some values values = [3.0, 5.0, 7.0] for val in values: tracker.update(val) # Check state before reset assert tracker.count == 3 assert tracker.min_value == 3.0 assert tracker.max_value == 7.0 # Reset and verify tracker.reset() assert tracker.count == 0 assert tracker.min_value == float('inf') assert tracker.max_value == float('-inf') # Should raise an error after reset with pytest.raises(ValueError): tracker.get_normalized(5.0)