Skip to content

Commit

Permalink
Merge pull request #10 from EtiennePelletier/test_flask_signals
Browse files Browse the repository at this point in the history
Fix Flask/Quart signal name typo and add tests for Flask signals
  • Loading branch information
sponsfreixes authored Apr 25, 2023
2 parents ea1eed1 + 139ae25 commit 6c4820b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/jinja2_fragments/flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
before_render_template_block = jinja2_fragments_signals.signal(
"before-render-template-block"
)
template_block_rendered = jinja2_fragments_signals.signal("template-bock-rendered")
template_block_rendered = jinja2_fragments_signals.signal("template-block-rendered")


def render_block(template_name: str, block_name: str, **context: typing.Any) -> str:
Expand Down
2 changes: 1 addition & 1 deletion src/jinja2_fragments/quart.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
before_render_template_block = jinja2_fragments_signals.signal(
"before-render-template-block"
)
template_block_rendered = jinja2_fragments_signals.signal("template-bock-rendered")
template_block_rendered = jinja2_fragments_signals.signal("template-block-rendered")


async def render_block(
Expand Down
47 changes: 46 additions & 1 deletion tests/test_flask.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import pytest

from jinja2_fragments import BlockNotFoundError
from jinja2_fragments.flask import render_block
from jinja2_fragments.flask import (
before_render_template_block,
render_block,
template_block_rendered,
)


class TestFlaskRenderBlock:
Expand Down Expand Up @@ -39,3 +43,44 @@ def test_exception(self, flask_app):
render_block("simple_page.html.jinja2", "invalid_block")
assert "invalid_block" in exc.value
assert "simple_page.html.jinja2" in exc.value

@pytest.mark.parametrize(
"signal",
[
before_render_template_block,
template_block_rendered,
],
)
def test_signals(app, flask_app, flask_client, signal):
recorded = []

def record(sender, template_name, block_name, context):
context["testing"] = "yes please"
recorded.append((sender, template_name, block_name, context))

def call():
flask_client.get("/simple_page", query_string={"only_content": "true"})

# Test for absence of typo in signal name
assert signal is globals().get(signal.name.replace("-", "_"))

# No signal should be recorded
call()
assert len(recorded) == 0

# Connect, record one signal
signal.connect(record, flask_app)
call()
assert len(recorded) == 1

# Disconnect, stop recording signals
signal.disconnect(record, flask_app)
call()
assert len(recorded) == 1

# Verify values sent in the signal
sender, template_name, block_name, context = recorded[0]
assert sender == flask_app
assert template_name == "simple_page.html.jinja2"
assert block_name == "content"
assert context["testing"] == "yes please"

0 comments on commit 6c4820b

Please sign in to comment.