<s> from client . utils import software_update <EOL> import client <EOL> import json <EOL> import mock <EOL> import unittest <EOL> import urllib . error <EOL> class CheckVersionTest ( unittest . TestCase ) : <EOL> SERVER = '<STR_LIT>' <EOL> PREVIOUS_VERSION = '<STR_LIT>' <EOL> CURRENT_VERSION = '<STR_LIT>' <EOL> CURRENT_DOWNLOAD_LINK = '<STR_LIT>' <EOL> FILE_NAME = '<STR_LIT>' <EOL> def setUp ( self ) : <EOL> self . patcherRequest = mock . patch ( '<STR_LIT>' ) <EOL> self . addCleanup ( self . patcherRequest . stop ) <EOL> self . mockRequest = self . patcherRequest . start ( ) <EOL> self . patcherUrlopen = mock . patch ( '<STR_LIT>' ) <EOL> self . addCleanup ( self . patcherUrlopen . stop ) <EOL> self . mockUrlopen = self . patcherUrlopen . start ( ) <EOL> self . mockRead = self . mockUrlopen . return_value . read <EOL> self . mockDecode = self . mockUrlopen . return_value . read . return_value . decode <EOL> self . patcherWriteZip = mock . patch ( '<STR_LIT>' ) <EOL> self . addCleanup ( self . patcherWriteZip . stop ) <EOL> self . mockWriteZip = self . patcherWriteZip . start ( ) <EOL> def createVersionApiJson ( self , current_version , current_download_link ) : <EOL> self . mockDecode . return_value = json . dumps ( { <EOL> '<STR_LIT:data>' : { <EOL> '<STR_LIT>' : [ <EOL> { <EOL> '<STR_LIT>' : current_version , <EOL> '<STR_LIT>' : current_download_link , <EOL> } <EOL> ] <EOL> } <EOL> } ) <EOL> def testMalformedApiResponse ( self ) : <EOL> self . mockDecode . return_value = json . dumps ( { '<STR_LIT:data>' : [ ] } ) <EOL> self . assertFalse ( software_update . check_version ( self . SERVER , <EOL> self . CURRENT_VERSION , <EOL> self . FILE_NAME ) ) <EOL> def testUpToDate ( self ) : <EOL> self . createVersionApiJson ( self . CURRENT_VERSION , <EOL> self . CURRENT_DOWNLOAD_LINK ) <EOL> self . assertTrue ( software_update . check_version ( self . SERVER , <EOL> self . CURRENT_VERSION , <EOL> self . FILE_NAME ) ) <EOL> self . assertFalse ( self . mockWriteZip . called ) <EOL> def testNeedsUpdate ( self ) : <EOL> self . createVersionApiJson ( self . CURRENT_VERSION , <EOL> self . CURRENT_DOWNLOAD_LINK ) <EOL> self . assertTrue ( software_update . check_version ( self . SERVER , <EOL> self . PREVIOUS_VERSION , <EOL> self . FILE_NAME ) ) <EOL> expected_zip_call = ( ( self . FILE_NAME , self . mockRead . return_value ) , ) <EOL> self . assertEqual ( expected_zip_call , self . mockWriteZip . call_args ) <EOL> def testWriteZip_IOError ( self ) : <EOL> self . createVersionApiJson ( self . CURRENT_VERSION , <EOL> self . CURRENT_DOWNLOAD_LINK ) <EOL> self . mockWriteZip . side_effect = IOError <EOL> self . assertFalse ( software_update . check_version ( self . SERVER , <EOL> self . PREVIOUS_VERSION , <EOL> self . FILE_NAME ) ) <EOL> self . assertTrue ( self . mockWriteZip . called ) </s>
