Skip to content

Commit

Permalink
Test for .properties(), cross-linked event docs
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Jan 31, 2024
1 parent f9f7b6f commit 6720ca1
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
3 changes: 3 additions & 0 deletions datasette/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class LoginEvent(Event):
A user (represented by ``event.actor``) has logged in.
"""

name = "login"


Expand All @@ -34,6 +35,7 @@ class LogoutEvent(Event):
A user (represented by ``event.actor``) has logged out.
"""

name = "logout"


Expand All @@ -51,6 +53,7 @@ class CreateTableEvent(Event):
:ivar schema: The SQL schema definition for the new table.
:type schema: str
"""

name = "create-table"
database: str
table: str
Expand Down
4 changes: 3 additions & 1 deletion docs/events.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.. _events:

Events
======

Expand All @@ -9,4 +11,4 @@ Plugins can listen for events using the :ref:`plugin_hook_track_event` plugin ho

.. automodule:: datasette.events
:members:
:exclude-members: Event
:exclude-members: Event
2 changes: 1 addition & 1 deletion docs/plugin_hooks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1791,7 +1791,7 @@ The ``event`` object will always have the following properties:

Other properties on the event will be available depending on the type of event. You can also access those as a dictionary using ``event.properties()``.

**TODO: Link to documentation of default core events**
The events fired by Datasette core are :ref:`documented here <events>`.

This example plugin logs details of all events to standard error:

Expand Down
11 changes: 10 additions & 1 deletion tests/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
TestClient as _TestClient,
) # noqa
from click.testing import CliRunner
from dataclasses import dataclass
from datasette.app import Datasette
from datasette import cli, hookimpl, Event, Permission
from datasette.filters import FilterArguments
Expand Down Expand Up @@ -1442,9 +1443,12 @@ async def test_hook_top_canned_query(ds_client):
class TrackEventPlugin:
__name__ = "TrackEventPlugin"

@dataclass
class OneEvent(Event):
name = "one"

extra: str

@hookimpl
def register_events(self, datasette):
async def inner():
Expand All @@ -1464,9 +1468,14 @@ async def test_hook_track_event():
pm.register(TrackEventPlugin(), name="TrackEventPlugin")
datasette = Datasette(memory=True)
await datasette.invoke_startup()
await datasette.track_event(TrackEventPlugin.OneEvent(None))
await datasette.track_event(
TrackEventPlugin.OneEvent(None, extra="extra extra")
)
assert len(datasette._tracked_events) == 1
assert isinstance(datasette._tracked_events[0], TrackEventPlugin.OneEvent)
event = datasette._tracked_events[0]
assert event.name == "one"
assert event.properties() == {"extra": "extra extra"}
finally:
pm.unregister(name="TrackEventPlugin")

Expand Down

0 comments on commit 6720ca1

Please sign in to comment.