Fix tests for Python 2

This commit is contained in:
Bradley Ayers 2013-03-28 20:49:54 +11:00
parent 7e8fad55f0
commit c9f3b9ac2a
5 changed files with 51 additions and 37 deletions

View File

@ -75,7 +75,7 @@ v0.14.0
be in the context (backwards incompatible) -- #127
- Add Travis CI support
- Add support for Django 1.5
- Add L10N control for columns #120 (no-op in < Django 1.3)
- Add L10N control for columns #120 (ignored in < Django 1.3)
- Drop Python 2.6.4 support in favour of Python 3.2 support
- Non-queryset data ordering is different between Python 3 and 2. When
comparing different types, their truth values are now compared before falling

View File

@ -605,4 +605,4 @@ class TableBase(object):
self._template = value
# Python 2/3 compatible way to enable the metaclass
Table = DeclarativeColumnsMetaclass('Table', (TableBase, ), {})
Table = DeclarativeColumnsMetaclass(str('Table'), (TableBase, ), {})

View File

@ -507,23 +507,23 @@ def build_request(uri='/'):
"""
path, _, querystring = uri.partition('?')
return WSGIRequest({
'CONTENT_TYPE': 'text/html; charset=utf-8',
'PATH_INFO': path,
'QUERY_STRING': querystring,
'REMOTE_ADDR': '127.0.0.1',
'REQUEST_METHOD': 'GET',
'SCRIPT_NAME': '',
'SERVER_NAME': 'testserver',
'SERVER_PORT': '80',
'SERVER_PROTOCOL': 'HTTP/1.1',
'wsgi.version': (1, 0),
'wsgi.url_scheme': 'http',
'wsgi.input': FakePayload(b''),
'wsgi.errors': six.StringIO(),
'wsgi.multiprocess': True,
'wsgi.multithread': False,
'wsgi.run_once': False,
})
'CONTENT_TYPE': 'text/html; charset=utf-8',
'PATH_INFO': path,
'QUERY_STRING': querystring,
'REMOTE_ADDR': '127.0.0.1',
'REQUEST_METHOD': 'GET',
'SCRIPT_NAME': '',
'SERVER_NAME': 'testserver',
'SERVER_PORT': '80',
'SERVER_PROTOCOL': 'HTTP/1.1',
'wsgi.version': (1, 0),
'wsgi.url_scheme': 'http',
'wsgi.input': FakePayload(b''),
'wsgi.errors': six.StringIO(),
'wsgi.multiprocess': True,
'wsgi.multithread': False,
'wsgi.run_once': False,
})
def total_ordering(cls):
@ -548,7 +548,7 @@ def total_ordering(cls):
root = max(roots) # prefer __lt__ to __le__ to __gt__ to __ge__
for opname, opfunc in convert[root]:
if opname not in roots:
opfunc.__name__ = opname
opfunc.__name__ = str(opname) # Py2 requires non-unicode, Py3 requires unicode.
opfunc.__doc__ = getattr(int, opname).__doc__
setattr(cls, opname, opfunc)
return cls

View File

@ -7,6 +7,7 @@ from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
import django_tables2 as tables
from django_tables2.tables import DeclarativeColumnsMetaclass
import os
import six
core = Tests()
@ -73,7 +74,8 @@ def metaclass_inheritance():
__metaclass__ = Meta
name = tables.Column()
TweakedTable = Meta('TweakedTable', (TweakedTableBase, ), {})
# Python 2/3 compatible way to enable the metaclass
TweakedTable = Meta(str('TweakedTable'), (TweakedTableBase, ), {})
table = TweakedTable([])
assert 'name' in table.columns
@ -86,7 +88,8 @@ def metaclass_inheritance():
class FlippedTweakedTableBase(tables.Table):
name = tables.Column()
FlippedTweakedTable = FlippedMeta('FlippedTweakedTable', (FlippedTweakedTableBase, ), {})
# Python 2/3 compatible way to enable the metaclass
FlippedTweakedTable = FlippedMeta(str('FlippedTweakedTable'), (FlippedTweakedTableBase, ), {})
table = FlippedTweakedTable([])
assert 'name' in table.columns
@ -263,7 +266,11 @@ def ordering_different_types():
assert "" == table.rows[0]['alpha']
table = OrderedTable(data, order_by='i')
assert {} == table.rows[0]['i']
if six.PY3:
assert {} == table.rows[0]['i']
else:
assert 1 == table.rows[0]['i']
table = OrderedTable(data, order_by='beta')
assert [] == table.rows[0]['beta']

View File

@ -2,6 +2,7 @@
from attest import assert_hook, raises, Tests
from django_tables2.utils import (Accessor, AttributeDict, OrderByTuple,
OrderBy, segment)
import six
utils = Tests()
@ -55,11 +56,18 @@ def orderbytuple_sort_key_empty_comes_first():
{"a": ""},
{"a": 2},
]
assert sorted(items, key=obt.key) == [
{"a": ""},
{"a": 1},
{"a": 2},
]
if six.PY3:
assert sorted(items, key=obt.key) == [
{"a": ""},
{"a": 1},
{"a": 2},
]
else:
assert sorted(items, key=obt.key) == [
{"a": 1},
{"a": 2},
{"a": ""},
]
@utils.test
def orderby():
@ -108,7 +116,7 @@ def accessor_wont_honors_alters_data():
foo = Foo()
with raises(ValueError):
Accessor('delete').resolve(foo)
assert foo.deleted == False
assert foo.deleted is False
@utils.test
@ -126,11 +134,10 @@ def attribute_dict_handles_escaping():
@utils.test
def segment_should_return_all_candidates():
assert list(segment(("a", "-b", "c"), {
"x": ("a"),
"y": ("b", "-c"),
"-z": ("b", "-c"),
})) == [
["x", "-y"],
["x", "z"],
]
"x": ("a"),
"y": ("b", "-c"),
"-z": ("b", "-c"),
})) == [
["x", "-y"],
["x", "z"],
]