Skip to content

Commit

Permalink
Fix a bug where existing Jinja2 entity contexts would go lost upon se…
Browse files Browse the repository at this point in the history
…tting another one (#1719)
  • Loading branch information
bartfeenstra authored Jul 16, 2024
1 parent 13076d3 commit 400b426
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 8 deletions.
4 changes: 3 additions & 1 deletion betty/jinja2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ def __call__(self, *entities: Entity) -> EntityContexts:
"""
Create a new context with the given entities.
"""
updated_contexts = EntityContexts()
updated_contexts = EntityContexts(
*(entity for entity in self._contexts.values() if entity is not None)
)
for entity in entities:
updated_contexts._contexts[entity.type] = entity
return updated_contexts
Expand Down
14 changes: 13 additions & 1 deletion betty/tests/coverage/test_coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,19 @@ class TestKnownToBeMissing:
"fully_qualified_type_name": TestKnownToBeMissing,
},
"betty/html.py": TestKnownToBeMissing,
"betty/jinja2/__init__.py": TestKnownToBeMissing,
"betty/jinja2/__init__.py": {
"context_app": TestKnownToBeMissing,
"context_job_context": TestKnownToBeMissing,
"context_localizer": TestKnownToBeMissing,
"Environment": TestKnownToBeMissing,
"Jinja2Provider": {
"new_context_vars": TestKnownToBeMissing,
"tests": TestKnownToBeMissing,
},
"Jinja2Renderer": {
"file_extensions": TestKnownToBeMissing,
},
},
"betty/jinja2/filter.py": TestKnownToBeMissing,
"betty/jinja2/test.py": TestKnownToBeMissing,
"betty/job.py": {
Expand Down
Empty file added betty/tests/jinja2/__init__.py
Empty file.
43 changes: 37 additions & 6 deletions betty/tests/test_jinja2.py → betty/tests/jinja2/test___init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
from aiofiles.tempfile import TemporaryDirectory

from betty.app import App
from betty.jinja2 import Jinja2Renderer, _Citer, Jinja2Provider
from betty.fs import ASSETS_DIRECTORY_PATH
from betty.jinja2 import Jinja2Renderer, _Citer, Jinja2Provider, EntityContexts
from betty.locale import Date, Datey, DateRange, Localized
from betty.media_type import MediaType
from betty.model import get_entity_type_name, Entity
Expand Down Expand Up @@ -189,15 +190,15 @@ class TestFilterFile(TemplateTestCase):
("expected", "template", "file"),
[
(
"/file/F1/file/test_jinja2.py",
"/file/F1/file/test___init__.py",
"{{ file | file }}",
File(
id="F1",
path=Path(__file__),
),
),
(
"/file/F1/file/test_jinja2.py:/file/F1/file/test_jinja2.py",
"/file/F1/file/test___init__.py:/file/F1/file/test___init__.py",
"{{ file | file }}:{{ file | file }}",
File(
id="F1",
Expand All @@ -221,9 +222,7 @@ async def test(self, expected: str, template: str, file: File) -> None:


class TestFilterImage(TemplateTestCase):
image_path = (
Path(__file__).parents[1] / "assets" / "public" / "static" / "betty-512x512.png"
)
image_path = ASSETS_DIRECTORY_PATH / "public" / "static" / "betty-512x512.png"

@pytest.mark.parametrize(
("expected", "template", "file"),
Expand Down Expand Up @@ -671,3 +670,35 @@ async def test(
},
) as (actual, _):
assert expected == actual


class EntityContextsTestEntityA(Entity):
pass


class EntityContextsTestEntityB(Entity):
pass


class TestEntityContexts:
async def test___getitem__(self) -> None:
sut = EntityContexts()
assert sut[EntityContextsTestEntityA] is None

async def test___getitem___with___init__(self) -> None:
a = EntityContextsTestEntityA()
sut = EntityContexts(a)
assert sut[EntityContextsTestEntityA] is a

async def test___call__(self) -> None:
a = EntityContextsTestEntityA()
contexts = EntityContexts()
sut = contexts(a)
assert sut[EntityContextsTestEntityA] is a

async def test___call___with___init__(self) -> None:
a = EntityContextsTestEntityA()
b = EntityContextsTestEntityA()
contexts = EntityContexts(a)
sut = contexts(b)
assert sut[EntityContextsTestEntityA] is b

0 comments on commit 400b426

Please sign in to comment.