Files
notifications-admin/tests/app/test_utils.py
Chris Hill-Scott 6540701aa7 Replace uses of client to set request context
Some tests use the `client` fixture but don’t call any of its methods.
The reason for doing this is because the test depends on something in
the request context.

This commit replaces all those instances with `client_request`, which
also sets the request context.

These tests are the last ones that still use the `client` fixture. By
replacing it with `client_request` we will be able to say that no tests
should be using the `client` fixture directly.
2022-01-10 14:39:46 +00:00

51 lines
2.8 KiB
Python

import pytest
from app.utils import get_logo_cdn_domain, merge_jsonlike
def test_get_cdn_domain_on_localhost(client_request, mocker):
mocker.patch.dict('app.current_app.config', values={'ADMIN_BASE_URL': 'http://localhost:6012'})
domain = get_logo_cdn_domain()
assert domain == 'static-logos.notify.tools'
def test_get_cdn_domain_on_non_localhost(client_request, mocker):
mocker.patch.dict('app.current_app.config', values={'ADMIN_BASE_URL': 'https://some.admintest.com'})
domain = get_logo_cdn_domain()
assert domain == 'static-logos.admintest.com'
@pytest.mark.parametrize("source_object, destination_object, expected_result", [
# simple dicts:
({"a": "b"}, {"c": "d"}, {"a": "b", "c": "d"}),
# dicts with nested dict, both under same key, additive behaviour:
({"a": {"b": "c"}}, {"a": {"e": "f"}}, {"a": {"b": "c", "e": "f"}}),
# same key in both dicts, value is a string, destination supercedes source:
({"a": "b"}, {"a": "c"}, {"a": "c"}),
# nested dict added to new key of dict, additive behaviour:
({"a": "b"}, {"c": {"d": "e"}}, {"a": "b", "c": {"d": "e"}}),
# lists with same length but different items, destination supercedes source:
(["b", "c", "d"], ["b", "e", "f"], ["b", "e", "f"]),
# lists in dicts behave as top level lists
({"a": ["b", "c", "d"]}, {"a": ["b", "e", "f"]}, {"a": ["b", "e", "f"]}),
# lists with same string in both, at different positions, result in duplicates keeping their positions
(["a", "b", "c", "d"], ["d", "e", "f"], ["d", "e", "f", "d"]),
# lists with same dict in both result in a list with one instance of that dict
([{"b": "c"}], [{"b": "c"}], [{"b": "c"}]),
# if dicts in lists have different values, they are not merged
([{"b": "c"}], [{"b": "e"}], [{"b": "e"}]),
# if nested dicts in lists have different keys, additive behaviour
([{"b": "c"}], [{"d": {"e": "f"}}], [{"b": "c", "d": {"e": "f"}}]),
# if dicts in destination list but not source, they just get added to end of source
([{"a": "b"}], [{"a": "b"}, {"a": "b"}, {"c": "d"}], [{"a": "b"}, {"a": "b"}, {"c": "d"}]),
# merge a dict with a null object returns that dict (does not work the other way round)
({"a": {"b": "c"}}, None, {"a": {"b": "c"}}),
# double nested dicts, new adds new Boolean key: value, additive behaviour
({"a": {"b": {"c": "d"}}}, {"a": {"b": {"e": True}}}, {"a": {"b": {"c": "d", "e": True}}}),
# double nested dicts, both have same key, different values, destination supercedes source
({"a": {"b": {"c": "d"}}}, {"a": {"b": {"c": "e"}}}, {"a": {"b": {"c": "e"}}})
])
def test_merge_jsonlike_merges_jsonlike_objects_correctly(source_object, destination_object, expected_result):
merge_jsonlike(source_object, destination_object)
assert source_object == expected_result