import unittest from sia.xml_validator import XMLValidator class XMLValidatorTest(unittest.TestCase): def setUp(self): """Set up test schema""" self.test_schema = """ """ self.validator = XMLValidator(self.test_schema) def test_initialization(self): """Test validator initialization and root element extraction""" valid_elements = self.validator.get_valid_root_elements() self.assertEqual(valid_elements, {'test_element', 'simple_element'}) def test_invalid_schema(self): """Test handling invalid schema""" with self.assertRaises(ValueError): XMLValidator("invalid schema") def test_valid_xml(self): """Test validation of valid XML""" xml = 'test content' result = self.validator.validate(xml) self.assertIsNone(result) # Test without optional attribute xml = 'test content' result = self.validator.validate(xml) self.assertIsNone(result) # Test simple element xml = 'test content' result = self.validator.validate(xml) self.assertIsNone(result) def test_invalid_root_element(self): """Test validation with invalid root element""" xml = 'test content' result = self.validator.validate(xml) self.assertIsNotNone(result) self.assertIn("Invalid root element", result) def test_missing_required_attribute(self): """Test validation with missing required attribute""" xml = 'test content' result = self.validator.validate(xml) self.assertIsNotNone(result) self.assertIn("Missing required attribute", result) def test_invalid_integer_attribute(self): """Test validation with invalid integer attribute""" xml = 'test content' result = self.validator.validate(xml) self.assertIsNotNone(result) self.assertIn("Invalid integer value", result) def test_unexpected_attribute(self): """Test validation with unexpected attribute""" xml = 'test content' result = self.validator.validate(xml) self.assertIsNotNone(result) self.assertIn("Unexpected attribute", result) def test_invalid_xml_syntax(self): """Test validation with invalid XML syntax""" xml = 'unclosed tag' result = self.validator.validate(xml) self.assertIsNotNone(result) self.assertIn("Invalid XML", result) def test_whitespace_handling(self): """Test validation with whitespace in XML""" xml = """ test content """ result = self.validator.validate(xml) self.assertIsNone(result) def test_empty_content(self): """Test validation with empty element content""" xml = '' result = self.validator.validate(xml) self.assertIsNone(result) xml = '' result = self.validator.validate(xml) self.assertIsNone(result) def test_special_characters(self): """Test validation with special characters""" xml = ' " \' chars]]>' result = self.validator.validate(xml) self.assertIsNone(result)