|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import pytest |
| 4 | +from django.template.backends.django import Template |
| 5 | +from django.template.exceptions import TemplateDoesNotExist |
| 6 | + |
| 7 | +from django_bird.components import Component |
| 8 | +from django_bird.components import Registry |
| 9 | + |
| 10 | + |
| 11 | +class TestComponent: |
| 12 | + def test_from_name(self, create_bird_template): |
| 13 | + create_bird_template("button", "<button>Click me</button>") |
| 14 | + |
| 15 | + comp = Component.from_name("button") |
| 16 | + |
| 17 | + assert comp.name == "button" |
| 18 | + assert isinstance(comp.template, Template) |
| 19 | + assert comp.render({}) == "<button>Click me</button>" |
| 20 | + |
| 21 | + |
| 22 | +class TestRegistry: |
| 23 | + @pytest.fixture |
| 24 | + def registry(self): |
| 25 | + return Registry(maxsize=2) |
| 26 | + |
| 27 | + def test_get_component_caches(self, registry, create_bird_template): |
| 28 | + create_bird_template(name="button", content="<button>Click me</button>") |
| 29 | + |
| 30 | + component1 = registry.get_component("button") |
| 31 | + component2 = registry.get_component("button") |
| 32 | + |
| 33 | + assert component1 is component2 |
| 34 | + |
| 35 | + def test_lru_cache_behavior(self, registry, create_bird_template): |
| 36 | + create_bird_template(name="button1", content="1") |
| 37 | + create_bird_template(name="button2", content="2") |
| 38 | + create_bird_template(name="button3", content="3") |
| 39 | + |
| 40 | + button1 = registry.get_component("button1") |
| 41 | + button2 = registry.get_component("button2") |
| 42 | + button3 = registry.get_component("button3") |
| 43 | + |
| 44 | + new_button1 = registry.get_component("button1") |
| 45 | + assert new_button1 is not button1 |
| 46 | + |
| 47 | + cached_button2 = registry.get_component("button2") |
| 48 | + assert cached_button2.name == button2.name |
| 49 | + assert cached_button2.render({}) == button2.render({}) |
| 50 | + |
| 51 | + cached_button3 = registry.get_component("button3") |
| 52 | + assert cached_button3.name == button3.name |
| 53 | + assert cached_button3.render({}) == button3.render({}) |
| 54 | + |
| 55 | + def test_component_not_found(self, registry): |
| 56 | + with pytest.raises(TemplateDoesNotExist): |
| 57 | + registry.get_component("nonexistent") |
0 commit comments