Skip to content

Commit df6d7be

Browse files
committed
Simplify comparisons and do not allow to compare with strings
Do not allow to compare with things that are convertible to ints, such as a "404" string.
1 parent 0db3ac8 commit df6d7be

File tree

2 files changed

+15
-24
lines changed

2 files changed

+15
-24
lines changed

pytest_flask/plugin.py

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,35 +30,23 @@ def json(self):
3030
return json.loads(self.data)
3131

3232
def __eq__(self, other):
33-
try:
34-
status_code = int(other)
35-
return self.status_code == status_code
36-
except:
37-
super_class = super(JSONResponse, self)
38-
try:
39-
super_eq = getattr(super_class, '__eq__')
40-
except AttributeError:
41-
return self is other
42-
else:
43-
return super_eq(other)
33+
if isinstance(other, int):
34+
return self.status_code == other
35+
return super(JSONResponse, self).__eq__(other)
4436

4537
def __ne__(self, other):
46-
return not self.__eq__(other)
38+
return not self == other
4739

4840

4941
def pytest_assertrepr_compare(op, left, right):
50-
if isinstance(left, JSONResponse) and op == '==':
51-
try:
52-
right = int(right)
53-
return [
54-
'Mismatch in status code for response: {} != {}'.format(
55-
left.status_code,
56-
right,
57-
),
58-
'Response status: {}'.format(left.status),
59-
]
60-
except:
61-
pass
42+
if isinstance(left, JSONResponse) and op == '==' and isinstance(right, int):
43+
return [
44+
'Mismatch in status code for response: {} != {}'.format(
45+
left.status_code,
46+
right,
47+
),
48+
'Response status: {}'.format(left.status),
49+
]
6250
return None
6351

6452

tests/test_fixtures.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,15 @@ def test_json_response(self, client, accept_json):
3636
def test_json_response_compare_to_status_code(self, client, accept_json):
3737
assert client.get(url_for('ping'), headers=accept_json) == 200
3838
assert client.get('fake-route', headers=accept_json) == 404
39+
assert client.get('fake-route', headers=accept_json) != '404'
3940
res = client.get(url_for('ping'), headers=accept_json)
4041
assert res == res
4142

4243
def test_mismatching_eq_comparison(self, client, accept_json):
4344
with pytest.raises(AssertionError, match=r'Mismatch in status code'):
4445
assert client.get('fake-route', headers=accept_json) == 200
46+
with pytest.raises(AssertionError, match=r'404 NOT FOUND'):
47+
assert client.get('fake-route', headers=accept_json) == '200'
4548

4649
def test_dont_rewrite_existing_implementation(self, app, accept_json):
4750
class MyResponse(app.response_class):

0 commit comments

Comments
 (0)