mirror of
https://github.com/simonw/datasette.git
synced 2026-11-20 00:58:31 +01:00
extra_field() - Context fields documented by their Extra class
A Context dataclass field declared with extra_field() takes its documentation from the description on the registered Extra of the same name, validated against the class's extras_scope. This keeps doc strings next to the resolve() code instead of duplicating them on the dataclass, ahead of introducing TableContext and RowContext. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
parent
3ea7ed8606
commit
63995ce823
2 changed files with 91 additions and 1 deletions
|
|
@ -44,6 +44,51 @@ def test_context_dataclass_fields_all_have_help(klass):
|
|||
)
|
||||
|
||||
|
||||
def test_extra_field_documentation_comes_from_the_extra_class():
|
||||
from datasette.views import extra_field
|
||||
from datasette.views.table_extras import CountExtra
|
||||
|
||||
@dataclass
|
||||
class DemoContext(Context):
|
||||
extras_scope = ExtraScope.TABLE
|
||||
|
||||
count: int = extra_field()
|
||||
name: str = field(metadata={"help": "The name"})
|
||||
|
||||
fields = {f.name: f for f in DemoContext.documented_fields()}
|
||||
assert fields["count"].help == CountExtra.description
|
||||
assert fields["count"].from_extra
|
||||
assert fields["name"].help == "The name"
|
||||
assert not fields["name"].from_extra
|
||||
|
||||
|
||||
def test_extra_field_must_match_a_registered_extra():
|
||||
from datasette.views import extra_field
|
||||
|
||||
@dataclass
|
||||
class BadContext(Context):
|
||||
extras_scope = ExtraScope.TABLE
|
||||
|
||||
not_a_real_extra: str = extra_field()
|
||||
|
||||
with pytest.raises(KeyError):
|
||||
BadContext.documented_fields()
|
||||
|
||||
|
||||
def test_extra_field_must_be_available_for_the_scope():
|
||||
from datasette.views import extra_field
|
||||
|
||||
@dataclass
|
||||
class WrongScopeContext(Context):
|
||||
extras_scope = ExtraScope.ROW
|
||||
|
||||
# count is a TABLE-scope extra, not available for ROW
|
||||
count: int = extra_field()
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
WrongScopeContext.documented_fields()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def isolate_extra_template_vars_plugins():
|
||||
# Datasette instances created with plugins_dir (e.g. the session-scoped
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue