Don't make ExpectedRequest orderable

Instead, make it hashable and use collections.Counter instead of sorted
lists. This actually produces a nicer output for differences between
the counters.
This commit is contained in:
Daniel Schadt 2015-11-21 00:20:09 +01:00
parent c12011c84d
commit c7294781f5
2 changed files with 6 additions and 9 deletions

View File

@ -22,6 +22,7 @@
import os import os
import re import re
import os.path import os.path
import collections
import pytest import pytest
@ -102,7 +103,8 @@ def test_mhtml(test_name, download_dir, quteproc, httpbin):
expected_requests.append(httpbin.ExpectedRequest('GET', path)) expected_requests.append(httpbin.ExpectedRequest('GET', path))
actual_requests = httpbin.get_requests() actual_requests = httpbin.get_requests()
# Requests are unorderable, we need to convert to ExpectedRequests # Requests are not hashable, we need to convert to ExpectedRequests
actual_requests = map(httpbin.ExpectedRequest.from_request, actual_requests = map(httpbin.ExpectedRequest.from_request,
actual_requests) actual_requests)
assert sorted(actual_requests) == sorted(expected_requests) assert (collections.Counter(actual_requests) ==
collections.Counter(expected_requests))

View File

@ -26,7 +26,6 @@ import re
import sys import sys
import socket import socket
import os.path import os.path
import functools
import pytest import pytest
from PyQt5.QtCore import pyqtSignal from PyQt5.QtCore import pyqtSignal
@ -90,7 +89,6 @@ class Request(testprocess.Line):
return NotImplemented return NotImplemented
@functools.total_ordering
class ExpectedRequest: class ExpectedRequest:
"""Class to compare expected requests easily.""" """Class to compare expected requests easily."""
@ -111,11 +109,8 @@ class ExpectedRequest:
else: else:
return NotImplemented return NotImplemented
def __lt__(self, other): def __hash__(self):
if isinstance(other, (Request, ExpectedRequest)): return hash(('ExpectedRequest', self.verb, self.path))
return (self.verb, self.path) < (other.verb, other.path)
else:
return NotImplemented
def __repr__(self): def __repr__(self):
return ('ExpectedRequest(verb={!r}, path={!r})' return ('ExpectedRequest(verb={!r}, path={!r})'