Files
notifications-admin/tests/app/test_utils.py

64 lines
2.9 KiB
Python
Raw Normal View History

2017-07-27 16:30:26 +01:00
import pytest
from flask import Flask
Show one field at a time on send yourself a test The send yourself a test feature is useful for two things: - constructing an email/text message/letter without uploading a CSV file - seeing what the thing your going to send will look like (either by getting it in your inbox or downloading the PDF) - learning the concept of placeholders, ie understanding they’re thing that gets populated with _stuff_ The problem we’re seeing is that the current UI breaks when a template has a lot of placeholders. This is especially apparent with letter templates, which have a minimum of 7 placeholders by virtue of the address. The idea behind having the form fields side-by-side was to help people understand the relationship between their spreadsheet columns and the placeholders. But this means that the page was doing a lot of work, trying to teach: - replacement of placeholders - link between placeholders and spreadsheet columns The latter is better explained by the example spreadsheet shown on the upload page. So it can safely be removed from the send yourself a test page – in other words the fields don’t need to be shown side by side. Showing them one-at-a-time works well because: - it’s really obvious, even on first use, what the page is asking you to do - as your step through each placeholder, you see the message build up with the data you’ve entered – you’re learning how replacement of placeholders works by repetition This also means adding a matching endpoint for viewing each step of making the test letter as a PDF/PNG because we can’t reuse the view of the template without any placeholders filled any more.
2017-05-04 09:30:55 +01:00
from app import create_app
from app.utils import merge_jsonlike
@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
2024-03-14 13:25:02 -07:00
2024-03-14 13:26:05 -07:00
2024-03-14 13:25:02 -07:00
def test_commit_hash():
# Assert that we have trimmed the default (unknown) commit hash to seven characters
# The real commit hash is supplied at deploy time.
app = Flask("app")
create_app(app)
with app.app_context() as current_app:
assert current_app.app.config["COMMIT_HASH"] == "-------"