Rewrite test_collections.py to be pytest-style
MODIFY
test/test_collections.py
test/test_collections.py
-import unittest
-
from urllib3._collections import (
HTTPHeaderDict,
RecentlyUsedContainer as Container
)
-from nose.plugins.skip import SkipTest
+import pytest
from urllib3.packages import six
xrange = six.moves.xrange
-class TestLRUContainer(unittest.TestCase):
+class TestLRUContainer(object):
def test_maxsize(self):
d = Container(5)
for i in xrange(5):
d[i] = str(i)
- self.assertEqual(len(d), 5)
+ assert len(d) == 5
for i in xrange(5):
- self.assertEqual(d[i], str(i))
+ assert d[i] == str(i)
d[i+1] = str(i+1)
- self.assertEqual(len(d), 5)
- self.assertFalse(0 in d)
- self.assertTrue(i+1 in d)
+ assert len(d) == 5
+ assert 0 not in d
+ assert (i + 1) in d
def test_expire(self):
d = Container(5)
@@ -41,7 +39,7 @@ class TestLRUContainer(unittest.TestCase):
d[5] = '5'
# Check state
- self.assertEqual(list(d.keys()), [2, 3, 4, 0, 5])
+ assert list(d.keys()) == [2, 3, 4, 0, 5]
def test_same_key(self):
d = Container(5)
@@ -49,8 +47,8 @@ class TestLRUContainer(unittest.TestCase):
for i in xrange(10):
d['foo'] = i
- self.assertEqual(list(d.keys()), ['foo'])
- self.assertEqual(len(d), 1)
+ assert list(d.keys()) == ['foo']
+ assert len(d) == 1
def test_access_ordering(self):
d = Container(5)
@@ -59,13 +57,13 @@ class TestLRUContainer(unittest.TestCase):
d[i] = True
# Keys should be ordered by access time
- self.assertEqual(list(d.keys()), [5, 6, 7, 8, 9])
+ assert list(d.keys()) == [5, 6, 7, 8, 9]
new_order = [7, 8, 6, 9, 5]
for k in new_order:
d[k]
- self.assertEqual(list(d.keys()), new_order)
+ assert list(d.keys()) == new_order
def test_delete(self):
d = Container(5)
@@ -74,10 +72,10 @@ class TestLRUContainer(unittest.TestCase):
d[i] = True
del d[0]
- self.assertFalse(0 in d)
+ assert 0 not in d
d.pop(1)
- self.assertFalse(1 in d)
+ assert 1 not in d
d.pop(1, None)
@@ -88,15 +86,16 @@ class TestLRUContainer(unittest.TestCase):
d[i] = True
r = d.get(4)
- self.assertEqual(r, True)
+ assert r is True
r = d.get(5)
- self.assertEqual(r, None)
+ assert r is None
r = d.get(5, 42)
- self.assertEqual(r, 42)
+ assert r == 42
- self.assertRaises(KeyError, lambda: d[5])
+ with pytest.raises(KeyError):
+ d[5]
def test_disposal(self):
evicted_items = []
@@ -108,23 +107,24 @@ class TestLRUContainer(unittest.TestCase):
d = Container(5, dispose_func=dispose_func)
for i in xrange(5):
d[i] = i
- self.assertEqual(list(d.keys()), list(xrange(5)))
- self.assertEqual(evicted_items, []) # Nothing disposed
+ assert list(d.keys()) == list(xrange(5))
+ assert evicted_items == [] # Nothing disposed
d[5] = 5
- self.assertEqual(list(d.keys()), list(xrange(1, 6)))
- self.assertEqual(evicted_items, [0])
+ assert list(d.keys()) == list(xrange(1, 6))
+ assert evicted_items == [0]
del d[1]
- self.assertEqual(evicted_items, [0, 1])
+ assert evicted_items == [0, 1]
d.clear()
- self.assertEqual(evicted_items, [0, 1, 2, 3, 4, 5])
+ assert evicted_items == [0, 1, 2, 3, 4, 5]
def test_iter(self):
d = Container()
- self.assertRaises(NotImplementedError, d.__iter__)
+ with pytest.raises(NotImplementedError):
+ d.__iter__()
class NonMappingHeaderContainer(object):
@@ -139,185 +139,193 @@ class NonMappingHeaderContainer(object):
return self._data[key]
-class TestHTTPHeaderDict(unittest.TestCase):
- def setUp(self):
- self.d = HTTPHeaderDict(Cookie='foo')
- self.d.add('cookie', 'bar')
+@pytest.fixture()
+def d():
+ header_dict = HTTPHeaderDict(Cookie='foo')
+ header_dict.add('cookie', 'bar')
+ return header_dict
+
+class TestHTTPHeaderDict(object):
def test_create_from_kwargs(self):
h = HTTPHeaderDict(ab=1, cd=2, ef=3, gh=4)
- self.assertEqual(len(h), 4)
- self.assertTrue('ab' in h)
+ assert len(h) == 4
+ assert 'ab' in h
def test_create_from_dict(self):
h = HTTPHeaderDict(dict(ab=1, cd=2, ef=3, gh=4))
- self.assertEqual(len(h), 4)
- self.assertTrue('ab' in h)
+ assert len(h) == 4
+ assert 'ab' in h
def test_create_from_iterator(self):
teststr = 'urllib3ontherocks'
h = HTTPHeaderDict((c, c*5) for c in teststr)
- self.assertEqual(len(h), len(set(teststr)))
+ assert len(h) == len(set(teststr))
def test_create_from_list(self):
headers = [('ab', 'A'), ('cd', 'B'), ('cookie', 'C'), ('cookie', 'D'), ('cookie', 'E')]
h = HTTPHeaderDict(headers)
- self.assertEqual(len(h), 3)
- self.assertTrue('ab' in h)
+ assert len(h) == 3
+ assert 'ab' in h
clist = h.getlist('cookie')
- self.assertEqual(len(clist), 3)
- self.assertEqual(clist[0], 'C')
- self.assertEqual(clist[-1], 'E')
+ assert len(clist) == 3
+ assert clist[0] == 'C'
+ assert clist[-1] == 'E'
def test_create_from_headerdict(self):
headers = [('ab', 'A'), ('cd', 'B'), ('cookie', 'C'), ('cookie', 'D'), ('cookie', 'E')]
org = HTTPHeaderDict(headers)
h = HTTPHeaderDict(org)
- self.assertEqual(len(h), 3)
- self.assertTrue('ab' in h)
+ assert len(h) == 3
+ assert 'ab' in h
clist = h.getlist('cookie')
- self.assertEqual(len(clist), 3)
- self.assertEqual(clist[0], 'C')
- self.assertEqual(clist[-1], 'E')
- self.assertFalse(h is org)
- self.assertEqual(h, org)
-
- def test_setitem(self):
- self.d['Cookie'] = 'foo'
- self.assertEqual(self.d['cookie'], 'foo')
- self.d['cookie'] = 'with, comma'
- self.assertEqual(self.d.getlist('cookie'), ['with, comma'])
-
- def test_update(self):
- self.d.update(dict(Cookie='foo'))
- self.assertEqual(self.d['cookie'], 'foo')
- self.d.update(dict(cookie='with, comma'))
- self.assertEqual(self.d.getlist('cookie'), ['with, comma'])
-
- def test_delitem(self):
- del self.d['cookie']
- self.assertFalse('cookie' in self.d)
- self.assertFalse('COOKIE' in self.d)
-
- def test_add_well_known_multiheader(self):
- self.d.add('COOKIE', 'asdf')
- self.assertEqual(self.d.getlist('cookie'), ['foo', 'bar', 'asdf'])
- self.assertEqual(self.d['cookie'], 'foo, bar, asdf')
-
- def test_add_comma_separated_multiheader(self):
- self.d.add('bar', 'foo')
- self.d.add('BAR', 'bar')
- self.d.add('Bar', 'asdf')
- self.assertEqual(self.d.getlist('bar'), ['foo', 'bar', 'asdf'])
- self.assertEqual(self.d['bar'], 'foo, bar, asdf')
-
- def test_extend_from_list(self):
- self.d.extend([('set-cookie', '100'), ('set-cookie', '200'), ('set-cookie', '300')])
- self.assertEqual(self.d['set-cookie'], '100, 200, 300')
-
- def test_extend_from_dict(self):
- self.d.extend(dict(cookie='asdf'), b='100')
- self.assertEqual(self.d['cookie'], 'foo, bar, asdf')
- self.assertEqual(self.d['b'], '100')
- self.d.add('cookie', 'with, comma')
- self.assertEqual(self.d.getlist('cookie'), ['foo', 'bar', 'asdf', 'with, comma'])
-
- def test_extend_from_container(self):
+ assert len(clist) == 3
+ assert clist[0] == 'C'
+ assert clist[-1] == 'E'
+ assert h is not org
+ assert h == org
+
+ def test_setitem(self, d):
+ d['Cookie'] = 'foo'
+ assert d['cookie'] == 'foo'
+ d['cookie'] = 'with, comma'
+ assert d.getlist('cookie') == ['with, comma']
+
+ def test_update(self, d):
+ d.update(dict(Cookie='foo'))
+ assert d['cookie'] == 'foo'
+ d.update(dict(cookie='with, comma'))
+ assert d.getlist('cookie') == ['with, comma']
+
+ def test_delitem(self, d):
+ del d['cookie']
+ assert 'cookie' not in d
+ assert 'COOKIE' not in d
+
+ def test_add_well_known_multiheader(self, d):
+ d.add('COOKIE', 'asdf')
+ assert d.getlist('cookie') == ['foo', 'bar', 'asdf']
+ assert d['cookie'] == 'foo, bar, asdf'
+
+ def test_add_comma_separated_multiheader(self, d):
+ d.add('bar', 'foo')
+ d.add('BAR', 'bar')
+ d.add('Bar', 'asdf')
+ assert d.getlist('bar') == ['foo', 'bar', 'asdf']
+ assert d['bar'] == 'foo, bar, asdf'
+
+ def test_extend_from_list(self, d):
+ d.extend([('set-cookie', '100'), ('set-cookie', '200'), ('set-cookie', '300')])
+ assert d['set-cookie'] == '100, 200, 300'
+
+ def test_extend_from_dict(self, d):
+ d.extend(dict(cookie='asdf'), b='100')
+ assert d['cookie'] == 'foo, bar, asdf'
+ assert d['b'] == '100'
+ d.add('cookie', 'with, comma')
+ assert d.getlist('cookie') == ['foo', 'bar', 'asdf', 'with, comma']
+
+ def test_extend_from_container(self, d):
h = NonMappingHeaderContainer(Cookie='foo', e='foofoo')
- self.d.extend(h)
- self.assertEqual(self.d['cookie'], 'foo, bar, foo')
- self.assertEqual(self.d['e'], 'foofoo')
- self.assertEqual(len(self.d), 2)
+ d.extend(h)
+ assert d['cookie'] == 'foo, bar, foo'
+ assert d['e'] == 'foofoo'
+ assert len(d) == 2
- def test_extend_from_headerdict(self):
+ def test_extend_from_headerdict(self, d):
h = HTTPHeaderDict(Cookie='foo', e='foofoo')
- self.d.extend(h)
- self.assertEqual(self.d['cookie'], 'foo, bar, foo')
- self.assertEqual(self.d['e'], 'foofoo')
- self.assertEqual(len(self.d), 2)
-
- def test_copy(self):
- h = self.d.copy()
- self.assertTrue(self.d is not h)
- self.assertEqual(self.d, h)
-
- def test_getlist(self):
- self.assertEqual(self.d.getlist('cookie'), ['foo', 'bar'])
- self.assertEqual(self.d.getlist('Cookie'), ['foo', 'bar'])
- self.assertEqual(self.d.getlist('b'), [])
- self.d.add('b', 'asdf')
- self.assertEqual(self.d.getlist('b'), ['asdf'])
-
- def test_getlist_after_copy(self):
- self.assertEqual(self.d.getlist('cookie'), HTTPHeaderDict(self.d).getlist('cookie'))
-
- def test_equal(self):
+ d.extend(h)
+ assert d['cookie'] == 'foo, bar, foo'
+ assert d['e'] == 'foofoo'
+ assert len(d) == 2
+
+ def test_copy(self, d):
+ h = d.copy()
+ assert d is not h
+ assert d == h
+
+ def test_getlist(self, d):
+ assert d.getlist('cookie') == ['foo', 'bar']
+ assert d.getlist('Cookie') == ['foo', 'bar']
+ assert d.getlist('b') == []
+ d.add('b', 'asdf')
+ assert d.getlist('b') == ['asdf']
+
+ def test_getlist_after_copy(self, d):
+ assert d.getlist('cookie') == HTTPHeaderDict(d).getlist('cookie')
+
+ def test_equal(self, d):
b = HTTPHeaderDict(cookie='foo, bar')
c = NonMappingHeaderContainer(cookie='foo, bar')
- self.assertEqual(self.d, b)
- self.assertEqual(self.d, c)
- self.assertNotEqual(self.d, 2)
+ assert d == b
+ assert d == c
+ assert d != 2
- def test_not_equal(self):
+ def test_not_equal(self, d):
b = HTTPHeaderDict(cookie='foo, bar')
c = NonMappingHeaderContainer(cookie='foo, bar')
- self.assertFalse(self.d != b)
- self.assertFalse(self.d != c)
- self.assertNotEqual(self.d, 2)
+ assert not (d != b)
+ assert not (d != c)
+ assert d != 2
- def test_pop(self):
+ def test_pop(self, d):
key = 'Cookie'
- a = self.d[key]
- b = self.d.pop(key)
- self.assertEqual(a, b)
- self.assertFalse(key in self.d)
- self.assertRaises(KeyError, self.d.pop, key)
+ a = d[key]
+ b = d.pop(key)
+ assert a == b
+ assert key not in d
+ with pytest.raises(KeyError):
+ d.pop(key)
dummy = object()
- self.assertTrue(dummy is self.d.pop(key, dummy))
+ assert dummy is d.pop(key, dummy)
- def test_discard(self):
- self.d.discard('cookie')
- self.assertFalse('cookie' in self.d)
- self.d.discard('cookie')
+ def test_discard(self, d):
+ d.discard('cookie')
+ assert 'cookie' not in d
+ d.discard('cookie')
- def test_len(self):
- self.assertEqual(len(self.d), 1)
- self.d.add('cookie', 'bla')
- self.d.add('asdf', 'foo')
+ def test_len(self, d):
+ assert len(d) == 1
+ d.add('cookie', 'bla')
+ d.add('asdf', 'foo')
# len determined by unique fieldnames
- self.assertEqual(len(self.d), 2)
+ assert len(d) == 2
- def test_repr(self):
+ def test_repr(self, d):
rep = "HTTPHeaderDict({'Cookie': 'foo, bar'})"
- self.assertEqual(repr(self.d), rep)
+ assert repr(d) == rep
- def test_items(self):
- items = self.d.items()
- self.assertEqual(len(items), 2)
- self.assertEqual(items[0][0], 'Cookie')
- self.assertEqual(items[0][1], 'foo')
- self.assertEqual(items[1][0], 'Cookie')
- self.assertEqual(items[1][1], 'bar')
+ def test_items(self, d):
+ items = d.items()
+ assert len(items) == 2
+ assert items[0][0] == 'Cookie'
+ assert items[0][1] == 'foo'
+ assert items[1][0] == 'Cookie'
+ assert items[1][1] == 'bar'
- def test_dict_conversion(self):
+ def test_dict_conversion(self, d):
# Also tested in connectionpool, needs to preserve case
hdict = {'Content-Length': '0',
'Content-type': 'text/plain',
'Server': 'TornadoServer/1.2.3'}
h = dict(HTTPHeaderDict(hdict).items())
- self.assertEqual(hdict, h)
- self.assertEqual(hdict, dict(HTTPHeaderDict(hdict)))
-
- def test_string_enforcement(self):
- # This currently throws AttributeError on key.lower(), should probably be something nicer
- self.assertRaises(Exception, self.d.__setitem__, 3, 5)
- self.assertRaises(Exception, self.d.add, 3, 4)
- self.assertRaises(Exception, self.d.__delitem__, 3)
- self.assertRaises(Exception, HTTPHeaderDict, {3: 3})
-
+ assert hdict == h
+ assert hdict == dict(HTTPHeaderDict(hdict))
+
+ def test_string_enforcement(self, d):
+ # This currently throws AttributeError on key.lower(), should
+ # probably be something nicer
+ with pytest.raises(Exception):
+ d[3] = 5
+ with pytest.raises(Exception):
+ d.add(3, 4)
+ with pytest.raises(Exception):
+ del d[3]
+ with pytest.raises(Exception):
+ HTTPHeaderDict({3: 3})
+
+ @pytest.mark.skipif(six.PY3, reason="python3 has a different internal header implementation")
def test_from_httplib_py2(self):
- if six.PY3:
- raise SkipTest("python3 has a different internal header implementation")
msg = """
Server: nginx
Content-Type: text/html; charset=windows-1251
@@ -334,15 +342,11 @@ www-authenticate: bla
buffer = six.moves.StringIO(msg.lstrip().replace('\n', '\r\n'))
msg = six.moves.http_client.HTTPMessage(buffer)
d = HTTPHeaderDict.from_httplib(msg)
- self.assertEqual(d['server'], 'nginx')
+ assert d['server'] == 'nginx'
cookies = d.getlist('set-cookie')
- self.assertEqual(len(cookies), 2)
- self.assertTrue(cookies[0].startswith("bb_lastvisit"))
- self.assertTrue(cookies[1].startswith("bb_lastactivity"))
- self.assertEqual(d['x-some-multiline'].split(), ['asdf', 'asdf', 'asdf'])
- self.assertEqual(d['www-authenticate'], 'asdf, bla')
- self.assertEqual(d.getlist('www-authenticate'), ['asdf', 'bla'])
-
-
-if __name__ == '__main__':
- unittest.main()
+ assert len(cookies) == 2
+ assert cookies[0].startswith("bb_lastvisit")
+ assert cookies[1].startswith("bb_lastactivity")
+ assert d['x-some-multiline'].split() == ['asdf', 'asdf', 'asdf']
+ assert d['www-authenticate'] == 'asdf, bla'
+ assert d.getlist('www-authenticate') == ['asdf', 'bla']

Rewrite test_collections.py to be pytest-style