<s> import unittest <EOL> import os <EOL> from xserverpy . utils . settings import Settings , EmptySettings <EOL> import glob <EOL> import shutil <EOL> from mock import MagicMock <EOL> class SettingsTests ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . f = "<STR_LIT>" <EOL> try : <EOL> os . remove ( "<STR_LIT>" ) <EOL> except : pass <EOL> try : <EOL> os . remove ( os . path . expanduser ( "<STR_LIT>" ) ) <EOL> except : pass <EOL> def test_can_save_to_local_file ( self ) : <EOL> server = MagicMock ( ) <EOL> server . __dict__ = { "<STR_LIT:host>" : "<STR_LIT>" , "<STR_LIT:port>" : "<STR_LIT>" } <EOL> user = MagicMock ( ) <EOL> user . __dict__ = { "<STR_LIT:user>" : "<STR_LIT>" , "<STR_LIT:password>" : "<STR_LIT:password>" } <EOL> self . assertEqual ( os . path . exists ( "<STR_LIT>" ) , False ) <EOL> settings = Settings ( server , user ) <EOL> settings . store ( False , "<STR_LIT>" ) <EOL> self . assertEqual ( os . path . exists ( "<STR_LIT>" ) , True ) <EOL> def test_can_save_to_global_file ( self ) : <EOL> server = MagicMock ( ) <EOL> server . __dict__ = { "<STR_LIT:host>" : "<STR_LIT>" , "<STR_LIT:port>" : "<STR_LIT>" } <EOL> user = MagicMock ( ) <EOL> user . __dict__ = { "<STR_LIT:user>" : "<STR_LIT>" , "<STR_LIT:password>" : "<STR_LIT:password>" } <EOL> path = os . path . expanduser ( "<STR_LIT>" ) <EOL> self . assertEqual ( os . path . exists ( path ) , False ) <EOL> settings = Settings ( server , user ) <EOL> settings . store ( True , "<STR_LIT>" ) <EOL> self . assertEqual ( os . path . exists ( path ) , True ) <EOL> def test_can_load_from_global_file ( self ) : <EOL> self . dummy_store ( True ) <EOL> path = os . path . expanduser ( "<STR_LIT>" ) <EOL> self . assertEqual ( os . path . exists ( path ) , True ) <EOL> self . assertEqual ( os . path . exists ( "<STR_LIT>" ) , False ) <EOL> settings = Settings . load ( "<STR_LIT>" ) <EOL> self . assertEqual ( settings . server . host , "<STR_LIT>" ) <EOL> self . assertEqual ( settings . server . port , "<STR_LIT>" ) <EOL> self . assertEqual ( settings . user . user , "<STR_LIT>" ) <EOL> self . assertEqual ( settings . user . password , "<STR_LIT:password>" ) <EOL> def test_can_load_from_local_file ( self ) : <EOL> self . dummy_store ( False ) <EOL> path = os . path . expanduser ( "<STR_LIT>" ) <EOL> self . assertEqual ( os . path . exists ( path ) , False ) <EOL> self . assertEqual ( os . path . exists ( "<STR_LIT>" ) , True ) <EOL> settings = Settings . load ( "<STR_LIT>" ) <EOL> self . assertEqual ( settings . server . host , "<STR_LIT>" ) <EOL> self . assertEqual ( settings . server . port , "<STR_LIT>" ) <EOL> self . assertEqual ( settings . user . user , "<STR_LIT>" ) <EOL> self . assertEqual ( settings . user . password , "<STR_LIT:password>" ) <EOL> def test_returns_none_if_no_file ( self ) : <EOL> path = os . path . expanduser ( "<STR_LIT>" ) <EOL> self . assertEqual ( os . path . exists ( path ) , False ) <EOL> self . assertEqual ( os . path . exists ( "<STR_LIT>" ) , False ) <EOL> settings = Settings . load ( "<STR_LIT>" ) <EOL> self . assertIsInstance ( settings , EmptySettings ) <EOL> def test_can_updated_an_empty_with_user_and_server ( self ) : <EOL> settings = EmptySettings ( ) <EOL> self . assertIsNone ( settings . server ) <EOL> self . assertIsNone ( settings . user ) <EOL> settings . update ( MagicMock ( ) , MagicMock ( ) ) <EOL> self . assertIsNotNone ( settings . server ) <EOL> self . assertIsNotNone ( settings . user ) <EOL> def test_validates_correctly ( self ) : <EOL> server = MagicMock ( ) <EOL> server . __dict__ = { "<STR_LIT:host>" : None , "<STR_LIT:port>" : "<STR_LIT>" } <EOL> user = MagicMock ( ) <EOL> user . __dict__ = { "<STR_LIT:user>" : "<STR_LIT>" , "<STR_LIT:password>" : "<STR_LIT>" } <EOL> settings = Settings ( server , user ) <EOL> with self . assertRaises ( RuntimeError ) : <EOL> settings . validate ( ) <EOL> server = MagicMock ( ) <EOL> server . __dict__ = { "<STR_LIT:host>" : "<STR_LIT>" , "<STR_LIT:port>" : None } <EOL> settings . server = server <EOL> with self . assertRaises ( RuntimeError ) : <EOL> settings . validate ( ) <EOL> def test_can_updated_a_non_empty_with_user_and_server ( self ) : <EOL> self . dummy_store ( False ) <EOL> path = os . path . expanduser ( "<STR_LIT>" ) <EOL> self . assertEqual ( os . path . exists ( path ) , False ) <EOL> self . assertEqual ( os . path . exists ( "<STR_LIT>" ) , True ) <EOL> settings = Settings . load ( "<STR_LIT>" ) <EOL> self . assertEqual ( settings . server . host , "<STR_LIT>" ) <EOL> self . assertEqual ( settings . server . port , "<STR_LIT>" ) <EOL> self . assertEqual ( settings . user . user , "<STR_LIT>" ) <EOL> self . assertEqual ( settings . user . password , "<STR_LIT:password>" ) <EOL> server = MagicMock ( ) <EOL> server . __dict__ = { "<STR_LIT:host>" : "<STR_LIT>" , "<STR_LIT:port>" : "<STR_LIT>" } <EOL> user = MagicMock ( ) <EOL> user . __dict__ = { "<STR_LIT:user>" : "<STR_LIT>" , "<STR_LIT:password>" : "<STR_LIT>" } <EOL> settings . update ( server , user ) <EOL> self . assertEqual ( settings . server . host , "<STR_LIT>" ) <EOL> self . assertEqual ( settings . server . port , "<STR_LIT>" ) <EOL> self . assertEqual ( settings . user . user , "<STR_LIT>" ) <EOL> self . assertEqual ( settings . user . password , "<STR_LIT>" ) <EOL> server = MagicMock ( ) <EOL> server . __dict__ = { "<STR_LIT:host>" : None , "<STR_LIT:port>" : None } <EOL> user = MagicMock ( ) <EOL> user . __dict__ = { "<STR_LIT:user>" : "<STR_LIT>" , "<STR_LIT:password>" : "<STR_LIT>" } <EOL> settings . update ( server , user ) <EOL> self . assertEqual ( settings . server . host , "<STR_LIT>" ) <EOL> self . assertEqual ( settings . server . port , "<STR_LIT>" ) <EOL> self . assertEqual ( settings . user . user , "<STR_LIT>" ) <EOL> self . assertEqual ( settings . user . password , "<STR_LIT>" ) <EOL> def dummy_store ( self , is_global ) : <EOL> server = MagicMock ( ) <EOL> server . __dict__ = { "<STR_LIT:host>" : "<STR_LIT>" , "<STR_LIT:port>" : "<STR_LIT>" } <EOL> user = MagicMock ( ) <EOL> user . __dict__ = { "<STR_LIT:user>" : "<STR_LIT>" , "<STR_LIT:password>" : "<STR_LIT:password>" } <EOL> settings = Settings ( server , user ) <EOL> settings . store ( is_global , "<STR_LIT>" ) </s>
