<s> import os <EOL> import tempfile <EOL> import unittest <EOL> from cStringIO import StringIO <EOL> from trac . config import ConfigurationError <EOL> from trac . env import Environment <EOL> from trac . test import MockRequest <EOL> from trac . tests . compat import rmtree <EOL> from trac . util import translation <EOL> class DatabaseFileTestCase ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . env_path = tempfile . mkdtemp ( prefix = '<STR_LIT>' ) <EOL> self . db_path = os . path . join ( self . env_path , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def tearDown ( self ) : <EOL> rmtree ( self . env_path ) <EOL> def _create_env ( self ) : <EOL> env = Environment ( self . env_path , create = True ) <EOL> env . shutdown ( ) <EOL> def _db_query ( self , env ) : <EOL> env . db_query ( "<STR_LIT>" ) <EOL> def _make_environ ( self , scheme = '<STR_LIT:http>' , server_name = '<STR_LIT>' , <EOL> server_port = <NUM_LIT> , method = '<STR_LIT:GET>' , script_name = '<STR_LIT>' , <EOL> cookie = None , ** kwargs ) : <EOL> environ = { '<STR_LIT>' : scheme , '<STR_LIT>' : StringIO ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : method , '<STR_LIT>' : server_name , <EOL> '<STR_LIT>' : server_port , '<STR_LIT>' : script_name } <EOL> if cookie : <EOL> environ [ '<STR_LIT>' ] = cookie <EOL> environ . update ( kwargs ) <EOL> return environ <EOL> def test_missing_tracdb ( self ) : <EOL> self . _create_env ( ) <EOL> os . remove ( self . db_path ) <EOL> env = Environment ( self . env_path ) <EOL> try : <EOL> self . _db_query ( env ) <EOL> self . fail ( '<STR_LIT>' ) <EOL> except ConfigurationError as e : <EOL> self . assertIn ( '<STR_LIT>' , unicode ( e ) ) <EOL> self . assertIn ( '<STR_LIT>' , unicode ( e ) ) <EOL> def test_no_permissions ( self ) : <EOL> self . _create_env ( ) <EOL> os . chmod ( self . db_path , <NUM_LIT:0> <NUM_LIT> ) <EOL> env = Environment ( self . env_path ) <EOL> try : <EOL> self . _db_query ( env ) <EOL> self . fail ( '<STR_LIT>' ) <EOL> except ConfigurationError as e : <EOL> self . assertIn ( '<STR_LIT>' , unicode ( e ) ) <EOL> if os . name == '<STR_LIT>' and os . getuid ( ) == <NUM_LIT:0> : <EOL> del test_no_permissions <EOL> def test_error_with_lazy_translation ( self ) : <EOL> self . _create_env ( ) <EOL> os . remove ( self . db_path ) <EOL> env = Environment ( self . env_path ) <EOL> req = MockRequest ( env , authname = '<STR_LIT>' ) <EOL> translation . make_activable ( lambda : req . locale , env . path ) <EOL> try : <EOL> self . _db_query ( env ) <EOL> self . fail ( '<STR_LIT>' ) <EOL> except ConfigurationError as e : <EOL> message = unicode ( e ) <EOL> self . assertIn ( '<STR_LIT>' , message ) <EOL> self . assertIn ( '<STR_LIT>' , message ) <EOL> finally : <EOL> translation . deactivate ( ) <EOL> def suite ( ) : <EOL> suite = unittest . TestSuite ( ) <EOL> suite . addTest ( unittest . makeSuite ( DatabaseFileTestCase ) ) <EOL> return suite <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( defaultTest = '<STR_LIT>' ) </s>
