<s> import os <EOL> import sys <EOL> import lxml . etree <EOL> from mock import Mock , MagicMock , patch <EOL> import Bcfg2 . Server . Plugins . Cfg . CfgGenshiGenerator <EOL> from Bcfg2 . Server . Plugins . Cfg . CfgGenshiGenerator import * <EOL> from Bcfg2 . Server . Plugin import PluginExecutionError <EOL> path = os . path . dirname ( __file__ ) <EOL> while path != "<STR_LIT:/>" : <EOL> if os . path . basename ( path ) . lower ( ) . startswith ( "<STR_LIT:test>" ) : <EOL> sys . path . append ( path ) <EOL> if os . path . basename ( path ) == "<STR_LIT>" : <EOL> break <EOL> path = os . path . dirname ( path ) <EOL> from common import * <EOL> from TestServer . TestPlugins . TestCfg . Test_init import TestCfgGenerator <EOL> class TestCfgGenshiGenerator ( TestCfgGenerator ) : <EOL> test_obj = CfgGenshiGenerator <EOL> def setUp ( self ) : <EOL> TestCfgGenerator . setUp ( self ) <EOL> set_setup_default ( "<STR_LIT>" , datastore ) <EOL> def test__init ( self ) : <EOL> TestCfgGenerator . test__init ( self ) <EOL> cgg = self . get_obj ( ) <EOL> self . assertIsInstance ( cgg . loader , cgg . __loader_cls__ ) <EOL> @ patch ( "<STR_LIT>" ) <EOL> def test_get_data ( self , mock_get_template_data ) : <EOL> cgg = self . get_obj ( ) <EOL> cgg . _handle_genshi_exception = Mock ( ) <EOL> cgg . template = Mock ( ) <EOL> fltr = Mock ( ) <EOL> cgg . template . generate . return_value = fltr <EOL> stream = Mock ( ) <EOL> fltr . filter . return_value = stream <EOL> entry = lxml . etree . Element ( "<STR_LIT>" , name = "<STR_LIT>" ) <EOL> metadata = Mock ( ) <EOL> def reset ( ) : <EOL> cgg . template . reset_mock ( ) <EOL> cgg . _handle_genshi_exception . reset_mock ( ) <EOL> mock_get_template_data . reset_mock ( ) <EOL> template_vars = dict ( name = entry . get ( "<STR_LIT:name>" ) , <EOL> metadata = metadata , <EOL> path = cgg . name , <EOL> source_path = cgg . name , <EOL> repo = datastore ) <EOL> mock_get_template_data . return_value = template_vars <EOL> self . assertEqual ( cgg . get_data ( entry , metadata ) , <EOL> stream . render . return_value ) <EOL> cgg . template . generate . assert_called_with ( ** template_vars ) <EOL> self . assertItemsEqual ( mock_get_template_data . call_args [ <NUM_LIT:0> ] , <EOL> [ entry , metadata , cgg . name ] ) <EOL> self . assertIsInstance ( mock_get_template_data . call_args [ <NUM_LIT:1> ] [ '<STR_LIT:default>' ] , <EOL> DefaultGenshiDataProvider ) <EOL> fltr . filter . assert_called_with ( removecomment ) <EOL> stream . render . assert_called_with ( <EOL> "<STR_LIT:text>" , <EOL> encoding = Bcfg2 . Options . setup . encoding , <EOL> strip_whitespace = False ) <EOL> reset ( ) <EOL> def render ( fmt , ** kwargs ) : <EOL> stream . render . side_effect = None <EOL> raise TypeError <EOL> stream . render . side_effect = render <EOL> self . assertEqual ( cgg . get_data ( entry , metadata ) , <EOL> stream . render . return_value ) <EOL> cgg . template . generate . assert_called_with ( ** template_vars ) <EOL> self . assertItemsEqual ( mock_get_template_data . call_args [ <NUM_LIT:0> ] , <EOL> [ entry , metadata , cgg . name ] ) <EOL> self . assertIsInstance ( mock_get_template_data . call_args [ <NUM_LIT:1> ] [ '<STR_LIT:default>' ] , <EOL> DefaultGenshiDataProvider ) <EOL> fltr . filter . assert_called_with ( removecomment ) <EOL> self . assertEqual ( stream . render . call_args_list , <EOL> [ call ( "<STR_LIT:text>" , <EOL> encoding = Bcfg2 . Options . setup . encoding , <EOL> strip_whitespace = False ) , <EOL> call ( "<STR_LIT:text>" , <EOL> encoding = Bcfg2 . Options . setup . encoding ) ] ) <EOL> reset ( ) <EOL> stream . render . side_effect = UndefinedError ( "<STR_LIT:test>" ) <EOL> self . assertRaises ( UndefinedError , <EOL> cgg . get_data , entry , metadata ) <EOL> cgg . template . generate . assert_called_with ( ** template_vars ) <EOL> self . assertItemsEqual ( mock_get_template_data . call_args [ <NUM_LIT:0> ] , <EOL> [ entry , metadata , cgg . name ] ) <EOL> self . assertIsInstance ( mock_get_template_data . call_args [ <NUM_LIT:1> ] [ '<STR_LIT:default>' ] , <EOL> DefaultGenshiDataProvider ) <EOL> fltr . filter . assert_called_with ( removecomment ) <EOL> stream . render . assert_called_with ( "<STR_LIT:text>" , <EOL> encoding = Bcfg2 . Options . setup . encoding , <EOL> strip_whitespace = False ) <EOL> reset ( ) <EOL> stream . render . side_effect = ValueError <EOL> cgg . _handle_genshi_exception . side_effect = ValueError <EOL> self . assertRaises ( ValueError , <EOL> cgg . get_data , entry , metadata ) <EOL> cgg . template . generate . assert_called_with ( ** template_vars ) <EOL> self . assertItemsEqual ( mock_get_template_data . call_args [ <NUM_LIT:0> ] , <EOL> [ entry , metadata , cgg . name ] ) <EOL> self . assertIsInstance ( mock_get_template_data . call_args [ <NUM_LIT:1> ] [ '<STR_LIT:default>' ] , <EOL> DefaultGenshiDataProvider ) <EOL> fltr . filter . assert_called_with ( removecomment ) <EOL> stream . render . assert_called_with ( "<STR_LIT:text>" , <EOL> encoding = Bcfg2 . Options . setup . encoding , <EOL> strip_whitespace = False ) <EOL> self . assertTrue ( cgg . _handle_genshi_exception . called ) <EOL> def test_handle_event ( self ) : <EOL> cgg = self . get_obj ( ) <EOL> cgg . loader = Mock ( ) <EOL> event = Mock ( ) <EOL> cgg . handle_event ( event ) <EOL> cgg . loader . load . assert_called_with ( <EOL> cgg . name , <EOL> cls = NewTextTemplate , <EOL> encoding = Bcfg2 . Options . setup . encoding ) <EOL> cgg . loader . reset_mock ( ) <EOL> cgg . loader . load . side_effect = TemplateError ( "<STR_LIT:test>" ) <EOL> self . assertRaises ( PluginExecutionError , <EOL> cgg . handle_event , event ) <EOL> cgg . loader . load . assert_called_with ( <EOL> cgg . name , <EOL> cls = NewTextTemplate , <EOL> encoding = Bcfg2 . Options . setup . encoding ) </s>
