11#!/usr/bin/python3
2- """
3- Unit tests for cifmw_validations_xml_filter.
4-
5- These tests verify that the filter:
6- 1. Returns str (not bytes) - CRITICAL for the fix
7- 2. Handles normal cases
8- 3. Handles malformed/edge-case input gracefully
9- 4. Generates valid XML output
10- """
2+ # Copyright (c) 2026 Red Hat, Inc.
3+ # SPDX-License-Identifier: Apache-2.0
114
125import pytest
136import sys
1811
1912
2013class TestFilterReturnType :
21- """CRITICAL: Tests to verify the fix for the hang issue """
14+ """Tests to verify the filter works correctly """
2215
2316 def setup_method (self ):
2417 self .filter_module = FilterModule ()
2518 self .filter_func = self .filter_module .filters ()['cifmw_validations_xml_filter' ]
2619
27- def test_returns_string_not_bytes (self ):
28- """
29- ⚠️ CRITICAL TEST ⚠️
30-
31- Ensure filter returns str, not bytes.
32-
33- Before fix: encoding='utf-8' → returns bytes → Ansible hangs
34- After fix: encoding='unicode' → returns str → Ansible works
35- """
20+ def test_returns_format (self ):
21+ """Verify filter returns str format, not bytes."""
3622 result = self .filter_func ({})
37- assert isinstance (result , str ), \
38- f"CRITICAL: Filter must return str, not { type (result ).__name__ } . " \
39- "Returning bytes causes Ansible to hang!"
23+ assert isinstance (result , str ), "Filter must return str, not bytes!"
4024
4125 def test_empty_results (self ):
42- """Test with empty test results"""
26+ """Verify filter generates valid XML with empty test results. """
4327 result = self .filter_func ({})
4428 assert isinstance (result , str )
45- assert '<?xml' in result
4629 assert 'tests="0"' in result
4730
4831 def test_single_passing_test (self ):
49- """Test with single passing test"""
32+ """Verify filter correctly formats a single passing test case. """
5033 result = self .filter_func ({"test-1" : {"time" : 1.5 }})
5134 assert isinstance (result , str )
52- assert '<testcase name="test-1"' in result
5335 assert 'tests="1"' in result
5436 assert 'failures="0"' in result
5537
5638 def test_single_failing_test (self ):
57- """Test with single failing test"""
58- result = self .filter_func ({
59- "test-1" : {"time" : 1.0 , "error" : "Test failed" }
60- })
39+ """Verify filter correctly formats a single failing test case."""
40+ result = self .filter_func ({"test-1" : {"time" : 1.0 , "error" : "Test failed" }})
6141 assert 'failures="1"' in result
62- assert 'error' in result .lower () or 'failure' in result .lower ()
6342
6443 def test_multiple_mixed_tests (self ):
65- """Test with multiple passing and failing tests"""
44+ """Verify filter correctly formats multiple passing and failing tests. """
6645 result = self .filter_func ({
6746 "test-1" : {"time" : 1.0 },
6847 "test-2.yml" : {"time" : 2.0 , "error" : "failed" },
@@ -72,74 +51,52 @@ def test_multiple_mixed_tests(self):
7251 assert 'failures="1"' in result
7352
7453 def test_valid_xml_output (self ):
75- """Verify output is valid, parseable XML"""
76- result = self .filter_func ({
77- "test-1" : {"time" : 1.0 , "error" : "err" },
78- "test-2" : {"time" : 2.0 }
79- })
80-
81- # Should be parseable as XML (not corrupted)
54+ """Verify filter generates valid, parseable XML with correct structure."""
55+ result = self .filter_func ({"test-1" : {"time" : 1.0 , "error" : "err" }, "test-2" : {"time" : 2.0 }})
8256 root = ET .fromstring (result )
8357 assert root .tag == 'testsuites'
84-
85- # Verify structure
8658 ts = root .find ('testsuite' )
87- assert ts is not None
88- assert ts .attrib ['name' ] == 'validations'
8959 assert ts .attrib ['tests' ] == '2'
9060 assert ts .attrib ['failures' ] == '1'
9161
9262
9363class TestMalformedInput :
94- """Tests with edge-case/malformed input to ensure robustness """
64+ """Tests to verify filter handles edge cases and malformed input gracefully """
9565
9666 def setup_method (self ):
9767 self .filter_module = FilterModule ()
9868 self .filter_func = self .filter_module .filters ()['cifmw_validations_xml_filter' ]
9969
10070 def test_missing_time_field (self ):
101- """Test with missing time field in some results"""
102- result = self .filter_func ({
103- "test-1" : {"error" : "some error" },
104- "test-2" : {"time" : 1.5 }
105- })
71+ """Verify filter handles test results with missing time field."""
72+ result = self .filter_func ({"test-1" : {"error" : "some error" }, "test-2" : {"time" : 1.5 }})
10673 assert isinstance (result , str )
10774 assert 'tests="2"' in result
10875
10976 def test_very_long_error_message (self ):
110- """Test with very long error message (5000+ chars) """
77+ """Verify filter handles very long error messages (5000+ characters). """
11178 long_error = "x" * 5000
112- result = self .filter_func ({
113- "test-1" : {"time" : 1.0 , "error" : long_error }
114- })
79+ result = self .filter_func ({"test-1" : {"time" : 1.0 , "error" : long_error }})
11580 assert isinstance (result , str )
11681 root = ET .fromstring (result )
11782 assert root is not None
11883
11984 def test_xml_special_characters_in_error (self ):
120- """Test with XML special characters that need escaping"""
121- result = self .filter_func ({
122- "test-1" : {"time" : 1.0 , "error" : "<tag> & special > chars" }
123- })
85+ """Verify filter properly escapes XML special characters in error messages."""
86+ result = self .filter_func ({"test-1" : {"time" : 1.0 , "error" : "<tag> & special > chars" }})
12487 assert isinstance (result , str )
125- # Should be properly escaped by ElementTree
12688 root = ET .fromstring (result )
12789 assert root is not None
12890
12991 def test_unicode_characters (self ):
130- """Test with unicode characters"""
131- result = self .filter_func ({
132- "test-unicode" : {"time" : 1.0 , "error" : "Error message" }
133- })
92+ """Verify filter handles unicode characters in test names and messages."""
93+ result = self .filter_func ({"test-unicode" : {"time" : 1.0 , "error" : "Error message" }})
13494 assert isinstance (result , str )
13595 assert 'tests="1"' in result
13696
13797 def test_large_number_of_tests (self ):
138- """Test with large number of test cases (100+)"""
139- large_test_data = {
140- f"test-{ i } " : {"time" : 0.1 * i }
141- for i in range (100 )
142- }
98+ """Verify filter performs well with large number of test cases (100+)."""
99+ large_test_data = {f"test-{ i } " : {"time" : 0.1 * i } for i in range (100 )}
143100 result = self .filter_func (large_test_data )
144101 assert isinstance (result , str )
145102 assert 'tests="100"' in result
0 commit comments