<s> '''<STR_LIT>''' <EOL> import unittest <EOL> from biicode . client . shell . updates_manager import ( UpdatesManager , UpdatesStore , <EOL> UpdateInfo ) <EOL> from biicode . common . test . bii_test_case import BiiTestCase <EOL> from mock import Mock <EOL> from biicode . common . model . server_info import ServerInfo , ClientVersion <EOL> import datetime <EOL> import os <EOL> from biicode . client . exception import ObsoleteClient <EOL> from biicode . client . test . shell . user_io_mock import mocked_user_io <EOL> from biicode . common . output_stream import OutputStream <EOL> class UpdatesStoreTest ( BiiTestCase ) : <EOL> def setUp ( self ) : <EOL> BiiTestCase . setUp ( self ) <EOL> self . folder = self . new_tmp_folder ( ) <EOL> self . file_path = os . path . join ( self . folder , "<STR_LIT>" ) <EOL> self . store = UpdatesStore ( self . file_path ) <EOL> def model_serialization_test ( self ) : <EOL> server_info = ServerInfo ( ClientVersion ( "<STR_LIT>" ) , '<STR_LIT>' , "<STR_LIT>" ) <EOL> now = datetime . datetime . utcnow ( ) <EOL> info = UpdateInfo ( server_info , now ) <EOL> seri = info . serialize ( ) <EOL> dese = UpdateInfo . deserialize ( seri ) <EOL> self . assertEquals ( info , dese ) <EOL> def test_save_and_load ( self ) : <EOL> server_info = ServerInfo ( ClientVersion ( "<STR_LIT>" ) , '<STR_LIT>' , "<STR_LIT>" ) <EOL> now = datetime . datetime . utcnow ( ) <EOL> info = UpdateInfo ( server_info , now ) <EOL> self . store . save ( info ) <EOL> self . assertTrue ( os . path . exists ( self . file_path ) ) <EOL> update_info = self . store . load ( ) <EOL> self . assertEquals ( update_info . server_info , server_info ) <EOL> self . assertEquals ( update_info . time , info . time ) <EOL> class MockStore ( object ) : <EOL> update_info = UpdateInfo ( None , None ) <EOL> def save ( self , update_info ) : <EOL> self . update_info = update_info <EOL> def load ( self ) : <EOL> return self . update_info <EOL> class UpdatesManagerTest ( BiiTestCase ) : <EOL> def setUp ( self ) : <EOL> unittest . TestCase . setUp ( self ) <EOL> self . folder = self . new_tmp_folder ( ) <EOL> self . file_path = os . path . join ( self . folder , "<STR_LIT>" ) <EOL> self . biiapi = Mock ( ) <EOL> self . user_io = mocked_user_io ( ) <EOL> self . store = MockStore ( ) <EOL> def test_not_checked_ever ( self ) : <EOL> """<STR_LIT>""" <EOL> server_info = ServerInfo ( "<STR_LIT>" , '<STR_LIT>' , "<STR_LIT>" ) <EOL> self . biiapi . get_server_info = Mock ( return_value = server_info ) <EOL> obsolete_client = ClientVersion ( "<STR_LIT>" ) <EOL> manager = UpdatesManager ( self . store , self . biiapi , obsolete_client , <EOL> time_between_checks = datetime . timedelta ( days = <NUM_LIT:10> ) ) <EOL> self . assertRaises ( ObsoleteClient , manager . check_for_updates , self . user_io . out ) <EOL> self . assertEquals ( self . user_io . out . stream . buf , "<STR_LIT>" ) <EOL> self . assertEquals ( self . store . load ( ) . server_info , server_info ) <EOL> self . biiapi . get_server_info . call_count = <NUM_LIT:0> <EOL> self . assertRaises ( ObsoleteClient , manager . check_for_updates , self . user_io . out ) <EOL> self . assertEquals ( self . biiapi . get_server_info . call_count , <NUM_LIT:0> ) <EOL> self . assertEquals ( self . store . load ( ) . server_info , server_info ) <EOL> def test_check_needed ( self ) : <EOL> server_info = ServerInfo ( version = "<STR_LIT>" , <EOL> message = '<STR_LIT>' , last_compatible = "<STR_LIT>" ) <EOL> self . biiapi . get_server_info = Mock ( return_value = server_info ) <EOL> manager = UpdatesManager ( self . store , self . biiapi , ClientVersion ( "<STR_LIT>" ) ) <EOL> last_time = self . _save_info ( manager , server_info , datetime . timedelta ( days = - <NUM_LIT> ) ) <EOL> biiout = OutputStream ( ) <EOL> manager . check_for_updates ( biiout ) <EOL> self . assert_in_response ( biiout , "<STR_LIT>" ) <EOL> update_info = manager . store . load ( ) <EOL> self . assertNotEquals ( update_info . time , last_time ) <EOL> def test_higher_version_ok ( self ) : <EOL> server_info = ServerInfo ( version = "<STR_LIT>" , <EOL> message = '<STR_LIT>' , last_compatible = "<STR_LIT>" ) <EOL> self . biiapi . get_server_info = Mock ( return_value = server_info ) <EOL> manager = UpdatesManager ( self . store , self . biiapi , ClientVersion ( "<STR_LIT:1.0>" ) ) <EOL> last_time = self . _save_info ( manager , server_info , datetime . timedelta ( days = - <NUM_LIT> ) ) <EOL> biiout = OutputStream ( ) <EOL> manager . check_for_updates ( biiout ) <EOL> self . assert_not_in_response ( biiout , "<STR_LIT>" ) <EOL> def _save_info ( self , manager , server_info , timedelta ) : <EOL> now = datetime . datetime . utcnow ( ) <EOL> thetime = now + timedelta <EOL> tmp = thetime . strftime ( UpdateInfo . DATE_PATTERN ) <EOL> roundthetime = datetime . datetime . strptime ( tmp , UpdateInfo . DATE_PATTERN ) <EOL> manager . store . save ( UpdateInfo ( server_info , roundthetime ) ) <EOL> return server_info , roundthetime </s>
