<s> """<STR_LIT>""" <EOL> import unittest , sys , os . path <EOL> from lxml import isoschematron <EOL> this_dir = os . path . dirname ( __file__ ) <EOL> if this_dir not in sys . path : <EOL> sys . path . insert ( <NUM_LIT:0> , this_dir ) <EOL> from common_imports import etree , HelperTestCase , fileInTestDir <EOL> from common_imports import doctest , make_doctest <EOL> class ETreeISOSchematronTestCase ( HelperTestCase ) : <EOL> def test_schematron ( self ) : <EOL> tree_valid = self . parse ( '<STR_LIT>' ) <EOL> tree_invalid = self . parse ( '<STR_LIT>' ) <EOL> schema = self . parse ( '''<STR_LIT>''' ) <EOL> schema = isoschematron . Schematron ( schema ) <EOL> self . assertTrue ( schema . validate ( tree_valid ) ) <EOL> self . assertTrue ( not schema . validate ( tree_invalid ) ) <EOL> def test_schematron_elementtree_error ( self ) : <EOL> self . assertRaises ( ValueError , isoschematron . Schematron , etree . ElementTree ( ) ) <EOL> def test_schematron_empty_pattern ( self ) : <EOL> schema = self . parse ( '''<STR_LIT>''' ) <EOL> schema = isoschematron . Schematron ( schema ) <EOL> self . assertTrue ( schema ) <EOL> def test_schematron_invalid_schema_empty ( self ) : <EOL> schema = self . parse ( '''<STR_LIT>''' ) <EOL> self . assertRaises ( etree . SchematronParseError , <EOL> isoschematron . Schematron , schema ) <EOL> def test_schematron_invalid_schema_namespace ( self ) : <EOL> schema = self . parse ( '''<STR_LIT>''' ) <EOL> self . assertRaises ( etree . SchematronParseError , <EOL> isoschematron . Schematron , schema ) <EOL> def test_schematron_from_tree ( self ) : <EOL> schema = self . parse ( '''<STR_LIT>''' ) <EOL> schematron = isoschematron . Schematron ( schema ) <EOL> self . assertTrue ( isinstance ( schematron , isoschematron . Schematron ) ) <EOL> def test_schematron_from_element ( self ) : <EOL> schema = self . parse ( '''<STR_LIT>''' ) <EOL> schematron = isoschematron . Schematron ( schema . getroot ( ) ) <EOL> self . assertTrue ( isinstance ( schematron , isoschematron . Schematron ) ) <EOL> def test_schematron_from_file ( self ) : <EOL> schematron = isoschematron . Schematron ( file = fileInTestDir ( '<STR_LIT>' ) ) <EOL> self . assertTrue ( isinstance ( schematron , isoschematron . Schematron ) ) <EOL> def test_schematron_call ( self ) : <EOL> schema = self . parse ( '''<STR_LIT>''' ) <EOL> tree_valid = self . parse ( '''<STR_LIT>''' ) <EOL> tree_invalid = self . parse ( '''<STR_LIT>''' ) <EOL> schematron = isoschematron . Schematron ( schema ) <EOL> self . assertTrue ( schematron ( tree_valid ) , schematron . error_log ) <EOL> valid = schematron ( tree_invalid ) <EOL> self . assertTrue ( not valid ) <EOL> def test_schematron_validate ( self ) : <EOL> schema = self . parse ( '''<STR_LIT>''' ) <EOL> tree_valid = self . parse ( '''<STR_LIT>''' ) <EOL> tree_invalid = self . parse ( '''<STR_LIT>''' ) <EOL> schematron = isoschematron . Schematron ( schema ) <EOL> self . assertTrue ( schematron . validate ( tree_valid ) , schematron . error_log ) <EOL> valid = schematron . validate ( tree_invalid ) <EOL> self . assertTrue ( not valid ) <EOL> def test_schematron_assertValid ( self ) : <EOL> schema = self . parse ( '''<STR_LIT>''' ) <EOL> tree_valid = self . parse ( '''<STR_LIT>''' ) <EOL> tree_invalid = self . parse ( '''<STR_LIT>''' ) <EOL> schematron = isoschematron . Schematron ( schema ) <EOL> self . assertTrue ( schematron ( tree_valid ) , schematron . error_log ) <EOL> self . assertRaises ( etree . DocumentInvalid , schematron . assertValid , <EOL> tree_invalid ) <EOL> def test_schematron_error_log ( self ) : <EOL> schema = self . parse ( '''<STR_LIT>''' ) <EOL> tree_valid = self . parse ( '''<STR_LIT>''' ) <EOL> tree_invalid = self . parse ( '''<STR_LIT>''' ) <EOL> schematron = isoschematron . Schematron ( schema ) <EOL> self . assertTrue ( schematron ( tree_valid ) , schematron . error_log ) <EOL> valid = schematron ( tree_invalid ) <EOL> self . assertTrue ( not valid ) <EOL> self . assertEqual ( len ( schematron . error_log ) , <NUM_LIT:1> , <EOL> '<STR_LIT>' % <EOL> ( schematron . error_log , len ( schematron . error_log ) ) ) <EOL> def test_schematron_result_report ( self ) : <EOL> schema = self . parse ( '''<STR_LIT>''' ) <EOL> tree_valid = self . parse ( '''<STR_LIT>''' ) <EOL> tree_invalid = self . parse ( '''<STR_LIT>''' ) <EOL> schematron = isoschematron . Schematron ( schema , store_report = True ) <EOL> self . assertTrue ( schematron ( tree_valid ) , schematron . error_log ) <EOL> valid = schematron ( tree_invalid ) <EOL> self . assertTrue ( not valid ) <EOL> self . assertTrue ( <EOL> isinstance ( schematron . validation_report , etree . _ElementTree ) , <EOL> '<STR_LIT>' % <EOL> ( schematron . validation_report ) ) <EOL> schematron = isoschematron . Schematron ( schema , store_report = False ) <EOL> self . assertTrue ( schematron ( tree_valid ) , schematron . error_log ) <EOL> valid = schematron ( tree_invalid ) <EOL> self . assertTrue ( not valid ) <EOL> self . assertTrue ( schematron . validation_report is None , <EOL> '<STR_LIT>' % <EOL> ( schematron . validation_report ) ) <EOL> def test_schematron_store_schematron ( self ) : <EOL> schema = self . parse ( '''<STR_LIT>''' ) <EOL> schematron = isoschematron . Schematron ( schema ) <EOL> self . assertTrue ( schematron . validator_xslt is None ) <EOL> schematron = isoschematron . Schematron ( schema , store_schematron = True ) <EOL> self . assertTrue ( isinstance ( schematron . schematron , etree . _ElementTree ) , <EOL> '<STR_LIT>' ) <EOL> def test_schematron_store_xslt ( self ) : <EOL> schema = self . parse ( '''<STR_LIT>''' ) <EOL> schematron = isoschematron . Schematron ( schema ) <EOL> self . assertTrue ( schematron . validator_xslt is None ) <EOL> schematron = isoschematron . Schematron ( schema , store_xslt = True ) <EOL> self . assertTrue ( isinstance ( schematron . validator_xslt , etree . _ElementTree ) , <EOL> '<STR_LIT>' ) <EOL> def test_schematron_abstract ( self ) : <EOL> schema = self . parse ( '''<STR_LIT>''' ) <EOL> valid_trees = [ <EOL> self . parse ( '''<STR_LIT>''' ) , <EOL> self . parse ( '''<STR_LIT>''' ) , <EOL> self . parse ( '''<STR_LIT>''' ) , <EOL> ] <EOL> schematron = isoschematron . Schematron ( schema ) <EOL> for tree_valid in valid_trees : <EOL> self . assertTrue ( schematron ( tree_valid ) , schematron . error_log ) <EOL> tree_invalid = self . parse ( '''<STR_LIT>''' ) <EOL> expected = <NUM_LIT:2> <EOL> valid = schematron ( tree_invalid ) <EOL> self . assertTrue ( not valid ) <EOL> self . assertEqual ( <EOL> len ( schematron . error_log ) , expected , <EOL> '<STR_LIT>' % <EOL> ( expected , schematron . error_log , len ( schematron . error_log ) ) ) <EOL> tree_invalid = self . parse ( '''<STR_LIT>''' ) <EOL> expected = <NUM_LIT:1> <EOL> valid = schematron ( tree_invalid ) <EOL> self . assertTrue ( not valid ) <EOL> self . assertEqual ( <EOL> len ( schematron . error_log ) , expected , <EOL> '<STR_LIT>' % <EOL> ( expected , schematron . error_log , len ( schematron . error_log ) ) ) <EOL> def test_schematron_phases ( self ) : <EOL> schema = self . parse ( '''<STR_LIT>''' ) <EOL> tree_valid = self . parse ( '''<STR_LIT>''' ) <EOL> tree_invalid = self . parse ( '''<STR_LIT>''' ) <EOL> schematron = isoschematron . Schematron ( schema ) <EOL> self . assertTrue ( schematron ( tree_valid ) , schematron . error_log ) <EOL> expected = <NUM_LIT:3> <EOL> valid = schematron ( tree_invalid ) <EOL> self . assertTrue ( not valid ) <EOL> self . assertEqual ( <EOL> len ( schematron . error_log ) , expected , <EOL> '<STR_LIT>' % <EOL> ( expected , schematron . error_log , len ( schematron . error_log ) ) ) <EOL> schematron = isoschematron . Schematron ( <EOL> schema , compile_params = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertTrue ( schematron ( tree_valid ) , schematron . error_log ) <EOL> expected = <NUM_LIT:1> <EOL> valid = schematron ( tree_invalid ) <EOL> self . assertTrue ( not valid ) <EOL> self . assertEqual ( <EOL> len ( schematron . error_log ) , expected , <EOL> '<STR_LIT>' % <EOL> ( expected , schematron . error_log , len ( schematron . error_log ) ) ) <EOL> schematron = isoschematron . Schematron ( <EOL> schema , compile_params = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertTrue ( schematron ( tree_valid ) , schematron . error_log ) <EOL> expected = <NUM_LIT:2> <EOL> valid = schematron ( tree_invalid ) <EOL> self . assertTrue ( not valid ) <EOL> self . assertEqual ( <EOL> len ( schematron . error_log ) , expected , <EOL> '<STR_LIT>' % <EOL> ( expected , schematron . error_log , len ( schematron . error_log ) ) ) <EOL> schematron = isoschematron . Schematron ( <EOL> schema , compile_params = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertTrue ( schematron ( tree_valid ) , schematron . error_log ) <EOL> expected = <NUM_LIT:3> <EOL> valid = schematron ( tree_invalid ) <EOL> self . assertTrue ( not valid ) <EOL> self . assertEqual ( <EOL> len ( schematron . error_log ) , expected , <EOL> '<STR_LIT>' % <EOL> ( expected , schematron . error_log , len ( schematron . error_log ) ) ) <EOL> def test_schematron_phases_kwarg ( self ) : <EOL> schema = self . parse ( '''<STR_LIT>''' ) <EOL> tree_valid = self . parse ( '''<STR_LIT>''' ) <EOL> tree_invalid = self . parse ( '''<STR_LIT>''' ) <EOL> schematron = isoschematron . Schematron ( schema ) <EOL> self . assertTrue ( schematron ( tree_valid ) , schematron . error_log ) <EOL> expected = <NUM_LIT:3> <EOL> valid = schematron ( tree_invalid ) <EOL> self . assertTrue ( not valid ) <EOL> self . assertEqual ( <EOL> len ( schematron . error_log ) , expected , <EOL> '<STR_LIT>' % <EOL> ( expected , schematron . error_log , len ( schematron . error_log ) ) ) <EOL> schematron = isoschematron . Schematron ( schema , phase = '<STR_LIT>' ) <EOL> self . assertTrue ( schematron ( tree_valid ) , schematron . error_log ) <EOL> expected = <NUM_LIT:1> <EOL> valid = schematron ( tree_invalid ) <EOL> self . assertTrue ( not valid ) <EOL> self . assertEqual ( <EOL> len ( schematron . error_log ) , expected , <EOL> '<STR_LIT>' % <EOL> ( expected , schematron . error_log , len ( schematron . error_log ) ) ) <EOL> schematron = isoschematron . Schematron ( schema , phase = '<STR_LIT>' ) <EOL> self . assertTrue ( schematron ( tree_valid ) , schematron . error_log ) <EOL> expected = <NUM_LIT:2> <EOL> valid = schematron ( tree_invalid ) <EOL> self . assertTrue ( not valid ) <EOL> self . assertEqual ( <EOL> len ( schematron . error_log ) , expected , <EOL> '<STR_LIT>' % <EOL> ( expected , schematron . error_log , len ( schematron . error_log ) ) ) <EOL> schematron = isoschematron . Schematron ( schema , phase = '<STR_LIT>' ) <EOL> self . assertTrue ( schematron ( tree_valid ) , schematron . error_log ) <EOL> expected = <NUM_LIT:3> <EOL> valid = schematron ( tree_invalid ) <EOL> self . assertTrue ( not valid ) <EOL> self . assertEqual ( <EOL> len ( schematron . error_log ) , expected , '<STR_LIT>' % <EOL> ( expected , schematron . error_log , len ( schematron . error_log ) ) ) <EOL> def test_schematron_xmlschema_embedded ( self ) : <EOL> schema = self . parse ( '''<STR_LIT>''' ) <EOL> tree_valid = self . parse ( '''<STR_LIT>''' ) <EOL> tree_invalid = self . parse ( '''<STR_LIT>''' ) <EOL> xmlschema = etree . XMLSchema ( schema ) <EOL> schematron = isoschematron . Schematron ( schema ) <EOL> self . assertTrue ( xmlschema ( tree_valid ) , xmlschema . error_log ) <EOL> self . assertTrue ( schematron ( tree_valid ) ) <EOL> self . assertTrue ( xmlschema ( tree_invalid ) , xmlschema . error_log ) <EOL> self . assertTrue ( not schematron ( tree_invalid ) ) <EOL> def test_schematron_relaxng_embedded ( self ) : <EOL> schema = self . parse ( '''<STR_LIT>''' ) <EOL> tree_valid = self . parse ( '''<STR_LIT>''' ) <EOL> tree_invalid = self . parse ( '''<STR_LIT>''' ) <EOL> relaxng = etree . RelaxNG ( schema ) <EOL> schematron = isoschematron . Schematron ( schema ) <EOL> self . assertTrue ( relaxng ( tree_valid ) , relaxng . error_log ) <EOL> self . assertTrue ( schematron ( tree_valid ) ) <EOL> self . assertTrue ( relaxng ( tree_invalid ) , relaxng . error_log ) <EOL> self . assertTrue ( not schematron ( tree_invalid ) ) <EOL> def test_schematron_invalid_args ( self ) : <EOL> schema = self . parse ( '''<STR_LIT>''' ) <EOL> self . assertRaises ( TypeError , isoschematron . Schematron , schema , <EOL> compile_params = { '<STR_LIT>' : None } ) <EOL> def test_schematron_customization ( self ) : <EOL> class MySchematron ( isoschematron . Schematron ) : <EOL> def _extract ( self , root ) : <EOL> schematron = ( root . xpath ( <EOL> '<STR_LIT>' , <EOL> namespaces = { '<STR_LIT>' : "<STR_LIT>" } ) <EOL> or [ None ] ) [ <NUM_LIT:0> ] <EOL> return schematron <EOL> def _include ( self , schematron , ** kwargs ) : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> def _expand ( self , schematron , ** kwargs ) : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> def _validation_errors ( self , validationReport ) : <EOL> valid = etree . XPath ( <EOL> '<STR_LIT>' , <EOL> namespaces = { '<STR_LIT>' : isoschematron . SVRL_NS } ) ( <EOL> validationReport ) <EOL> if valid : <EOL> return [ ] <EOL> error = etree . Element ( '<STR_LIT>' ) <EOL> error . text = '<STR_LIT>' <EOL> return [ error ] <EOL> tree_valid = self . parse ( '<STR_LIT>' ) <EOL> tree_invalid = self . parse ( '<STR_LIT>' ) <EOL> schema = self . parse ( '''<STR_LIT>''' ) <EOL> self . assertRaises ( RuntimeError , MySchematron , schema , store_report = True ) <EOL> self . assertRaises ( RuntimeError , MySchematron , schema , store_report = True , <EOL> include = False ) <EOL> schema = MySchematron ( schema , store_report = True , include = False , <EOL> expand = False ) <EOL> self . assertTrue ( schema . validate ( tree_valid ) ) <EOL> self . assertTrue ( not schema . validate ( tree_invalid ) ) <EOL> def test_suite ( ) : <EOL> suite = unittest . TestSuite ( ) <EOL> suite . addTests ( [ unittest . makeSuite ( ETreeISOSchematronTestCase ) ] ) <EOL> suite . addTests ( <EOL> [ make_doctest ( '<STR_LIT>' ) ] ) <EOL> return suite <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> print ( '<STR_LIT>' % __file__ ) </s>
