Changeset 157

Show
Ignore:
Timestamp:
12/01/06 18:01:40 (2 years ago)
Author:
mk
Message:

Mock urllib.urlretrieve() for test_index_url_download.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/tests/unit/_helper_cheesecake.py

    r155 r157  
    99 
    1010DATA_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), '../data/')) 
    11 NOSE_PACKAGE_URL = "http://www.agilistas.org/cheesecake/nose-0.8.3.tar.gz" 
     11SAMPLE_PACKAGE_PATH = os.path.join(DATA_PATH, "nose-0.8.3.tar.gz") 
     12SAMPLE_PACKAGE_URL = "http://www.agilistas.org/cheesecake/nose-0.8.3.tar.gz" 
     13 
     14VALID_URLS = [ SAMPLE_PACKAGE_URL, 'file://%s' % SAMPLE_PACKAGE_PATH ] 
     15 
    1216 
    1317class Glutton(object): 
     
    3741 
    3842def mocked_urlretrieve(url, filename): 
    39     if url == NOSE_PACKAGE_URL
     43    if url in VALID_URLS
    4044        shutil.copy(os.path.join(DATA_PATH, "nose-0.8.3.tar.gz"), filename) 
    4145        headers = Mock({'gettype': 'application/x-gzip'}) 
     46    elif url == 'connection_refused': 
     47        raise IOError("[Errno socket error] (111, 'Connection refused')") 
    4248    else: 
    4349        response_content = '''<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> 
  • trunk/tests/unit/test_index_installability.py

    r155 r157  
    22 
    33import _path_cheesecake 
    4 from _helper_cheesecake import DATA_PATH, NOSE_PACKAGE_URL 
    5 from _helper_cheesecake import dump_str_to_file 
     4from _helper_cheesecake import SAMPLE_PACKAGE_PATH, SAMPLE_PACKAGE_URL 
    65from _helper_cheesecake import mocked_urlretrieve 
    76 
     
    2019    def setUp(self): 
    2120        self.cheesecake = None 
    22         self.nose_file_path = os.path.join(DATA_PATH, "nose-0.8.3.tar.gz") 
     21        cheesecake_index.urlretrieve = mocked_urlretrieve 
    2322 
    2423    def tearDown(self): 
     
    2827 
    2928    def test_index_installability_local_path(self): 
    30         self.cheesecake = Cheesecake(path=self.nose_file_path
     29        self.cheesecake = Cheesecake(path=SAMPLE_PACKAGE_PATH
    3130 
    3231        index = self.cheesecake.index["INSTALLABILITY"] 
     
    3938 
    4039    def test_index_installability_url_download(self): 
    41         # Mock a successful file download. 
    42         cheesecake_index.urlretrieve = mocked_urlretrieve 
    43  
    44         self.cheesecake = Cheesecake(url=NOSE_PACKAGE_URL) 
     40        self.cheesecake = Cheesecake(url=SAMPLE_PACKAGE_URL) 
    4541 
    4642        index = self.cheesecake.index["INSTALLABILITY"] 
  • trunk/tests/unit/test_index_url_download.py

    r146 r157  
    33 
    44import _path_cheesecake 
    5 from _helper_cheesecake import DATA_PATH 
     5from _helper_cheesecake import VALID_URLS 
     6from _helper_cheesecake import mocked_urlretrieve 
     7 
     8import cheesecake.cheesecake_index as cheesecake_index 
    69from cheesecake.cheesecake_index import Cheesecake, CheesecakeError, pad_msg, IndexUrlDownload 
    710         
     
    1215    def setUp(self): 
    1316        self.cheesecake = None 
     17        cheesecake_index.urlretrieve = mocked_urlretrieve 
    1418 
    1519    def _run_it(self, test_fun): 
     
    2630 
    2731    def test_index_url_download_valid_url(self): 
    28         urls = [ 
    29             "http://www.agilistas.org/cheesecake/nose-0.8.3.tar.gz", 
    30             "file://%s" % os.path.join(DATA_PATH, "nose-0.8.3.tar.gz") 
    31         ] 
     32        urls = VALID_URLS 
    3233 
    3334        for url in urls: 
    3435            def test_fun(logfile): 
    35                 try: 
    36                     self.cheesecake = Cheesecake(url=url, logfile=logfile) 
     36                self.cheesecake = Cheesecake(url=url, logfile=logfile) 
    3737 
    38                     index = self.cheesecake.index["INSTALLABILITY"]["IndexUrlDownload"] 
    39                     index.compute_with(self.cheesecake) 
     38                index = self.cheesecake.index["INSTALLABILITY"]["IndexUrlDownload"] 
     39                index.compute_with(self.cheesecake) 
    4040 
    41                     assert index.name == "IndexUrlDownload" 
    42                     assert index.value == IndexUrlDownload.max_value 
    43                     assert index.details == "downloaded package " + \ 
    44                            self.cheesecake.package + " from URL " + \ 
    45                            self.cheesecake.url 
    46                 except CheesecakeError, e: 
    47                     # it's OK if we get "connection refused" sometimes 
    48                     msg = "[Errno socket error] (111, 'Connection refused')\n" 
    49                     msg += "Detailed info available in log file %s" % logfile 
    50                     assert str(e) == msg 
     41                assert index.name == "IndexUrlDownload" 
     42                assert index.value == IndexUrlDownload.max_value 
     43                assert index.details == "downloaded package " + \ 
     44                       self.cheesecake.package + " from URL " + \ 
     45                       self.cheesecake.url 
    5146 
    5247            self._run_it(test_fun) 
     
    5550        def test_fun(logfile): 
    5651            try: 
    57                 self.cheesecake = Cheesecake(url="http://www.agilistas.org/cheesecake/not_there.tar.gz", 
     52                self.cheesecake = Cheesecake(url="invalid_url", 
    5853                                             sandbox=default_temp_directory, logfile=logfile) 
    59                 assert 0 # This statement should not be reached 
     54                assert False, "Should throw a CheesecakeError." 
    6055            except CheesecakeError, e: 
    6156                msg = "Error: Got '404 Not Found' error while trying to download package ... exiting" 
     
    6459 
    6560        self._run_it(test_fun) 
     61 
     62    def test_index_url_download_connection_refused(self): 
     63        def test_fun(logfile): 
     64            try: 
     65                self.cheesecake = Cheesecake(url='connection_refused', 
     66                                             sandbox=default_temp_directory, logfile=logfile) 
     67                assert False, "Should throw a CheesecakeError." 
     68            except CheesecakeError, e: 
     69                print str(e) 
     70                msg = "Error: [Errno socket error] (111, 'Connection refused')\n" 
     71                msg += "Detailed info available in log file %s" % logfile 
     72                assert str(e) == msg 
     73 
     74        self._run_it(test_fun)