summaryrefslogtreecommitdiff
path: root/test/test_byterange.py
diff options
context:
space:
mode:
Diffstat (limited to 'test/test_byterange.py')
-rwxr-xr-x[-rw-r--r--]test/test_byterange.py103
1 files changed, 49 insertions, 54 deletions
diff --git a/test/test_byterange.py b/test/test_byterange.py
index 96f1573..dfed311 100644..100755
--- a/test/test_byterange.py
+++ b/test/test_byterange.py
@@ -11,98 +11,93 @@
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
-# License along with this library; if not, write to the
-# Free Software Foundation, Inc.,
-# 59 Temple Place, Suite 330,
+# License along with this library; if not, write to the
+# Free Software Foundation, Inc.,
+# 59 Temple Place, Suite 330,
# Boston, MA 02111-1307 USA
# This file is part of urlgrabber, a high-level cross-protocol url-grabber
# Copyright 2002-2004 Michael D. Stenner, Ryan Tomayko
-"""byterange.py tests"""
+from __future__ import print_function
-# $Id: test_byterange.py,v 1.6 2004/03/31 17:02:00 mstenner Exp $
+"""byterange.py tests"""
import sys
-from StringIO import StringIO
+if sys.version_info >= (3,):
+ # We do an explicit version check here because because python2
+ # also has an io module with StringIO, but it is incompatible,
+ # and returns str instead of unicode somewhere.
+ from io import StringIO
+else:
+ from cStringIO import StringIO
+
from urlgrabber.byterange import RangeableFileObject
from base_test_code import *
class RangeableFileObjectTestCase(TestCase):
"""Test range.RangeableFileObject class"""
-
+
def setUp(self):
# 0 1 2 3 4 5 6 7 8 9
# 0123456789012345678901234567890123456789012345678901234567 890123456789012345678901234567890
self.test = 'Why cannot we write the entire 24 volumes of Encyclopaedia\nBrittanica on the head of a pin?\n'
self.fo = StringIO(self.test)
self.rfo = RangeableFileObject(self.fo, (20,69))
-
+
def tearDown(self):
pass
-
+
def test_seek(self):
"""RangeableFileObject.seek()"""
self.rfo.seek(11)
- self.assertEquals('24', self.rfo.read(2))
+ self.assertEqual('24', self.rfo.read(2))
self.rfo.seek(14)
- self.assertEquals('volumes', self.rfo.read(7))
+ self.assertEqual('volumes', self.rfo.read(7))
self.rfo.seek(1,1)
- self.assertEquals('of', self.rfo.read(2))
-
- def test_poor_mans_seek(self):
- """RangeableFileObject.seek() poor mans version..
-
- We just delete the seek method from StringIO so we can
- excercise RangeableFileObject when the file object supplied
- doesn't support seek.
- """
- seek = StringIO.seek
- del(StringIO.seek)
- self.test_seek()
- StringIO.seek = seek
-
+ self.assertEqual('of', self.rfo.read(2))
+
def test_read(self):
"""RangeableFileObject.read()"""
- self.assertEquals('the', self.rfo.read(3))
- self.assertEquals(' entire 24 volumes of ', self.rfo.read(22))
- self.assertEquals('Encyclopaedia\nBrittanica', self.rfo.read(50))
- self.assertEquals('', self.rfo.read())
-
+ self.assertEqual('the', self.rfo.read(3))
+ self.assertEqual(' entire 24 volumes of ', self.rfo.read(22))
+ self.assertEqual('Encyclopaedia\nBrittanica', self.rfo.read(50))
+ self.assertEqual('', self.rfo.read())
+
def test_readall(self):
"""RangeableFileObject.read(): to end of file."""
rfo = RangeableFileObject(StringIO(self.test),(11,))
- self.assertEquals(self.test[11:],rfo.read())
-
+ self.assertEqual(self.test[11:],rfo.read())
+
def test_readline(self):
"""RangeableFileObject.readline()"""
- self.assertEquals('the entire 24 volumes of Encyclopaedia\n', self.rfo.readline())
- self.assertEquals('Brittanica', self.rfo.readline())
- self.assertEquals('', self.rfo.readline())
-
+ self.assertEqual('the entire 24 volumes of Encyclopaedia\n', self.rfo.readline())
+ self.assertEqual('Brittanica', self.rfo.readline())
+ self.assertEqual('', self.rfo.readline())
+
def test_tell(self):
"""RangeableFileObject.tell()"""
- self.assertEquals(0,self.rfo.tell())
+ self.assertEqual(0,self.rfo.tell())
self.rfo.read(5)
- self.assertEquals(5,self.rfo.tell())
+ self.assertEqual(5,self.rfo.tell())
self.rfo.readline()
- self.assertEquals(39,self.rfo.tell())
-
+ self.assertEqual(39,self.rfo.tell())
+
class RangeModuleTestCase(TestCase):
"""Test module level functions defined in range.py"""
def setUp(self):
pass
-
+
def tearDown(self):
pass
-
+
def test_range_tuple_normalize(self):
"""byterange.range_tuple_normalize()"""
from urlgrabber.byterange import range_tuple_normalize
from urlgrabber.byterange import RangeError
- tests = (
+ tests = (
((None,50), (0,50)),
((500,600), (500,600)),
((500,), (500,'')),
@@ -112,28 +107,28 @@ class RangeModuleTestCase(TestCase):
(None, None)
)
for test, ex in tests:
- self.assertEquals( range_tuple_normalize(test), ex )
-
+ self.assertEqual( range_tuple_normalize(test), ex )
+
try: range_tuple_normalize( (10,8) )
except RangeError: pass
else: self.fail("range_tuple_normalize( (10,8) ) should have raised RangeError")
-
+
def test_range_header_to_tuple(self):
"""byterange.range_header_to_tuple()"""
from urlgrabber.byterange import range_header_to_tuple
- tests = (
+ tests = (
('bytes=500-600', (500,601)),
('bytes=500-', (500,'')),
('bla bla', ()),
(None, None)
)
for test, ex in tests:
- self.assertEquals( range_header_to_tuple(test), ex )
-
+ self.assertEqual( range_header_to_tuple(test), ex )
+
def test_range_tuple_to_header(self):
"""byterange.range_tuple_to_header()"""
from urlgrabber.byterange import range_tuple_to_header
- tests = (
+ tests = (
((500,600), 'bytes=500-599'),
((500,''), 'bytes=500-'),
((500,), 'bytes=500-'),
@@ -142,16 +137,16 @@ class RangeModuleTestCase(TestCase):
(None, None),
)
for test, ex in tests:
- self.assertEquals( range_tuple_to_header(test), ex )
-
+ self.assertEqual( range_tuple_to_header(test), ex )
+
try: range_tuple_to_header( ('not an int',500) )
except ValueError: pass
else: self.fail("range_tuple_to_header( ('not an int',500) ) should have raised ValueError")
-
+
try: range_tuple_to_header( (0,'not an int') )
except ValueError: pass
else: self.fail("range_tuple_to_header( (0, 'not an int') ) should have raised ValueError")
-
+
def suite():
tl = TestLoader()
return tl.loadTestsFromModule(sys.modules[__name__])