Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
test.test_urllib.urlretrieve_FileTests Class Reference

List of all members.

Public Member Functions

def setUp
def tearDown
def constructLocalFileUrl
def createNewTempFile
def registerFileForCleanUp
def test_basic
def test_copy
def test_reporthook
def test_reporthook_0_bytes
def test_reporthook_5_bytes
def test_reporthook_8193_bytes

Public Attributes

 tempFiles
 text

Detailed Description

Test urllib.urlretrieve() on local files

Definition at line 259 of file test_urllib.py.


Member Function Documentation

Definition at line 289 of file test_urllib.py.

00289 
00290     def constructLocalFileUrl(self, filePath):
00291         filePath = os.path.abspath(filePath)
00292         try:
00293             filePath.encode("utf8")
00294         except UnicodeEncodeError:
00295             raise unittest.SkipTest("filePath is not encodable to utf8")
00296         return "file://%s" % urllib.request.pathname2url(filePath)

Here is the call graph for this function:

Here is the caller graph for this function:

Creates a new temporary file containing the specified data,
registers the file for deletion during the test fixture tear down, and
returns the absolute path of the file.

Definition at line 297 of file test_urllib.py.

00297 
00298     def createNewTempFile(self, data=b""):
00299         """Creates a new temporary file containing the specified data,
00300         registers the file for deletion during the test fixture tear down, and
00301         returns the absolute path of the file."""
00302 
00303         newFd, newFilePath = tempfile.mkstemp()
00304         try:
00305             self.registerFileForCleanUp(newFilePath)
00306             newFile = os.fdopen(newFd, "wb")
00307             newFile.write(data)
00308             newFile.close()
00309         finally:
00310             try: newFile.close()
00311             except: pass
00312         return newFilePath

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 313 of file test_urllib.py.

00313 
00314     def registerFileForCleanUp(self, fileName):
00315         self.tempFiles.append(fileName)

Here is the caller graph for this function:

Definition at line 262 of file test_urllib.py.

00262 
00263     def setUp(self):
00264         # Create a list of temporary files. Each item in the list is a file
00265         # name (absolute path or relative to the current working directory).
00266         # All files in this list will be deleted in the tearDown method. Note,
00267         # this only helps to makes sure temporary files get deleted, but it
00268         # does nothing about trying to close files that may still be open. It
00269         # is the responsibility of the developer to properly close files even
00270         # when exceptional conditions occur.
00271         self.tempFiles = []
00272 
00273         # Create a temporary file.
00274         self.registerFileForCleanUp(support.TESTFN)
00275         self.text = b'testing urllib.urlretrieve'
00276         try:
00277             FILE = open(support.TESTFN, 'wb')
00278             FILE.write(self.text)
00279             FILE.close()
00280         finally:
00281             try: FILE.close()
00282             except: pass

Here is the caller graph for this function:

Definition at line 283 of file test_urllib.py.

00283 
00284     def tearDown(self):
00285         # Delete the temporary files.
00286         for each in self.tempFiles:
00287             try: os.remove(each)
00288             except: pass

Here is the caller graph for this function:

Definition at line 316 of file test_urllib.py.

00316 
00317     def test_basic(self):
00318         # Make sure that a local file just gets its own location returned and
00319         # a headers value is returned.
00320         result = urllib.request.urlretrieve("file:%s" % support.TESTFN)
00321         self.assertEqual(result[0], support.TESTFN)
00322         self.assertIsInstance(result[1], email.message.Message,
00323                               "did not get a email.message.Message instance "
00324                               "as second returned value")

Here is the call graph for this function:

Definition at line 325 of file test_urllib.py.

00325 
00326     def test_copy(self):
00327         # Test that setting the filename argument works.
00328         second_temp = "%s.2" % support.TESTFN
00329         self.registerFileForCleanUp(second_temp)
00330         result = urllib.request.urlretrieve(self.constructLocalFileUrl(
00331             support.TESTFN), second_temp)
00332         self.assertEqual(second_temp, result[0])
00333         self.assertTrue(os.path.exists(second_temp), "copy of the file was not "
00334                                                   "made")
00335         FILE = open(second_temp, 'rb')
00336         try:
00337             text = FILE.read()
00338             FILE.close()
00339         finally:
00340             try: FILE.close()
00341             except: pass
00342         self.assertEqual(self.text, text)

Here is the call graph for this function:

Definition at line 343 of file test_urllib.py.

00343 
00344     def test_reporthook(self):
00345         # Make sure that the reporthook works.
00346         def hooktester(count, block_size, total_size, count_holder=[0]):
00347             self.assertIsInstance(count, int)
00348             self.assertIsInstance(block_size, int)
00349             self.assertIsInstance(total_size, int)
00350             self.assertEqual(count, count_holder[0])
00351             count_holder[0] = count_holder[0] + 1
00352         second_temp = "%s.2" % support.TESTFN
00353         self.registerFileForCleanUp(second_temp)
00354         urllib.request.urlretrieve(
00355             self.constructLocalFileUrl(support.TESTFN),
00356             second_temp, hooktester)

Here is the call graph for this function:

Definition at line 357 of file test_urllib.py.

00357 
00358     def test_reporthook_0_bytes(self):
00359         # Test on zero length file. Should call reporthook only 1 time.
00360         report = []
00361         def hooktester(count, block_size, total_size, _report=report):
00362             _report.append((count, block_size, total_size))
00363         srcFileName = self.createNewTempFile()
00364         urllib.request.urlretrieve(self.constructLocalFileUrl(srcFileName),
00365             support.TESTFN, hooktester)
00366         self.assertEqual(len(report), 1)
00367         self.assertEqual(report[0][2], 0)

Here is the call graph for this function:

Definition at line 368 of file test_urllib.py.

00368 
00369     def test_reporthook_5_bytes(self):
00370         # Test on 5 byte file. Should call reporthook only 2 times (once when
00371         # the "network connection" is established and once when the block is
00372         # read). Since the block size is 8192 bytes, only one block read is
00373         # required to read the entire file.
00374         report = []
00375         def hooktester(count, block_size, total_size, _report=report):
00376             _report.append((count, block_size, total_size))
00377         srcFileName = self.createNewTempFile(b"x" * 5)
00378         urllib.request.urlretrieve(self.constructLocalFileUrl(srcFileName),
00379             support.TESTFN, hooktester)
00380         self.assertEqual(len(report), 2)
00381         self.assertEqual(report[0][1], 8192)
00382         self.assertEqual(report[0][2], 5)

Here is the call graph for this function:

Definition at line 383 of file test_urllib.py.

00383 
00384     def test_reporthook_8193_bytes(self):
00385         # Test on 8193 byte file. Should call reporthook only 3 times (once
00386         # when the "network connection" is established, once for the next 8192
00387         # bytes, and once for the last byte).
00388         report = []
00389         def hooktester(count, block_size, total_size, _report=report):
00390             _report.append((count, block_size, total_size))
00391         srcFileName = self.createNewTempFile(b"x" * 8193)
00392         urllib.request.urlretrieve(self.constructLocalFileUrl(srcFileName),
00393             support.TESTFN, hooktester)
00394         self.assertEqual(len(report), 3)
00395         self.assertEqual(report[0][1], 8192)
00396         self.assertEqual(report[0][2], 8193)

Here is the call graph for this function:


Member Data Documentation

Definition at line 270 of file test_urllib.py.

Definition at line 274 of file test_urllib.py.


The documentation for this class was generated from the following file: