Make query string comparison ignore order

Query string ordering is non-deterministic. This can cause tests to fail
in a non helpful way when we’re comparing two URLs. The values in the
query string can match, but the string won’t because the order is
different. This commit adds some code to split up the URL and check that
each part of it matches, rather than checking the thing as a whole.
This commit is contained in:
Chris Hill-Scott
2019-07-15 13:52:42 +01:00
parent 886992af17
commit c17aa249dc
3 changed files with 60 additions and 15 deletions

View File

@@ -1,6 +1,7 @@
import uuid
from datetime import datetime, timedelta, timezone
from unittest.mock import patch
from urllib.parse import parse_qs, urlparse
import pytest
from flask import url_for
@@ -28,7 +29,7 @@ class TestClient(FlaskClient):
login_user(model_user)
def logout(self, user):
self.get(url_for("main.logout"))
self.get(url_for("main.sign_out"))
def sample_uuid():
@@ -579,3 +580,28 @@ def validate_route_permission_with_client(mocker,
if resp.status_code != response_code:
pytest.fail("Invalid permissions set for endpoint {}".format(route))
return resp
def assert_url_expected(actual, expected):
actual_parts = urlparse(actual)
expected_parts = urlparse(expected)
for attribute in actual_parts._fields:
if attribute == 'query':
# query string ordering can be non-deterministic
# so we need to parse it first, which gives us a
# dictionary of keys and values, not a
# serialized string
assert parse_qs(
expected_parts.query
) == parse_qs(
actual_parts.query
)
else:
assert getattr(
actual_parts, attribute
) == getattr(
expected_parts, attribute
), (
'Expected redirect: {}\n'
'Actual redirect: {}'
).format(expected, actual)