1- """
2- Additional tests to increase coverage for remaining uncovered lines
3- Targeting: __init__.py (40-42), admin_base.py (223-224, 246-247, 337, 349-351, 356, 359), models.py (206-215)
4- """
1+ """Tests for edge cases and error handling."""
52
63import sys
74from unittest .mock import MagicMock , patch
1815
1916
2017class TestVersionParsing (TestCase ):
21- """Test version parsing edge cases - covers __init__.py lines 40-42 """
18+ """Test version parsing edge cases. """
2219
2320 def test_version_with_build_metadata (self ):
24- """Test version parsing with build metadata to cover line 40 """
21+ """Test version parsing with build metadata. """
2522
26- # Test the exact code path from __init__.py
27- # We need to test the actual regex parsing logic
23+ # Test regex parsing logic for version strings
2824 import re
2925
30- # Test a version string that matches the pattern with build metadata
26+ # Test with build metadata
3127 version_string = "1.2.3-alpha.1+build.123"
3228 pattern = (
3329 r"(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?"
@@ -40,21 +36,21 @@ def test_version_with_build_metadata(self):
4036 version_info = (int (major ), int (minor ), int (patch ))
4137 if prerelease :
4238 version_info += (prerelease ,)
43- if build : # This covers line 40
39+ if build :
4440 version_info += (build ,)
4541 else :
46- version_info = (0 , 1 , 0 ) # This covers lines 41-42
42+ version_info = (0 , 1 , 0 )
4743
4844 # Verify build metadata was included
4945 assert len (version_info ) == 5
5046 assert version_info [- 1 ] == "build.123"
5147
5248 def test_version_parsing_fallback (self ):
53- """Test version parsing fallback to cover lines 41-42 """
49+ """Test version parsing with invalid input. """
5450
5551 import re
5652
57- # Test with invalid version string that won't match regex
53+ # Invalid version string
5854 version_string = "invalid-version"
5955 pattern = (
6056 r"(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?"
@@ -70,14 +66,14 @@ def test_version_parsing_fallback(self):
7066 if build :
7167 version_info += (build ,)
7268 else :
73- version_info = (0 , 1 , 0 ) # This covers lines 41-42
69+ version_info = (0 , 1 , 0 )
7470
7571 # Should fallback to default
7672 assert version_info == (0 , 1 , 0 )
7773
7874
7975class TestAdminBaseExceptions (TestCase ):
80- """Test admin base exception handling to increase coverage """
76+ """Test admin exception handling. """
8177
8278 def setUp (self ):
8379 self .admin = BaseAnonymizationAdmin (MaskingRule , AdminSite ())
@@ -87,7 +83,7 @@ def setUp(self):
8783 self .request .META = {}
8884
8985 def test_dry_run_batch_with_database_error (self ):
90- """Test dry run batch database error - covers lines 223-224 """
86+ """Test dry run batch operation with database error. """
9187
9288 rule = MaskingRule .objects .create (
9389 table_name = "test_table" , column_name = "test_column" , function_expr = "anon.fake_email()" , enabled = True
@@ -101,13 +97,13 @@ def test_dry_run_batch_with_database_error(self):
10197
10298 result = self .admin ._execute_dry_run_batch (queryset , self .admin .apply_rule_operation , "apply" )
10399
104- # Should handle the exception and return it in errors (covers lines 223-224)
100+ # Should handle the exception and return it in errors
105101 assert result ["applied_count" ] == 0
106102 assert len (result ["errors" ]) > 0
107103 assert "Database error" in result ["errors" ][0 ]
108104
109105 def test_transaction_batch_with_database_error (self ):
110- """Test transaction batch database error - covers lines 246-247 """
106+ """Test transaction batch operation with database error. """
111107
112108 rule = MaskingRule .objects .create (
113109 table_name = "test_table" , column_name = "test_column" , function_expr = "anon.fake_email()" , enabled = True
@@ -122,13 +118,13 @@ def test_transaction_batch_with_database_error(self):
122118
123119 result = self .admin ._execute_transaction_batch (queryset , self .admin .apply_rule_operation , "apply" )
124120
125- # Should handle the exception and return it in errors (covers lines 246-247)
121+ # Should handle the exception and return it in errors
126122 assert result ["applied_count" ] == 0
127123 assert len (result ["errors" ]) > 0
128124 assert "Transaction failed" in result ["errors" ][0 ]
129125
130126 def test_enable_rules_no_effect (self ):
131- """Test enable rules when no rules need enabling - covers line 337 """
127+ """Test enable operation when all rules are already enabled. """
132128
133129 # Create rules that are already enabled
134130 rule1 = MaskingRule .objects .create (
@@ -150,11 +146,11 @@ def test_enable_rules_no_effect(self):
150146 with patch .object (self .admin , "message_user" ) as mock_message :
151147 self .admin .enable_rules_operation (self .request , queryset )
152148
153- # Should show warning message (covers line 337)
149+ # Should show warning message
154150 mock_message .assert_called_with (self .request , "No rules were enabled" , level = messages .WARNING )
155151
156152 def test_disable_rules_with_save_failure (self ):
157- """Test disable rules with save failure - covers lines 349-351 """
153+ """Test disable operation when save fails. """
158154
159155 rule = MaskingRule .objects .create (
160156 table_name = "test_table" , column_name = "test_column" , function_expr = "anon.fake_email()" , enabled = True
@@ -168,11 +164,11 @@ def test_disable_rules_with_save_failure(self):
168164 with patch .object (self .admin , "message_user" ):
169165 self .admin .disable_rules_operation (self .request , queryset )
170166
171- # Should log the error (covers lines 349-351)
167+ # Should log the error
172168 mock_logger .error .assert_called ()
173169
174170 def test_disable_rules_no_enabled_rules (self ):
175- """Test disable rules when no rules are enabled - covers line 359 """
171+ """Test disable operation when all rules are already disabled. """
176172
177173 # Create rules that are already disabled
178174 rule = MaskingRule .objects .create (
@@ -187,12 +183,12 @@ def test_disable_rules_no_enabled_rules(self):
187183 with patch .object (self .admin , "message_user" ) as mock_message :
188184 self .admin .disable_rules_operation (self .request , queryset )
189185
190- # Should show warning message (covers line 359)
186+ # Should show warning message
191187 mock_message .assert_called_with (self .request , "No rules were disabled" , level = messages .WARNING )
192188
193189
194190class TestSignalDatabaseOperations (TestCase ):
195- """Test signal database operations - covers models.py lines 206-215 """
191+ """Test signal database operations. """
196192
197193 def test_signal_database_execution_success (self ):
198194 """Test successful database operation in signal"""
@@ -243,7 +239,7 @@ def test_signal_database_execution_with_exception(self):
243239 rule .enabled = False
244240
245241 # Let the signal execute naturally - it will hit the database
246- # and handle the error gracefully (covers lines 216-219)
242+ # and handle the error gracefully
247243 from django_postgres_anon .models import handle_rule_disabled
248244
249245 # This should execute the database code and handle any exceptions
0 commit comments